diff --git a/src/openforms/registrations/tests/test_pre_registration.py b/src/openforms/registrations/tests/test_pre_registration.py index 498c9575e4..cb4bcd8ed9 100644 --- a/src/openforms/registrations/tests/test_pre_registration.py +++ b/src/openforms/registrations/tests/test_pre_registration.py @@ -14,6 +14,7 @@ from openforms.submissions.constants import PostSubmissionEvents from openforms.submissions.tasks.registration import pre_registration from openforms.submissions.tests.factories import SubmissionFactory +from openforms.utils.tests.logging import ensure_logger_level class PreRegistrationTests(TestCase): @@ -272,6 +273,7 @@ def test_retry_after_too_many_attempts_skips(self, m_get_solo): patch( "openforms.registrations.contrib.zgw_apis.plugin.ZGWRegistration.pre_register_submission" ) as mock_pre_register, + ensure_logger_level("DEBUG"), self.assertLogs(level="DEBUG") as logs, ): pre_registration(submission.id, PostSubmissionEvents.on_retry) diff --git a/src/openforms/registrations/tests/test_registration_hook.py b/src/openforms/registrations/tests/test_registration_hook.py index 96cd41f51c..8ef7341c74 100644 --- a/src/openforms/registrations/tests/test_registration_hook.py +++ b/src/openforms/registrations/tests/test_registration_hook.py @@ -19,6 +19,7 @@ from openforms.logging.models import TimelineLogProxy from openforms.submissions.constants import PostSubmissionEvents, RegistrationStatuses from openforms.submissions.tests.factories import SubmissionFactory +from openforms.utils.tests.logging import ensure_logger_level from ..base import BasePlugin from ..exceptions import RegistrationFailed @@ -286,6 +287,7 @@ def register_submission(self, submission, options): with self.subTest("on retry"): with ( + ensure_logger_level("DEBUG"), self.assertRaises(RegistrationFailed), self.assertLogs(level="DEBUG") as logs, ): diff --git a/src/openforms/submissions/tests/test_post_submission_event.py b/src/openforms/submissions/tests/test_post_submission_event.py index a62cec967a..e770887e10 100644 --- a/src/openforms/submissions/tests/test_post_submission_event.py +++ b/src/openforms/submissions/tests/test_post_submission_event.py @@ -16,6 +16,7 @@ from openforms.forms.tests.factories import FormDefinitionFactory from openforms.payments.constants import PaymentStatus from openforms.payments.tests.factories import SubmissionPaymentFactory +from openforms.utils.tests.logging import ensure_logger_level from ..constants import PostSubmissionEvents from ..models import SubmissionReport @@ -1058,6 +1059,7 @@ def test_payment_required_and_not_should_wait_for_registration(self): "openforms.registrations.tasks.GlobalConfiguration.get_solo", return_value=GlobalConfiguration(wait_for_payment_to_register=True), ), + ensure_logger_level("DEBUG"), LogCapture() as logs, ): on_post_submission_event(submission.id, PostSubmissionEvents.on_completion) diff --git a/src/openforms/utils/tests/logging.py b/src/openforms/utils/tests/logging.py index 8966c4600b..f37fd235c5 100644 --- a/src/openforms/utils/tests/logging.py +++ b/src/openforms/utils/tests/logging.py @@ -1,4 +1,6 @@ import logging +from contextlib import contextmanager +from typing import Iterator from django.test.utils import TestContextDecorator @@ -9,3 +11,23 @@ def enable(self): def disable(self): logging.disable(logging.NOTSET) + + +@contextmanager +def ensure_logger_level(level: str = "INFO", name="openforms") -> Iterator[None]: + """ + Set the (minimum) level for a given logger. + + Base or individual settings may tweak the log level to reduce the overload for the + developer, but this affects tests that verify that log records are "written" by + application code for a certain log level. + + This context manager allows you to pin the log level for a given text, irrespective + of individual preferences/developer settings. + """ + logger = logging.getLogger(name) + original_level = logger.getEffectiveLevel() + + logger.setLevel(logging._nameToLevel[level]) + yield + logger.setLevel(original_level)