| 1 |
from http import HTTPStatus |
| 2 |
from unittest.mock import ANY |
| 3 |
|
| 4 |
from django.conf import settings |
| 5 |
from django.urls import reverse |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
from allauth.account.authentication import AUTHENTICATION_METHODS_SESSION_KEY |
| 10 |
from allauth.mfa import app_settings |
| 11 |
from allauth.mfa.adapter import get_adapter |
| 12 |
from allauth.mfa.models import Authenticator |
| 13 |
|
| 14 |
|
| 15 |
def test_generate_recovery_codes_require_other_authenticator( |
| 16 |
auth_client, user, settings, reauthentication_bypass |
| 17 |
): |
| 18 |
with reauthentication_bypass(): |
| 19 |
resp = auth_client.post(reverse("mfa_generate_recovery_codes")) |
| 20 |
assert resp.context["form"].errors == { |
| 21 |
"__all__": [ |
| 22 |
"You cannot generate recovery codes without having two-factor authentication enabled." |
| 23 |
] |
| 24 |
} |
| 25 |
assert not Authenticator.objects.filter(user=user).exists() |
| 26 |
|
| 27 |
|
| 28 |
@pytest.mark.parametrize( |
| 29 |
"show_once, status", [(False, HTTPStatus.OK), (True, HTTPStatus.FORBIDDEN)] |
| 30 |
) |
| 31 |
def test_download_recovery_codes( |
| 32 |
auth_client, user_with_recovery_codes, user_password, settings, show_once, status |
| 33 |
): |
| 34 |
settings.MFA_RECOVERY_CODES_SHOW_ONCE = show_once |
| 35 |
resp = auth_client.get(reverse("mfa_download_recovery_codes")) |
| 36 |
assert resp["location"].startswith(reverse("account_reauthenticate")) |
| 37 |
resp = auth_client.post(resp["location"], {"password": user_password}) |
| 38 |
assert resp.status_code == HTTPStatus.FOUND |
| 39 |
resp = auth_client.get(resp["location"]) |
| 40 |
assert resp["content-disposition"] == 'attachment; filename="recovery-codes.txt"' |
| 41 |
# Download a 2nd time |
| 42 |
resp = auth_client.get(reverse("mfa_download_recovery_codes")) |
| 43 |
assert resp.status_code == status |
| 44 |
|
| 45 |
|
| 46 |
@pytest.mark.parametrize("show_once", [False, True]) |
| 47 |
def test_view_recovery_codes( |
| 48 |
auth_client, user_with_recovery_codes, user_password, settings, show_once |
| 49 |
): |
| 50 |
settings.MFA_RECOVERY_CODES_SHOW_ONCE = show_once |
| 51 |
resp = auth_client.get(reverse("mfa_view_recovery_codes")) |
| 52 |
assert resp["location"].startswith(reverse("account_reauthenticate")) |
| 53 |
resp = auth_client.post(resp["location"], {"password": user_password}) |
| 54 |
assert resp.status_code == HTTPStatus.FOUND |
| 55 |
resp = auth_client.get(resp["location"]) |
| 56 |
assert len(resp.context["unused_codes"]) == app_settings.RECOVERY_CODE_COUNT |
| 57 |
assert resp.context["can_view_codes"] |
| 58 |
|
| 59 |
# View a 2nd time |
| 60 |
resp = auth_client.get(reverse("mfa_view_recovery_codes")) |
| 61 |
assert resp.status_code == HTTPStatus.OK |
| 62 |
assert resp.context["can_view_codes"] == (not show_once) |
| 63 |
|
| 64 |
|
| 65 |
def test_generate_recovery_codes( |
| 66 |
auth_client, user_with_recovery_codes, user_password, settings, mailoutbox |
| 67 |
): |
| 68 |
settings.ACCOUNT_EMAIL_NOTIFICATIONS = True |
| 69 |
rc = Authenticator.objects.get( |
| 70 |
user=user_with_recovery_codes, type=Authenticator.Type.RECOVERY_CODES |
| 71 |
).wrap() |
| 72 |
prev_code = rc.get_unused_codes()[0] |
| 73 |
|
| 74 |
resp = auth_client.get(reverse("mfa_generate_recovery_codes")) |
| 75 |
assert resp["location"].startswith(reverse("account_reauthenticate")) |
| 76 |
resp = auth_client.post(resp["location"], {"password": user_password}) |
| 77 |
assert resp.status_code == HTTPStatus.FOUND |
| 78 |
resp = auth_client.post(resp["location"]) |
| 79 |
assert resp["location"] == reverse("mfa_view_recovery_codes") |
| 80 |
|
| 81 |
rc = Authenticator.objects.get( |
| 82 |
user=user_with_recovery_codes, type=Authenticator.Type.RECOVERY_CODES |
| 83 |
).wrap() |
| 84 |
assert not rc.validate_code(prev_code) |
| 85 |
assert len(mailoutbox) == 1 |
| 86 |
assert "New Recovery Codes Generated" in mailoutbox[0].subject |
| 87 |
assert "A new set of" in mailoutbox[0].body |
| 88 |
|
| 89 |
|
| 90 |
def test_recovery_codes_login( |
| 91 |
client, user_with_totp, user_with_recovery_codes, user_password |
| 92 |
): |
| 93 |
resp = client.post( |
| 94 |
reverse("account_login"), |
| 95 |
{"login": user_with_totp.username, "password": user_password}, |
| 96 |
) |
| 97 |
assert resp.status_code == HTTPStatus.FOUND |
| 98 |
assert resp["location"] == reverse("mfa_authenticate") |
| 99 |
resp = client.get(reverse("mfa_authenticate")) |
| 100 |
assert resp.context["request"].user.is_anonymous |
| 101 |
resp = client.post(reverse("mfa_authenticate"), {"code": "123"}) |
| 102 |
assert resp.context["form"].errors == { |
| 103 |
"code": [get_adapter().error_messages["incorrect_code"]] |
| 104 |
} |
| 105 |
rc = Authenticator.objects.get( |
| 106 |
user=user_with_recovery_codes, type=Authenticator.Type.RECOVERY_CODES |
| 107 |
) |
| 108 |
resp = client.post( |
| 109 |
reverse("mfa_authenticate"), |
| 110 |
{"code": rc.wrap().get_unused_codes()[0]}, |
| 111 |
) |
| 112 |
assert resp.status_code == HTTPStatus.FOUND |
| 113 |
assert resp["location"] == settings.LOGIN_REDIRECT_URL |
| 114 |
assert client.session[AUTHENTICATION_METHODS_SESSION_KEY] == [ |
| 115 |
{"method": "password", "at": ANY, "username": user_with_totp.username}, |
| 116 |
{ |
| 117 |
"method": "mfa", |
| 118 |
"at": ANY, |
| 119 |
"id": ANY, |
| 120 |
"type": Authenticator.Type.RECOVERY_CODES, |
| 121 |
}, |
| 122 |
] |