From 5b5d4af681201751e73461c3f7f3a757d483a5f1 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Fri, 27 May 2022 15:44:29 +0100 Subject: [PATCH] Work around inconsistent request context in tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The failing test here[1] does two things: 1. makes a request to /sign-out 2. calls the index route, without actually making a request This means that when the `login_manager.unauthorized_handler`[2] looks at Flask’s `request` object it gets the request context from 1., not 2., because 2. isn’t actually a request. The means it sets the value of the `next` parameter to that of the request, not of the index route. Basically at some point Flask has changed and decided that 2. isn’t a proper request, so won’t set new request context. This isn’t a realistic test because nothing would call the index function directly, it would always be as part of a request to that page. But to make the minimal change to fix the breaking tests this commit makes the check a bit more general, i.e. that the redirect is to the sign in page with any `next` parameter, not a specific `next` parameter. 1. https://github.com/alphagov/notifications-admin/blob/9111a7fc86b4ed2f88f9f913c0ea5bfe98f20ead/tests/app/utils/test_user.py#L130-L138 2. https://github.com/alphagov/notifications-admin/blob/9111a7fc86b4ed2f88f9f913c0ea5bfe98f20ead/app/main/views/sign_in.py#L86 --- tests/app/utils/test_user.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/app/utils/test_user.py b/tests/app/utils/test_user.py index 266eadfa3..d9811e71c 100644 --- a/tests/app/utils/test_user.py +++ b/tests/app/utils/test_user.py @@ -24,9 +24,10 @@ def _test_permissions( decorated_index() else: try: - if ( - decorated_index().location != '/sign-in?next=%2F' or - decorated_index().status_code != 302 + response = decorated_index() + if not ( + response.location.startswith('/sign-in?next=') and + response.status_code == 302 ): pytest.fail("Failed to throw a forbidden or unauthorised exception") except (Forbidden, Unauthorized):