Skip to content

Commit

Permalink
fix(headless): Login by code, when expired, now returns 409
Browse files Browse the repository at this point in the history
  • Loading branch information
pennersr committed Oct 13, 2024
1 parent e9972e6 commit d203ecf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
32 changes: 32 additions & 0 deletions allauth/headless/account/tests/test_login_by_code.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import time

from allauth.account.models import EmailAddress
from allauth.headless.constants import Flow

Expand Down Expand Up @@ -112,3 +114,33 @@ def test_login_by_code_required(
assert data["meta"]["is_authenticated"]
email_address.refresh_from_db()
assert email_address.verified


def test_login_by_code_expired(headless_reverse, user, client, mailoutbox):
resp = client.post(
headless_reverse("headless:account:request_login_code"),
data={"email": user.email},
content_type="application/json",
)
assert resp.status_code == 401
data = resp.json()
assert [f for f in data["data"]["flows"] if f["id"] == Flow.LOGIN_BY_CODE][0][
"is_pending"
]
assert len(mailoutbox) == 1
code = [line for line in mailoutbox[0].body.splitlines() if len(line) == 6][0]

# Expire code
session = client.headless_session()
login = session["account_login"]
login["state"]["login_code"]["at"] = time.time() - 24 * 60 * 60
session["account_login"] = login
session.save()

# Post valid code
resp = client.post(
headless_reverse("headless:account:confirm_login_code"),
data={"code": code},
content_type="application/json",
)
assert resp.status_code == 409
2 changes: 2 additions & 0 deletions allauth/headless/account/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ def dispatch(self, request, *args, **kwargs):
self.user, self.pending_login = flows.login_by_code.get_pending_login(
request, self.stage.login, peek=True
)
if not self.pending_login:
return ConflictResponse(request)
return super().dispatch(request, *args, **kwargs)

def post(self, request, *args, **kwargs):
Expand Down

0 comments on commit d203ecf

Please sign in to comment.