From 6b24b3072c722ec93869164eba7795d4af841ac9 Mon Sep 17 00:00:00 2001 From: Katie Smith Date: Wed, 3 Nov 2021 09:33:16 +0000 Subject: [PATCH] Fix failing test with new Python version This test is now failing due to the combination of both Werkzeug having had a major version bump in a previous PR and the Python version changing. Calling `current_service` is now giving an error when trying to get the `service` from the `_request_ctx_stack` because `_request_ctx_stack` has no `service` attribute. There was no error before, so it appears some underlying behaviour has changed. I've fixed the test by changing the setup, but there is no effect on production code - when the app is running `load_service_before_request` is called before every request. --- tests/app/test_template_previews.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/app/test_template_previews.py b/tests/app/test_template_previews.py index 37554be34..1954c3c9d 100644 --- a/tests/app/test_template_previews.py +++ b/tests/app/test_template_previews.py @@ -5,6 +5,7 @@ from unittest.mock import Mock import pytest from notifications_utils.template import LetterPreviewTemplate +from app import load_service_before_request from app.template_previews import ( TemplatePreview, get_page_count_for_letter, @@ -58,6 +59,10 @@ def test_from_database_object_makes_request( expected_filename, mock_get_service_letter_template ): + # This test is calling `current_service` outside a Flask endpoint, so we need to make sure + # `service` is in the `_request_ctx_stack` to avoid an error + load_service_before_request() + resp = Mock(content='a', status_code='b', headers={'c': 'd'}) request_mock = mocker.patch('app.template_previews.requests.post', return_value=resp) mocker.patch('app.template_previews.current_service', letter_branding=letter_branding)