OLD | NEW |
1 # This file is part of the Adblock Plus web scripts, | 1 # This file is part of the Adblock Plus web scripts, |
2 # Copyright (C) 2006-2016 Eyeo GmbH | 2 # Copyright (C) 2006-2016 Eyeo GmbH |
3 # | 3 # |
4 # Adblock Plus is free software: you can redistribute it and/or modify | 4 # Adblock Plus is free software: you can redistribute it and/or modify |
5 # it under the terms of the GNU General Public License version 3 as | 5 # it under the terms of the GNU General Public License version 3 as |
6 # published by the Free Software Foundation. | 6 # published by the Free Software Foundation. |
7 # | 7 # |
8 # Adblock Plus is distributed in the hope that it will be useful, | 8 # Adblock Plus is distributed in the hope that it will be useful, |
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 # GNU General Public License for more details. | 11 # GNU General Public License for more details. |
12 # | 12 # |
13 # You should have received a copy of the GNU General Public License | 13 # You should have received a copy of the GNU General Public License |
14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
15 | 15 |
16 import json | 16 import json |
17 import mock | 17 import mock |
18 import unittest | 18 import unittest |
19 | 19 |
20 import sitescripts.notifications.web.notification as notification | 20 import sitescripts.notifications.web.notification as notification |
21 | 21 |
22 | 22 |
23 class TestNotification(unittest.TestCase): | 23 class TestNotification(unittest.TestCase): |
24 def setUp(self): | 24 def setUp(self): |
25 self.load_notifications_patcher = mock.patch("sitescripts.notifications.
web.notification.load_notifications") | 25 self.load_notifications_patcher = mock.patch('sitescripts.notifications.
web.notification.load_notifications') |
26 self.load_notifications_mock = self.load_notifications_patcher.start() | 26 self.load_notifications_mock = self.load_notifications_patcher.start() |
27 | 27 |
28 def tearDown(self): | 28 def tearDown(self): |
29 self.load_notifications_patcher.stop() | 29 self.load_notifications_patcher.stop() |
30 | 30 |
31 def test_no_group(self): | 31 def test_no_group(self): |
32 self.load_notifications_mock.return_value = [ | 32 self.load_notifications_mock.return_value = [ |
33 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}} | 33 {'id': '1', 'title': {'en-US': ''}, 'message': {'en-US': ''}} |
34 ] | 34 ] |
35 result = json.loads(notification.notification({}, lambda *args: None)) | 35 result = json.loads(notification.notification({}, lambda *args: None)) |
36 self.assertEqual(len(result["notifications"]), 1) | 36 self.assertEqual(len(result['notifications']), 1) |
37 self.assertEqual(result["notifications"][0]["id"], "1") | 37 self.assertEqual(result['notifications'][0]['id'], '1') |
38 self.assertFalse("-" in result["version"]) | 38 self.assertFalse('-' in result['version']) |
39 | 39 |
40 def test_not_in_group(self): | 40 def test_not_in_group(self): |
41 self.load_notifications_mock.return_value = [ | 41 self.load_notifications_mock.return_value = [ |
42 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}}, | 42 {'id': '1', 'title': {'en-US': ''}, 'message': {'en-US': ''}}, |
43 {"id": "a", "variants": [ | 43 {'id': 'a', 'variants': [ |
44 {"title": {"en-US": ""}, "message": {"en-US": ""}} | 44 {'title': {'en-US': ''}, 'message': {'en-US': ''}} |
45 ]} | 45 ]} |
46 ] | 46 ] |
47 result = json.loads(notification.notification({ | 47 result = json.loads(notification.notification({ |
48 "QUERY_STRING": "lastVersion=197001010000-a/0" | 48 'QUERY_STRING': 'lastVersion=197001010000-a/0' |
49 }, lambda *args: None)) | 49 }, lambda *args: None)) |
50 self.assertEqual(len(result["notifications"]), 1) | 50 self.assertEqual(len(result['notifications']), 1) |
51 self.assertEqual(result["notifications"][0]["id"], "1") | 51 self.assertEqual(result['notifications'][0]['id'], '1') |
52 self.assertRegexpMatches(result["version"], r"-a/0") | 52 self.assertRegexpMatches(result['version'], r'-a/0') |
53 | 53 |
54 def test_in_group(self): | 54 def test_in_group(self): |
55 self.load_notifications_mock.return_value = [ | 55 self.load_notifications_mock.return_value = [ |
56 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}}, | 56 {'id': '1', 'title': {'en-US': ''}, 'message': {'en-US': ''}}, |
57 {"id": "a", "variants": [ | 57 {'id': 'a', 'variants': [ |
58 {"title": {"en-US": ""}, "message": {"en-US": ""}} | 58 {'title': {'en-US': ''}, 'message': {'en-US': ''}} |
59 ]} | 59 ]} |
60 ] | 60 ] |
61 result = json.loads(notification.notification({ | 61 result = json.loads(notification.notification({ |
62 "QUERY_STRING": "lastVersion=197001010000-a/1" | 62 'QUERY_STRING': 'lastVersion=197001010000-a/1' |
63 }, lambda *args: None)) | 63 }, lambda *args: None)) |
64 self.assertEqual(len(result["notifications"]), 1) | 64 self.assertEqual(len(result['notifications']), 1) |
65 self.assertEqual(result["notifications"][0]["id"], "a") | 65 self.assertEqual(result['notifications'][0]['id'], 'a') |
66 self.assertRegexpMatches(result["version"], r"-a/1") | 66 self.assertRegexpMatches(result['version'], r'-a/1') |
67 | 67 |
68 def test_not_in_one_of_many_groups(self): | 68 def test_not_in_one_of_many_groups(self): |
69 self.load_notifications_mock.return_value = [ | 69 self.load_notifications_mock.return_value = [ |
70 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}}, | 70 {'id': '1', 'title': {'en-US': ''}, 'message': {'en-US': ''}}, |
71 {"id": "a", "variants": [ | 71 {'id': 'a', 'variants': [ |
72 {"title": {"en-US": ""}, "message": {"en-US": ""}} | 72 {'title': {'en-US': ''}, 'message': {'en-US': ''}} |
73 ]}, | 73 ]}, |
74 {"id": "b", "variants": [ | 74 {'id': 'b', 'variants': [ |
75 {"title": {"en-US": ""}, "message": {"en-US": ""}} | 75 {'title': {'en-US': ''}, 'message': {'en-US': ''}} |
76 ]}, | 76 ]}, |
77 {"id": "c", "variants": [ | 77 {'id': 'c', 'variants': [ |
78 {"title": {"en-US": ""}, "message": {"en-US": ""}} | 78 {'title': {'en-US': ''}, 'message': {'en-US': ''}} |
79 ]} | 79 ]} |
80 ] | 80 ] |
81 result = json.loads(notification.notification({ | 81 result = json.loads(notification.notification({ |
82 "QUERY_STRING": "lastVersion=197001010000-a/0-b/0-c/0" | 82 'QUERY_STRING': 'lastVersion=197001010000-a/0-b/0-c/0' |
83 }, lambda *args: None)) | 83 }, lambda *args: None)) |
84 self.assertEqual(len(result["notifications"]), 1) | 84 self.assertEqual(len(result['notifications']), 1) |
85 self.assertEqual(result["notifications"][0]["id"], "1") | 85 self.assertEqual(result['notifications'][0]['id'], '1') |
86 self.assertRegexpMatches(result["version"], r"-a/0-b/0-c/0") | 86 self.assertRegexpMatches(result['version'], r'-a/0-b/0-c/0') |
87 | 87 |
88 def test_in_one_of_many_groups(self): | 88 def test_in_one_of_many_groups(self): |
89 self.load_notifications_mock.return_value = [ | 89 self.load_notifications_mock.return_value = [ |
90 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}}, | 90 {'id': '1', 'title': {'en-US': ''}, 'message': {'en-US': ''}}, |
91 {"id": "a", "variants": [ | 91 {'id': 'a', 'variants': [ |
92 {"title": {"en-US": ""}, "message": {"en-US": ""}} | 92 {'title': {'en-US': ''}, 'message': {'en-US': ''}} |
93 ]}, | 93 ]}, |
94 {"id": "b", "variants": [ | 94 {'id': 'b', 'variants': [ |
95 {"title": {"en-US": ""}, "message": {"en-US": ""}} | 95 {'title': {'en-US': ''}, 'message': {'en-US': ''}} |
96 ]}, | 96 ]}, |
97 {"id": "c", "variants": [ | 97 {'id': 'c', 'variants': [ |
98 {"title": {"en-US": ""}, "message": {"en-US": ""}} | 98 {'title': {'en-US': ''}, 'message': {'en-US': ''}} |
99 ]} | 99 ]} |
100 ] | 100 ] |
101 result = json.loads(notification.notification({ | 101 result = json.loads(notification.notification({ |
102 "QUERY_STRING": "lastVersion=197001010000-a/0-b/1-c/0" | 102 'QUERY_STRING': 'lastVersion=197001010000-a/0-b/1-c/0' |
103 }, lambda *args: None)) | 103 }, lambda *args: None)) |
104 self.assertEqual(len(result["notifications"]), 1) | 104 self.assertEqual(len(result['notifications']), 1) |
105 self.assertEqual(result["notifications"][0]["id"], "b") | 105 self.assertEqual(result['notifications'][0]['id'], 'b') |
106 self.assertRegexpMatches(result["version"], r"-a/0-b/1-c/0") | 106 self.assertRegexpMatches(result['version'], r'-a/0-b/1-c/0') |
107 | 107 |
108 def test_not_put_in_group(self): | 108 def test_not_put_in_group(self): |
109 self.load_notifications_mock.return_value = [ | 109 self.load_notifications_mock.return_value = [ |
110 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}}, | 110 {'id': '1', 'title': {'en-US': ''}, 'message': {'en-US': ''}}, |
111 {"id": "a", "variants": [ | 111 {'id': 'a', 'variants': [ |
112 {"sample": 0, "title": {"en-US": ""}, "message": {"en-US": ""}} | 112 {'sample': 0, 'title': {'en-US': ''}, 'message': {'en-US': ''}} |
113 ]} | 113 ]} |
114 ] | 114 ] |
115 result = json.loads(notification.notification({ | 115 result = json.loads(notification.notification({ |
116 "QUERY_STRING": "lastVersion=197001010000" | 116 'QUERY_STRING': 'lastVersion=197001010000' |
117 }, lambda *args: None)) | 117 }, lambda *args: None)) |
118 self.assertEqual(len(result["notifications"]), 1) | 118 self.assertEqual(len(result['notifications']), 1) |
119 self.assertEqual(result["notifications"][0]["id"], "1") | 119 self.assertEqual(result['notifications'][0]['id'], '1') |
120 self.assertRegexpMatches(result["version"], r"-a/0") | 120 self.assertRegexpMatches(result['version'], r'-a/0') |
121 | 121 |
122 def test_put_in_group(self): | 122 def test_put_in_group(self): |
123 self.load_notifications_mock.return_value = [ | 123 self.load_notifications_mock.return_value = [ |
124 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}}, | 124 {'id': '1', 'title': {'en-US': ''}, 'message': {'en-US': ''}}, |
125 {"id": "a", "variants": [ | 125 {'id': 'a', 'variants': [ |
126 {"sample": 1, "title": {"en-US": ""}, "message": {"en-US": ""}} | 126 {'sample': 1, 'title': {'en-US': ''}, 'message': {'en-US': ''}} |
127 ]} | 127 ]} |
128 ] | 128 ] |
129 result = json.loads(notification.notification({ | 129 result = json.loads(notification.notification({ |
130 "QUERY_STRING": "lastVersion=197001010000" | 130 'QUERY_STRING': 'lastVersion=197001010000' |
131 }, lambda *args: None)) | 131 }, lambda *args: None)) |
132 self.assertEqual(len(result["notifications"]), 1) | 132 self.assertEqual(len(result['notifications']), 1) |
133 self.assertEqual(result["notifications"][0]["id"], "a") | 133 self.assertEqual(result['notifications'][0]['id'], 'a') |
134 self.assertRegexpMatches(result["version"], r"-a/1") | 134 self.assertRegexpMatches(result['version'], r'-a/1') |
135 | 135 |
136 def test_notification_variant_merged(self): | 136 def test_notification_variant_merged(self): |
137 self.load_notifications_mock.return_value = [ | 137 self.load_notifications_mock.return_value = [ |
138 { | 138 { |
139 "id": "a", | 139 'id': 'a', |
140 "title": {"en-US": "default"}, | 140 'title': {'en-US': 'default'}, |
141 "message": {"en-US": "default"}, | 141 'message': {'en-US': 'default'}, |
142 "variants": [ | 142 'variants': [ |
143 {"sample": 1, "message": {"en-US": "variant"}} | 143 {'sample': 1, 'message': {'en-US': 'variant'}} |
144 ] | 144 ] |
145 } | 145 } |
146 ] | 146 ] |
147 result = json.loads(notification.notification({}, lambda *args: None)) | 147 result = json.loads(notification.notification({}, lambda *args: None)) |
148 self.assertEqual(len(result["notifications"]), 1) | 148 self.assertEqual(len(result['notifications']), 1) |
149 self.assertEqual(result["notifications"][0]["id"], "a") | 149 self.assertEqual(result['notifications'][0]['id'], 'a') |
150 self.assertEqual(result["notifications"][0]["title"]["en-US"], "default"
) | 150 self.assertEqual(result['notifications'][0]['title']['en-US'], 'default'
) |
151 self.assertEqual(result["notifications"][0]["message"]["en-US"], "varian
t") | 151 self.assertEqual(result['notifications'][0]['message']['en-US'], 'varian
t') |
152 self.assertFalse("variants" in result["notifications"][0]) | 152 self.assertFalse('variants' in result['notifications'][0]) |
153 self.assertFalse("sample" in result["notifications"][0]) | 153 self.assertFalse('sample' in result['notifications'][0]) |
154 | 154 |
155 def test_no_variant_no_notifications(self): | 155 def test_no_variant_no_notifications(self): |
156 self.load_notifications_mock.return_value = [ | 156 self.load_notifications_mock.return_value = [ |
157 {"id": "a", "variants": [{"sample": 0}]} | 157 {'id': 'a', 'variants': [{'sample': 0}]} |
158 ] | 158 ] |
159 result = json.loads(notification.notification({}, lambda *args: None)) | 159 result = json.loads(notification.notification({}, lambda *args: None)) |
160 self.assertEqual(len(result["notifications"]), 0) | 160 self.assertEqual(len(result['notifications']), 0) |
161 | 161 |
162 @mock.patch("random.random") | 162 @mock.patch('random.random') |
163 def test_probability_distribution_single_group(self, random_call): | 163 def test_probability_distribution_single_group(self, random_call): |
164 self.load_notifications_mock.return_value = [ | 164 self.load_notifications_mock.return_value = [ |
165 { | 165 { |
166 "id": "a", | 166 'id': 'a', |
167 "variants": [ | 167 'variants': [ |
168 {"sample": 0.5, "title": {"en-US": "1"}, "message": {"en-US"
: ""}}, | 168 {'sample': 0.5, 'title': {'en-US': '1'}, 'message': {'en-US'
: ''}}, |
169 {"sample": 0.25, "title": {"en-US": "2"}, "message": {"en-US
": ""}}, | 169 {'sample': 0.25, 'title': {'en-US': '2'}, 'message': {'en-US
': ''}}, |
170 {"sample": 0.25, "title": {"en-US": "3"}, "message": {"en-US
": ""}} | 170 {'sample': 0.25, 'title': {'en-US': '3'}, 'message': {'en-US
': ''}} |
171 ] | 171 ] |
172 } | 172 } |
173 ] | 173 ] |
174 random_call.return_value = 0 | 174 random_call.return_value = 0 |
175 result = json.loads(notification.notification({}, lambda *args: None)) | 175 result = json.loads(notification.notification({}, lambda *args: None)) |
176 self.assertEqual(len(result["notifications"]), 1) | 176 self.assertEqual(len(result['notifications']), 1) |
177 self.assertEqual(result["notifications"][0]["title"]["en-US"], "1") | 177 self.assertEqual(result['notifications'][0]['title']['en-US'], '1') |
178 self.assertRegexpMatches(result["version"], r"-a/1") | 178 self.assertRegexpMatches(result['version'], r'-a/1') |
179 random_call.return_value = 0.5 | 179 random_call.return_value = 0.5 |
180 result = json.loads(notification.notification({}, lambda *args: None)) | 180 result = json.loads(notification.notification({}, lambda *args: None)) |
181 self.assertEqual(len(result["notifications"]), 1) | 181 self.assertEqual(len(result['notifications']), 1) |
182 self.assertEqual(result["notifications"][0]["title"]["en-US"], "1") | 182 self.assertEqual(result['notifications'][0]['title']['en-US'], '1') |
183 self.assertRegexpMatches(result["version"], r"-a/1") | 183 self.assertRegexpMatches(result['version'], r'-a/1') |
184 random_call.return_value = 0.51 | 184 random_call.return_value = 0.51 |
185 result = json.loads(notification.notification({}, lambda *args: None)) | 185 result = json.loads(notification.notification({}, lambda *args: None)) |
186 self.assertEqual(len(result["notifications"]), 1) | 186 self.assertEqual(len(result['notifications']), 1) |
187 self.assertEqual(result["notifications"][0]["title"]["en-US"], "2") | 187 self.assertEqual(result['notifications'][0]['title']['en-US'], '2') |
188 self.assertRegexpMatches(result["version"], r"-a/2") | 188 self.assertRegexpMatches(result['version'], r'-a/2') |
189 random_call.return_value = 0.75 | 189 random_call.return_value = 0.75 |
190 result = json.loads(notification.notification({}, lambda *args: None)) | 190 result = json.loads(notification.notification({}, lambda *args: None)) |
191 self.assertEqual(len(result["notifications"]), 1) | 191 self.assertEqual(len(result['notifications']), 1) |
192 self.assertEqual(result["notifications"][0]["title"]["en-US"], "2") | 192 self.assertEqual(result['notifications'][0]['title']['en-US'], '2') |
193 self.assertRegexpMatches(result["version"], r"-a/2") | 193 self.assertRegexpMatches(result['version'], r'-a/2') |
194 random_call.return_value = 0.751 | 194 random_call.return_value = 0.751 |
195 result = json.loads(notification.notification({}, lambda *args: None)) | 195 result = json.loads(notification.notification({}, lambda *args: None)) |
196 self.assertEqual(len(result["notifications"]), 1) | 196 self.assertEqual(len(result['notifications']), 1) |
197 self.assertEqual(result["notifications"][0]["title"]["en-US"], "3") | 197 self.assertEqual(result['notifications'][0]['title']['en-US'], '3') |
198 self.assertRegexpMatches(result["version"], r"-a/3") | 198 self.assertRegexpMatches(result['version'], r'-a/3') |
199 random_call.return_value = 1 | 199 random_call.return_value = 1 |
200 result = json.loads(notification.notification({}, lambda *args: None)) | 200 result = json.loads(notification.notification({}, lambda *args: None)) |
201 self.assertEqual(len(result["notifications"]), 1) | 201 self.assertEqual(len(result['notifications']), 1) |
202 self.assertEqual(result["notifications"][0]["title"]["en-US"], "3") | 202 self.assertEqual(result['notifications'][0]['title']['en-US'], '3') |
203 self.assertRegexpMatches(result["version"], r"-a/3") | 203 self.assertRegexpMatches(result['version'], r'-a/3') |
204 | 204 |
205 @mock.patch("random.random") | 205 @mock.patch('random.random') |
206 def test_probability_distribution_multiple_groups(self, random_call): | 206 def test_probability_distribution_multiple_groups(self, random_call): |
207 self.load_notifications_mock.return_value = [ | 207 self.load_notifications_mock.return_value = [ |
208 { | 208 { |
209 "id": "a", | 209 'id': 'a', |
210 "variants": [ | 210 'variants': [ |
211 {"sample": 0.25, "title": {"en-US": "1"}, "message": {"en-US
": ""}}, | 211 {'sample': 0.25, 'title': {'en-US': '1'}, 'message': {'en-US
': ''}}, |
212 {"sample": 0.25, "title": {"en-US": "2"}, "message": {"en-US
": ""}} | 212 {'sample': 0.25, 'title': {'en-US': '2'}, 'message': {'en-US
': ''}} |
213 ] | 213 ] |
214 }, | 214 }, |
215 { | 215 { |
216 "id": "b", | 216 'id': 'b', |
217 "variants": [ | 217 'variants': [ |
218 {"sample": 0.25, "title": {"en-US": "1"}, "message": {"en-US
": ""}}, | 218 {'sample': 0.25, 'title': {'en-US': '1'}, 'message': {'en-US
': ''}}, |
219 {"sample": 0.25, "title": {"en-US": "2"}, "message": {"en-US
": ""}} | 219 {'sample': 0.25, 'title': {'en-US': '2'}, 'message': {'en-US
': ''}} |
220 ] | 220 ] |
221 } | 221 } |
222 ] | 222 ] |
223 random_call.return_value = 0 | 223 random_call.return_value = 0 |
224 result = json.loads(notification.notification({}, lambda *args: None)) | 224 result = json.loads(notification.notification({}, lambda *args: None)) |
225 self.assertEqual(len(result["notifications"]), 1) | 225 self.assertEqual(len(result['notifications']), 1) |
226 self.assertEqual(result["notifications"][0]["id"], "a") | 226 self.assertEqual(result['notifications'][0]['id'], 'a') |
227 self.assertEqual(result["notifications"][0]["title"]["en-US"], "1") | 227 self.assertEqual(result['notifications'][0]['title']['en-US'], '1') |
228 self.assertRegexpMatches(result["version"], r"-a/1-b/0") | 228 self.assertRegexpMatches(result['version'], r'-a/1-b/0') |
229 random_call.return_value = 0.251 | 229 random_call.return_value = 0.251 |
230 result = json.loads(notification.notification({}, lambda *args: None)) | 230 result = json.loads(notification.notification({}, lambda *args: None)) |
231 self.assertEqual(len(result["notifications"]), 1) | 231 self.assertEqual(len(result['notifications']), 1) |
232 self.assertEqual(result["notifications"][0]["id"], "a") | 232 self.assertEqual(result['notifications'][0]['id'], 'a') |
233 self.assertEqual(result["notifications"][0]["title"]["en-US"], "2") | 233 self.assertEqual(result['notifications'][0]['title']['en-US'], '2') |
234 self.assertRegexpMatches(result["version"], r"-a/2-b/0") | 234 self.assertRegexpMatches(result['version'], r'-a/2-b/0') |
235 random_call.return_value = 0.51 | 235 random_call.return_value = 0.51 |
236 result = json.loads(notification.notification({}, lambda *args: None)) | 236 result = json.loads(notification.notification({}, lambda *args: None)) |
237 self.assertEqual(len(result["notifications"]), 1) | 237 self.assertEqual(len(result['notifications']), 1) |
238 self.assertEqual(result["notifications"][0]["id"], "b") | 238 self.assertEqual(result['notifications'][0]['id'], 'b') |
239 self.assertEqual(result["notifications"][0]["title"]["en-US"], "1") | 239 self.assertEqual(result['notifications'][0]['title']['en-US'], '1') |
240 self.assertRegexpMatches(result["version"], r"-a/0-b/1") | 240 self.assertRegexpMatches(result['version'], r'-a/0-b/1') |
241 random_call.return_value = 0.751 | 241 random_call.return_value = 0.751 |
242 result = json.loads(notification.notification({}, lambda *args: None)) | 242 result = json.loads(notification.notification({}, lambda *args: None)) |
243 self.assertEqual(len(result["notifications"]), 1) | 243 self.assertEqual(len(result['notifications']), 1) |
244 self.assertEqual(result["notifications"][0]["id"], "b") | 244 self.assertEqual(result['notifications'][0]['id'], 'b') |
245 self.assertEqual(result["notifications"][0]["title"]["en-US"], "2") | 245 self.assertEqual(result['notifications'][0]['title']['en-US'], '2') |
246 self.assertRegexpMatches(result["version"], r"-a/0-b/2") | 246 self.assertRegexpMatches(result['version'], r'-a/0-b/2') |
247 | 247 |
248 def test_invalid_last_version(self): | 248 def test_invalid_last_version(self): |
249 self.load_notifications_mock.return_value = [] | 249 self.load_notifications_mock.return_value = [] |
250 notification.notification({"QUERY_STRING": "lastVersion="}, | 250 notification.notification({'QUERY_STRING': 'lastVersion='}, |
251 lambda *args: None) | 251 lambda *args: None) |
252 notification.notification({"QUERY_STRING": "lastVersion=-"}, | 252 notification.notification({'QUERY_STRING': 'lastVersion=-'}, |
253 lambda *args: None) | 253 lambda *args: None) |
254 notification.notification({"QUERY_STRING": "lastVersion=-/"}, | 254 notification.notification({'QUERY_STRING': 'lastVersion=-/'}, |
255 lambda *args: None) | 255 lambda *args: None) |
256 notification.notification({"QUERY_STRING": "lastVersion=-//"}, | 256 notification.notification({'QUERY_STRING': 'lastVersion=-//'}, |
257 lambda *args: None) | 257 lambda *args: None) |
258 | 258 |
259 def test_version_header_present(self): | 259 def test_version_header_present(self): |
260 self.load_notifications_mock.return_value = [ | 260 self.load_notifications_mock.return_value = [ |
261 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}} | 261 {'id': '1', 'title': {'en-US': ''}, 'message': {'en-US': ''}} |
262 ] | 262 ] |
263 response_header_map = {} | 263 response_header_map = {} |
264 | 264 |
265 def start_response(status, response_headers): | 265 def start_response(status, response_headers): |
266 for name, value in response_headers: | 266 for name, value in response_headers: |
267 response_header_map[name] = value | 267 response_header_map[name] = value |
268 result = json.loads(notification.notification({}, start_response)) | 268 result = json.loads(notification.notification({}, start_response)) |
269 self.assertEqual(result["version"], | 269 self.assertEqual(result['version'], |
270 response_header_map["ABP-Notification-Version"]) | 270 response_header_map['ABP-Notification-Version']) |
271 | 271 |
272 def test_default_group_notification_returned_if_valid(self): | 272 def test_default_group_notification_returned_if_valid(self): |
273 self.load_notifications_mock.return_value = [ | 273 self.load_notifications_mock.return_value = [ |
274 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}}, | 274 {'id': '1', 'title': {'en-US': ''}, 'message': {'en-US': ''}}, |
275 { | 275 { |
276 "id": "a", | 276 'id': 'a', |
277 "title": {"en-US": "0"}, | 277 'title': {'en-US': '0'}, |
278 "message": {"en-US": "0"}, | 278 'message': {'en-US': '0'}, |
279 "variants": [ | 279 'variants': [ |
280 {"title": {"en-US": "1"}, "message": {"en-US": "1"}} | 280 {'title': {'en-US': '1'}, 'message': {'en-US': '1'}} |
281 ] | 281 ] |
282 } | 282 } |
283 ] | 283 ] |
284 result = json.loads(notification.notification({ | 284 result = json.loads(notification.notification({ |
285 "QUERY_STRING": "lastVersion=197001010000-a/0" | 285 'QUERY_STRING': 'lastVersion=197001010000-a/0' |
286 }, lambda *args: None)) | 286 }, lambda *args: None)) |
287 self.assertEqual(len(result["notifications"]), 2) | 287 self.assertEqual(len(result['notifications']), 2) |
288 self.assertEqual(result["notifications"][0]["id"], "1") | 288 self.assertEqual(result['notifications'][0]['id'], '1') |
289 self.assertEqual(result["notifications"][1]["id"], "a") | 289 self.assertEqual(result['notifications'][1]['id'], 'a') |
290 self.assertEqual(result["notifications"][1]["title"]["en-US"], "0") | 290 self.assertEqual(result['notifications'][1]['title']['en-US'], '0') |
291 self.assertNotIn("variants", result["notifications"][1]) | 291 self.assertNotIn('variants', result['notifications'][1]) |
292 self.assertRegexpMatches(result["version"], r"-a/0") | 292 self.assertRegexpMatches(result['version'], r'-a/0') |
293 | 293 |
294 def test_default_group_notification_not_returned_if_invalid(self): | 294 def test_default_group_notification_not_returned_if_invalid(self): |
295 self.load_notifications_mock.return_value = [ | 295 self.load_notifications_mock.return_value = [ |
296 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}}, | 296 {'id': '1', 'title': {'en-US': ''}, 'message': {'en-US': ''}}, |
297 { | 297 { |
298 "id": "a", | 298 'id': 'a', |
299 "title": {"en-US": "0"}, | 299 'title': {'en-US': '0'}, |
300 "variants": [ | 300 'variants': [ |
301 {"title": {"en-US": "1"}, "message": {"en-US": "1"}} | 301 {'title': {'en-US': '1'}, 'message': {'en-US': '1'}} |
302 ] | 302 ] |
303 } | 303 } |
304 ] | 304 ] |
305 result = json.loads(notification.notification({ | 305 result = json.loads(notification.notification({ |
306 "QUERY_STRING": "lastVersion=197001010000-a/0" | 306 'QUERY_STRING': 'lastVersion=197001010000-a/0' |
307 }, lambda *args: None)) | 307 }, lambda *args: None)) |
308 self.assertEqual(len(result["notifications"]), 1) | 308 self.assertEqual(len(result['notifications']), 1) |
309 self.assertEqual(result["notifications"][0]["id"], "1") | 309 self.assertEqual(result['notifications'][0]['id'], '1') |
310 self.assertRegexpMatches(result["version"], r"-a/0") | 310 self.assertRegexpMatches(result['version'], r'-a/0') |
311 | 311 |
312 def test_invalid_notification_not_returned(self): | 312 def test_invalid_notification_not_returned(self): |
313 self.load_notifications_mock.return_value = [ | 313 self.load_notifications_mock.return_value = [ |
314 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}}, | 314 {'id': '1', 'title': {'en-US': ''}, 'message': {'en-US': ''}}, |
315 {"id": "2", "title": {"en-US": ""}, "message": {}}, | 315 {'id': '2', 'title': {'en-US': ''}, 'message': {}}, |
316 {"id": "3", "title": {}, "message": {"en-US": ""}}, | 316 {'id': '3', 'title': {}, 'message': {'en-US': ''}}, |
317 {"id": "4", "title": {}}, | 317 {'id': '4', 'title': {}}, |
318 {"id": "5", "message": {}}, | 318 {'id': '5', 'message': {}}, |
319 {"id": "6"} | 319 {'id': '6'} |
320 ] | 320 ] |
321 result = json.loads(notification.notification({}, lambda *args: None)) | 321 result = json.loads(notification.notification({}, lambda *args: None)) |
322 self.assertEqual(len(result["notifications"]), 1) | 322 self.assertEqual(len(result['notifications']), 1) |
323 self.assertEqual(result["notifications"][0]["id"], "1") | 323 self.assertEqual(result['notifications'][0]['id'], '1') |
324 | 324 |
325 def test_stays_in_group_when_notification_present(self): | 325 def test_stays_in_group_when_notification_present(self): |
326 self.load_notifications_mock.return_value = [ | 326 self.load_notifications_mock.return_value = [ |
327 {"id": "a"} | 327 {'id': 'a'} |
328 ] | 328 ] |
329 result = json.loads(notification.notification({ | 329 result = json.loads(notification.notification({ |
330 "QUERY_STRING": "lastVersion=197001010000-a/0-b/1" | 330 'QUERY_STRING': 'lastVersion=197001010000-a/0-b/1' |
331 }, lambda *args: None)) | 331 }, lambda *args: None)) |
332 self.assertEqual(len(result["notifications"]), 0) | 332 self.assertEqual(len(result['notifications']), 0) |
333 self.assertRegexpMatches(result["version"], r"-a/0") | 333 self.assertRegexpMatches(result['version'], r'-a/0') |
334 | 334 |
335 def test_leaves_group_when_notification_absent(self): | 335 def test_leaves_group_when_notification_absent(self): |
336 self.load_notifications_mock.return_value = [] | 336 self.load_notifications_mock.return_value = [] |
337 result = json.loads(notification.notification({ | 337 result = json.loads(notification.notification({ |
338 "QUERY_STRING": "lastVersion=197001010000-a/0-b/1" | 338 'QUERY_STRING': 'lastVersion=197001010000-a/0-b/1' |
339 }, lambda *args: None)) | 339 }, lambda *args: None)) |
340 self.assertEqual(len(result["notifications"]), 0) | 340 self.assertEqual(len(result['notifications']), 0) |
341 self.assertRegexpMatches(result["version"], r"[^-]*") | 341 self.assertRegexpMatches(result['version'], r'[^-]*') |
342 | 342 |
343 def test_stays_in_group_when_notification_inactive(self): | 343 def test_stays_in_group_when_notification_inactive(self): |
344 self.load_notifications_mock.return_value = [ | 344 self.load_notifications_mock.return_value = [ |
345 {"id": "a", "inactive": True} | 345 {'id': 'a', 'inactive': True} |
346 ] | 346 ] |
347 result = json.loads(notification.notification({ | 347 result = json.loads(notification.notification({ |
348 "QUERY_STRING": "lastVersion=197001010000-a/0-b/1" | 348 'QUERY_STRING': 'lastVersion=197001010000-a/0-b/1' |
349 }, lambda *args: None)) | 349 }, lambda *args: None)) |
350 self.assertEqual(len(result["notifications"]), 0) | 350 self.assertEqual(len(result['notifications']), 0) |
351 self.assertRegexpMatches(result["version"], r"-a/0") | 351 self.assertRegexpMatches(result['version'], r'-a/0') |
352 | 352 |
353 def test_inactive_notifications_not_returned(self): | 353 def test_inactive_notifications_not_returned(self): |
354 self.load_notifications_mock.return_value = [ | 354 self.load_notifications_mock.return_value = [ |
355 {"id": "a", "title": {"en-US": ""}, "message": {"en-US": ""}, "inact
ive": True}, | 355 {'id': 'a', 'title': {'en-US': ''}, 'message': {'en-US': ''}, 'inact
ive': True}, |
356 {"id": "b", "title": {"en-US": ""}, "message": {"en-US": ""}, "inact
ive": False}, | 356 {'id': 'b', 'title': {'en-US': ''}, 'message': {'en-US': ''}, 'inact
ive': False}, |
357 {"id": "c", "title": {"en-US": ""}, "message": {"en-US": ""}} | 357 {'id': 'c', 'title': {'en-US': ''}, 'message': {'en-US': ''}} |
358 ] | 358 ] |
359 result = json.loads(notification.notification({}, lambda *args: None)) | 359 result = json.loads(notification.notification({}, lambda *args: None)) |
360 self.assertEqual(len(result["notifications"]), 2) | 360 self.assertEqual(len(result['notifications']), 2) |
361 self.assertEqual(result["notifications"][0]["id"], "b") | 361 self.assertEqual(result['notifications'][0]['id'], 'b') |
362 self.assertEqual(result["notifications"][1]["id"], "c") | 362 self.assertEqual(result['notifications'][1]['id'], 'c') |
363 | 363 |
364 def test_inactive_notification_variant_not_returned(self): | 364 def test_inactive_notification_variant_not_returned(self): |
365 self.load_notifications_mock.return_value = [ | 365 self.load_notifications_mock.return_value = [ |
366 {"id": "a", "inactive": True} | 366 {'id': 'a', 'inactive': True} |
367 ] | 367 ] |
368 result = json.loads(notification.notification({ | 368 result = json.loads(notification.notification({ |
369 "QUERY_STRING": "lastVersion=197001010000-a/1" | 369 'QUERY_STRING': 'lastVersion=197001010000-a/1' |
370 }, lambda *args: None)) | 370 }, lambda *args: None)) |
371 self.assertEqual(len(result["notifications"]), 0) | 371 self.assertEqual(len(result['notifications']), 0) |
372 | 372 |
373 if __name__ == '__main__': | 373 if __name__ == '__main__': |
374 unittest.main() | 374 unittest.main() |
OLD | NEW |