Left: | ||
Right: |
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-present eyeo GmbH | 2 # Copyright (C) 2006-present 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 os | |
16 import json | 17 import json |
17 import mock | 18 import mock |
18 import unittest | 19 import unittest |
20 import tempfile | |
21 import shutil | |
22 import time | |
23 import subprocess | |
24 | |
25 from sitescripts.utils import get_config | |
19 | 26 |
20 import sitescripts.notifications.web.notification as notification | 27 import sitescripts.notifications.web.notification as notification |
21 | 28 |
29 mock_time = mock.Mock() | |
30 mock_time.return_value = time.gmtime(0) | |
31 | |
22 | 32 |
23 class TestNotification(unittest.TestCase): | 33 class TestNotification(unittest.TestCase): |
24 def setUp(self): | 34 def setUp(self): |
25 self.load_notifications_patcher = mock.patch('sitescripts.notifications. web.notification.load_notifications') | 35 self.repo_dir = tempfile.mkdtemp('md_repo') |
wspee
2017/10/09 09:12:03
As discussed: This created a new repository for ev
Vasily Kuznetsov
2017/10/09 22:29:11
Hm. Now after I looked at the whole test suite it
wspee
2017/10/10 09:03:53
No worries. Half of it was based on my assumptions
| |
26 self.load_notifications_mock = self.load_notifications_patcher.start() | 36 subprocess.check_call(['hg', 'init'], cwd=self.repo_dir) |
37 | |
38 open(os.path.join(self.repo_dir, '.hgignore'), 'w+').close() | |
39 | |
40 subprocess.check_call( | |
41 ['hg', 'commit', '-q', '-m "Initial commit"', '-A'], | |
42 cwd=self.repo_dir) | |
43 | |
44 config = get_config() | |
45 self._notification_repository_orig = config.get( | |
46 'notifications', 'repository') | |
47 get_config().set('notifications', 'repository', self.repo_dir) | |
27 | 48 |
28 def tearDown(self): | 49 def tearDown(self): |
29 self.load_notifications_patcher.stop() | 50 shutil.rmtree(self.repo_dir) |
51 get_config().set( | |
52 'notifications', 'repository', self._notification_repository_orig) | |
53 | |
54 def add_notifications(self, notifications): | |
55 for id_, content in notifications: | |
56 with open(os.path.join(self.repo_dir, id_), 'w+') as f: | |
57 f.write(content) | |
58 f.flush() | |
59 | |
60 subprocess.check_call( | |
61 ['hg', 'commit', '-q', '-m', id_, '-A'], | |
62 cwd=self.repo_dir) | |
30 | 63 |
31 def test_no_group(self): | 64 def test_no_group(self): |
32 self.load_notifications_mock.return_value = [ | 65 self.add_notifications([ |
33 {'id': '1', 'title': {'en-US': ''}, 'message': {'en-US': ''}} | 66 ('1', 'title.en-US = 1\nmessage.en-US = 1\n')]) |
34 ] | 67 |
35 result = json.loads(notification.notification({}, lambda *args: None)) | 68 result = json.loads(notification.notification({}, lambda *args: None)) |
36 self.assertEqual(len(result['notifications']), 1) | 69 self.assertEqual(len(result['notifications']), 1) |
37 self.assertEqual(result['notifications'][0]['id'], '1') | 70 self.assertEqual(result['notifications'][0]['id'], '1') |
38 self.assertFalse('-' in result['version']) | 71 self.assertFalse('-' in result['version']) |
39 | 72 |
40 def test_not_in_group(self): | 73 def test_not_in_group(self): |
41 self.load_notifications_mock.return_value = [ | 74 self.add_notifications([ |
42 {'id': '1', 'title': {'en-US': ''}, 'message': {'en-US': ''}}, | 75 ('1', 'title.en-US = 1\nmessage.en-US = 1\n'), |
43 {'id': 'a', 'variants': [ | 76 ('2', '[1]\ntitle.en-US = 2\nmessage.en-US = 2\nsample = 1'), |
44 {'title': {'en-US': ''}, 'message': {'en-US': ''}} | 77 ]) |
45 ]} | 78 |
46 ] | 79 result = json.loads(notification.notification({ |
47 result = json.loads(notification.notification({ | 80 'QUERY_STRING': 'lastVersion=197001010000-2/0' |
48 'QUERY_STRING': 'lastVersion=197001010000-a/0' | 81 }, lambda *args: None)) |
49 }, lambda *args: None)) | 82 self.assertEqual(len(result['notifications']), 1) |
50 self.assertEqual(len(result['notifications']), 1) | 83 self.assertEqual(result['notifications'][0]['id'], '1') |
51 self.assertEqual(result['notifications'][0]['id'], '1') | 84 self.assertRegexpMatches(result['version'], r'-2/0') |
52 self.assertRegexpMatches(result['version'], r'-a/0') | |
53 | 85 |
54 def test_in_group(self): | 86 def test_in_group(self): |
55 self.load_notifications_mock.return_value = [ | 87 self.add_notifications([ |
56 {'id': '1', 'title': {'en-US': ''}, 'message': {'en-US': ''}}, | 88 ('1', 'title.en-US = 1\nmessage.en-US = 1\n'), |
57 {'id': 'a', 'variants': [ | 89 ('2', '[1]\ntitle.en-US = 2.1\nmessage.en-US = 2.1\nsample = 1'), |
58 {'title': {'en-US': ''}, 'message': {'en-US': ''}} | 90 ]) |
59 ]} | 91 |
60 ] | 92 result = json.loads(notification.notification({ |
61 result = json.loads(notification.notification({ | 93 'QUERY_STRING': 'lastVersion=197001010000-2/1' |
62 'QUERY_STRING': 'lastVersion=197001010000-a/1' | 94 }, lambda *args: None)) |
63 }, lambda *args: None)) | 95 self.assertEqual(len(result['notifications']), 1) |
64 self.assertEqual(len(result['notifications']), 1) | 96 self.assertEqual(result['notifications'][0]['id'], '2') |
65 self.assertEqual(result['notifications'][0]['id'], 'a') | 97 self.assertRegexpMatches(result['version'], r'-2/1') |
66 self.assertRegexpMatches(result['version'], r'-a/1') | |
67 | 98 |
68 def test_not_in_one_of_many_groups(self): | 99 def test_not_in_one_of_many_groups(self): |
69 self.load_notifications_mock.return_value = [ | 100 self.add_notifications([ |
70 {'id': '1', 'title': {'en-US': ''}, 'message': {'en-US': ''}}, | 101 ('1', 'title.en-US = 1\nmessage.en-US = 1\n'), |
71 {'id': 'a', 'variants': [ | 102 ('2', '[1]\ntitle.en-US = 2.1\nmessage.en-US = 2.1\nsample = 1'), |
72 {'title': {'en-US': ''}, 'message': {'en-US': ''}} | 103 ('3', '[1]\ntitle.en-US = 3.1\nmessage.en-US = 3.1\nsample = 1'), |
73 ]}, | 104 ('4', '[1]\ntitle.en-US = 4.1\nmessage.en-US = 4.1\nsample = 1'), |
74 {'id': 'b', 'variants': [ | 105 ]) |
75 {'title': {'en-US': ''}, 'message': {'en-US': ''}} | 106 |
76 ]}, | 107 result = json.loads(notification.notification({ |
77 {'id': 'c', 'variants': [ | 108 'QUERY_STRING': 'lastVersion=197001010000-2/0-3/0-4/0' |
78 {'title': {'en-US': ''}, 'message': {'en-US': ''}} | 109 }, lambda *args: None)) |
79 ]} | 110 self.assertEqual(len(result['notifications']), 1) |
80 ] | 111 self.assertEqual(result['notifications'][0]['id'], '1') |
81 result = json.loads(notification.notification({ | 112 self.assertRegexpMatches(result['version'], r'-2/0-3/0-4/0') |
82 'QUERY_STRING': 'lastVersion=197001010000-a/0-b/0-c/0' | |
83 }, lambda *args: None)) | |
84 self.assertEqual(len(result['notifications']), 1) | |
85 self.assertEqual(result['notifications'][0]['id'], '1') | |
86 self.assertRegexpMatches(result['version'], r'-a/0-b/0-c/0') | |
87 | 113 |
88 def test_in_one_of_many_groups(self): | 114 def test_in_one_of_many_groups(self): |
89 self.load_notifications_mock.return_value = [ | 115 self.add_notifications([ |
90 {'id': '1', 'title': {'en-US': ''}, 'message': {'en-US': ''}}, | 116 ('1', 'title.en-US = 1\nmessage.en-US = 1\n'), |
91 {'id': 'a', 'variants': [ | 117 ('2', '[1]\ntitle.en-US = 2.1\nmessage.en-US = 2.1\nsample = 1'), |
92 {'title': {'en-US': ''}, 'message': {'en-US': ''}} | 118 ('3', '[1]\ntitle.en-US = 3.1\nmessage.en-US = 3.1\nsample = 1'), |
93 ]}, | 119 ('4', '[1]\ntitle.en-US = 4.1\nmessage.en-US = 4.1\nsample = 1'), |
94 {'id': 'b', 'variants': [ | 120 ]) |
95 {'title': {'en-US': ''}, 'message': {'en-US': ''}} | 121 |
96 ]}, | 122 result = json.loads(notification.notification({ |
97 {'id': 'c', 'variants': [ | 123 'QUERY_STRING': 'lastVersion=197001010000-2/0-3/1-4/0' |
98 {'title': {'en-US': ''}, 'message': {'en-US': ''}} | 124 }, lambda *args: None)) |
99 ]} | 125 self.assertEqual(len(result['notifications']), 1) |
100 ] | 126 self.assertEqual(result['notifications'][0]['id'], '3') |
101 result = json.loads(notification.notification({ | 127 self.assertRegexpMatches(result['version'], r'-2/0-3/1-4/0') |
102 'QUERY_STRING': 'lastVersion=197001010000-a/0-b/1-c/0' | |
103 }, lambda *args: None)) | |
104 self.assertEqual(len(result['notifications']), 1) | |
105 self.assertEqual(result['notifications'][0]['id'], 'b') | |
106 self.assertRegexpMatches(result['version'], r'-a/0-b/1-c/0') | |
107 | 128 |
108 def test_not_put_in_group(self): | 129 def test_not_put_in_group(self): |
109 self.load_notifications_mock.return_value = [ | 130 self.add_notifications([ |
Vasily Kuznetsov
2017/10/09 22:29:11
There is and was a lot of code duplication in this
wspee
2017/10/10 09:03:53
Acknowledged.
| |
110 {'id': '1', 'title': {'en-US': ''}, 'message': {'en-US': ''}}, | 131 ('1', 'title.en-US = 1\nmessage.en-US = 1\n'), |
111 {'id': 'a', 'variants': [ | 132 ('2', '[1]\ntitle.en-US = 2.1\nmessage.en-US = 2.1\nsample = 0'), |
112 {'sample': 0, 'title': {'en-US': ''}, 'message': {'en-US': ''}} | 133 ]) |
113 ]} | 134 |
114 ] | |
115 result = json.loads(notification.notification({ | 135 result = json.loads(notification.notification({ |
116 'QUERY_STRING': 'lastVersion=197001010000' | 136 'QUERY_STRING': 'lastVersion=197001010000' |
117 }, lambda *args: None)) | 137 }, lambda *args: None)) |
118 self.assertEqual(len(result['notifications']), 1) | 138 self.assertEqual(len(result['notifications']), 1) |
119 self.assertEqual(result['notifications'][0]['id'], '1') | 139 self.assertEqual(result['notifications'][0]['id'], '1') |
120 self.assertRegexpMatches(result['version'], r'-a/0') | 140 self.assertRegexpMatches(result['version'], r'-2/0') |
121 | 141 |
122 def test_put_in_group(self): | 142 def test_put_in_group(self): |
123 self.load_notifications_mock.return_value = [ | 143 self.add_notifications([ |
124 {'id': '1', 'title': {'en-US': ''}, 'message': {'en-US': ''}}, | 144 ('1', 'title.en-US = 1\nmessage.en-US = 1\n'), |
125 {'id': 'a', 'variants': [ | 145 ('2', '[1]\ntitle.en-US = 2.1\nmessage.en-US = 2.1\nsample = 1'), |
126 {'sample': 1, 'title': {'en-US': ''}, 'message': {'en-US': ''}} | 146 ]) |
127 ]} | 147 |
128 ] | |
129 result = json.loads(notification.notification({ | 148 result = json.loads(notification.notification({ |
130 'QUERY_STRING': 'lastVersion=197001010000' | 149 'QUERY_STRING': 'lastVersion=197001010000' |
131 }, lambda *args: None)) | 150 }, lambda *args: None)) |
132 self.assertEqual(len(result['notifications']), 1) | 151 self.assertEqual(len(result['notifications']), 1) |
133 self.assertEqual(result['notifications'][0]['id'], 'a') | 152 self.assertEqual(result['notifications'][0]['id'], '2') |
134 self.assertRegexpMatches(result['version'], r'-a/1') | 153 self.assertRegexpMatches(result['version'], r'-2/1') |
135 | |
136 def test_notification_variant_merged(self): | |
wspee
2017/10/09 09:12:03
This "merging" is not possible with the notificati
| |
137 self.load_notifications_mock.return_value = [ | |
138 { | |
139 'id': 'a', | |
140 'title': {'en-US': 'default'}, | |
141 'message': {'en-US': 'default'}, | |
142 'variants': [ | |
143 {'sample': 1, 'message': {'en-US': 'variant'}} | |
144 ] | |
145 } | |
146 ] | |
147 result = json.loads(notification.notification({}, lambda *args: None)) | |
148 self.assertEqual(len(result['notifications']), 1) | |
149 self.assertEqual(result['notifications'][0]['id'], 'a') | |
150 self.assertEqual(result['notifications'][0]['title']['en-US'], 'default' ) | |
151 self.assertEqual(result['notifications'][0]['message']['en-US'], 'varian t') | |
152 self.assertFalse('variants' 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.add_notifications([ |
157 {'id': 'a', 'variants': [{'sample': 0}]} | 157 ('1', '[1]\ntitle.en-US = 1\nmessage.en-US = 1\nsample = 0'), |
158 ] | 158 ]) |
159 | |
159 result = json.loads(notification.notification({}, lambda *args: None)) | 160 result = json.loads(notification.notification({}, lambda *args: None)) |
160 self.assertEqual(len(result['notifications']), 0) | 161 self.assertEqual(len(result['notifications']), 0) |
161 | 162 |
162 @mock.patch('random.random') | 163 @mock.patch('random.random') |
163 def test_probability_distribution_single_group(self, random_call): | 164 def test_probability_distribution_single_group(self, random_call): |
164 self.load_notifications_mock.return_value = [ | 165 self.add_notifications([ |
165 { | 166 ('1', '[1]\n' |
166 'id': 'a', | 167 'title.en-US = 1.1\n' |
167 'variants': [ | 168 'message.en-US = 1.1\n' |
168 {'sample': 0.5, 'title': {'en-US': '1'}, 'message': {'en-US' : ''}}, | 169 'sample = 0.5\n' |
169 {'sample': 0.25, 'title': {'en-US': '2'}, 'message': {'en-US ': ''}}, | 170 '[2]\n' |
170 {'sample': 0.25, 'title': {'en-US': '3'}, 'message': {'en-US ': ''}} | 171 'title.en-US = 1.2\n' |
171 ] | 172 'message.en-US = 1.2\n' |
172 } | 173 'sample = 0.25\n' |
173 ] | 174 '[3]\n' |
175 'title.en-US = 1.3\n' | |
176 'message.en-US = 1.3\n' | |
177 'sample = 0.25\n') | |
178 ]) | |
179 | |
174 random_call.return_value = 0 | 180 random_call.return_value = 0 |
175 result = json.loads(notification.notification({}, lambda *args: None)) | 181 result = json.loads(notification.notification({}, lambda *args: None)) |
176 self.assertEqual(len(result['notifications']), 1) | 182 self.assertEqual(len(result['notifications']), 1) |
177 self.assertEqual(result['notifications'][0]['title']['en-US'], '1') | 183 self.assertEqual(result['notifications'][0]['title']['en-US'], '1.1') |
178 self.assertRegexpMatches(result['version'], r'-a/1') | 184 self.assertRegexpMatches(result['version'], r'-1/1') |
179 random_call.return_value = 0.5 | 185 random_call.return_value = 0.5 |
180 result = json.loads(notification.notification({}, lambda *args: None)) | 186 result = json.loads(notification.notification({}, lambda *args: None)) |
181 self.assertEqual(len(result['notifications']), 1) | 187 self.assertEqual(len(result['notifications']), 1) |
182 self.assertEqual(result['notifications'][0]['title']['en-US'], '1') | 188 self.assertEqual(result['notifications'][0]['title']['en-US'], '1.1') |
183 self.assertRegexpMatches(result['version'], r'-a/1') | 189 self.assertRegexpMatches(result['version'], r'-1/1') |
184 random_call.return_value = 0.51 | 190 random_call.return_value = 0.51 |
185 result = json.loads(notification.notification({}, lambda *args: None)) | 191 result = json.loads(notification.notification({}, lambda *args: None)) |
186 self.assertEqual(len(result['notifications']), 1) | 192 self.assertEqual(len(result['notifications']), 1) |
187 self.assertEqual(result['notifications'][0]['title']['en-US'], '2') | 193 self.assertEqual(result['notifications'][0]['title']['en-US'], '1.2') |
188 self.assertRegexpMatches(result['version'], r'-a/2') | 194 self.assertRegexpMatches(result['version'], r'-1/2') |
189 random_call.return_value = 0.75 | 195 random_call.return_value = 0.75 |
190 result = json.loads(notification.notification({}, lambda *args: None)) | 196 result = json.loads(notification.notification({}, lambda *args: None)) |
191 self.assertEqual(len(result['notifications']), 1) | 197 self.assertEqual(len(result['notifications']), 1) |
192 self.assertEqual(result['notifications'][0]['title']['en-US'], '2') | 198 self.assertEqual(result['notifications'][0]['title']['en-US'], '1.2') |
193 self.assertRegexpMatches(result['version'], r'-a/2') | 199 self.assertRegexpMatches(result['version'], r'-1/2') |
194 random_call.return_value = 0.751 | 200 random_call.return_value = 0.751 |
195 result = json.loads(notification.notification({}, lambda *args: None)) | 201 result = json.loads(notification.notification({}, lambda *args: None)) |
196 self.assertEqual(len(result['notifications']), 1) | 202 self.assertEqual(len(result['notifications']), 1) |
197 self.assertEqual(result['notifications'][0]['title']['en-US'], '3') | 203 self.assertEqual(result['notifications'][0]['title']['en-US'], '1.3') |
198 self.assertRegexpMatches(result['version'], r'-a/3') | 204 self.assertRegexpMatches(result['version'], r'-1/3') |
199 random_call.return_value = 1 | 205 random_call.return_value = 1 |
200 result = json.loads(notification.notification({}, lambda *args: None)) | 206 result = json.loads(notification.notification({}, lambda *args: None)) |
201 self.assertEqual(len(result['notifications']), 1) | 207 self.assertEqual(len(result['notifications']), 1) |
202 self.assertEqual(result['notifications'][0]['title']['en-US'], '3') | 208 self.assertEqual(result['notifications'][0]['title']['en-US'], '1.3') |
203 self.assertRegexpMatches(result['version'], r'-a/3') | 209 self.assertRegexpMatches(result['version'], r'-1/3') |
204 | 210 |
205 @mock.patch('random.random') | 211 @mock.patch('random.random') |
206 def test_probability_distribution_multiple_groups(self, random_call): | 212 def test_probability_distribution_multiple_groups(self, random_call): |
207 self.load_notifications_mock.return_value = [ | 213 self.add_notifications([ |
208 { | 214 ('1', '[1]\n' |
209 'id': 'a', | 215 'title.en-US = 1.1\n' |
210 'variants': [ | 216 'message.en-US = 1.1\n' |
211 {'sample': 0.25, 'title': {'en-US': '1'}, 'message': {'en-US ': ''}}, | 217 'sample = 0.25\n' |
212 {'sample': 0.25, 'title': {'en-US': '2'}, 'message': {'en-US ': ''}} | 218 '[2]\n' |
213 ] | 219 'title.en-US = 1.2\n' |
214 }, | 220 'message.en-US = 1.2\n' |
215 { | 221 'sample = 0.25\n'), |
216 'id': 'b', | 222 ('2', '[1]\n' |
217 'variants': [ | 223 'title.en-US = 2.1\n' |
218 {'sample': 0.25, 'title': {'en-US': '1'}, 'message': {'en-US ': ''}}, | 224 'message.en-US = 2.1\n' |
219 {'sample': 0.25, 'title': {'en-US': '2'}, 'message': {'en-US ': ''}} | 225 'sample = 0.25\n' |
220 ] | 226 '[2]\n' |
221 } | 227 'title.en-US = 2.2\n' |
222 ] | 228 'message.en-US = 2.1\n' |
229 'sample = 0.25\n') | |
230 ]) | |
231 | |
223 random_call.return_value = 0 | 232 random_call.return_value = 0 |
224 result = json.loads(notification.notification({}, lambda *args: None)) | 233 result = json.loads(notification.notification({}, lambda *args: None)) |
225 self.assertEqual(len(result['notifications']), 1) | 234 self.assertEqual(len(result['notifications']), 1) |
226 self.assertEqual(result['notifications'][0]['id'], 'a') | 235 self.assertEqual(result['notifications'][0]['id'], '1') |
227 self.assertEqual(result['notifications'][0]['title']['en-US'], '1') | 236 self.assertEqual(result['notifications'][0]['title']['en-US'], '1.1') |
228 self.assertRegexpMatches(result['version'], r'-a/1-b/0') | 237 self.assertRegexpMatches(result['version'], r'-1/1-2/0') |
229 random_call.return_value = 0.251 | 238 random_call.return_value = 0.251 |
230 result = json.loads(notification.notification({}, lambda *args: None)) | 239 result = json.loads(notification.notification({}, lambda *args: None)) |
231 self.assertEqual(len(result['notifications']), 1) | 240 self.assertEqual(len(result['notifications']), 1) |
232 self.assertEqual(result['notifications'][0]['id'], 'a') | 241 self.assertEqual(result['notifications'][0]['id'], '1') |
233 self.assertEqual(result['notifications'][0]['title']['en-US'], '2') | 242 self.assertEqual(result['notifications'][0]['title']['en-US'], '1.2') |
234 self.assertRegexpMatches(result['version'], r'-a/2-b/0') | 243 self.assertRegexpMatches(result['version'], r'-1/2-2/0') |
235 random_call.return_value = 0.51 | 244 random_call.return_value = 0.51 |
236 result = json.loads(notification.notification({}, lambda *args: None)) | 245 result = json.loads(notification.notification({}, lambda *args: None)) |
237 self.assertEqual(len(result['notifications']), 1) | 246 self.assertEqual(len(result['notifications']), 1) |
238 self.assertEqual(result['notifications'][0]['id'], 'b') | 247 self.assertEqual(result['notifications'][0]['id'], '2') |
239 self.assertEqual(result['notifications'][0]['title']['en-US'], '1') | 248 self.assertEqual(result['notifications'][0]['title']['en-US'], '2.1') |
240 self.assertRegexpMatches(result['version'], r'-a/0-b/1') | 249 self.assertRegexpMatches(result['version'], r'-1/0-2/1') |
241 random_call.return_value = 0.751 | 250 random_call.return_value = 0.751 |
242 result = json.loads(notification.notification({}, lambda *args: None)) | 251 result = json.loads(notification.notification({}, lambda *args: None)) |
243 self.assertEqual(len(result['notifications']), 1) | 252 self.assertEqual(len(result['notifications']), 1) |
244 self.assertEqual(result['notifications'][0]['id'], 'b') | 253 self.assertEqual(result['notifications'][0]['id'], '2') |
245 self.assertEqual(result['notifications'][0]['title']['en-US'], '2') | 254 self.assertEqual(result['notifications'][0]['title']['en-US'], '2.2') |
246 self.assertRegexpMatches(result['version'], r'-a/0-b/2') | 255 self.assertRegexpMatches(result['version'], r'-1/0-2/2') |
247 | 256 |
257 @mock.patch('time.gmtime', mock_time) | |
248 def test_invalid_last_version(self): | 258 def test_invalid_last_version(self): |
249 self.load_notifications_mock.return_value = [] | |
250 notification.notification({'QUERY_STRING': 'lastVersion='}, | 259 notification.notification({'QUERY_STRING': 'lastVersion='}, |
251 lambda *args: None) | 260 lambda *args: None) |
261 result = json.loads(notification.notification({}, lambda *args: None)) | |
262 self.assertEqual(result['version'], '197001010000') | |
263 | |
252 notification.notification({'QUERY_STRING': 'lastVersion=-'}, | 264 notification.notification({'QUERY_STRING': 'lastVersion=-'}, |
253 lambda *args: None) | 265 lambda *args: None) |
266 result = json.loads(notification.notification({}, lambda *args: None)) | |
267 self.assertEqual(result['version'], '197001010000') | |
268 | |
254 notification.notification({'QUERY_STRING': 'lastVersion=-/'}, | 269 notification.notification({'QUERY_STRING': 'lastVersion=-/'}, |
255 lambda *args: None) | 270 lambda *args: None) |
271 result = json.loads(notification.notification({}, lambda *args: None)) | |
272 self.assertEqual(result['version'], '197001010000') | |
273 | |
256 notification.notification({'QUERY_STRING': 'lastVersion=-//'}, | 274 notification.notification({'QUERY_STRING': 'lastVersion=-//'}, |
257 lambda *args: None) | 275 lambda *args: None) |
276 result = json.loads(notification.notification({}, lambda *args: None)) | |
277 self.assertEqual(result['version'], '197001010000') | |
258 | 278 |
259 def test_version_header_present(self): | 279 def test_version_header_present(self): |
260 self.load_notifications_mock.return_value = [ | 280 self.add_notifications([ |
261 {'id': '1', 'title': {'en-US': ''}, 'message': {'en-US': ''}} | 281 ('1', 'title.en-US = 1\nmessage.en-US = 1\n') |
262 ] | 282 ]) |
263 response_header_map = {} | 283 response_header_map = {} |
264 | 284 |
265 def start_response(status, response_headers): | 285 def start_response(status, response_headers): |
266 for name, value in response_headers: | 286 for name, value in response_headers: |
267 response_header_map[name] = value | 287 response_header_map[name] = value |
268 result = json.loads(notification.notification({}, start_response)) | 288 result = json.loads(notification.notification({}, start_response)) |
269 self.assertEqual(result['version'], | 289 self.assertEqual(result['version'], |
270 response_header_map['ABP-Notification-Version']) | 290 response_header_map['ABP-Notification-Version']) |
271 | 291 |
272 def test_default_group_notification_returned_if_valid(self): | 292 def test_default_group_notification_returned_if_valid(self): |
273 self.load_notifications_mock.return_value = [ | 293 self.add_notifications([ |
274 {'id': '1', 'title': {'en-US': ''}, 'message': {'en-US': ''}}, | 294 ('1', 'title.en-US = 1\nmessage.en-US = 1\n'), |
275 { | 295 ('2', 'title.en-US = 2\nmessage.en-US = 2\n' |
276 'id': 'a', | 296 '[1]\ntitle.en-US = 2.1\nmessage.en-US = 2.1'), |
277 'title': {'en-US': '0'}, | 297 ]) |
278 'message': {'en-US': '0'}, | 298 |
279 'variants': [ | |
280 {'title': {'en-US': '1'}, 'message': {'en-US': '1'}} | |
281 ] | |
282 } | |
283 ] | |
284 result = json.loads(notification.notification({ | 299 result = json.loads(notification.notification({ |
285 'QUERY_STRING': 'lastVersion=197001010000-a/0' | 300 'QUERY_STRING': 'lastVersion=197001010000-2/0' |
286 }, lambda *args: None)) | 301 }, lambda *args: None)) |
287 self.assertEqual(len(result['notifications']), 2) | 302 self.assertEqual(len(result['notifications']), 2) |
288 self.assertEqual(result['notifications'][0]['id'], '1') | 303 self.assertEqual(result['notifications'][0]['id'], '1') |
289 self.assertEqual(result['notifications'][1]['id'], 'a') | 304 self.assertEqual(result['notifications'][1]['id'], '2') |
290 self.assertEqual(result['notifications'][1]['title']['en-US'], '0') | 305 self.assertEqual(result['notifications'][1]['title']['en-US'], '2') |
291 self.assertNotIn('variants', result['notifications'][1]) | 306 self.assertNotIn('variants', result['notifications'][1]) |
292 self.assertRegexpMatches(result['version'], r'-a/0') | 307 self.assertRegexpMatches(result['version'], r'-2/0') |
293 | 308 |
294 def test_default_group_notification_not_returned_if_invalid(self): | 309 def test_default_group_notification_not_returned_if_invalid(self): |
295 self.load_notifications_mock.return_value = [ | 310 self.add_notifications([ |
296 {'id': '1', 'title': {'en-US': ''}, 'message': {'en-US': ''}}, | 311 ('1', 'title.en-US = 1\nmessage.en-US = 1\n'), |
297 { | 312 ('2', 'title.en-US = 2\n' |
298 'id': 'a', | 313 '[1]\ntitle.en-US = 2.1\nmessage.en-US = 2.1'), |
299 'title': {'en-US': '0'}, | 314 ]) |
300 'variants': [ | |
301 {'title': {'en-US': '1'}, 'message': {'en-US': '1'}} | |
302 ] | |
303 } | |
304 ] | |
305 result = json.loads(notification.notification({ | 315 result = json.loads(notification.notification({ |
306 'QUERY_STRING': 'lastVersion=197001010000-a/0' | 316 'QUERY_STRING': 'lastVersion=197001010000-2/0' |
307 }, lambda *args: None)) | 317 }, lambda *args: None)) |
308 self.assertEqual(len(result['notifications']), 1) | 318 self.assertEqual(len(result['notifications']), 1) |
309 self.assertEqual(result['notifications'][0]['id'], '1') | 319 self.assertEqual(result['notifications'][0]['id'], '1') |
310 self.assertRegexpMatches(result['version'], r'-a/0') | 320 self.assertRegexpMatches(result['version'], r'-2/0') |
311 | 321 |
312 def test_invalid_notification_not_returned(self): | 322 def test_invalid_notification_not_returned(self): |
313 self.load_notifications_mock.return_value = [ | 323 self.add_notifications([ |
314 {'id': '1', 'title': {'en-US': ''}, 'message': {'en-US': ''}}, | 324 ('1', 'title.en-US = 1\nmessage.en-US = 1\n'), |
315 {'id': '2', 'title': {'en-US': ''}, 'message': {}}, | 325 ('2', 'title.en-US = 2\n'), |
wspee
2017/10/09 09:12:03
This is not possible with the parser, so I removed
| |
316 {'id': '3', 'title': {}, 'message': {'en-US': ''}}, | 326 ('3', 'title.en-US = \n'), |
317 {'id': '4', 'title': {}}, | 327 ('4', 'message.en-US = \n'), |
318 {'id': '5', 'message': {}}, | 328 ('5', '\n'), |
319 {'id': '6'} | 329 ]) |
320 ] | 330 |
321 result = json.loads(notification.notification({}, lambda *args: None)) | 331 result = json.loads(notification.notification({}, lambda *args: None)) |
322 self.assertEqual(len(result['notifications']), 1) | 332 self.assertEqual(len(result['notifications']), 1) |
323 self.assertEqual(result['notifications'][0]['id'], '1') | 333 self.assertEqual(result['notifications'][0]['id'], '1') |
324 | 334 |
325 def test_stays_in_group_when_notification_present(self): | 335 def test_stays_in_group_when_notification_present(self): |
326 self.load_notifications_mock.return_value = [ | 336 self.add_notifications([ |
327 {'id': 'a'} | 337 ('1', '\n'), |
328 ] | 338 ]) |
329 result = json.loads(notification.notification({ | 339 result = json.loads(notification.notification({ |
330 'QUERY_STRING': 'lastVersion=197001010000-a/0-b/1' | 340 'QUERY_STRING': 'lastVersion=197001010000-1/0-2/1' |
331 }, lambda *args: None)) | 341 }, lambda *args: None)) |
332 self.assertEqual(len(result['notifications']), 0) | 342 self.assertEqual(len(result['notifications']), 0) |
333 self.assertRegexpMatches(result['version'], r'-a/0') | 343 self.assertRegexpMatches(result['version'], r'-1/0') |
334 | 344 |
335 def test_leaves_group_when_notification_absent(self): | 345 def test_leaves_group_when_notification_absent(self): |
336 self.load_notifications_mock.return_value = [] | |
337 result = json.loads(notification.notification({ | 346 result = json.loads(notification.notification({ |
338 'QUERY_STRING': 'lastVersion=197001010000-a/0-b/1' | 347 'QUERY_STRING': 'lastVersion=197001010000-1/0-2/1' |
339 }, lambda *args: None)) | 348 }, lambda *args: None)) |
340 self.assertEqual(len(result['notifications']), 0) | 349 self.assertEqual(len(result['notifications']), 0) |
341 self.assertRegexpMatches(result['version'], r'[^-]*') | 350 self.assertRegexpMatches(result['version'], r'[^-]*') |
342 | 351 |
343 def test_stays_in_group_when_notification_inactive(self): | 352 def test_stays_in_group_when_notification_inactive(self): |
344 self.load_notifications_mock.return_value = [ | 353 self.add_notifications( |
345 {'id': 'a', 'inactive': True} | 354 [('1', 'inactive = true\ntitle.en-US = 1\nmessage.en-US = 1\n')]) |
346 ] | |
347 result = json.loads(notification.notification({ | 355 result = json.loads(notification.notification({ |
348 'QUERY_STRING': 'lastVersion=197001010000-a/0-b/1' | 356 'QUERY_STRING': 'lastVersion=197001010000-1/0-2/1' |
349 }, lambda *args: None)) | 357 }, lambda *args: None)) |
350 self.assertEqual(len(result['notifications']), 0) | 358 self.assertEqual(len(result['notifications']), 0) |
351 self.assertRegexpMatches(result['version'], r'-a/0') | 359 self.assertRegexpMatches(result['version'], r'-1/0') |
360 | |
361 def test_stays_in_group_when_notification_inactive_assign_new_group(self): | |
362 # See: https://issues.adblockplus.org/ticket/5827 | |
363 self.add_notifications([ | |
364 ('1', 'inactive = true\ntitle.en-US = 1.1\nmessage.en-US = 1.1'), | |
365 ('2', '[1]\ntitle.en-US = 2.1\nmessage.en-US = 2.1\nsample = 1\n'), | |
366 ]) | |
367 result = json.loads(notification.notification({ | |
368 'QUERY_STRING': 'lastVersion=197001010000-1/0' | |
369 }, lambda *args: None)) | |
370 | |
371 self.assertEqual(len(result['notifications']), 1) | |
372 self.assertRegexpMatches(result['version'], r'-1/0-2/1') | |
352 | 373 |
353 def test_inactive_notifications_not_returned(self): | 374 def test_inactive_notifications_not_returned(self): |
354 self.load_notifications_mock.return_value = [ | 375 self.add_notifications([ |
355 {'id': 'a', 'title': {'en-US': ''}, 'message': {'en-US': ''}, 'inact ive': True}, | 376 ('1', 'inactive = true\ntitle.en-US = 1\nmessage.en-US = 1\n'), |
356 {'id': 'b', 'title': {'en-US': ''}, 'message': {'en-US': ''}, 'inact ive': False}, | 377 ('2', 'inactive = false\ntitle.en-US = 2\nmessage.en-US = 2\n'), |
357 {'id': 'c', 'title': {'en-US': ''}, 'message': {'en-US': ''}} | 378 ('3', 'title.en-US = 3\nmessage.en-US = 3\n'), |
358 ] | 379 ]) |
380 | |
359 result = json.loads(notification.notification({}, lambda *args: None)) | 381 result = json.loads(notification.notification({}, lambda *args: None)) |
360 self.assertEqual(len(result['notifications']), 2) | 382 self.assertEqual(len(result['notifications']), 2) |
361 self.assertEqual(result['notifications'][0]['id'], 'b') | 383 self.assertEqual(result['notifications'][0]['id'], '2') |
362 self.assertEqual(result['notifications'][1]['id'], 'c') | 384 self.assertEqual(result['notifications'][1]['id'], '3') |
363 | 385 |
364 def test_inactive_notification_variant_not_returned(self): | 386 def test_inactive_notification_variant_not_returned(self): |
365 self.load_notifications_mock.return_value = [ | 387 self.add_notifications([ |
366 {'id': 'a', 'inactive': True} | 388 ('1', 'inactive = true\ntitle.en-US = 1\nmessage.en-US = 1\n'), |
367 ] | 389 ]) |
368 result = json.loads(notification.notification({ | 390 result = json.loads(notification.notification({ |
369 'QUERY_STRING': 'lastVersion=197001010000-a/1' | 391 'QUERY_STRING': 'lastVersion=197001010000-a/1' |
370 }, lambda *args: None)) | 392 }, lambda *args: None)) |
371 self.assertEqual(len(result['notifications']), 0) | 393 self.assertEqual(len(result['notifications']), 0) |
372 | 394 |
373 if __name__ == '__main__': | 395 if __name__ == '__main__': |
374 unittest.main() | 396 unittest.main() |
OLD | NEW |