Debian Codegraph @

tests/apps/headless/account/test_email_verification.py

raw
1 from http import HTTPStatus
2
3 import pytest
4
5 from allauth.account.models import (
6 EmailAddress,
7 EmailConfirmationHMAC,
8 get_emailconfirmation_model,
9 )
10 from allauth.headless.constants import Flow
11
12
13 def test_verify_email_other_user(auth_client, user, user_factory, headless_reverse):
14 other_user = user_factory(email_verified=False)
15 email_address = EmailAddress.objects.get(user=other_user, verified=False)
16 assert not email_address.verified
17 key = EmailConfirmationHMAC(email_address).key
18 resp = auth_client.post(
19 headless_reverse("headless:account:verify_email"),
20 data={"key": key},
21 content_type="application/json",
22 )
23 assert resp.status_code == HTTPStatus.OK
24 data = resp.json()
25 # We're still authenticated as the user originally logged in, not the
26 # other_user.
27 assert data["data"]["user"]["id"] == user.pk
28
29
30 @pytest.mark.parametrize(
31 "login_on_email_verification,status_code",
32 [(False, HTTPStatus.UNAUTHORIZED), (True, HTTPStatus.OK)],
33 )
34 def test_auth_unverified_email(
35 client,
36 user_factory,
37 password_factory,
38 settings,
39 headless_reverse,
40 login_on_email_verification,
41 status_code,
42 ):
43 settings.ACCOUNT_LOGIN_ON_EMAIL_CONFIRMATION = login_on_email_verification
44 settings.ACCOUNT_LOGIN_METHODS = {"email"}
45 settings.ACCOUNT_EMAIL_VERIFICATION = "mandatory"
46 password = password_factory()
47 user = user_factory(email_verified=False, password=password)
48 resp = client.post(
49 headless_reverse("headless:account:login"),
50 data={
51 "email": user.email,
52 "password": password,
53 },
54 content_type="application/json",
55 )
56 assert resp.status_code == HTTPStatus.UNAUTHORIZED
57 data = resp.json()
58 flows = data["data"]["flows"]
59 assert [f for f in flows if f["id"] == Flow.VERIFY_EMAIL][0]["is_pending"]
60 emailaddress = EmailAddress.objects.filter(user=user, verified=False).get()
61 key = get_emailconfirmation_model().create(emailaddress).key
62 resp = client.post(
63 headless_reverse("headless:account:verify_email"),
64 data={"key": key},
65 content_type="application/json",
66 )
67 assert resp.status_code == status_code
68
69
70 def test_verify_email_bad_key(
71 client, settings, password_factory, user_factory, headless_reverse
72 ):
73 settings.ACCOUNT_LOGIN_METHODS = {"email"}
74 settings.ACCOUNT_EMAIL_VERIFICATION = "mandatory"
75 password = password_factory()
76 user = user_factory(email_verified=False, password=password)
77 resp = client.post(
78 headless_reverse("headless:account:login"),
79 data={
80 "email": user.email,
81 "password": password,
82 },
83 content_type="application/json",
84 )
85 assert resp.status_code == HTTPStatus.UNAUTHORIZED
86 resp = client.get(
87 headless_reverse("headless:account:verify_email"),
88 data={"key": "bad"},
89 content_type="application/json",
90 )
91 assert resp.status_code == HTTPStatus.BAD_REQUEST
92 resp = client.post(
93 headless_reverse("headless:account:verify_email"),
94 data={"key": "bad"},
95 content_type="application/json",
96 )
97 assert resp.status_code == HTTPStatus.BAD_REQUEST
98 assert resp.json() == {
99 "status": HTTPStatus.BAD_REQUEST,
100 "errors": [
101 {
102 "code": "invalid_or_expired_key",
103 "param": "key",
104 "message": "Invalid or expired key.",
105 }
106 ],
107 }