Skip to content

Commit

Permalink
Gaslight the app, so we're allowed to add a new route in API auth test.
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven-Eardley committed Oct 11, 2024
1 parent 600f92a commit 42e08e1
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions doajtest/unit/api_tests/test_api_account.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from flask import Response

from doajtest import helpers
from doajtest.helpers import DoajTestCase, with_es
from doajtest.helpers import DoajTestCase
from portality import models
from portality.core import load_account_for_login_manager
from portality.decorators import api_key_required, api_key_optional
Expand All @@ -13,19 +13,27 @@ def setUpClass(cls):
super(TestAPIClient, cls).setUpClass()
helpers.initialise_index()

# Turn off debug so we're allowed to add these routes after the app has been used in other tests
# Turn off debug and so we're allowed to add these routes after the app has been used in other tests
cls.app_test.debug = False

with cls.app_test.app_context():
@cls.app_test.route('/hello')
@api_key_required
def hello_world():
return Response("hello, world!")

@cls.app_test.route('/helloopt')
@api_key_optional
def hello_world_opt():
return Response("hello, world!")
cls.app_test.testing = False

"""This is a lie, but it allows us to circumnavigate a check to prevent routes being added after first request:
AssertionError: The setup method 'route' can no longer be called on the application. It has already handled its
first request, any changes will not be applied consistently.
Make sure all imports, decorators, functions, etc. needed to set up the application are done before running it.
"""
cls.app_test._got_first_request = False

@cls.app_test.route('/hello')
@api_key_required
def hello_world():
return Response("hello, world!")

@cls.app_test.route('/helloopt')
@api_key_optional
def hello_world_opt():
return Response("hello, world!")

# Reinstate debug
cls.app_test.debug = True
Expand All @@ -37,7 +45,6 @@ def tearDownClass(cls) -> None:
# put debug back on
cls.app_test.debug = True

#@with_es(indices=[models.Account.__type__])
def test_01_api_role(self):
"""test the new roles added for the API"""
a1 = models.Account.make_account(email="[email protected]", username="a1_user", name="a1_name",
Expand All @@ -56,7 +63,6 @@ def test_01_api_role(self):
a1.remove_role('api')
assert a1.api_key is None

#@with_es(indices=[models.Account.__type__])
def test_02_api_required_decorator(self):
"""test the api_key_required decorator"""
a1 = models.Account.make_account(email="[email protected]", username="a1_user", name="a1_name",
Expand All @@ -79,7 +85,6 @@ def test_02_api_required_decorator(self):
response_denied = t_client.get('/hello?api_key=' + a2_key)
assert response_denied.status_code == 401

#@with_es(indices=[models.Account.__type__, models.Journal.__type__, models.Article.__type__])
def test_03_api_optional_decorator(self):
"""test the api_key_optional decorator"""
a1 = models.Account.make_account(email="[email protected]", username="a1_user", name="a1_name",
Expand Down

0 comments on commit 42e08e1

Please sign in to comment.