OLD | NEW |
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, |
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 # GNU General Public License for more details. | 13 # GNU General Public License for more details. |
14 # | 14 # |
15 # You should have received a copy of the GNU General Public License | 15 # You should have received a copy of the GNU General Public License |
16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 |
18 import json | 18 import json |
19 import mock | 19 import mock |
20 import unittest | 20 import unittest |
21 | 21 |
22 import sitescripts.notifications.web.notification as notification | 22 import sitescripts.notifications.web.notification as notification |
23 | 23 |
24 class TestNotification(unittest.TestCase): | 24 class TestNotification(unittest.TestCase): |
25 @mock.patch("sitescripts.notifications.web.notification.load_notifications") | 25 def setUp(self): |
26 def test_no_group(self, load_notifications_call): | 26 self.load_notifications_patcher = mock.patch("sitescripts.notifications.web.
notification.load_notifications") |
27 load_notifications_call.return_value = [{"id": "1"}] | 27 self.load_notifications_mock = self.load_notifications_patcher.start() |
| 28 |
| 29 def tearDown(self): |
| 30 self.load_notifications_patcher.stop() |
| 31 |
| 32 def test_no_group(self): |
| 33 self.load_notifications_mock.return_value = [{"id": "1"}] |
28 result = json.loads(notification.notification({}, lambda *args: None)) | 34 result = json.loads(notification.notification({}, lambda *args: None)) |
29 self.assertEqual(len(result["notifications"]), 1) | 35 self.assertEqual(len(result["notifications"]), 1) |
30 self.assertEqual(result["notifications"][0]["id"], "1") | 36 self.assertEqual(result["notifications"][0]["id"], "1") |
31 self.assertFalse("-" in result["version"]) | 37 self.assertFalse("-" in result["version"]) |
32 | 38 |
33 @mock.patch("sitescripts.notifications.web.notification.load_notifications") | 39 def test_not_in_group(self): |
34 def test_not_in_group(self, load_notifications_call): | 40 self.load_notifications_mock.return_value = [ |
35 load_notifications_call.return_value = [ | |
36 {"id": "1"}, | 41 {"id": "1"}, |
37 { | 42 { |
38 "id": "a", | 43 "id": "a", |
39 "variants": [{}] | 44 "variants": [{}] |
40 } | 45 } |
41 ] | 46 ] |
42 result = json.loads(notification.notification({ | 47 result = json.loads(notification.notification({ |
43 "QUERY_STRING": "lastVersion=197001010000-a/0" | 48 "QUERY_STRING": "lastVersion=197001010000-a/0" |
44 }, lambda *args: None)) | 49 }, lambda *args: None)) |
45 self.assertEqual(len(result["notifications"]), 1) | 50 self.assertEqual(len(result["notifications"]), 1) |
46 self.assertEqual(result["notifications"][0]["id"], "1") | 51 self.assertEqual(result["notifications"][0]["id"], "1") |
47 self.assertRegexpMatches(result["version"], r"-a/0") | 52 self.assertRegexpMatches(result["version"], r"-a/0") |
48 | 53 |
49 @mock.patch("sitescripts.notifications.web.notification.load_notifications") | 54 def test_in_group(self): |
50 def test_in_group(self, load_notifications_call): | 55 self.load_notifications_mock.return_value = [ |
51 load_notifications_call.return_value = [ | |
52 {"id": "1"}, | 56 {"id": "1"}, |
53 { | 57 { |
54 "id": "a", | 58 "id": "a", |
55 "variants": [{}] | 59 "variants": [{}] |
56 } | 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 @mock.patch("sitescripts.notifications.web.notification.load_notifications") | 69 def test_not_in_one_of_many_groups(self): |
66 def test_not_in_one_of_many_groups(self, load_notifications_call): | 70 self.load_notifications_mock.return_value = [ |
67 load_notifications_call.return_value = [ | |
68 {"id": "1"}, | 71 {"id": "1"}, |
69 { | 72 { |
70 "id": "a", | 73 "id": "a", |
71 "variants": [{}] | 74 "variants": [{}] |
72 }, | 75 }, |
73 { | 76 { |
74 "id": "b", | 77 "id": "b", |
75 "variants": [{}] | 78 "variants": [{}] |
76 }, | 79 }, |
77 { | 80 { |
78 "id": "c", | 81 "id": "c", |
79 "variants": [{}] | 82 "variants": [{}] |
80 } | 83 } |
81 ] | 84 ] |
82 result = json.loads(notification.notification({ | 85 result = json.loads(notification.notification({ |
83 "QUERY_STRING": "lastVersion=197001010000-a/0-b/0-c/0" | 86 "QUERY_STRING": "lastVersion=197001010000-a/0-b/0-c/0" |
84 }, lambda *args: None)) | 87 }, lambda *args: None)) |
85 self.assertEqual(len(result["notifications"]), 1) | 88 self.assertEqual(len(result["notifications"]), 1) |
86 self.assertEqual(result["notifications"][0]["id"], "1") | 89 self.assertEqual(result["notifications"][0]["id"], "1") |
87 self.assertRegexpMatches(result["version"], r"-a/0-b/0-c/0") | 90 self.assertRegexpMatches(result["version"], r"-a/0-b/0-c/0") |
88 | 91 |
89 @mock.patch("sitescripts.notifications.web.notification.load_notifications") | 92 def test_in_one_of_many_groups(self): |
90 def test_in_one_of_many_groups(self, load_notifications_call): | 93 self.load_notifications_mock.return_value = [ |
91 load_notifications_call.return_value = [ | |
92 {"id": "1"}, | 94 {"id": "1"}, |
93 { | 95 { |
94 "id": "a", | 96 "id": "a", |
95 "variants": [{}] | 97 "variants": [{}] |
96 }, | 98 }, |
97 { | 99 { |
98 "id": "b", | 100 "id": "b", |
99 "variants": [{}] | 101 "variants": [{}] |
100 }, | 102 }, |
101 { | 103 { |
102 "id": "c", | 104 "id": "c", |
103 "variants": [{}] | 105 "variants": [{}] |
104 } | 106 } |
105 ] | 107 ] |
106 result = json.loads(notification.notification({ | 108 result = json.loads(notification.notification({ |
107 "QUERY_STRING": "lastVersion=197001010000-a/0-b/1-c/0" | 109 "QUERY_STRING": "lastVersion=197001010000-a/0-b/1-c/0" |
108 }, lambda *args: None)) | 110 }, lambda *args: None)) |
109 self.assertEqual(len(result["notifications"]), 1) | 111 self.assertEqual(len(result["notifications"]), 1) |
110 self.assertEqual(result["notifications"][0]["id"], "b") | 112 self.assertEqual(result["notifications"][0]["id"], "b") |
111 self.assertRegexpMatches(result["version"], r"-a/0-b/1-c/0") | 113 self.assertRegexpMatches(result["version"], r"-a/0-b/1-c/0") |
112 | 114 |
113 @mock.patch("sitescripts.notifications.web.notification.load_notifications") | 115 def test_not_put_in_group(self): |
114 def test_not_put_in_group(self, load_notifications_call): | 116 self.load_notifications_mock.return_value = [ |
115 load_notifications_call.return_value = [ | |
116 {"id": "1"}, | 117 {"id": "1"}, |
117 { | 118 { |
118 "id": "a", | 119 "id": "a", |
119 "variants": [{"sample": 0}] | 120 "variants": [{"sample": 0}] |
120 } | 121 } |
121 ] | 122 ] |
122 result = json.loads(notification.notification({ | 123 result = json.loads(notification.notification({ |
123 "QUERY_STRING": "lastVersion=197001010000" | 124 "QUERY_STRING": "lastVersion=197001010000" |
124 }, lambda *args: None)) | 125 }, lambda *args: None)) |
125 self.assertEqual(len(result["notifications"]), 1) | 126 self.assertEqual(len(result["notifications"]), 1) |
126 self.assertEqual(result["notifications"][0]["id"], "1") | 127 self.assertEqual(result["notifications"][0]["id"], "1") |
127 self.assertRegexpMatches(result["version"], r"-a/0") | 128 self.assertRegexpMatches(result["version"], r"-a/0") |
128 | 129 |
129 @mock.patch("sitescripts.notifications.web.notification.load_notifications") | 130 def test_put_in_group(self): |
130 def test_put_in_group(self, load_notifications_call): | 131 self.load_notifications_mock.return_value = [ |
131 load_notifications_call.return_value = [ | |
132 {"id": "1"}, | 132 {"id": "1"}, |
133 { | 133 { |
134 "id": "a", | 134 "id": "a", |
135 "variants": [{"sample": 1}] | 135 "variants": [{"sample": 1}] |
136 } | 136 } |
137 ] | 137 ] |
138 result = json.loads(notification.notification({ | 138 result = json.loads(notification.notification({ |
139 "QUERY_STRING": "lastVersion=197001010000" | 139 "QUERY_STRING": "lastVersion=197001010000" |
140 }, lambda *args: None)) | 140 }, lambda *args: None)) |
141 self.assertEqual(len(result["notifications"]), 1) | 141 self.assertEqual(len(result["notifications"]), 1) |
142 self.assertEqual(result["notifications"][0]["id"], "a") | 142 self.assertEqual(result["notifications"][0]["id"], "a") |
143 self.assertRegexpMatches(result["version"], r"-a/1") | 143 self.assertRegexpMatches(result["version"], r"-a/1") |
144 | 144 |
145 @mock.patch("sitescripts.notifications.web.notification.load_notifications") | 145 def test_notification_variant_merged(self): |
146 def test_notification_variant_merged(self, load_notifications_call): | 146 self.load_notifications_mock.return_value = [ |
147 load_notifications_call.return_value = [ | |
148 { | 147 { |
149 "id": "a", | 148 "id": "a", |
150 "title.en-GB": "default", | 149 "title.en-GB": "default", |
151 "message.en-GB": "default", | 150 "message.en-GB": "default", |
152 "message.de-DE": "vorgabe", | 151 "message.de-DE": "vorgabe", |
153 "variants": [ | 152 "variants": [ |
154 { | 153 { |
155 "sample": 1, | 154 "sample": 1, |
156 "message.en-GB": "variant" | 155 "message.en-GB": "variant" |
157 } | 156 } |
158 ] | 157 ] |
159 } | 158 } |
160 ] | 159 ] |
161 result = json.loads(notification.notification({}, lambda *args: None)) | 160 result = json.loads(notification.notification({}, lambda *args: None)) |
162 self.assertEqual(len(result["notifications"]), 1) | 161 self.assertEqual(len(result["notifications"]), 1) |
163 self.assertEqual(result["notifications"][0]["id"], "a") | 162 self.assertEqual(result["notifications"][0]["id"], "a") |
164 self.assertEqual(result["notifications"][0]["title.en-GB"], "default") | 163 self.assertEqual(result["notifications"][0]["title.en-GB"], "default") |
165 self.assertEqual(result["notifications"][0]["message.en-GB"], "variant") | 164 self.assertEqual(result["notifications"][0]["message.en-GB"], "variant") |
166 self.assertEqual(result["notifications"][0]["message.de-DE"], "vorgabe") | 165 self.assertEqual(result["notifications"][0]["message.de-DE"], "vorgabe") |
167 self.assertFalse("variants" in result["notifications"][0]) | 166 self.assertFalse("variants" in result["notifications"][0]) |
168 self.assertFalse("sample" in result["notifications"][0]) | 167 self.assertFalse("sample" in result["notifications"][0]) |
169 | 168 |
170 @mock.patch("sitescripts.notifications.web.notification.load_notifications") | 169 def test_no_variant_no_notifications(self): |
171 def test_no_variant_no_notifications(self, load_notifications_call): | 170 self.load_notifications_mock.return_value = [ |
172 load_notifications_call.return_value = [ | |
173 { | 171 { |
174 "id": "a", | 172 "id": "a", |
175 "variants": [{"sample": 0}] | 173 "variants": [{"sample": 0}] |
176 } | 174 } |
177 ] | 175 ] |
178 result = json.loads(notification.notification({}, lambda *args: None)) | 176 result = json.loads(notification.notification({}, lambda *args: None)) |
179 self.assertEqual(len(result["notifications"]), 0) | 177 self.assertEqual(len(result["notifications"]), 0) |
180 | 178 |
181 @mock.patch("random.random") | 179 @mock.patch("random.random") |
182 @mock.patch("sitescripts.notifications.web.notification.load_notifications") | 180 def test_probability_distribution_single_group(self, random_call): |
183 def test_probability_distribution_single_group( | 181 self.load_notifications_mock.return_value = [ |
184 self, load_notifications_call, random_call): | |
185 load_notifications_call.return_value = [ | |
186 { | 182 { |
187 "id": "a", | 183 "id": "a", |
188 "variants": [ | 184 "variants": [ |
189 { | 185 { |
190 "sample": 0.5, | 186 "sample": 0.5, |
191 "title.en-GB": "1" | 187 "title.en-GB": "1" |
192 }, | 188 }, |
193 { | 189 { |
194 "sample": 0.25, | 190 "sample": 0.25, |
195 "title.en-GB": "2" | 191 "title.en-GB": "2" |
(...skipping 30 matching lines...) Expand all Loading... |
226 self.assertEqual(len(result["notifications"]), 1) | 222 self.assertEqual(len(result["notifications"]), 1) |
227 self.assertEqual(result["notifications"][0]["title.en-GB"], "3") | 223 self.assertEqual(result["notifications"][0]["title.en-GB"], "3") |
228 self.assertRegexpMatches(result["version"], r"-a/3") | 224 self.assertRegexpMatches(result["version"], r"-a/3") |
229 random_call.return_value = 1 | 225 random_call.return_value = 1 |
230 result = json.loads(notification.notification({}, lambda *args: None)) | 226 result = json.loads(notification.notification({}, lambda *args: None)) |
231 self.assertEqual(len(result["notifications"]), 1) | 227 self.assertEqual(len(result["notifications"]), 1) |
232 self.assertEqual(result["notifications"][0]["title.en-GB"], "3") | 228 self.assertEqual(result["notifications"][0]["title.en-GB"], "3") |
233 self.assertRegexpMatches(result["version"], r"-a/3") | 229 self.assertRegexpMatches(result["version"], r"-a/3") |
234 | 230 |
235 @mock.patch("random.random") | 231 @mock.patch("random.random") |
236 @mock.patch("sitescripts.notifications.web.notification.load_notifications") | 232 def test_probability_distribution_multiple_groups(self, random_call): |
237 def test_probability_distribution_multiple_groups( | 233 self.load_notifications_mock.return_value = [ |
238 self, load_notifications_call, random_call): | |
239 load_notifications_call.return_value = [ | |
240 { | 234 { |
241 "id": "a", | 235 "id": "a", |
242 "variants": [ | 236 "variants": [ |
243 { | 237 { |
244 "sample": 0.25, | 238 "sample": 0.25, |
245 "title.en-GB": "1" | 239 "title.en-GB": "1" |
246 }, | 240 }, |
247 { | 241 { |
248 "sample": 0.25, | 242 "sample": 0.25, |
249 "title.en-GB": "2" | 243 "title.en-GB": "2" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 self.assertEqual(result["notifications"][0]["id"], "b") | 276 self.assertEqual(result["notifications"][0]["id"], "b") |
283 self.assertEqual(result["notifications"][0]["title.en-GB"], "1") | 277 self.assertEqual(result["notifications"][0]["title.en-GB"], "1") |
284 self.assertRegexpMatches(result["version"], r"-a/0-b/1") | 278 self.assertRegexpMatches(result["version"], r"-a/0-b/1") |
285 random_call.return_value = 0.751 | 279 random_call.return_value = 0.751 |
286 result = json.loads(notification.notification({}, lambda *args: None)) | 280 result = json.loads(notification.notification({}, lambda *args: None)) |
287 self.assertEqual(len(result["notifications"]), 1) | 281 self.assertEqual(len(result["notifications"]), 1) |
288 self.assertEqual(result["notifications"][0]["id"], "b") | 282 self.assertEqual(result["notifications"][0]["id"], "b") |
289 self.assertEqual(result["notifications"][0]["title.en-GB"], "2") | 283 self.assertEqual(result["notifications"][0]["title.en-GB"], "2") |
290 self.assertRegexpMatches(result["version"], r"-a/0-b/2") | 284 self.assertRegexpMatches(result["version"], r"-a/0-b/2") |
291 | 285 |
292 @mock.patch("sitescripts.notifications.web.notification.load_notifications") | 286 def test_invalid_last_version(self): |
293 def test_invalid_last_version(self, load_notifications_call): | 287 self.load_notifications_mock.return_value = [] |
294 load_notifications_call.return_value = [] | |
295 notification.notification({"QUERY_STRING": "lastVersion="}, | 288 notification.notification({"QUERY_STRING": "lastVersion="}, |
296 lambda *args: None) | 289 lambda *args: None) |
297 notification.notification({"QUERY_STRING": "lastVersion=-"}, | 290 notification.notification({"QUERY_STRING": "lastVersion=-"}, |
298 lambda *args: None) | 291 lambda *args: None) |
299 notification.notification({"QUERY_STRING": "lastVersion=-/"}, | 292 notification.notification({"QUERY_STRING": "lastVersion=-/"}, |
300 lambda *args: None) | 293 lambda *args: None) |
301 notification.notification({"QUERY_STRING": "lastVersion=-//"}, | 294 notification.notification({"QUERY_STRING": "lastVersion=-//"}, |
302 lambda *args: None) | 295 lambda *args: None) |
303 | 296 |
304 @mock.patch("sitescripts.notifications.web.notification.load_notifications") | 297 def test_version_header_present(self): |
305 def test_version_header_present(self, load_notifications_call): | 298 self.load_notifications_mock.return_value = [{"id": "1"}] |
306 load_notifications_call.return_value = [{"id": "1"}] | |
307 response_header_map = {} | 299 response_header_map = {} |
308 def start_response(status, response_headers): | 300 def start_response(status, response_headers): |
309 for name, value in response_headers: | 301 for name, value in response_headers: |
310 response_header_map[name] = value | 302 response_header_map[name] = value |
311 result = json.loads(notification.notification({}, start_response)) | 303 result = json.loads(notification.notification({}, start_response)) |
312 self.assertEqual(result["version"], | 304 self.assertEqual(result["version"], |
313 response_header_map["ABP-Notification-Version"]) | 305 response_header_map["ABP-Notification-Version"]) |
314 | 306 |
315 if __name__ == '__main__': | 307 if __name__ == '__main__': |
316 unittest.main() | 308 unittest.main() |
OLD | NEW |