| 1 |
import time |
| 2 |
from http import HTTPStatus |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
from allauth.account.internal.stagekit import LOGIN_SESSION_KEY |
| 7 |
from allauth.account.models import EmailAddress |
| 8 |
from allauth.account.stages import LoginByCodeStage |
| 9 |
from allauth.headless.constants import Flow |
| 10 |
|
| 11 |
|
| 12 |
@pytest.fixture |
| 13 |
def get_last_login_code(mailoutbox): |
| 14 |
def f(): |
| 15 |
return [line for line in mailoutbox[-1].body.splitlines() if len(line) == 9][0] |
| 16 |
|
| 17 |
return f |
| 18 |
|
| 19 |
|
| 20 |
def test_login_by_code(headless_reverse, user, client, mailoutbox, get_last_login_code): |
| 21 |
resp = client.post( |
| 22 |
headless_reverse("headless:account:request_login_code"), |
| 23 |
data={"email": user.email}, |
| 24 |
content_type="application/json", |
| 25 |
) |
| 26 |
assert resp.status_code == HTTPStatus.UNAUTHORIZED |
| 27 |
data = resp.json() |
| 28 |
assert [f for f in data["data"]["flows"] if f["id"] == Flow.LOGIN_BY_CODE][0][ |
| 29 |
"is_pending" |
| 30 |
] |
| 31 |
assert len(mailoutbox) == 1 |
| 32 |
code = get_last_login_code() |
| 33 |
resp = client.post( |
| 34 |
headless_reverse("headless:account:confirm_login_code"), |
| 35 |
data={"code": code}, |
| 36 |
content_type="application/json", |
| 37 |
) |
| 38 |
assert resp.status_code == HTTPStatus.OK |
| 39 |
data = resp.json() |
| 40 |
assert data["meta"]["is_authenticated"] |
| 41 |
|
| 42 |
|
| 43 |
def test_login_by_code_rate_limit( |
| 44 |
headless_reverse, user, client, mailoutbox, settings, enable_cache |
| 45 |
): |
| 46 |
settings.ACCOUNT_RATE_LIMITS = {"request_login_code": "1/m/ip"} |
| 47 |
for attempt in range(2): |
| 48 |
resp = client.post( |
| 49 |
headless_reverse("headless:account:request_login_code"), |
| 50 |
data={"email": user.email}, |
| 51 |
content_type="application/json", |
| 52 |
) |
| 53 |
expected_code = HTTPStatus.BAD_REQUEST if attempt else HTTPStatus.UNAUTHORIZED |
| 54 |
assert resp.status_code == expected_code |
| 55 |
data = resp.json() |
| 56 |
assert data["status"] == expected_code |
| 57 |
if expected_code == HTTPStatus.BAD_REQUEST: |
| 58 |
assert data["errors"] == [ |
| 59 |
{ |
| 60 |
"code": "too_many_login_attempts", |
| 61 |
"message": "Too many failed login attempts. Try again later.", |
| 62 |
"param": "email", |
| 63 |
}, |
| 64 |
] |
| 65 |
|
| 66 |
|
| 67 |
def test_login_by_code_max_attemps(headless_reverse, user, client, settings): |
| 68 |
settings.ACCOUNT_LOGIN_BY_CODE_MAX_ATTEMPTS = 2 |
| 69 |
resp = client.post( |
| 70 |
headless_reverse("headless:account:request_login_code"), |
| 71 |
data={"email": user.email}, |
| 72 |
content_type="application/json", |
| 73 |
) |
| 74 |
assert resp.status_code == HTTPStatus.UNAUTHORIZED |
| 75 |
for i in range(3): |
| 76 |
resp = client.post( |
| 77 |
headless_reverse("headless:account:confirm_login_code"), |
| 78 |
data={"code": "wrong"}, |
| 79 |
content_type="application/json", |
| 80 |
) |
| 81 |
session_resp = client.get( |
| 82 |
headless_reverse("headless:account:current_session"), |
| 83 |
data={"code": "wrong"}, |
| 84 |
content_type="application/json", |
| 85 |
) |
| 86 |
assert session_resp.status_code == HTTPStatus.UNAUTHORIZED |
| 87 |
pending_flows = [ |
| 88 |
f for f in session_resp.json()["data"]["flows"] if f.get("is_pending") |
| 89 |
] |
| 90 |
if i >= 1: |
| 91 |
assert ( |
| 92 |
resp.status_code == HTTPStatus.CONFLICT |
| 93 |
if i >= 2 |
| 94 |
else HTTPStatus.BAD_REQUEST |
| 95 |
) |
| 96 |
assert len(pending_flows) == 0 |
| 97 |
else: |
| 98 |
assert resp.status_code == HTTPStatus.BAD_REQUEST |
| 99 |
assert len(pending_flows) == 1 |
| 100 |
|
| 101 |
|
| 102 |
def test_login_by_code_required( |
| 103 |
client, settings, user_factory, password_factory, headless_reverse, mailoutbox |
| 104 |
): |
| 105 |
settings.ACCOUNT_LOGIN_BY_CODE_REQUIRED = True |
| 106 |
password = password_factory() |
| 107 |
user = user_factory(password=password, email_verified=False) |
| 108 |
email_address = EmailAddress.objects.get(email=user.email) |
| 109 |
assert not email_address.verified |
| 110 |
resp = client.post( |
| 111 |
headless_reverse("headless:account:login"), |
| 112 |
data={ |
| 113 |
"username": user.username, |
| 114 |
"password": password, |
| 115 |
}, |
| 116 |
content_type="application/json", |
| 117 |
) |
| 118 |
assert resp.status_code == HTTPStatus.UNAUTHORIZED |
| 119 |
pending_flow = [f for f in resp.json()["data"]["flows"] if f.get("is_pending")][0][ |
| 120 |
"id" |
| 121 |
] |
| 122 |
assert pending_flow == Flow.LOGIN_BY_CODE |
| 123 |
code = [line for line in mailoutbox[0].body.splitlines() if len(line) == 9][0] |
| 124 |
resp = client.post( |
| 125 |
headless_reverse("headless:account:confirm_login_code"), |
| 126 |
data={"code": code}, |
| 127 |
content_type="application/json", |
| 128 |
) |
| 129 |
assert resp.status_code == HTTPStatus.OK |
| 130 |
data = resp.json() |
| 131 |
assert data["meta"]["is_authenticated"] |
| 132 |
email_address.refresh_from_db() |
| 133 |
assert email_address.verified |
| 134 |
|
| 135 |
|
| 136 |
def test_login_by_code_expired(headless_reverse, user, client, mailoutbox): |
| 137 |
resp = client.post( |
| 138 |
headless_reverse("headless:account:request_login_code"), |
| 139 |
data={"email": user.email}, |
| 140 |
content_type="application/json", |
| 141 |
) |
| 142 |
assert resp.status_code == HTTPStatus.UNAUTHORIZED |
| 143 |
data = resp.json() |
| 144 |
assert [f for f in data["data"]["flows"] if f["id"] == Flow.LOGIN_BY_CODE][0][ |
| 145 |
"is_pending" |
| 146 |
] |
| 147 |
assert len(mailoutbox) == 1 |
| 148 |
code = [line for line in mailoutbox[0].body.splitlines() if len(line) == 9][0] |
| 149 |
|
| 150 |
# Expire code |
| 151 |
session = client.headless_session() |
| 152 |
login = session[LOGIN_SESSION_KEY] |
| 153 |
login["state"]["stages"][LoginByCodeStage.key]["data"]["at"] = ( |
| 154 |
time.time() - 24 * 60 * 60 |
| 155 |
) |
| 156 |
session["account_login"] = login |
| 157 |
session.save() |
| 158 |
|
| 159 |
# Post valid code |
| 160 |
resp = client.post( |
| 161 |
headless_reverse("headless:account:confirm_login_code"), |
| 162 |
data={"code": code}, |
| 163 |
content_type="application/json", |
| 164 |
) |
| 165 |
assert resp.status_code == HTTPStatus.CONFLICT |
| 166 |
|
| 167 |
|
| 168 |
def test_post_login_code_when_flow_not_pending( |
| 169 |
db, |
| 170 |
client, |
| 171 |
settings_impacting_urls, |
| 172 |
headless_reverse, |
| 173 |
mailoutbox, |
| 174 |
get_last_email_verification_code, |
| 175 |
): |
| 176 |
with settings_impacting_urls( |
| 177 |
ACCOUNT_LOGIN_METHODS={"email"}, |
| 178 |
ACCOUNT_SIGNUP_FIELDS=["email*"], |
| 179 |
ACCOUNT_EMAIL_VERIFICATION="mandatory", |
| 180 |
ACCOUNT_LOGIN_BY_CODE_ENABLED=True, |
| 181 |
ACCOUNT_LOGIN_BY_CODE_REQUIRED=False, |
| 182 |
ACCOUNT_EMAIL_VERIFICATION_BY_CODE_ENABLED=True, |
| 183 |
): |
| 184 |
resp = client.post( |
| 185 |
headless_reverse("headless:account:signup"), |
| 186 |
data={"email": "[email protected]"}, |
| 187 |
content_type="application/json", |
| 188 |
) |
| 189 |
assert resp.status_code == HTTPStatus.UNAUTHORIZED |
| 190 |
resp = client.post( |
| 191 |
headless_reverse("headless:account:confirm_login_code"), |
| 192 |
data={"code": "123"}, |
| 193 |
content_type="application/json", |
| 194 |
) |
| 195 |
assert resp.status_code == HTTPStatus.CONFLICT |
| 196 |
|
| 197 |
|
| 198 |
def test_login_by_code_resend_limited( |
| 199 |
settings, headless_reverse, user, client, mailoutbox, get_last_login_code |
| 200 |
): |
| 201 |
settings.ACCOUNT_LOGIN_BY_CODE_SUPPORTS_RESEND = 2 |
| 202 |
resp = client.post( |
| 203 |
headless_reverse("headless:account:request_login_code"), |
| 204 |
data={"email": user.email}, |
| 205 |
content_type="application/json", |
| 206 |
) |
| 207 |
assert resp.status_code == HTTPStatus.UNAUTHORIZED |
| 208 |
code = get_last_login_code() |
| 209 |
for i in range(3): |
| 210 |
resp = client.post( |
| 211 |
headless_reverse("headless:account:resend_login_code"), |
| 212 |
) |
| 213 |
assert resp.status_code == (HTTPStatus.CONFLICT if i == 2 else HTTPStatus.OK) |
| 214 |
new_code = get_last_login_code() |
| 215 |
if i == 2: |
| 216 |
assert new_code == code |
| 217 |
else: |
| 218 |
assert new_code != code |
| 219 |
code = new_code |