Skip to content

Commit

Permalink
Add tests for email change confirmation
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalp committed Jun 12, 2024
1 parent 00df4fd commit dec3c80
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 3 deletions.
84 changes: 84 additions & 0 deletions misago/account/tests/test_account_email_confirm_change.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
from django.urls import reverse

from ...conf.test import override_dynamic_settings
from ...test import assert_contains
from ..emailchange import create_email_change_token


@override_dynamic_settings(enable_oauth2_client=True)
def test_account_email_confirm_change_returns_error_if_oauth_client_is_enabled(
user, user_client
):
response = user_client.get(
reverse(
"misago:account-email-confirm-change",
kwargs={"user_id": user.id, "token": "any"},
)
)
assert response.status_code == 404


def test_account_email_confirm_change_changes_email_if_user_is_guest(user, client):
new_email = "[email protected]"
token = create_email_change_token(user, new_email)
response = client.get(
reverse(
"misago:account-email-confirm-change",
kwargs={"user_id": user.id, "token": token},
)
)
assert_contains(response, "your email has been changed")

user.refresh_from_db()
assert user.email == new_email


def test_account_email_confirm_change_changes_email_if_user_is_authenticated(
user, user_client
):
new_email = "[email protected]"
token = create_email_change_token(user, new_email)
response = user_client.get(
reverse(
"misago:account-email-confirm-change",
kwargs={"user_id": user.id, "token": token},
)
)
assert_contains(response, "your email has been changed")

user.refresh_from_db()
assert user.email == new_email


def test_account_email_confirm_change_returns_error_if_user_id_is_invalid(user, client):
response = client.get(
reverse(
"misago:account-email-confirm-change",
kwargs={"user_id": user.id + 1, "token": "invalid"},
)
)
assert response.status_code == 404


def test_account_email_confirm_change_returns_error_if_token_is_invalid(user, client):
response = client.get(
reverse(
"misago:account-email-confirm-change",
kwargs={"user_id": user.id, "token": "invalid"},
)
)
assert_contains(response, "Mail change confirmation link is invalid.")


def test_account_email_confirm_change_returns_error_if_token_email_is_invalid(
admin, user, client
):
token = create_email_change_token(user, admin.email)
response = client.get(
reverse(
"misago:account-email-confirm-change",
kwargs={"user_id": user.id, "token": token},
)
)
print(response.content)
assert_contains(response, "This e-mail address is not available.")
7 changes: 4 additions & 3 deletions misago/account/views/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,9 @@ def get(self, request: HttpRequest) -> HttpResponse:


def account_email_confirm_change(request, user_id, token):
if request.settings.enable_oauth2_client:
raise Http404()

user = get_object_or_404(User.objects, id=user_id, is_active=True)

try:
Expand All @@ -390,14 +393,12 @@ def account_email_confirm_change(request, user_id, token):
"message": str(e),
"error_code": str(e.code),
},
status=400,
)
except ValidationError as e:
return render(
request,
"misago/account/settings/email_change_error.html",
{"message": e.error_list[0]},
status=400,
{"message": str(e.messages[0])},
)

if new_email != user.email:
Expand Down

0 comments on commit dec3c80

Please sign in to comment.