diff --git a/misago/account/forms.py b/misago/account/forms.py index 551f3773e4..2570de21ca 100644 --- a/misago/account/forms.py +++ b/misago/account/forms.py @@ -32,28 +32,26 @@ def __init__(self, *args, **kwargs): coerce=int, choices=( ( - 1, + 0, pgettext_lazy( "account privacy choice", "Show other users when I am online", ), ), ( - 0, + 1, pgettext_lazy( "account privacy choice", "Don't show other users when I am online", ), ), ), - initial=0 if user.is_hiding_presence else 1, + initial=1 if user.is_hiding_presence else 0, widget=forms.RadioSelect(), ) def save(self): - self.instance.is_hiding_presence = not self.cleaned_data.get( - "is_hiding_presence" - ) + self.instance.is_hiding_presence = self.cleaned_data.get("is_hiding_presence") return super().save() diff --git a/misago/account/tests/test_account_preferences.py b/misago/account/tests/test_account_preferences.py new file mode 100644 index 0000000000..6692108be6 --- /dev/null +++ b/misago/account/tests/test_account_preferences.py @@ -0,0 +1,93 @@ +from django.urls import reverse + +from ...test import assert_contains, assert_has_success_message + + +def test_account_preferences_returns_error_for_guests(user, client): + response = client.get(reverse("misago:account-preferences")) + assert_contains(response, "You need to be signed in", status_code=403) + + +def test_account_preferences_renders_form(user_client): + response = user_client.get(reverse("misago:account-preferences")) + assert_contains(response, "Change preferences") + + +def create_form_data(data: dict | None = None) -> dict: + default_data = { + "is_hiding_presence": "0", + "limits_private_thread_invites_to": "0", + "watch_started_threads": "0", + "watch_replied_threads": "0", + "watch_new_private_threads_by_followed": "0", + "watch_new_private_threads_by_other_users": "0", + "notify_new_private_threads_by_followed": "0", + "notify_new_private_threads_by_other_users": "0", + } + + if data: + default_data.update(data) + + return default_data + + +def test_account_preferences_form_updates_user_account(user, user_client): + response = user_client.post( + reverse("misago:account-preferences"), + create_form_data( + { + "is_hiding_presence": "1", + "limits_private_thread_invites_to": "1", + "watch_started_threads": "1", + "watch_replied_threads": "2", + "watch_new_private_threads_by_followed": "1", + "watch_new_private_threads_by_other_users": "2", + "notify_new_private_threads_by_followed": "1", + "notify_new_private_threads_by_other_users": "2", + } + ), + ) + + assert response.status_code == 302 + assert_has_success_message(response, "Preferences updated") + + user.refresh_from_db() + assert user.is_hiding_presence + assert user.limits_private_thread_invites_to == 1 + assert user.watch_started_threads == 1 + assert user.watch_replied_threads == 2 + assert user.watch_new_private_threads_by_followed == 1 + assert user.watch_new_private_threads_by_other_users == 2 + assert user.notify_new_private_threads_by_followed == 1 + assert user.notify_new_private_threads_by_other_users == 2 + + +def test_account_preferences_form_updates_user_account_in_htmx(user, user_client): + response = user_client.post( + reverse("misago:account-preferences"), + create_form_data( + { + "is_hiding_presence": "1", + "limits_private_thread_invites_to": "1", + "watch_started_threads": "1", + "watch_replied_threads": "2", + "watch_new_private_threads_by_followed": "1", + "watch_new_private_threads_by_other_users": "2", + "notify_new_private_threads_by_followed": "1", + "notify_new_private_threads_by_other_users": "2", + } + ), + headers={"hx-request": "true"}, + ) + + assert_contains(response, "Preferences updated") + + user.refresh_from_db() + assert user.is_hiding_presence + assert user.limits_private_thread_invites_to == 1 + assert user.watch_started_threads == 1 + assert user.watch_replied_threads == 2 + assert user.watch_new_private_threads_by_followed == 1 + assert user.watch_new_private_threads_by_other_users == 2 + assert user.notify_new_private_threads_by_followed == 1 + assert user.notify_new_private_threads_by_other_users == 2