Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactoring test suite #333

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 1 addition & 13 deletions tests/unit/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 1 addition & 12 deletions tests/unit/test_auth_jwt.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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):
Expand Down
39 changes: 8 additions & 31 deletions tests/unit/test_routes/test_api_key.py
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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)

Expand All @@ -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)
Expand All @@ -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)
9 changes: 0 additions & 9 deletions tests/unit/test_routes/test_searching.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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

Expand Down
28 changes: 28 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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


def apikey_commit(client, email, password, **kwargs):

response = client.post(
'api/v1/apikey',
**kwargs,
json=dict(
email=email,
password=password
),
)

return response