From 42e08e11a19804e638bb85f474734b83836f4eb5 Mon Sep 17 00:00:00 2001 From: Steven Eardley Date: Fri, 11 Oct 2024 12:42:24 +0100 Subject: [PATCH] Gaslight the app, so we're allowed to add a new route in API auth test. --- doajtest/unit/api_tests/test_api_account.py | 37 ++++++++++++--------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/doajtest/unit/api_tests/test_api_account.py b/doajtest/unit/api_tests/test_api_account.py index 0a2826499..829af49e4 100644 --- a/doajtest/unit/api_tests/test_api_account.py +++ b/doajtest/unit/api_tests/test_api_account.py @@ -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 @@ -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 @@ -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="a1@example.com", username="a1_user", name="a1_name", @@ -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="a1@example.com", username="a1_user", name="a1_name", @@ -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="a1@example.com", username="a1_user", name="a1_name",