From 7bcb7ec13652678e5e95e006d567030ec9d1c399 Mon Sep 17 00:00:00 2001 From: Sergei Maertens Date: Thu, 1 Feb 2024 14:05:50 +0100 Subject: [PATCH] :sparkles: Provide test utility to disable MFA (during tests) TODO: need to look into django-webtest backend and add it to the list. --- maykin_2fa/test/__init__.py | 14 ++++++++++++++ tests/test_test_utils.py | 17 +++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 maykin_2fa/test/__init__.py create mode 100644 tests/test_test_utils.py diff --git a/maykin_2fa/test/__init__.py b/maykin_2fa/test/__init__.py new file mode 100644 index 0000000..2807696 --- /dev/null +++ b/maykin_2fa/test/__init__.py @@ -0,0 +1,14 @@ +from django.conf import settings +from django.test import override_settings + + +def disable_admin_mfa(): + """ + Test helper to disable MFA requirements in the admin. + + Based on :func:`django.test.override_settings`, so you can use it as a decorator + or context manager. + """ + return override_settings( + MAYKIN_2FA_ALLOW_MFA_BYPASS_BACKENDS=settings.AUTHENTICATION_BACKENDS, + ) diff --git a/tests/test_test_utils.py b/tests/test_test_utils.py new file mode 100644 index 0000000..79ee02a --- /dev/null +++ b/tests/test_test_utils.py @@ -0,0 +1,17 @@ +from django.contrib.auth.models import User +from django.test import TestCase +from django.urls import reverse + +from maykin_2fa.test import disable_admin_mfa + + +class TestHelperTests(TestCase): + + def test_mfa_disabling(self): + User.objects.create_user(username="johny", password="password", is_staff=True) + self.client.login(username="johny", password="password") + + with disable_admin_mfa(): + response = self.client.get(reverse("admin:index")) + + self.assertEqual(response.status_code, 200)