|
17 | 17 | # [This file includes modifications made by New Vector Limited] |
18 | 18 | # |
19 | 19 | # |
20 | | -from typing import Any, List, Tuple |
| 20 | +from typing import Any, Dict, List, Tuple |
21 | 21 | from unittest.mock import Mock |
22 | 22 |
|
| 23 | +from parameterized import parameterized |
| 24 | + |
23 | 25 | from twisted.internet.defer import Deferred |
24 | 26 | from twisted.test.proto_helpers import MemoryReactor |
25 | 27 |
|
@@ -1085,3 +1087,83 @@ def test_jitter(self) -> None: |
1085 | 1087 | self.pump() |
1086 | 1088 |
|
1087 | 1089 | self.assertEqual(len(self.push_attempts), 11) |
| 1090 | + |
| 1091 | + @parameterized.expand( |
| 1092 | + [ |
| 1093 | + # Badge count disabled |
| 1094 | + (True, True), |
| 1095 | + (True, False), |
| 1096 | + # Badge count enabled |
| 1097 | + (False, True), |
| 1098 | + (False, False), |
| 1099 | + ] |
| 1100 | + ) |
| 1101 | + @override_config({"experimental_features": {"msc4076_enabled": True}}) |
| 1102 | + def test_msc4076_badge_count( |
| 1103 | + self, disable_badge_count: bool, event_id_only: bool |
| 1104 | + ) -> None: |
| 1105 | + # Register the user who gets notified |
| 1106 | + user_id = self.register_user("user", "pass") |
| 1107 | + access_token = self.login("user", "pass") |
| 1108 | + |
| 1109 | + # Register the user who sends the message |
| 1110 | + other_user_id = self.register_user("otheruser", "pass") |
| 1111 | + other_access_token = self.login("otheruser", "pass") |
| 1112 | + |
| 1113 | + # Register the pusher with disable_badge_count set to True |
| 1114 | + user_tuple = self.get_success( |
| 1115 | + self.hs.get_datastores().main.get_user_by_access_token(access_token) |
| 1116 | + ) |
| 1117 | + assert user_tuple is not None |
| 1118 | + device_id = user_tuple.device_id |
| 1119 | + |
| 1120 | + # Set the push data dict based on test input parameters |
| 1121 | + push_data: Dict[str, Any] = { |
| 1122 | + "url": "http://example.com/_matrix/push/v1/notify", |
| 1123 | + } |
| 1124 | + if disable_badge_count: |
| 1125 | + push_data["org.matrix.msc4076.disable_badge_count"] = True |
| 1126 | + if event_id_only: |
| 1127 | + push_data["format"] = "event_id_only" |
| 1128 | + |
| 1129 | + self.get_success( |
| 1130 | + self.hs.get_pusherpool().add_or_update_pusher( |
| 1131 | + user_id=user_id, |
| 1132 | + device_id=device_id, |
| 1133 | + kind="http", |
| 1134 | + app_id="m.http", |
| 1135 | + app_display_name="HTTP Push Notifications", |
| 1136 | + device_display_name="pushy push", |
| 1137 | + |
| 1138 | + lang=None, |
| 1139 | + data=push_data, |
| 1140 | + ) |
| 1141 | + ) |
| 1142 | + |
| 1143 | + # Create a room |
| 1144 | + room = self.helper.create_room_as(user_id, tok=access_token) |
| 1145 | + |
| 1146 | + # The other user joins |
| 1147 | + self.helper.join(room=room, user=other_user_id, tok=other_access_token) |
| 1148 | + |
| 1149 | + # The other user sends a message |
| 1150 | + self.helper.send(room, body="Hi!", tok=other_access_token) |
| 1151 | + |
| 1152 | + # Advance time a bit, so the pusher will register something has happened |
| 1153 | + self.pump() |
| 1154 | + |
| 1155 | + # One push was attempted to be sent |
| 1156 | + self.assertEqual(len(self.push_attempts), 1) |
| 1157 | + self.assertEqual( |
| 1158 | + self.push_attempts[0][1], "http://example.com/_matrix/push/v1/notify" |
| 1159 | + ) |
| 1160 | + |
| 1161 | + if disable_badge_count: |
| 1162 | + # Verify that the notification DOESN'T contain a counts field |
| 1163 | + self.assertNotIn("counts", self.push_attempts[0][2]["notification"]) |
| 1164 | + else: |
| 1165 | + # Ensure that the notification DOES contain a counts field |
| 1166 | + self.assertIn("counts", self.push_attempts[0][2]["notification"]) |
| 1167 | + self.assertEqual( |
| 1168 | + self.push_attempts[0][2]["notification"]["counts"]["unread"], 1 |
| 1169 | + ) |
0 commit comments