-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🎨 [#1637] added disable_admin_mfa directly in tests
- Loading branch information
1 parent
37ad034
commit 26749c7
Showing
8 changed files
with
89 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,49 @@ | ||
import factory | ||
from django_otp.plugins.otp_static.models import StaticDevice, StaticToken | ||
from django_otp.util import random_hex | ||
|
||
|
||
class TOTPDeviceFactory(factory.django.DjangoModelFactory): | ||
user = factory.SubFactory("nrc.accounts.tests.factories.UserFactory") | ||
key = factory.LazyAttribute(lambda o: random_hex()) | ||
|
||
class Meta: | ||
model = "otp_totp.TOTPDevice" | ||
|
||
|
||
class UserFactory(factory.django.DjangoModelFactory): | ||
username = factory.Sequence(lambda n: f"user-{n}") | ||
password = factory.PostGenerationMethodCall("set_password", "secret") | ||
|
||
class Params: | ||
with_totp_device = factory.Trait( | ||
device=factory.RelatedFactory( | ||
TOTPDeviceFactory, | ||
"user", | ||
name="default", | ||
) | ||
) | ||
|
||
class Meta: | ||
model = "accounts.User" | ||
|
||
|
||
class SuperUserFactory(UserFactory): | ||
is_staff = True | ||
is_superuser = True | ||
|
||
|
||
class RecoveryDeviceFactory(factory.django.DjangoModelFactory): | ||
user = factory.SubFactory("nrc.accounts.tests.factories.UserFactory") | ||
name = "backup" | ||
|
||
class Meta: | ||
model = StaticDevice | ||
|
||
|
||
class RecoveryTokenFactory(factory.django.DjangoModelFactory): | ||
device = factory.SubFactory(RecoveryDeviceFactory) | ||
token = factory.LazyFunction(StaticToken.random_token) | ||
|
||
class Meta: | ||
model = StaticToken |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from django.test import override_settings, tag | ||
from django.urls import reverse, reverse_lazy | ||
from django.utils.translation import gettext | ||
|
||
from django_webtest import WebTest | ||
|
||
from nrc.accounts.tests.factories import RecoveryTokenFactory, SuperUserFactory | ||
|
||
LOGIN_URL = reverse_lazy("admin:login") | ||
|
||
|
||
@override_settings( | ||
USE_OIDC_FOR_ADMIN_LOGIN=False, | ||
MAYKIN_2FA_ALLOW_MFA_BYPASS_BACKENDS=[], # enforce MFA | ||
) | ||
class RecoveryTokenTests(WebTest): | ||
@tag("gh-4072") | ||
def test_can_enter_recovery_token(self): | ||
user = SuperUserFactory.create( | ||
with_totp_device=True, | ||
username="admin", | ||
password="admin", | ||
) | ||
recovery_token = RecoveryTokenFactory.create(device__user=user).token | ||
login_page = self.app.get(LOGIN_URL, auto_follow=True) | ||
|
||
# log in with credentials | ||
form = login_page.forms["login-form"] | ||
form["auth-username"] = "admin" | ||
form["auth-password"] = "admin" | ||
response = form.submit() | ||
|
||
# we should now be on the enter-your-token page | ||
form = response.forms["login-form"] | ||
self.assertIn("token-otp_token", form.fields) | ||
|
||
# do not enter a token, but follow the link to use a recovery token | ||
link_name = gettext("Use a recovery token") | ||
recovery_page = response.click(description=link_name) | ||
self.assertEqual(recovery_page.status_code, 200) | ||
|
||
recovery_form = recovery_page.forms[0] | ||
recovery_form["backup-otp_token"] = recovery_token | ||
admin_index = recovery_form.submit().follow() | ||
self.assertEqual(admin_index.request.path, reverse("admin:index")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.