Skip to content

Commit

Permalink
✨ Provide test utility to disable MFA (during tests)
Browse files Browse the repository at this point in the history
TODO: need to look into django-webtest backend and add it
to the list.
  • Loading branch information
sergei-maertens committed Feb 1, 2024
1 parent a806a3b commit 7bcb7ec
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
14 changes: 14 additions & 0 deletions maykin_2fa/test/__init__.py
Original file line number Diff line number Diff line change
@@ -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,
)
17 changes: 17 additions & 0 deletions tests/test_test_utils.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 7bcb7ec

Please sign in to comment.