Skip to content

Commit

Permalink
Add account preferences form tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalp committed May 26, 2024
1 parent 9b92155 commit 6db57be
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 6 deletions.
10 changes: 4 additions & 6 deletions misago/account/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down
93 changes: 93 additions & 0 deletions misago/account/tests/test_account_preferences.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6db57be

Please sign in to comment.