Coverage for app/backend/src/tests/fixtures/api/notifications.py: 100%

53 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-20 12:21 +0000

1from collections.abc import Sequence 

2from contextlib import contextmanager 

3from typing import cast 

4 

5import grpc 

6from google.protobuf import empty_pb2 

7 

8from couchers.proto import notifications_pb2, notifications_pb2_grpc 

9from couchers.servicers.notifications import Notifications 

10from tests.fixtures.sessions import FakeChannel 

11 

12 

13@contextmanager 

14def _notifications_session(token: str): 

15 channel = FakeChannel(token) 

16 notifications_pb2_grpc.add_NotificationsServicer_to_server(Notifications(), cast(grpc.Server, channel)) 

17 yield notifications_pb2_grpc.NotificationsStub(cast(grpc.Channel, channel)) 

18 

19 

20def get_notification_settings(*, token: str) -> notifications_pb2.GetNotificationSettingsRes: 

21 with _notifications_session(token) as session: 

22 res: notifications_pb2.GetNotificationSettingsRes = session.GetNotificationSettings( 

23 notifications_pb2.GetNotificationSettingsReq() 

24 ) 

25 return res 

26 

27 

28def set_notification_settings( 

29 *, 

30 token: str, 

31 enable_do_not_email: bool = False, 

32 preferences: Sequence[notifications_pb2.SingleNotificationPreference] = (), 

33) -> notifications_pb2.GetNotificationSettingsRes: 

34 with _notifications_session(token) as session: 

35 res: notifications_pb2.GetNotificationSettingsRes = session.SetNotificationSettings( 

36 notifications_pb2.SetNotificationSettingsReq( 

37 enable_do_not_email=enable_do_not_email, 

38 preferences=preferences, 

39 ) 

40 ) 

41 return res 

42 

43 

44def list_notifications( 

45 *, 

46 token: str, 

47 only_unread: bool = False, 

48 page_size: int | None = None, 

49 page_token: str | None = None, 

50) -> notifications_pb2.ListNotificationsRes: 

51 with _notifications_session(token) as session: 

52 res: notifications_pb2.ListNotificationsRes = session.ListNotifications( 

53 notifications_pb2.ListNotificationsReq(only_unread=only_unread, page_size=page_size, page_token=page_token) 

54 ) 

55 return res 

56 

57 

58def mark_notification_seen(*, token: str, notification_id: int, set_seen: bool = True) -> None: 

59 with _notifications_session(token) as session: 

60 session.MarkNotificationSeen( 

61 notifications_pb2.MarkNotificationSeenReq(notification_id=notification_id, set_seen=set_seen) 

62 ) 

63 

64 

65def mark_all_notifications_seen(*, token: str, latest_notification_id: int) -> None: 

66 with _notifications_session(token) as session: 

67 session.MarkAllNotificationsSeen( 

68 notifications_pb2.MarkAllNotificationsSeenReq(latest_notification_id=latest_notification_id) 

69 ) 

70 

71 

72def get_vapid_public_key(*, token: str) -> notifications_pb2.GetVapidPublicKeyRes: 

73 with _notifications_session(token) as session: 

74 res: notifications_pb2.GetVapidPublicKeyRes = session.GetVapidPublicKey(empty_pb2.Empty()) 

75 return res 

76 

77 

78def register_push_notification_subscription( 

79 *, token: str, full_subscription_json: str, user_agent: str | None = None 

80) -> None: 

81 with _notifications_session(token) as session: 

82 session.RegisterPushNotificationSubscription( 

83 notifications_pb2.RegisterPushNotificationSubscriptionReq( 

84 full_subscription_json=full_subscription_json, 

85 user_agent=user_agent, 

86 ) 

87 ) 

88 

89 

90def send_test_push_notification(*, token: str) -> None: 

91 with _notifications_session(token) as session: 

92 session.SendTestPushNotification(empty_pb2.Empty()) 

93 

94 

95def register_mobile_push_notification_subscription( 

96 *, 

97 token: str, 

98 push_token: str, 

99 device_name: str | None = None, 

100 device_type: str | None = None, 

101) -> None: 

102 with _notifications_session(token) as session: 

103 session.RegisterMobilePushNotificationSubscription( 

104 notifications_pb2.RegisterMobilePushNotificationSubscriptionReq( 

105 token=push_token, 

106 device_name=device_name, 

107 device_type=device_type, 

108 ) 

109 ) 

110 

111 

112def send_test_mobile_push_notification(*, token: str) -> None: 

113 with _notifications_session(token) as session: 

114 session.SendTestMobilePushNotification(empty_pb2.Empty()) 

115 

116 

117def send_dev_push_notification( 

118 *, 

119 token: str, 

120 title: str | None = "Dummy title", 

121 body: str | None = "Dummy body.", 

122 icon: str | None = None, 

123 url: str | None = None, 

124 key: str | None = None, 

125 ttl: int | None = None, 

126) -> None: 

127 with _notifications_session(token) as session: 

128 session.SendDevPushNotification( 

129 notifications_pb2.SendDevPushNotificationReq( 

130 title=title, 

131 body=body, 

132 icon=icon, 

133 url=url, 

134 key=key, 

135 ttl=ttl, 

136 ) 

137 ) 

138 

139 

140def debug_redeliver_push_notification(*, token: str, notification_id: int) -> None: 

141 with _notifications_session(token) as session: 

142 session.DebugRedeliverPushNotification( 

143 notifications_pb2.DebugRedeliverPushNotificationReq(notification_id=notification_id) 

144 )