| 1 |
from unittest.mock import patch |
| 2 |
|
| 3 |
from django.contrib.auth import get_user_model |
| 4 |
from django.test import TestCase |
| 5 |
from django.test.utils import override_settings |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
from allauth.account import app_settings |
| 10 |
from allauth.account.auth_backends import AuthenticationBackend |
| 11 |
|
| 12 |
|
| 13 |
class AuthenticationBackendTests(TestCase): |
| 14 |
def setUp(self): |
| 15 |
user = get_user_model().objects.create( |
| 16 |
is_active=True, email="[email protected]", username="john" |
| 17 |
) |
| 18 |
user.set_password(user.username) |
| 19 |
user.save() |
| 20 |
self.user = user |
| 21 |
|
| 22 |
@override_settings( |
| 23 |
ACCOUNT_LOGIN_METHODS={app_settings.LoginMethod.USERNAME} |
| 24 |
) # noqa |
| 25 |
def test_auth_by_username(self): |
| 26 |
user = self.user |
| 27 |
backend = AuthenticationBackend() |
| 28 |
self.assertEqual( |
| 29 |
backend.authenticate( |
| 30 |
request=None, username=user.username, password=user.username |
| 31 |
).pk, |
| 32 |
user.pk, |
| 33 |
) |
| 34 |
self.assertEqual( |
| 35 |
backend.authenticate( |
| 36 |
request=None, username=user.email, password=user.username |
| 37 |
), |
| 38 |
None, |
| 39 |
) |
| 40 |
|
| 41 |
@override_settings(ACCOUNT_LOGIN_METHODS={app_settings.LoginMethod.EMAIL}) # noqa |
| 42 |
def test_auth_by_email(self): |
| 43 |
user = self.user |
| 44 |
backend = AuthenticationBackend() |
| 45 |
self.assertEqual( |
| 46 |
backend.authenticate( |
| 47 |
request=None, username=user.email, password=user.username |
| 48 |
).pk, |
| 49 |
user.pk, |
| 50 |
) |
| 51 |
self.assertEqual( |
| 52 |
backend.authenticate( |
| 53 |
request=None, username=user.username, password=user.username |
| 54 |
), |
| 55 |
None, |
| 56 |
) |
| 57 |
|
| 58 |
@override_settings( |
| 59 |
ACCOUNT_LOGIN_METHODS={ |
| 60 |
app_settings.LoginMethod.EMAIL, |
| 61 |
app_settings.LoginMethod.USERNAME, |
| 62 |
} |
| 63 |
) # noqa |
| 64 |
def test_auth_by_username_or_email(self): |
| 65 |
user = self.user |
| 66 |
backend = AuthenticationBackend() |
| 67 |
self.assertEqual( |
| 68 |
backend.authenticate( |
| 69 |
request=None, username=user.email, password=user.username |
| 70 |
).pk, |
| 71 |
user.pk, |
| 72 |
) |
| 73 |
self.assertEqual( |
| 74 |
backend.authenticate( |
| 75 |
request=None, username=user.username, password=user.username |
| 76 |
).pk, |
| 77 |
user.pk, |
| 78 |
) |
| 79 |
|
| 80 |
|
| 81 |
@pytest.mark.parametrize( |
| 82 |
"login_methods", |
| 83 |
[ |
| 84 |
{app_settings.LoginMethod.EMAIL}, |
| 85 |
{app_settings.LoginMethod.USERNAME}, |
| 86 |
{app_settings.LoginMethod.USERNAME, app_settings.LoginMethod.EMAIL}, |
| 87 |
], |
| 88 |
) |
| 89 |
def test_account_enumeration_timing_attack(user, db, rf, settings, login_methods): |
| 90 |
settings.ACCOUNT_LOGIN_METHODS = login_methods |
| 91 |
with patch("django.contrib.auth.models.User.set_password") as set_password_mock: |
| 92 |
with patch( |
| 93 |
"django.contrib.auth.models.User.check_password", new=set_password_mock |
| 94 |
): |
| 95 |
backend = AuthenticationBackend() |
| 96 |
backend.authenticate( |
| 97 |
rf.get("/"), |
| 98 |
email="[email protected]", |
| 99 |
username="not-known", |
| 100 |
password="secret", |
| 101 |
) |
| 102 |
set_password_mock.assert_called_once() |
| 103 |
set_password_mock.reset_mock() |
| 104 |
backend.authenticate(rf.get("/"), username=user.username, password="secret") |
| 105 |
set_password_mock.assert_called_once() |
| 106 |
set_password_mock.reset_mock() |
| 107 |
backend.authenticate( |
| 108 |
rf.get("/"), email=user.email, username="not-known", password="secret" |
| 109 |
) |
| 110 |
set_password_mock.assert_called_once() |