Skip to content

Commit

Permalink
#150: Rewrite session ID from header tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MRichards99 committed Dec 3, 2020
1 parent 89d58d6 commit e8f1139
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 24 deletions.
2 changes: 1 addition & 1 deletion test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def valid_db_credentials_header():

insert_row_into_table(SESSION, session)

yield {"Authorization": "Bearer Test"}
yield {"Authorization": f"Bearer {session.ID}"}

delete_row_by_id(SESSION, "Test")

Expand Down
20 changes: 0 additions & 20 deletions test/db/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,12 @@
DatabaseWhereFilter,
)
from datagateway_api.common.exceptions import (
AuthenticationError,
BadRequestError,
FilterError,
MissingCredentialsError,
MissingRecordError,
)
from datagateway_api.common.helpers import (
get_filters_from_query_string,
get_session_id_from_auth_header,
queries_records,
)
from test.test_base import FlaskAppTest
Expand Down Expand Up @@ -92,23 +89,6 @@ def raise_bad_request_error():
self.assertEqual(400, ctx.exception.status_code)


class TestGetSessionIDFromAuthHeader(FlaskAppTest):
def test_no_session_in_header(self):
with self.app:
self.app.get("/")
self.assertRaises(MissingCredentialsError, get_session_id_from_auth_header)

def test_with_bad_header(self):
with self.app:
self.app.get("/", headers={"Authorization": "test"})
self.assertRaises(AuthenticationError, get_session_id_from_auth_header)

def test_with_good_header(self):
with self.app:
self.app.get("/", headers={"Authorization": "Bearer test"})
self.assertEqual("test", get_session_id_from_auth_header())


class TestGetFiltersFromQueryString(FlaskAppTest):
def test_no_filters(self):
with self.app:
Expand Down
4 changes: 2 additions & 2 deletions test/icat/endpoints/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

class TestCreateData:
def test_valid_create_data(
self, flask_test_app_icat, valid_icat_credentials_header
self, flask_test_app_icat, valid_icat_credentials_header,
):
create_investigations_json = [
{
Expand Down Expand Up @@ -99,7 +99,7 @@ def test_valid_boundary_create_data(
)

def test_invalid_create_data(
self, flask_test_app_icat, valid_icat_credentials_header
self, flask_test_app_icat, valid_icat_credentials_header,
):
"""An investigation requires a minimum of: name, visitId, facility, type"""

Expand Down
2 changes: 1 addition & 1 deletion test/icat/endpoints/test_get_by_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_valid_get_with_id(
assert response_json == single_investigation_test_data

def test_invalid_get_with_id(
self, flask_test_app_icat, valid_icat_credentials_header
self, flask_test_app_icat, valid_icat_credentials_header,
):
"""Request with a non-existent ID"""

Expand Down
28 changes: 28 additions & 0 deletions test/test_get_session_id_from_auth_header.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import pytest

from datagateway_api.common.exceptions import (
AuthenticationError,
MissingCredentialsError,
)
from datagateway_api.common.helpers import get_session_id_from_auth_header


class TestGetSessionIDFromAuthHeader:
def test_invalid_no_credentials_in_header(self, flask_test_app_db):
with flask_test_app_db:
flask_test_app_db.get("/")
with pytest.raises(MissingCredentialsError):
get_session_id_from_auth_header()

def test_invalid_header(self, flask_test_app_db, invalid_credentials_header):
with flask_test_app_db:
flask_test_app_db.get("/", headers=invalid_credentials_header)
with pytest.raises(AuthenticationError):
get_session_id_from_auth_header()

def test_valid_header(self, flask_test_app_db, valid_db_credentials_header):
with flask_test_app_db:
flask_test_app_db.get("/", headers=valid_db_credentials_header)
session_id = valid_db_credentials_header["Authorization"].split()[1]

assert session_id == get_session_id_from_auth_header()

0 comments on commit e8f1139

Please sign in to comment.