Coverage for app/backend/src/tests/test_notification_settings.py: 100%

36 statements  

« prev     ^ index     » next       coverage.py v7.14.1, created at 2026-06-10 03:41 +0000

1import pytest 

2 

3from couchers.i18n.locales import DEFAULT_LOCALE 

4from couchers.models.notifications import NotificationTopicAction 

5from couchers.notifications.quick_links import can_unsubscribe_topic_key 

6from couchers.notifications.render_email import get_topic_action_unsubscribe_text, get_topic_key_unsubscribe_text 

7from couchers.notifications.settings import settings_layout 

8from couchers.notifications.utils import get_topic_action_description 

9 

10 

11def test_all_notifications_appear_in_settings() -> None: 

12 # check settings contain all actions+topics 

13 actions_by_topic: dict[str, list[str]] = {} 

14 for t in NotificationTopicAction: 

15 actions_by_topic[t.topic] = actions_by_topic.get(t.topic, []) + [t.action] 

16 

17 actions_by_topic_check = {} 

18 

19 for heading, group in settings_layout: 

20 for topic, name, items in group: 

21 actions = [] 

22 for topic_action in items: 

23 actions.append(topic_action.action) 

24 actions_by_topic_check[topic] = actions 

25 

26 for topic, actions in actions_by_topic.items(): 

27 assert sorted(actions) == sorted(actions_by_topic_check[topic]), ( 

28 f"Expected {actions} == {actions_by_topic_check[topic]} for {topic}" 

29 ) 

30 assert sorted(actions_by_topic.keys()) == sorted(actions_by_topic_check.keys()) 

31 

32 

33def test_all_notifications_have_descriptions() -> None: 

34 for topic_action in NotificationTopicAction: 

35 # Will throw if there's no string 

36 assert get_topic_action_description(topic_action, locale=DEFAULT_LOCALE) != "" 

37 

38 

39def test_topic_action_unsubscribe_text_iff_unsubscribable() -> None: 

40 for topic_action in NotificationTopicAction: 

41 if topic_action.is_critical: 

42 with pytest.raises(ValueError): 

43 get_topic_action_unsubscribe_text(topic_action) 

44 else: 

45 assert get_topic_action_unsubscribe_text(topic_action) 

46 

47 

48def test_topic_key_unsubscribe_text_iff_unsubscribable() -> None: 

49 for topic_action in NotificationTopicAction: 

50 if can_unsubscribe_topic_key(topic_action): 

51 assert get_topic_key_unsubscribe_text(topic_action) 

52 else: 

53 with pytest.raises(ValueError): 

54 get_topic_key_unsubscribe_text(topic_action)