Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: sitescripts/notifications/test/notification.py

Issue 29328658: Issue 2982 - Only return notifications that have at least one entry for title and message (Closed)
Patch Set: Created Sept. 25, 2015, 12:24 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | sitescripts/notifications/web/notification.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # coding: utf-8 1 # coding: utf-8
2 2
3 # This file is part of the Adblock Plus web scripts, 3 # This file is part of the Adblock Plus web scripts,
4 # Copyright (C) 2006-2015 Eyeo GmbH 4 # Copyright (C) 2006-2015 Eyeo GmbH
5 # 5 #
6 # Adblock Plus is free software: you can redistribute it and/or modify 6 # Adblock Plus is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License version 3 as 7 # it under the terms of the GNU General Public License version 3 as
8 # published by the Free Software Foundation. 8 # published by the Free Software Foundation.
9 # 9 #
10 # Adblock Plus is distributed in the hope that it will be useful, 10 # Adblock Plus is distributed in the hope that it will be useful,
(...skipping 13 matching lines...) Expand all
24 class TestNotification(unittest.TestCase): 24 class TestNotification(unittest.TestCase):
25 def setUp(self): 25 def setUp(self):
26 self.load_notifications_patcher = mock.patch("sitescripts.notifications.web. notification.load_notifications") 26 self.load_notifications_patcher = mock.patch("sitescripts.notifications.web. notification.load_notifications")
27 self.load_notifications_mock = self.load_notifications_patcher.start() 27 self.load_notifications_mock = self.load_notifications_patcher.start()
28 28
29 def tearDown(self): 29 def tearDown(self):
30 self.load_notifications_patcher.stop() 30 self.load_notifications_patcher.stop()
31 31
32 def test_no_group(self): 32 def test_no_group(self):
33 self.load_notifications_mock.return_value = [ 33 self.load_notifications_mock.return_value = [
34 {"id": "1", "title": {}, "message": {}} 34 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}}
35 ] 35 ]
36 result = json.loads(notification.notification({}, lambda *args: None)) 36 result = json.loads(notification.notification({}, lambda *args: None))
37 self.assertEqual(len(result["notifications"]), 1) 37 self.assertEqual(len(result["notifications"]), 1)
38 self.assertEqual(result["notifications"][0]["id"], "1") 38 self.assertEqual(result["notifications"][0]["id"], "1")
39 self.assertFalse("-" in result["version"]) 39 self.assertFalse("-" in result["version"])
40 40
41 def test_not_in_group(self): 41 def test_not_in_group(self):
42 self.load_notifications_mock.return_value = [ 42 self.load_notifications_mock.return_value = [
43 {"id": "1", "title": {}, "message": {}}, 43 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}},
44 {"id": "a", "variants": [{"title": {}, "message": {}}]} 44 {"id": "a", "variants": [
45 {"title": {"en-US": ""}, "message": {"en-US": ""}}
46 ]}
45 ] 47 ]
46 result = json.loads(notification.notification({ 48 result = json.loads(notification.notification({
47 "QUERY_STRING": "lastVersion=197001010000-a/0" 49 "QUERY_STRING": "lastVersion=197001010000-a/0"
48 }, lambda *args: None)) 50 }, lambda *args: None))
49 self.assertEqual(len(result["notifications"]), 1) 51 self.assertEqual(len(result["notifications"]), 1)
50 self.assertEqual(result["notifications"][0]["id"], "1") 52 self.assertEqual(result["notifications"][0]["id"], "1")
51 self.assertRegexpMatches(result["version"], r"-a/0") 53 self.assertRegexpMatches(result["version"], r"-a/0")
52 54
53 def test_in_group(self): 55 def test_in_group(self):
54 self.load_notifications_mock.return_value = [ 56 self.load_notifications_mock.return_value = [
55 {"id": "1", "title": {}, "message": {}}, 57 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}},
56 {"id": "a", "variants": [{"title": {}, "message": {}}]} 58 {"id": "a", "variants": [
59 {"title": {"en-US": ""}, "message": {"en-US": ""}}
60 ]}
57 ] 61 ]
58 result = json.loads(notification.notification({ 62 result = json.loads(notification.notification({
59 "QUERY_STRING": "lastVersion=197001010000-a/1" 63 "QUERY_STRING": "lastVersion=197001010000-a/1"
60 }, lambda *args: None)) 64 }, lambda *args: None))
61 self.assertEqual(len(result["notifications"]), 1) 65 self.assertEqual(len(result["notifications"]), 1)
62 self.assertEqual(result["notifications"][0]["id"], "a") 66 self.assertEqual(result["notifications"][0]["id"], "a")
63 self.assertRegexpMatches(result["version"], r"-a/1") 67 self.assertRegexpMatches(result["version"], r"-a/1")
64 68
65 def test_not_in_one_of_many_groups(self): 69 def test_not_in_one_of_many_groups(self):
66 self.load_notifications_mock.return_value = [ 70 self.load_notifications_mock.return_value = [
67 {"id": "1", "title": {}, "message": {}}, 71 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}},
68 {"id": "a", "variants": [{"title": {}, "message": {}}]}, 72 {"id": "a", "variants": [
69 {"id": "b", "variants": [{"title": {}, "message": {}}]}, 73 {"title": {"en-US": ""}, "message": {"en-US": ""}}
70 {"id": "c", "variants": [{"title": {}, "message": {}}]} 74 ]},
75 {"id": "b", "variants": [
76 {"title": {"en-US": ""}, "message": {"en-US": ""}}
77 ]},
78 {"id": "c", "variants": [
79 {"title": {"en-US": ""}, "message": {"en-US": ""}}
80 ]}
71 ] 81 ]
72 result = json.loads(notification.notification({ 82 result = json.loads(notification.notification({
73 "QUERY_STRING": "lastVersion=197001010000-a/0-b/0-c/0" 83 "QUERY_STRING": "lastVersion=197001010000-a/0-b/0-c/0"
74 }, lambda *args: None)) 84 }, lambda *args: None))
75 self.assertEqual(len(result["notifications"]), 1) 85 self.assertEqual(len(result["notifications"]), 1)
76 self.assertEqual(result["notifications"][0]["id"], "1") 86 self.assertEqual(result["notifications"][0]["id"], "1")
77 self.assertRegexpMatches(result["version"], r"-a/0-b/0-c/0") 87 self.assertRegexpMatches(result["version"], r"-a/0-b/0-c/0")
78 88
79 def test_in_one_of_many_groups(self): 89 def test_in_one_of_many_groups(self):
80 self.load_notifications_mock.return_value = [ 90 self.load_notifications_mock.return_value = [
81 {"id": "1", "title": {}, "message": {}}, 91 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}},
82 {"id": "a", "variants": [{"title": {}, "message": {}}]}, 92 {"id": "a", "variants": [
83 {"id": "b", "variants": [{"title": {}, "message": {}}]}, 93 {"title": {"en-US": ""}, "message": {"en-US": ""}}
84 {"id": "c", "variants": [{"title": {}, "message": {}}]} 94 ]},
95 {"id": "b", "variants": [
96 {"title": {"en-US": ""}, "message": {"en-US": ""}}
97 ]},
98 {"id": "c", "variants": [
99 {"title": {"en-US": ""}, "message": {"en-US": ""}}
100 ]}
85 ] 101 ]
86 result = json.loads(notification.notification({ 102 result = json.loads(notification.notification({
87 "QUERY_STRING": "lastVersion=197001010000-a/0-b/1-c/0" 103 "QUERY_STRING": "lastVersion=197001010000-a/0-b/1-c/0"
88 }, lambda *args: None)) 104 }, lambda *args: None))
89 self.assertEqual(len(result["notifications"]), 1) 105 self.assertEqual(len(result["notifications"]), 1)
90 self.assertEqual(result["notifications"][0]["id"], "b") 106 self.assertEqual(result["notifications"][0]["id"], "b")
91 self.assertRegexpMatches(result["version"], r"-a/0-b/1-c/0") 107 self.assertRegexpMatches(result["version"], r"-a/0-b/1-c/0")
92 108
93 def test_not_put_in_group(self): 109 def test_not_put_in_group(self):
94 self.load_notifications_mock.return_value = [ 110 self.load_notifications_mock.return_value = [
95 {"id": "1", "title": {}, "message": {}}, 111 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}},
96 {"id": "a", "variants": [{"sample": 0, "title": {}, "message": {}}]} 112 {"id": "a", "variants": [
113 {"sample": 0, "title": {"en-US": ""}, "message": {"en-US": ""}}
114 ]}
97 ] 115 ]
98 result = json.loads(notification.notification({ 116 result = json.loads(notification.notification({
99 "QUERY_STRING": "lastVersion=197001010000" 117 "QUERY_STRING": "lastVersion=197001010000"
100 }, lambda *args: None)) 118 }, lambda *args: None))
101 self.assertEqual(len(result["notifications"]), 1) 119 self.assertEqual(len(result["notifications"]), 1)
102 self.assertEqual(result["notifications"][0]["id"], "1") 120 self.assertEqual(result["notifications"][0]["id"], "1")
103 self.assertRegexpMatches(result["version"], r"-a/0") 121 self.assertRegexpMatches(result["version"], r"-a/0")
104 122
105 def test_put_in_group(self): 123 def test_put_in_group(self):
106 self.load_notifications_mock.return_value = [ 124 self.load_notifications_mock.return_value = [
107 {"id": "1", "title": {}, "message": {}}, 125 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}},
108 {"id": "a", "variants": [{"sample": 1, "title": {}, "message": {}}]} 126 {"id": "a", "variants": [
127 {"sample": 1, "title": {"en-US": ""}, "message": {"en-US": ""}}
128 ]}
109 ] 129 ]
110 result = json.loads(notification.notification({ 130 result = json.loads(notification.notification({
111 "QUERY_STRING": "lastVersion=197001010000" 131 "QUERY_STRING": "lastVersion=197001010000"
112 }, lambda *args: None)) 132 }, lambda *args: None))
113 self.assertEqual(len(result["notifications"]), 1) 133 self.assertEqual(len(result["notifications"]), 1)
114 self.assertEqual(result["notifications"][0]["id"], "a") 134 self.assertEqual(result["notifications"][0]["id"], "a")
115 self.assertRegexpMatches(result["version"], r"-a/1") 135 self.assertRegexpMatches(result["version"], r"-a/1")
116 136
117 def test_notification_variant_merged(self): 137 def test_notification_variant_merged(self):
118 self.load_notifications_mock.return_value = [ 138 self.load_notifications_mock.return_value = [
(...skipping 20 matching lines...) Expand all
139 ] 159 ]
140 result = json.loads(notification.notification({}, lambda *args: None)) 160 result = json.loads(notification.notification({}, lambda *args: None))
141 self.assertEqual(len(result["notifications"]), 0) 161 self.assertEqual(len(result["notifications"]), 0)
142 162
143 @mock.patch("random.random") 163 @mock.patch("random.random")
144 def test_probability_distribution_single_group(self, random_call): 164 def test_probability_distribution_single_group(self, random_call):
145 self.load_notifications_mock.return_value = [ 165 self.load_notifications_mock.return_value = [
146 { 166 {
147 "id": "a", 167 "id": "a",
148 "variants": [ 168 "variants": [
149 {"sample": 0.5, "title": {"en-US": "1"}, "message": {}}, 169 {"sample": 0.5, "title": {"en-US": "1"}, "message": {"en-US": ""}},
150 {"sample": 0.25, "title": {"en-US": "2"}, "message": {}}, 170 {"sample": 0.25, "title": {"en-US": "2"}, "message": {"en-US": ""}},
151 {"sample": 0.25, "title": {"en-US": "3"}, "message": {}} 171 {"sample": 0.25, "title": {"en-US": "3"}, "message": {"en-US": ""}}
152 ] 172 ]
153 } 173 }
154 ] 174 ]
155 random_call.return_value = 0 175 random_call.return_value = 0
156 result = json.loads(notification.notification({}, lambda *args: None)) 176 result = json.loads(notification.notification({}, lambda *args: None))
157 self.assertEqual(len(result["notifications"]), 1) 177 self.assertEqual(len(result["notifications"]), 1)
158 self.assertEqual(result["notifications"][0]["title"]["en-US"], "1") 178 self.assertEqual(result["notifications"][0]["title"]["en-US"], "1")
159 self.assertRegexpMatches(result["version"], r"-a/1") 179 self.assertRegexpMatches(result["version"], r"-a/1")
160 random_call.return_value = 0.5 180 random_call.return_value = 0.5
161 result = json.loads(notification.notification({}, lambda *args: None)) 181 result = json.loads(notification.notification({}, lambda *args: None))
(...skipping 20 matching lines...) Expand all
182 self.assertEqual(len(result["notifications"]), 1) 202 self.assertEqual(len(result["notifications"]), 1)
183 self.assertEqual(result["notifications"][0]["title"]["en-US"], "3") 203 self.assertEqual(result["notifications"][0]["title"]["en-US"], "3")
184 self.assertRegexpMatches(result["version"], r"-a/3") 204 self.assertRegexpMatches(result["version"], r"-a/3")
185 205
186 @mock.patch("random.random") 206 @mock.patch("random.random")
187 def test_probability_distribution_multiple_groups(self, random_call): 207 def test_probability_distribution_multiple_groups(self, random_call):
188 self.load_notifications_mock.return_value = [ 208 self.load_notifications_mock.return_value = [
189 { 209 {
190 "id": "a", 210 "id": "a",
191 "variants": [ 211 "variants": [
192 {"sample": 0.25, "title": {"en-US": "1"}, "message": {}}, 212 {"sample": 0.25, "title": {"en-US": "1"}, "message": {"en-US": ""}},
193 {"sample": 0.25, "title": {"en-US": "2"}, "message": {}} 213 {"sample": 0.25, "title": {"en-US": "2"}, "message": {"en-US": ""}}
194 ] 214 ]
195 }, 215 },
196 { 216 {
197 "id": "b", 217 "id": "b",
198 "variants": [ 218 "variants": [
199 {"sample": 0.25, "title": {"en-US": "1"}, "message": {}}, 219 {"sample": 0.25, "title": {"en-US": "1"}, "message": {"en-US": ""}},
200 {"sample": 0.25, "title": {"en-US": "2"}, "message": {}} 220 {"sample": 0.25, "title": {"en-US": "2"}, "message": {"en-US": ""}}
201 ] 221 ]
202 } 222 }
203 ] 223 ]
204 random_call.return_value = 0 224 random_call.return_value = 0
205 result = json.loads(notification.notification({}, lambda *args: None)) 225 result = json.loads(notification.notification({}, lambda *args: None))
206 self.assertEqual(len(result["notifications"]), 1) 226 self.assertEqual(len(result["notifications"]), 1)
207 self.assertEqual(result["notifications"][0]["id"], "a") 227 self.assertEqual(result["notifications"][0]["id"], "a")
208 self.assertEqual(result["notifications"][0]["title"]["en-US"], "1") 228 self.assertEqual(result["notifications"][0]["title"]["en-US"], "1")
209 self.assertRegexpMatches(result["version"], r"-a/1-b/0") 229 self.assertRegexpMatches(result["version"], r"-a/1-b/0")
210 random_call.return_value = 0.251 230 random_call.return_value = 0.251
(...skipping 21 matching lines...) Expand all
232 lambda *args: None) 252 lambda *args: None)
233 notification.notification({"QUERY_STRING": "lastVersion=-"}, 253 notification.notification({"QUERY_STRING": "lastVersion=-"},
234 lambda *args: None) 254 lambda *args: None)
235 notification.notification({"QUERY_STRING": "lastVersion=-/"}, 255 notification.notification({"QUERY_STRING": "lastVersion=-/"},
236 lambda *args: None) 256 lambda *args: None)
237 notification.notification({"QUERY_STRING": "lastVersion=-//"}, 257 notification.notification({"QUERY_STRING": "lastVersion=-//"},
238 lambda *args: None) 258 lambda *args: None)
239 259
240 def test_version_header_present(self): 260 def test_version_header_present(self):
241 self.load_notifications_mock.return_value = [ 261 self.load_notifications_mock.return_value = [
242 {"id": "1", "title": {}, "message": {}} 262 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}}
243 ] 263 ]
244 response_header_map = {} 264 response_header_map = {}
245 def start_response(status, response_headers): 265 def start_response(status, response_headers):
246 for name, value in response_headers: 266 for name, value in response_headers:
247 response_header_map[name] = value 267 response_header_map[name] = value
248 result = json.loads(notification.notification({}, start_response)) 268 result = json.loads(notification.notification({}, start_response))
249 self.assertEqual(result["version"], 269 self.assertEqual(result["version"],
250 response_header_map["ABP-Notification-Version"]) 270 response_header_map["ABP-Notification-Version"])
251 271
252 def test_default_group_notification_returned_if_valid(self): 272 def test_default_group_notification_returned_if_valid(self):
253 self.load_notifications_mock.return_value = [ 273 self.load_notifications_mock.return_value = [
254 {"id": "1", "title": {}, "message": {}}, 274 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}},
255 { 275 {
256 "id": "a", 276 "id": "a",
257 "title": {"en-US": "0"}, 277 "title": {"en-US": "0"},
258 "message": {"en-US": "0"}, 278 "message": {"en-US": "0"},
259 "variants": [ 279 "variants": [
260 {"title": {"en-US": "1"}, "message": {"en-US": "1"}} 280 {"title": {"en-US": "1"}, "message": {"en-US": "1"}}
261 ] 281 ]
262 } 282 }
263 ] 283 ]
264 result = json.loads(notification.notification({ 284 result = json.loads(notification.notification({
265 "QUERY_STRING": "lastVersion=197001010000-a/0" 285 "QUERY_STRING": "lastVersion=197001010000-a/0"
266 }, lambda *args: None)) 286 }, lambda *args: None))
267 self.assertEqual(len(result["notifications"]), 2) 287 self.assertEqual(len(result["notifications"]), 2)
268 self.assertEqual(result["notifications"][0]["id"], "1") 288 self.assertEqual(result["notifications"][0]["id"], "1")
269 self.assertEqual(result["notifications"][1]["id"], "a") 289 self.assertEqual(result["notifications"][1]["id"], "a")
270 self.assertEqual(result["notifications"][1]["title"]["en-US"], "0") 290 self.assertEqual(result["notifications"][1]["title"]["en-US"], "0")
271 self.assertNotIn("variants", result["notifications"][1]) 291 self.assertNotIn("variants", result["notifications"][1])
272 self.assertRegexpMatches(result["version"], r"-a/0") 292 self.assertRegexpMatches(result["version"], r"-a/0")
273 293
274 def test_default_group_notification_not_returned_if_invalid(self): 294 def test_default_group_notification_not_returned_if_invalid(self):
275 self.load_notifications_mock.return_value = [ 295 self.load_notifications_mock.return_value = [
276 {"id": "1", "title": {}, "message": {}}, 296 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}},
277 { 297 {
278 "id": "a", 298 "id": "a",
279 "title": {"en-US": "0"}, 299 "title": {"en-US": "0"},
280 "variants": [ 300 "variants": [
281 {"title": {"en-US": "1"}, "message": {"en-US": "1"}} 301 {"title": {"en-US": "1"}, "message": {"en-US": "1"}}
282 ] 302 ]
283 } 303 }
284 ] 304 ]
285 result = json.loads(notification.notification({ 305 result = json.loads(notification.notification({
286 "QUERY_STRING": "lastVersion=197001010000-a/0" 306 "QUERY_STRING": "lastVersion=197001010000-a/0"
287 }, lambda *args: None)) 307 }, lambda *args: None))
288 self.assertEqual(len(result["notifications"]), 1) 308 self.assertEqual(len(result["notifications"]), 1)
289 self.assertEqual(result["notifications"][0]["id"], "1") 309 self.assertEqual(result["notifications"][0]["id"], "1")
290 self.assertRegexpMatches(result["version"], r"-a/0") 310 self.assertRegexpMatches(result["version"], r"-a/0")
291 311
292 def test_invalid_notification_not_returned(self): 312 def test_invalid_notification_not_returned(self):
293 self.load_notifications_mock.return_value = [ 313 self.load_notifications_mock.return_value = [
294 {"id": "1", "title": {}, "message": {}}, 314 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}},
295 {"id": "2", "title": {}}, 315 {"id": "2", "title": {"en-US": ""}, "message": {}},
296 {"id": "3", "message": {}} 316 {"id": "3", "title": {}, "message": {"en-US": ""}},
317 {"id": "4", "title": {}},
318 {"id": "5", "message": {}},
319 {"id": "6"}
297 ] 320 ]
298 result = json.loads(notification.notification({}, lambda *args: None)) 321 result = json.loads(notification.notification({}, lambda *args: None))
299 self.assertEqual(len(result["notifications"]), 1) 322 self.assertEqual(len(result["notifications"]), 1)
300 self.assertEqual(result["notifications"][0]["id"], "1") 323 self.assertEqual(result["notifications"][0]["id"], "1")
301 324
302 def test_stays_in_group_when_notification_present(self): 325 def test_stays_in_group_when_notification_present(self):
303 self.load_notifications_mock.return_value = [ 326 self.load_notifications_mock.return_value = [
304 {"id": "a"} 327 {"id": "a"}
305 ] 328 ]
306 result = json.loads(notification.notification({ 329 result = json.loads(notification.notification({
(...skipping 15 matching lines...) Expand all
322 {"id": "a", "inactive": True} 345 {"id": "a", "inactive": True}
323 ] 346 ]
324 result = json.loads(notification.notification({ 347 result = json.loads(notification.notification({
325 "QUERY_STRING": "lastVersion=197001010000-a/0-b/1" 348 "QUERY_STRING": "lastVersion=197001010000-a/0-b/1"
326 }, lambda *args: None)) 349 }, lambda *args: None))
327 self.assertEqual(len(result["notifications"]), 0) 350 self.assertEqual(len(result["notifications"]), 0)
328 self.assertRegexpMatches(result["version"], r"-a/0") 351 self.assertRegexpMatches(result["version"], r"-a/0")
329 352
330 def test_inactive_notifications_not_returned(self): 353 def test_inactive_notifications_not_returned(self):
331 self.load_notifications_mock.return_value = [ 354 self.load_notifications_mock.return_value = [
332 {"id": "a", "title": {}, "message": {}, "inactive": True}, 355 {"id": "a", "title": {"en-US": ""}, "message": {"en-US": ""}, "inactive": True},
333 {"id": "b", "title": {}, "message": {}, "inactive": False}, 356 {"id": "b", "title": {"en-US": ""}, "message": {"en-US": ""}, "inactive": False},
334 {"id": "c", "title": {}, "message": {}} 357 {"id": "c", "title": {"en-US": ""}, "message": {"en-US": ""}}
335 ] 358 ]
336 result = json.loads(notification.notification({}, lambda *args: None)) 359 result = json.loads(notification.notification({}, lambda *args: None))
337 self.assertEqual(len(result["notifications"]), 2) 360 self.assertEqual(len(result["notifications"]), 2)
338 self.assertEqual(result["notifications"][0]["id"], "b") 361 self.assertEqual(result["notifications"][0]["id"], "b")
339 self.assertEqual(result["notifications"][1]["id"], "c") 362 self.assertEqual(result["notifications"][1]["id"], "c")
340 363
341 if __name__ == '__main__': 364 if __name__ == '__main__':
342 unittest.main() 365 unittest.main()
OLDNEW
« no previous file with comments | « no previous file | sitescripts/notifications/web/notification.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld