Skip to content

Commit

Permalink
#150: Rewrite DB session decorator tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MRichards99 committed Dec 3, 2020
1 parent b97b19b commit a07f0db
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 50 deletions.
29 changes: 27 additions & 2 deletions test/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime
from datetime import datetime, timedelta
import uuid

from flask import Flask
Expand All @@ -8,6 +8,11 @@
import pytest

from datagateway_api.common.config import config
from datagateway_api.common.database.helpers import (
delete_row_by_id,
insert_row_into_table,
)
from datagateway_api.common.database.models import SESSION
from datagateway_api.src.main import create_api_endpoints, create_app_infrastructure
from test.icat.test_query import prepare_icat_data_for_assertion

Expand All @@ -25,10 +30,30 @@ def valid_credentials_header(icat_client):


@pytest.fixture()
def invalid_credentials_header():
def valid_db_credentials_header():
session = SESSION()
session.ID = "Test"
session.EXPIREDATETIME = datetime.now() + timedelta(hours=1)
session.username = "Test User"

insert_row_into_table(SESSION, session)

yield {"Authorization": "Bearer Test"}

delete_row_by_id(SESSION, "Test")


@pytest.fixture()
def bad_credentials_header():
return {"Authorization": "Bearer Invalid"}


# TODO - Implement this in test_session_handling.py
@pytest.fixture()
def invalid_credentials_header():
return {"Authorization": "Test"}


@pytest.fixture()
def icat_query(icat_client):
return Query(icat_client, "Investigation")
Expand Down
48 changes: 0 additions & 48 deletions test/db/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from datetime import datetime, timedelta
from unittest import TestCase

from sqlalchemy.exc import IntegrityError
Expand All @@ -11,11 +10,6 @@
DatabaseSkipFilter,
DatabaseWhereFilter,
)
from datagateway_api.common.database.helpers import (
delete_row_by_id,
insert_row_into_table,
)
from datagateway_api.common.database.models import SESSION
from datagateway_api.common.exceptions import (
AuthenticationError,
BadRequestError,
Expand All @@ -31,48 +25,6 @@
from test.test_base import FlaskAppTest


class TestRequiresSessionID(FlaskAppTest):
def setUp(self):
super().setUp()
self.good_credentials_header = {"Authorization": "Bearer Test"}
self.invalid_credentials_header = {"Authorization": "Test"}
self.bad_credentials_header = {"Authorization": "Bearer BadTest"}
session = SESSION()
session.ID = "Test"
session.EXPIREDATETIME = datetime.now() + timedelta(hours=1)
session.username = "Test User"

insert_row_into_table(SESSION, session)

def tearDown(self):
delete_row_by_id(SESSION, "Test")

def test_missing_credentials(self):
self.assertEqual(401, self.app.get("/datafiles").status_code)

def test_invalid_credentials(self):
self.assertEqual(
403,
self.app.get(
"/datafiles", headers=self.invalid_credentials_header,
).status_code,
)

def test_bad_credentials(self):
self.assertEqual(
403,
self.app.get("/datafiles", headers=self.bad_credentials_header).status_code,
)

def test_good_credentials(self):
self.assertEqual(
200,
self.app.get(
"/datafiles?limit=0", headers=self.good_credentials_header,
).status_code,
)


class TestQueriesRecords(TestCase):
def test_missing_record_error(self):
@queries_records
Expand Down
31 changes: 31 additions & 0 deletions test/db/test_requires_session_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class TestRequiresSessionID:
"""
This class tests the session decorator used for the database backend. The equivalent
decorator for the Python ICAT backend is tested in `test_session_handling.py`
"""

def test_invalid_missing_credentials(self, flask_test_app_db):
test_response = flask_test_app_db.get("/datafiles")

assert test_response.status_code == 401

def test_invalid_credentials(self, flask_test_app_db, invalid_credentials_header):
test_response = flask_test_app_db.get(
"/datafiles", headers=invalid_credentials_header,
)

assert test_response.status_code == 403

def test_bad_credentials(self, flask_test_app_db, bad_credentials_header):
test_response = flask_test_app_db.get(
"/datafiles", headers=bad_credentials_header,
)

assert test_response.status_code == 403

def test_valid_credentials(self, flask_test_app_db, valid_db_credentials_header):
test_response = flask_test_app_db.get(
"/datafiles?limit=0", headers=valid_db_credentials_header,
)

assert test_response.status_code == 200

0 comments on commit a07f0db

Please sign in to comment.