| 1 |
import json |
| 2 |
from http import HTTPStatus |
| 3 |
from unittest.mock import patch |
| 4 |
|
| 5 |
from django.contrib.auth import get_user_model |
| 6 |
from django.urls import reverse |
| 7 |
|
| 8 |
import pytest |
| 9 |
from pytest_django.asserts import assertTemplateNotUsed, assertTemplateUsed |
| 10 |
|
| 11 |
from allauth.account.app_settings import LoginMethod |
| 12 |
from allauth.account.models import EmailAddress, EmailConfirmationHMAC |
| 13 |
from allauth.account.utils import user_email |
| 14 |
|
| 15 |
|
| 16 |
def test_ajax_get(auth_client, user): |
| 17 |
primary = EmailAddress.objects.filter(user=user).first() |
| 18 |
secondary = EmailAddress.objects.create( |
| 19 |
email="[email protected]", user=user, verified=False, primary=False |
| 20 |
) |
| 21 |
resp = auth_client.get( |
| 22 |
reverse("account_email"), HTTP_X_REQUESTED_WITH="XMLHttpRequest" |
| 23 |
) |
| 24 |
data = json.loads(resp.content.decode("utf8")) |
| 25 |
assert data["data"] == [ |
| 26 |
{ |
| 27 |
"id": primary.pk, |
| 28 |
"email": primary.email, |
| 29 |
"primary": True, |
| 30 |
"verified": True, |
| 31 |
}, |
| 32 |
{ |
| 33 |
"id": secondary.pk, |
| 34 |
"email": secondary.email, |
| 35 |
"primary": False, |
| 36 |
"verified": False, |
| 37 |
}, |
| 38 |
] |
| 39 |
|
| 40 |
|
| 41 |
def test_ajax_add(auth_client): |
| 42 |
resp = auth_client.post( |
| 43 |
reverse("account_email"), |
| 44 |
{"action_add": "", "email": "[email protected]"}, |
| 45 |
HTTP_X_REQUESTED_WITH="XMLHttpRequest", |
| 46 |
) |
| 47 |
data = json.loads(resp.content.decode("utf8")) |
| 48 |
assert data["location"] == reverse("account_email") |
| 49 |
|
| 50 |
|
| 51 |
def test_ajax_add_invalid(auth_client): |
| 52 |
resp = auth_client.post( |
| 53 |
reverse("account_email"), |
| 54 |
{"action_add": "", "email": "john3#example.org"}, |
| 55 |
HTTP_X_REQUESTED_WITH="XMLHttpRequest", |
| 56 |
) |
| 57 |
data = json.loads(resp.content.decode("utf8")) |
| 58 |
assert "valid" in data["form"]["fields"]["email"]["errors"][0] |
| 59 |
|
| 60 |
|
| 61 |
def test_ajax_remove_primary(auth_client, user, settings): |
| 62 |
settings.ACCOUNT_LOGIN_METHODS = {"email"} |
| 63 |
resp = auth_client.post( |
| 64 |
reverse("account_email"), |
| 65 |
{"action_remove": "", "email": user.email}, |
| 66 |
HTTP_X_REQUESTED_WITH="XMLHttpRequest", |
| 67 |
) |
| 68 |
assertTemplateUsed(resp, "account/messages/cannot_delete_primary_email.txt") |
| 69 |
data = json.loads(resp.content.decode("utf8")) |
| 70 |
assert data["location"] == reverse("account_email") |
| 71 |
|
| 72 |
|
| 73 |
def test_remove_secondary(auth_client, user, settings, mailoutbox): |
| 74 |
settings.ACCOUNT_EMAIL_NOTIFICATIONS = True |
| 75 |
|
| 76 |
secondary = EmailAddress.objects.create( |
| 77 |
email="[email protected]", user=user, verified=False, primary=False |
| 78 |
) |
| 79 |
resp = auth_client.post( |
| 80 |
reverse("account_email"), |
| 81 |
{"action_remove": "", "email": secondary.email}, |
| 82 |
) |
| 83 |
|
| 84 |
assert not EmailAddress.objects.filter(pk=secondary.pk).exists() |
| 85 |
assertTemplateUsed(resp, "account/messages/email_deleted.txt") |
| 86 |
assert len(mailoutbox) == 1 |
| 87 |
assert f"{secondary.email} has been removed" in mailoutbox[0].body |
| 88 |
|
| 89 |
|
| 90 |
def test_set_primary_unverified(auth_client, user): |
| 91 |
secondary = EmailAddress.objects.create( |
| 92 |
email="[email protected]", user=user, verified=False, primary=False |
| 93 |
) |
| 94 |
resp = auth_client.post( |
| 95 |
reverse("account_email"), |
| 96 |
{"action_primary": "", "email": secondary.email}, |
| 97 |
) |
| 98 |
primary = EmailAddress.objects.get(email=user.email) |
| 99 |
secondary.refresh_from_db() |
| 100 |
assert not secondary.primary |
| 101 |
assert primary.primary |
| 102 |
assertTemplateUsed(resp, "account/messages/unverified_primary_email.txt") |
| 103 |
|
| 104 |
|
| 105 |
def test_set_primary(auth_client, user): |
| 106 |
primary = EmailAddress.objects.get(email=user.email) |
| 107 |
secondary = EmailAddress.objects.create( |
| 108 |
email="[email protected]", user=user, verified=True, primary=False |
| 109 |
) |
| 110 |
resp = auth_client.post( |
| 111 |
reverse("account_email"), |
| 112 |
{"action_primary": "", "email": secondary.email}, |
| 113 |
) |
| 114 |
primary.refresh_from_db() |
| 115 |
secondary.refresh_from_db() |
| 116 |
assert not primary.primary |
| 117 |
assert secondary.primary |
| 118 |
assertTemplateUsed(resp, "account/messages/primary_email_set.txt") |
| 119 |
|
| 120 |
|
| 121 |
def test_verify(auth_client, user): |
| 122 |
secondary = EmailAddress.objects.create( |
| 123 |
email="[email protected]", user=user, verified=False, primary=False |
| 124 |
) |
| 125 |
resp = auth_client.post( |
| 126 |
reverse("account_email"), |
| 127 |
{"action_send": "", "email": secondary.email}, |
| 128 |
) |
| 129 |
assertTemplateUsed(resp, "account/messages/email_confirmation_sent.txt") |
| 130 |
|
| 131 |
|
| 132 |
def test_verify_unknown_email(auth_client, user): |
| 133 |
auth_client.post( |
| 134 |
reverse("account_email"), |
| 135 |
{"action_send": "", "email": "[email protected]"}, |
| 136 |
) |
| 137 |
# This unknown email address must not be implicitly added. |
| 138 |
assert EmailAddress.objects.filter(user=user).count() == 1 |
| 139 |
|
| 140 |
|
| 141 |
def test_add_with_two_limiter(auth_client, user, settings): |
| 142 |
EmailAddress.objects.create( |
| 143 |
email="[email protected]", user=user, verified=False, primary=False |
| 144 |
) |
| 145 |
settings.ACCOUNT_MAX_EMAIL_ADDRESSES = 2 |
| 146 |
resp = auth_client.post( |
| 147 |
reverse("account_email"), {"action_add": "", "email": "[email protected]"} |
| 148 |
) |
| 149 |
assertTemplateNotUsed(resp, "account/messages/email_confirmation_sent.txt") |
| 150 |
|
| 151 |
|
| 152 |
def test_add_with_none_limiter(auth_client, settings): |
| 153 |
settings.ACCOUNT_MAX_EMAIL_ADDRESSES = None |
| 154 |
resp = auth_client.post( |
| 155 |
reverse("account_email"), {"action_add": "", "email": "[email protected]"} |
| 156 |
) |
| 157 |
assertTemplateUsed(resp, "account/messages/email_confirmation_sent.txt") |
| 158 |
|
| 159 |
|
| 160 |
def test_add_with_zero_limiter(auth_client, settings): |
| 161 |
settings.ACCOUNT_MAX_EMAIL_ADDRESSES = 0 |
| 162 |
resp = auth_client.post( |
| 163 |
reverse("account_email"), {"action_add": "", "email": "[email protected]"} |
| 164 |
) |
| 165 |
assertTemplateUsed(resp, "account/messages/email_confirmation_sent.txt") |
| 166 |
|
| 167 |
|
| 168 |
@pytest.mark.parametrize("has_email_field", [True, False]) |
| 169 |
def test_set_email_as_primary_doesnt_override_existing_changes_on_the_user( |
| 170 |
db, has_email_field, settings |
| 171 |
): |
| 172 |
if not has_email_field: |
| 173 |
settings.ACCOUNT_USER_MODEL_EMAIL_FIELD = None |
| 174 |
user = get_user_model().objects.create( |
| 175 |
username="@raymond.penners", first_name="Before Update" |
| 176 |
) |
| 177 |
email = EmailAddress.objects.create( |
| 178 |
user=user, |
| 179 |
email="[email protected]", |
| 180 |
primary=True, |
| 181 |
verified=True, |
| 182 |
) |
| 183 |
updated_first_name = "Updated" |
| 184 |
get_user_model().objects.filter(id=user.id).update(first_name=updated_first_name) |
| 185 |
|
| 186 |
email.set_as_primary() |
| 187 |
|
| 188 |
user.refresh_from_db() |
| 189 |
assert user.first_name == updated_first_name |
| 190 |
|
| 191 |
|
| 192 |
def test_delete_email_changes_user_email(user_factory, client, email_factory): |
| 193 |
user = user_factory(email_verified=False) |
| 194 |
client.force_login(user) |
| 195 |
first_email = EmailAddress.objects.get(user=user) |
| 196 |
first_email.primary = False |
| 197 |
first_email.save() |
| 198 |
# other_unverified_email |
| 199 |
EmailAddress.objects.create( |
| 200 |
user=user, email=email_factory(), verified=False, primary=False |
| 201 |
) |
| 202 |
other_verified_email = EmailAddress.objects.create( |
| 203 |
user=user, email=email_factory(), verified=True, primary=False |
| 204 |
) |
| 205 |
assert user_email(user) == first_email.email |
| 206 |
resp = client.post( |
| 207 |
reverse("account_email"), |
| 208 |
{"action_remove": "", "email": first_email.email}, |
| 209 |
) |
| 210 |
assert resp.status_code == HTTPStatus.FOUND |
| 211 |
user.refresh_from_db() |
| 212 |
assert user_email(user) == other_verified_email.email |
| 213 |
|
| 214 |
|
| 215 |
def test_delete_email_wipes_user_email(user_factory, client): |
| 216 |
user = user_factory(email_verified=False) |
| 217 |
client.force_login(user) |
| 218 |
first_email = EmailAddress.objects.get(user=user) |
| 219 |
first_email.primary = False |
| 220 |
first_email.save() |
| 221 |
assert user_email(user) == first_email.email |
| 222 |
resp = client.post( |
| 223 |
reverse("account_email"), |
| 224 |
{"action_remove": "", "email": first_email.email}, |
| 225 |
) |
| 226 |
assert resp.status_code == HTTPStatus.FOUND |
| 227 |
user.refresh_from_db() |
| 228 |
assert user_email(user) == "" |
| 229 |
|
| 230 |
|
| 231 |
def test_change_email(user_factory, client, settings, mailoutbox): |
| 232 |
settings.ACCOUNT_CHANGE_EMAIL = True |
| 233 |
settings.ACCOUNT_EMAIL_CONFIRMATION_HMAC = True |
| 234 |
settings.ACCOUNT_EMAIL_NOTIFICATIONS = True |
| 235 |
|
| 236 |
user = user_factory(email_verified=True) |
| 237 |
client.force_login(user) |
| 238 |
current_email = EmailAddress.objects.get(user=user) |
| 239 |
resp = client.post( |
| 240 |
reverse("account_email"), |
| 241 |
{"action_add": "", "email": "[email protected]"}, |
| 242 |
) |
| 243 |
assert resp.status_code == HTTPStatus.FOUND |
| 244 |
assert len(mailoutbox) == 1 |
| 245 |
assert mailoutbox[0].subject == "[example.com] Please Confirm Your Email Address" |
| 246 |
new_email = EmailAddress.objects.get(email="[email protected]") |
| 247 |
key = EmailConfirmationHMAC(new_email).key |
| 248 |
with patch("allauth.account.signals.email_changed.send") as email_changed_mock: |
| 249 |
resp = client.post(reverse("account_confirm_email", args=[key])) |
| 250 |
assert resp.status_code == HTTPStatus.FOUND |
| 251 |
assert not EmailAddress.objects.filter(pk=current_email.pk).exists() |
| 252 |
assert EmailAddress.objects.filter(user=user).count() == 1 |
| 253 |
new_email.refresh_from_db() |
| 254 |
assert new_email.verified |
| 255 |
assert new_email.primary |
| 256 |
assert email_changed_mock.called |
| 257 |
assert len(mailoutbox) == 2 |
| 258 |
assert mailoutbox[1].subject == "[example.com] Email Changed" |
| 259 |
assert mailoutbox[1].to == [user.email] |
| 260 |
|
| 261 |
|
| 262 |
def test_add(auth_client, user, settings): |
| 263 |
resp = auth_client.post( |
| 264 |
reverse("account_email"), |
| 265 |
{"action_add": "", "email": "[email protected]"}, |
| 266 |
) |
| 267 |
EmailAddress.objects.get( |
| 268 |
email="[email protected]", |
| 269 |
user=user, |
| 270 |
verified=False, |
| 271 |
primary=False, |
| 272 |
) |
| 273 |
assertTemplateUsed(resp, "account/messages/email_confirmation_sent.txt") |
| 274 |
|
| 275 |
|
| 276 |
def test_add_with_reauthentication(auth_client, user, user_password, settings): |
| 277 |
settings.ACCOUNT_REAUTHENTICATION_REQUIRED = True |
| 278 |
resp = auth_client.post( |
| 279 |
reverse("account_email"), |
| 280 |
{"action_add": "", "email": "[email protected]"}, |
| 281 |
) |
| 282 |
assert not EmailAddress.objects.filter(email="[email protected]").exists() |
| 283 |
assert resp.status_code == HTTPStatus.FOUND |
| 284 |
assert ( |
| 285 |
resp["location"] |
| 286 |
== f"{reverse('account_reauthenticate')}?next=%2Faccounts%2Femail%2F" |
| 287 |
) |
| 288 |
resp = auth_client.post(resp["location"], {"password": user_password}) |
| 289 |
assert EmailAddress.objects.filter(email="[email protected]").exists() |
| 290 |
assertTemplateUsed(resp, "account/messages/email_confirmation_sent.txt") |
| 291 |
assert resp.status_code == HTTPStatus.FOUND |
| 292 |
assert resp["location"] == reverse("account_email") |
| 293 |
|
| 294 |
|
| 295 |
@pytest.mark.parametrize( |
| 296 |
"prevent_enumeration", |
| 297 |
[ |
| 298 |
False, |
| 299 |
True, |
| 300 |
"strict", |
| 301 |
], |
| 302 |
) |
| 303 |
def test_add_not_allowed( |
| 304 |
auth_client, user, settings, user_factory, prevent_enumeration |
| 305 |
): |
| 306 |
settings.ACCOUNT_PREVENT_ENUMERATION = prevent_enumeration |
| 307 |
email = "[email protected]" |
| 308 |
user_factory(email=email) |
| 309 |
resp = auth_client.post( |
| 310 |
reverse("account_email"), |
| 311 |
{"action_add": "", "email": email}, |
| 312 |
) |
| 313 |
if prevent_enumeration: |
| 314 |
assert resp.status_code == HTTPStatus.FOUND |
| 315 |
email_address = EmailAddress.objects.get( |
| 316 |
email=email, |
| 317 |
user=user, |
| 318 |
verified=False, |
| 319 |
primary=False, |
| 320 |
) |
| 321 |
assertTemplateUsed(resp, "account/messages/email_confirmation_sent.txt") |
| 322 |
key = EmailConfirmationHMAC(email_address).key |
| 323 |
resp = auth_client.post(reverse("account_confirm_email", args=[key])) |
| 324 |
assertTemplateUsed(resp, "account/messages/email_confirmation_failed.txt") |
| 325 |
assert resp.status_code == HTTPStatus.FOUND |
| 326 |
email_address.refresh_from_db() |
| 327 |
assert not email_address.verified |
| 328 |
else: |
| 329 |
assert resp.status_code == HTTPStatus.OK |
| 330 |
assert resp.context["form"].errors == { |
| 331 |
"email": ["A user is already registered with this email address."] |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
@pytest.mark.parametrize( |
| 336 |
"login_methods,primary_email,secondary_emails,delete_email,success", |
| 337 |
[ |
| 338 |
({LoginMethod.EMAIL}, "[email protected]", ["[email protected]"], "[email protected]", False), |
| 339 |
({LoginMethod.EMAIL}, "[email protected]", ["[email protected]"], "[email protected]", True), |
| 340 |
({LoginMethod.EMAIL}, "[email protected]", [], "[email protected]", False), |
| 341 |
({LoginMethod.USERNAME}, "[email protected]", ["[email protected]"], "[email protected]", False), |
| 342 |
({LoginMethod.USERNAME}, "[email protected]", ["[email protected]"], "[email protected]", True), |
| 343 |
({LoginMethod.USERNAME}, "[email protected]", [], "[email protected]", True), |
| 344 |
( |
| 345 |
{LoginMethod.USERNAME, LoginMethod.EMAIL}, |
| 346 |
"[email protected]", |
| 347 |
["[email protected]"], |
| 348 |
"[email protected]", |
| 349 |
False, |
| 350 |
), |
| 351 |
( |
| 352 |
{LoginMethod.USERNAME, LoginMethod.EMAIL}, |
| 353 |
"[email protected]", |
| 354 |
["[email protected]"], |
| 355 |
"[email protected]", |
| 356 |
True, |
| 357 |
), |
| 358 |
({LoginMethod.USERNAME, LoginMethod.EMAIL}, "[email protected]", [], "[email protected]", True), |
| 359 |
], |
| 360 |
) |
| 361 |
def test_remove_email( |
| 362 |
client, |
| 363 |
settings, |
| 364 |
user_factory, |
| 365 |
primary_email, |
| 366 |
secondary_emails, |
| 367 |
delete_email, |
| 368 |
login_methods, |
| 369 |
success, |
| 370 |
): |
| 371 |
settings.ACCOUNT_LOGIN_METHODS = login_methods |
| 372 |
user = user_factory(email=primary_email) |
| 373 |
EmailAddress.objects.bulk_create( |
| 374 |
[ |
| 375 |
EmailAddress(user=user, email=email, primary=False, verified=False) |
| 376 |
for email in secondary_emails |
| 377 |
] |
| 378 |
) |
| 379 |
client.force_login(user) |
| 380 |
resp = client.post( |
| 381 |
reverse("account_email"), |
| 382 |
{"action_remove": "", "email": delete_email}, |
| 383 |
) |
| 384 |
assert EmailAddress.objects.filter(email=delete_email).exists() == (not success) |
| 385 |
if not success: |
| 386 |
assertTemplateUsed(resp, "account/messages/cannot_delete_primary_email.txt") |
| 387 |
|
| 388 |
|
| 389 |
@pytest.mark.parametrize( |
| 390 |
"email,did_look_up", |
| 391 |
[ |
| 392 |
("[email protected]", True), |
| 393 |
("not-an-email", False), |
| 394 |
], |
| 395 |
) |
| 396 |
def test_dont_lookup_invalid_email(auth_client, email, did_look_up): |
| 397 |
with patch("allauth.account.views.EmailAddress.objects.get_for_user") as gfu_mock: |
| 398 |
gfu_mock.side_effect = EmailAddress.DoesNotExist |
| 399 |
auth_client.post( |
| 400 |
reverse("account_email"), |
| 401 |
{"action_remove": "", "email": email}, |
| 402 |
) |
| 403 |
assert gfu_mock.called == did_look_up |
| 404 |
|
| 405 |
|
| 406 |
def test_add_requires_reauthentication(settings, auth_client): |
| 407 |
settings.ACCOUNT_REAUTHENTICATION_REQUIRED = True |
| 408 |
resp = auth_client.post( |
| 409 |
reverse("account_email"), |
| 410 |
{"action_add": "", "email": "[email protected]"}, |
| 411 |
) |
| 412 |
assert not EmailAddress.objects.filter(email="[email protected]").exists() |
| 413 |
assert resp["location"].startswith(reverse("account_reauthenticate")) |
| 414 |
|
| 415 |
|
| 416 |
def test_remove_requires_reauthentication(auth_client, user, settings): |
| 417 |
settings.ACCOUNT_REAUTHENTICATION_REQUIRED = True |
| 418 |
secondary = EmailAddress.objects.create( |
| 419 |
email="[email protected]", user=user, verified=False, primary=False |
| 420 |
) |
| 421 |
resp = auth_client.post( |
| 422 |
reverse("account_email"), |
| 423 |
{"action_remove": "", "email": secondary.email}, |
| 424 |
) |
| 425 |
assert resp["location"].startswith(reverse("account_reauthenticate")) |
| 426 |
assert EmailAddress.objects.filter(pk=secondary.pk).exists() |
| 427 |
|
| 428 |
|
| 429 |
def test_set_primary_requires_reauthentication(auth_client, user, settings): |
| 430 |
settings.ACCOUNT_REAUTHENTICATION_REQUIRED = True |
| 431 |
primary = EmailAddress.objects.get(email=user.email) |
| 432 |
secondary = EmailAddress.objects.create( |
| 433 |
email="[email protected]", user=user, verified=True, primary=False |
| 434 |
) |
| 435 |
resp = auth_client.post( |
| 436 |
reverse("account_email"), |
| 437 |
{"action_primary": "", "email": secondary.email}, |
| 438 |
) |
| 439 |
assert resp["location"].startswith(reverse("account_reauthenticate")) |
| 440 |
primary.refresh_from_db() |
| 441 |
secondary.refresh_from_db() |
| 442 |
assert primary.primary |
| 443 |
assert not secondary.primary |
| 444 |
|
| 445 |
|
| 446 |
def test_change_email_links_restrict_email(user_factory, client, settings, mailoutbox): |
| 447 |
settings.ACCOUNT_CHANGE_EMAIL = True |
| 448 |
settings.ACCOUNT_EMAIL_CONFIRMATION_HMAC = True |
| 449 |
settings.ACCOUNT_EMAIL_NOTIFICATIONS = False |
| 450 |
|
| 451 |
user = user_factory(email_verified=True) |
| 452 |
client.force_login(user) |
| 453 |
|
| 454 |
# Change the email... |
| 455 |
resp = client.post( |
| 456 |
reverse("account_email"), |
| 457 |
{"action_add": "", "email": "[email protected]"}, |
| 458 |
) |
| 459 |
assert resp.status_code == HTTPStatus.FOUND |
| 460 |
|
| 461 |
# Extract the verification link from the outbox. |
| 462 |
assert len(mailoutbox) == 1 |
| 463 |
assert mailoutbox[-1].to[0] == "[email protected]" |
| 464 |
assert mailoutbox[-1].subject == "[example.com] Please Confirm Your Email Address" |
| 465 |
verify_link_1 = ( |
| 466 |
mailoutbox[-1].body[mailoutbox[-1].body.find("http://") :].split()[0] |
| 467 |
) |
| 468 |
|
| 469 |
# Without verifying, change it once more. |
| 470 |
resp = client.post( |
| 471 |
reverse("account_email"), |
| 472 |
{"action_add": "", "email": "[email protected]"}, |
| 473 |
) |
| 474 |
assert resp.status_code == HTTPStatus.FOUND |
| 475 |
|
| 476 |
# And, extract the 2nd link. |
| 477 |
assert len(mailoutbox) == 2 |
| 478 |
assert mailoutbox[-1].to[0] == "[email protected]" |
| 479 |
assert mailoutbox[-1].subject == "[example.com] Please Confirm Your Email Address" |
| 480 |
verify_link_2 = ( |
| 481 |
mailoutbox[-1].body[mailoutbox[-1].body.find("http://") :].split()[0] |
| 482 |
) |
| 483 |
|
| 484 |
# Try and verify the first link. That should no longer be possible. |
| 485 |
resp = client.post(verify_link_1) |
| 486 |
assert resp.status_code == HTTPStatus.NOT_FOUND |
| 487 |
|
| 488 |
# Confirm that the POST above had no effect. |
| 489 |
assert EmailAddress.objects.filter(user=user).count() == 2 |
| 490 |
email_address = EmailAddress.objects.get(user=user, verified=True) |
| 491 |
assert email_address.email == user.email |
| 492 |
email_address = EmailAddress.objects.get(user=user, verified=False) |
| 493 |
assert email_address.email == "[email protected]" |
| 494 |
|
| 495 |
# Use the 2nd verification link |
| 496 |
resp = client.post(verify_link_2) |
| 497 |
assert resp.status_code == HTTPStatus.FOUND |
| 498 |
|
| 499 |
# That one works. |
| 500 |
assert EmailAddress.objects.filter(user=user).count() == 1 |
| 501 |
email_address = EmailAddress.objects.get(user=user) |
| 502 |
assert email_address.verified |
| 503 |
assert email_address.email == "[email protected]" |
| 504 |
|
| 505 |
|
| 506 |
def test_change_unverified_email(user_factory, client, settings, mailoutbox): |
| 507 |
settings.ACCOUNT_CHANGE_EMAIL = True |
| 508 |
settings.ACCOUNT_EMAIL_CONFIRMATION_HMAC = True |
| 509 |
settings.ACCOUNT_EMAIL_NOTIFICATIONS = True |
| 510 |
|
| 511 |
user = user_factory(email_verified=False) |
| 512 |
client.force_login(user) |
| 513 |
current_email = EmailAddress.objects.get(user=user) |
| 514 |
assert user.email == current_email.email |
| 515 |
resp = client.post( |
| 516 |
reverse("account_email"), |
| 517 |
{"action_add": "", "email": "[email protected]"}, |
| 518 |
) |
| 519 |
assert resp.status_code == HTTPStatus.FOUND |
| 520 |
assert len(mailoutbox) == 1 |
| 521 |
assert not EmailAddress.objects.filter(pk=current_email.pk).exists() |
| 522 |
user.refresh_from_db() |
| 523 |
assert user.email == "" |
| 524 |
assert mailoutbox[0].subject == "[example.com] Please Confirm Your Email Address" |
| 525 |
new_email = EmailAddress.objects.get(email="[email protected]") |
| 526 |
key = EmailConfirmationHMAC(new_email).key |
| 527 |
with patch("allauth.account.signals.email_changed.send") as email_changed_mock: |
| 528 |
resp = client.post(reverse("account_confirm_email", args=[key])) |
| 529 |
assert resp.status_code == HTTPStatus.FOUND |
| 530 |
assert not EmailAddress.objects.filter(pk=current_email.pk).exists() |
| 531 |
assert EmailAddress.objects.filter(user=user).count() == 1 |
| 532 |
new_email.refresh_from_db() |
| 533 |
assert new_email.verified |
| 534 |
assert new_email.primary |
| 535 |
assert email_changed_mock.called |
| 536 |
|
| 537 |
|
| 538 |
def test_change_unverified_email_to_verified_email_of_other_user( |
| 539 |
user_factory, client, settings, mailoutbox |
| 540 |
): |
| 541 |
settings.ACCOUNT_CHANGE_EMAIL = True |
| 542 |
settings.ACCOUNT_EMAIL_CONFIRMATION_HMAC = True |
| 543 |
settings.ACCOUNT_EMAIL_NOTIFICATIONS = True |
| 544 |
|
| 545 |
verified_user = user_factory(email_verified=True) |
| 546 |
|
| 547 |
user = user_factory(email_verified=False) |
| 548 |
client.force_login(user) |
| 549 |
current_email = EmailAddress.objects.get(user=user) |
| 550 |
assert user.email == current_email.email |
| 551 |
resp = client.post( |
| 552 |
reverse("account_email"), |
| 553 |
{"action_add": "", "email": verified_user.email}, |
| 554 |
) |
| 555 |
|
| 556 |
assert resp.status_code == HTTPStatus.FOUND |
| 557 |
assert len(mailoutbox) == 1 |
| 558 |
assert not EmailAddress.objects.filter(pk=current_email.pk).exists() |
| 559 |
user.refresh_from_db() |
| 560 |
assert user.email == "" |
| 561 |
assert mailoutbox[0].subject == "[example.com] Please Confirm Your Email Address" |
| 562 |
new_email = EmailAddress.objects.get( |
| 563 |
user=user, verified=False, email=verified_user.email |
| 564 |
) |
| 565 |
key = EmailConfirmationHMAC(new_email).key |
| 566 |
with patch("allauth.account.signals.email_changed.send") as email_changed_mock: |
| 567 |
resp = client.post(reverse("account_confirm_email", args=[key])) |
| 568 |
assert resp.status_code == HTTPStatus.FOUND |
| 569 |
|
| 570 |
last_message = list(resp.context["messages"])[-1] |
| 571 |
assert last_message.message.startswith("Unable to confirm") |
| 572 |
|
| 573 |
assert EmailAddress.objects.filter(user=user).count() == 1 |
| 574 |
new_email.refresh_from_db() |
| 575 |
assert not new_email.verified |
| 576 |
assert not email_changed_mock.called |