From 8671725b2392e40ffebedcbace34aa3feba48623 Mon Sep 17 00:00:00 2001 From: Roosh S Date: Tue, 23 Jun 2020 22:21:44 +0500 Subject: [PATCH 1/3] refactored auth tests --- tests/unit/test_auth.py | 14 +------------- tests/unit/test_auth_jwt.py | 13 +------------ tests/utils.py | 14 ++++++++++++++ 3 files changed, 16 insertions(+), 25 deletions(-) create mode 100644 tests/utils.py diff --git a/tests/unit/test_auth.py b/tests/unit/test_auth.py index 327df298..cd1ccffd 100644 --- a/tests/unit/test_auth.py +++ b/tests/unit/test_auth.py @@ -3,21 +3,9 @@ from app.api.auth import (ApiKeyError, ApiKeyErrorCode, authenticate, blacklist_key, find_key_by_apikey_or_email, rotate_key) -from app.models import Key +from tests.utils import create_fake_key, FAKE_EMAIL, FAKE_APIKEY from flask import g -FAKE_EMAIL = 'test@example.org' -FAKE_APIKEY = 'abcdef1234567890' - - -def create_fake_key(session, **kwargs): - kwargs['email'] = kwargs.get('email', FAKE_EMAIL) - kwargs['apikey'] = kwargs.get('apikey', FAKE_APIKEY) - key = Key(**kwargs) - session.add(key) - session.commit() - return key - def test_authenticate_failure(module_client, function_empty_db): # Arrange diff --git a/tests/unit/test_auth_jwt.py b/tests/unit/test_auth_jwt.py index c384c4de..f97eb341 100644 --- a/tests/unit/test_auth_jwt.py +++ b/tests/unit/test_auth_jwt.py @@ -1,5 +1,5 @@ from app.api.auth import authenticate -from app.models import Key +from tests.utils import create_fake_key, FAKE_EMAIL from datetime import datetime, timedelta from jwt import encode from unittest.mock import patch @@ -33,8 +33,6 @@ DJQHadGUXFAGcrQKpxHv7QA0 -----END PRIVATE KEY-----""" -FAKE_EMAIL = 'test@example.org' -FAKE_APIKEY = 'abcdef1234567890' SECRET_KEY = open(".dev/dev-jwt-key").read() EXP = datetime.utcnow() + timedelta(seconds=10) delta = timedelta(seconds=-11) @@ -49,15 +47,6 @@ SECRET_KEY, algorithm='RS256').decode('utf-8') -def create_fake_key(session, **kwargs): - kwargs['email'] = kwargs.get('email', FAKE_EMAIL) - kwargs['apikey'] = kwargs.get('apikey', FAKE_APIKEY) - key = Key(**kwargs) - session.add(key) - session.commit() - return key - - def test_missing_auth_header(module_client): # Arrange def callback(*args, **kwargs): diff --git a/tests/utils.py b/tests/utils.py new file mode 100644 index 00000000..c244442f --- /dev/null +++ b/tests/utils.py @@ -0,0 +1,14 @@ +from app.models import Key + + +FAKE_EMAIL = 'test@example.org' +FAKE_APIKEY = 'abcdef1234567890' + + +def create_fake_key(session, **kwargs): + kwargs['email'] = kwargs.get('email', FAKE_EMAIL) + kwargs['apikey'] = kwargs.get('apikey', FAKE_APIKEY) + key = Key(**kwargs) + session.add(key) + session.commit() + return key From 706b6ebb7cd4deba507a2dfd204eb2130b2940e5 Mon Sep 17 00:00:00 2001 From: Roosh S Date: Tue, 23 Jun 2020 22:47:20 +0500 Subject: [PATCH 2/3] removed unnecessary parameters in test_searching.py --- tests/unit/test_routes/test_searching.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/tests/unit/test_routes/test_searching.py b/tests/unit/test_routes/test_searching.py index 6c3cf3d9..be0e33c3 100644 --- a/tests/unit/test_routes/test_searching.py +++ b/tests/unit/test_routes/test_searching.py @@ -44,9 +44,6 @@ def test_search( def test_search_paid_filter(module_client, - module_db, - fake_auth_from_oc, - fake_algolia_save, fake_algolia_search): client = module_client @@ -64,9 +61,6 @@ def test_search_paid_filter(module_client, def test_search_category_filter(module_client, - module_db, - fake_auth_from_oc, - fake_algolia_save, fake_algolia_search): client = module_client @@ -83,9 +77,6 @@ def test_search_category_filter(module_client, def test_search_language_filter(module_client, - module_db, - fake_auth_from_oc, - fake_algolia_save, fake_algolia_search): client = module_client From c871a5b2cd16510787d9fde91cda93af1a42d0e4 Mon Sep 17 00:00:00 2001 From: Roosh S Date: Tue, 23 Jun 2020 23:36:27 +0500 Subject: [PATCH 3/3] refactored test_api_key --- tests/unit/test_routes/test_api_key.py | 39 ++++++-------------------- tests/utils.py | 14 +++++++++ 2 files changed, 22 insertions(+), 31 deletions(-) diff --git a/tests/unit/test_routes/test_api_key.py b/tests/unit/test_routes/test_api_key.py index 06fa9915..d7c09748 100644 --- a/tests/unit/test_routes/test_api_key.py +++ b/tests/unit/test_routes/test_api_key.py @@ -1,14 +1,11 @@ from app.api.auth import blacklist_key from .helpers import get_api_key, assert_correct_response +from tests.utils import apikey_commit def test_get_api_key(module_client, module_db, fake_auth_from_oc): - client = module_client - response = client.post('api/v1/apikey', json=dict( - email="test@example.org", - password="supersecurepassword" - )) + response = apikey_commit(module_client, "test@example.org", "supersecurepassword") assert (response.status_code == 200) assert (response.json['credentials'].get('email') == "test@example.org") @@ -29,25 +26,15 @@ def test_rotate_api_key(module_client, module_db, fake_auth_from_oc): def test_apikey_commit_error( module_client, module_db, fake_auth_from_oc, fake_commit_error): - client = module_client - - response = client.post('api/v1/apikey', json=dict( - email="test@example.com", - password="password" - )) + response = apikey_commit(module_client, "test@example.com", "password") assert_correct_response(response, 500) def test_get_api_key_bad_password(module_client, module_db, fake_invalid_auth_from_oc): - client = module_client - response = client.post('api/v1/apikey', - follow_redirects=True, - json=dict( - email="test@example.org", - password="invalidpassword" - )) + response = apikey_commit(module_client, "test@example.org", "invalidpassword", + follow_redirects=True) assert_correct_response(response, 401) @@ -59,14 +46,8 @@ def test_get_api_key_blacklisted(module_client, module_db, fake_auth_from_oc): blacklist_key(apikey, True, module_db.session) try: - response = client.post( - 'api/v1/apikey', - follow_redirects=True, - json=dict( - email="test@example.org", - password="supersecurepassword" - ) - ) + response = apikey_commit(client, "test@example.org", "supersecurepassword", + follow_redirects=True) assert_correct_response(response, 401) finally: blacklist_key(apikey, False, module_db.session) @@ -82,9 +63,5 @@ def test_rotate_api_key_unauthorized(module_client, module_db): def test_key_query_error( module_client, module_db, fake_auth_from_oc, fake_key_query_error): - client = module_client - response = client.post('api/v1/apikey', json=dict( - email="test@example.org", - password="supersecurepassword" - )) + response = apikey_commit(module_client, "test@example.com", "supersecurepassword") assert_correct_response(response, 500) diff --git a/tests/utils.py b/tests/utils.py index c244442f..32e3b21b 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -12,3 +12,17 @@ def create_fake_key(session, **kwargs): session.add(key) session.commit() return key + + +def apikey_commit(client, email, password, **kwargs): + + response = client.post( + 'api/v1/apikey', + **kwargs, + json=dict( + email=email, + password=password + ), + ) + + return response