| Left: | ||
| Right: |
| 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, |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 23 | 23 |
| 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 = [{"id": "1"}] | 33 self.load_notifications_mock.return_value = [ |
| 34 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}} | |
|
Felix Dahlke
2015/09/04 09:08:40
Adding title and message this way to each notifica
Sebastian Noack
2015/09/04 11:38:12
DUMMY_TEXTS = {"title": {"en-US": ""}, "message":
Felix Dahlke
2015/09/07 07:54:36
Now that I've gotten rid of the "en-US" checking,
| |
| 35 ] | |
| 34 result = json.loads(notification.notification({}, lambda *args: None)) | 36 result = json.loads(notification.notification({}, lambda *args: None)) |
| 35 self.assertEqual(len(result["notifications"]), 1) | 37 self.assertEqual(len(result["notifications"]), 1) |
| 36 self.assertEqual(result["notifications"][0]["id"], "1") | 38 self.assertEqual(result["notifications"][0]["id"], "1") |
| 37 self.assertFalse("-" in result["version"]) | 39 self.assertFalse("-" in result["version"]) |
| 38 | 40 |
| 39 def test_not_in_group(self): | 41 def test_not_in_group(self): |
| 40 self.load_notifications_mock.return_value = [ | 42 self.load_notifications_mock.return_value = [ |
| 41 {"id": "1"}, | 43 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}}, |
| 42 { | 44 { |
| 43 "id": "a", | 45 "id": "a", |
| 44 "variants": [{}] | 46 "variants": [{"title": {"en-US": ""}, "message": {"en-US": ""}}] |
| 45 } | 47 } |
| 46 ] | 48 ] |
| 47 result = json.loads(notification.notification({ | 49 result = json.loads(notification.notification({ |
| 48 "QUERY_STRING": "lastVersion=197001010000-a/0" | 50 "QUERY_STRING": "lastVersion=197001010000-a/0" |
| 49 }, lambda *args: None)) | 51 }, lambda *args: None)) |
| 50 self.assertEqual(len(result["notifications"]), 1) | 52 self.assertEqual(len(result["notifications"]), 1) |
| 51 self.assertEqual(result["notifications"][0]["id"], "1") | 53 self.assertEqual(result["notifications"][0]["id"], "1") |
| 52 self.assertRegexpMatches(result["version"], r"-a/0") | 54 self.assertRegexpMatches(result["version"], r"-a/0") |
| 53 | 55 |
| 54 def test_in_group(self): | 56 def test_in_group(self): |
| 55 self.load_notifications_mock.return_value = [ | 57 self.load_notifications_mock.return_value = [ |
| 56 {"id": "1"}, | 58 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}}, |
| 57 { | 59 { |
| 58 "id": "a", | 60 "id": "a", |
| 59 "variants": [{}] | 61 "variants": [{"title": {"en-US": ""}, "message": {"en-US": ""}}] |
| 60 } | 62 } |
| 61 ] | 63 ] |
| 62 result = json.loads(notification.notification({ | 64 result = json.loads(notification.notification({ |
| 63 "QUERY_STRING": "lastVersion=197001010000-a/1" | 65 "QUERY_STRING": "lastVersion=197001010000-a/1" |
| 64 }, lambda *args: None)) | 66 }, lambda *args: None)) |
| 65 self.assertEqual(len(result["notifications"]), 1) | 67 self.assertEqual(len(result["notifications"]), 1) |
| 66 self.assertEqual(result["notifications"][0]["id"], "a") | 68 self.assertEqual(result["notifications"][0]["id"], "a") |
| 67 self.assertRegexpMatches(result["version"], r"-a/1") | 69 self.assertRegexpMatches(result["version"], r"-a/1") |
| 68 | 70 |
| 69 def test_not_in_one_of_many_groups(self): | 71 def test_not_in_one_of_many_groups(self): |
| 70 self.load_notifications_mock.return_value = [ | 72 self.load_notifications_mock.return_value = [ |
| 71 {"id": "1"}, | 73 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}}, |
| 72 { | 74 { |
| 73 "id": "a", | 75 "id": "a", |
| 74 "variants": [{}] | 76 "variants": [{"title": {"en-US": ""}, "message": {"en-US": ""}}] |
| 75 }, | 77 }, |
| 76 { | 78 { |
| 77 "id": "b", | 79 "id": "b", |
| 78 "variants": [{}] | 80 "variants": [{"title": {"en-US": ""}, "message": {"en-US": ""}}] |
| 79 }, | 81 }, |
| 80 { | 82 { |
| 81 "id": "c", | 83 "id": "c", |
| 82 "variants": [{}] | 84 "variants": [{"title": {"en-US": ""}, "message": {"en-US": ""}}] |
| 83 } | 85 } |
| 84 ] | 86 ] |
| 85 result = json.loads(notification.notification({ | 87 result = json.loads(notification.notification({ |
| 86 "QUERY_STRING": "lastVersion=197001010000-a/0-b/0-c/0" | 88 "QUERY_STRING": "lastVersion=197001010000-a/0-b/0-c/0" |
| 87 }, lambda *args: None)) | 89 }, lambda *args: None)) |
| 88 self.assertEqual(len(result["notifications"]), 1) | 90 self.assertEqual(len(result["notifications"]), 1) |
| 89 self.assertEqual(result["notifications"][0]["id"], "1") | 91 self.assertEqual(result["notifications"][0]["id"], "1") |
| 90 self.assertRegexpMatches(result["version"], r"-a/0-b/0-c/0") | 92 self.assertRegexpMatches(result["version"], r"-a/0-b/0-c/0") |
| 91 | 93 |
| 92 def test_in_one_of_many_groups(self): | 94 def test_in_one_of_many_groups(self): |
| 93 self.load_notifications_mock.return_value = [ | 95 self.load_notifications_mock.return_value = [ |
| 94 {"id": "1"}, | 96 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}}, |
| 95 { | 97 { |
| 96 "id": "a", | 98 "id": "a", |
| 97 "variants": [{}] | 99 "variants": [{"title": {"en-US": ""}, "message": {"en-US": ""}}] |
| 98 }, | 100 }, |
| 99 { | 101 { |
| 100 "id": "b", | 102 "id": "b", |
| 101 "variants": [{}] | 103 "variants": [{"title": {"en-US": ""}, "message": {"en-US": ""}}] |
| 102 }, | 104 }, |
| 103 { | 105 { |
| 104 "id": "c", | 106 "id": "c", |
| 105 "variants": [{}] | 107 "variants": [{"title": {"en-US": ""}, "message": {"en-US": ""}}] |
| 106 } | 108 } |
| 107 ] | 109 ] |
| 108 result = json.loads(notification.notification({ | 110 result = json.loads(notification.notification({ |
| 109 "QUERY_STRING": "lastVersion=197001010000-a/0-b/1-c/0" | 111 "QUERY_STRING": "lastVersion=197001010000-a/0-b/1-c/0" |
| 110 }, lambda *args: None)) | 112 }, lambda *args: None)) |
| 111 self.assertEqual(len(result["notifications"]), 1) | 113 self.assertEqual(len(result["notifications"]), 1) |
| 112 self.assertEqual(result["notifications"][0]["id"], "b") | 114 self.assertEqual(result["notifications"][0]["id"], "b") |
| 113 self.assertRegexpMatches(result["version"], r"-a/0-b/1-c/0") | 115 self.assertRegexpMatches(result["version"], r"-a/0-b/1-c/0") |
| 114 | 116 |
| 115 def test_not_put_in_group(self): | 117 def test_not_put_in_group(self): |
| 116 self.load_notifications_mock.return_value = [ | 118 self.load_notifications_mock.return_value = [ |
| 117 {"id": "1"}, | 119 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}}, |
| 118 { | 120 { |
| 119 "id": "a", | 121 "id": "a", |
| 120 "variants": [{"sample": 0}] | 122 "variants": [ |
| 123 {"sample": 0, "title": {"en-US": ""}, "message": {"en-US": ""}} | |
| 124 ] | |
| 121 } | 125 } |
| 122 ] | 126 ] |
| 123 result = json.loads(notification.notification({ | 127 result = json.loads(notification.notification({ |
| 124 "QUERY_STRING": "lastVersion=197001010000" | 128 "QUERY_STRING": "lastVersion=197001010000" |
| 125 }, lambda *args: None)) | 129 }, lambda *args: None)) |
| 126 self.assertEqual(len(result["notifications"]), 1) | 130 self.assertEqual(len(result["notifications"]), 1) |
| 127 self.assertEqual(result["notifications"][0]["id"], "1") | 131 self.assertEqual(result["notifications"][0]["id"], "1") |
| 128 self.assertRegexpMatches(result["version"], r"-a/0") | 132 self.assertRegexpMatches(result["version"], r"-a/0") |
| 129 | 133 |
| 130 def test_put_in_group(self): | 134 def test_put_in_group(self): |
| 131 self.load_notifications_mock.return_value = [ | 135 self.load_notifications_mock.return_value = [ |
| 132 {"id": "1"}, | 136 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}}, |
| 133 { | 137 { |
| 134 "id": "a", | 138 "id": "a", |
| 135 "variants": [{"sample": 1}] | 139 "variants": [ |
| 140 {"sample": 1, "title": {"en-US": ""}, "message": {"en-US": ""}} | |
| 141 ] | |
| 136 } | 142 } |
| 137 ] | 143 ] |
| 138 result = json.loads(notification.notification({ | 144 result = json.loads(notification.notification({ |
| 139 "QUERY_STRING": "lastVersion=197001010000" | 145 "QUERY_STRING": "lastVersion=197001010000" |
| 140 }, lambda *args: None)) | 146 }, lambda *args: None)) |
| 141 self.assertEqual(len(result["notifications"]), 1) | 147 self.assertEqual(len(result["notifications"]), 1) |
| 142 self.assertEqual(result["notifications"][0]["id"], "a") | 148 self.assertEqual(result["notifications"][0]["id"], "a") |
| 143 self.assertRegexpMatches(result["version"], r"-a/1") | 149 self.assertRegexpMatches(result["version"], r"-a/1") |
| 144 | 150 |
| 145 def test_notification_variant_merged(self): | 151 def test_notification_variant_merged(self): |
| 146 self.load_notifications_mock.return_value = [ | 152 self.load_notifications_mock.return_value = [ |
| 147 { | 153 { |
| 148 "id": "a", | 154 "id": "a", |
| 149 "title.en-GB": "default", | 155 "title": {"en-US": "default"}, |
|
Felix Dahlke
2015/09/04 09:08:40
No idea why I had that syntax there, it's not vali
| |
| 150 "message.en-GB": "default", | 156 "message": {"en-US": "default"}, |
| 151 "message.de-DE": "vorgabe", | |
| 152 "variants": [ | 157 "variants": [ |
| 153 { | 158 { |
| 154 "sample": 1, | 159 "sample": 1, |
| 155 "message.en-GB": "variant" | 160 "message": {"en-US": "variant"} |
| 156 } | 161 } |
| 157 ] | 162 ] |
| 158 } | 163 } |
| 159 ] | 164 ] |
| 160 result = json.loads(notification.notification({}, lambda *args: None)) | 165 result = json.loads(notification.notification({}, lambda *args: None)) |
| 161 self.assertEqual(len(result["notifications"]), 1) | 166 self.assertEqual(len(result["notifications"]), 1) |
| 162 self.assertEqual(result["notifications"][0]["id"], "a") | 167 self.assertEqual(result["notifications"][0]["id"], "a") |
| 163 self.assertEqual(result["notifications"][0]["title.en-GB"], "default") | 168 self.assertEqual(result["notifications"][0]["title"]["en-US"], "default") |
| 164 self.assertEqual(result["notifications"][0]["message.en-GB"], "variant") | 169 self.assertEqual(result["notifications"][0]["message"]["en-US"], "variant") |
| 165 self.assertEqual(result["notifications"][0]["message.de-DE"], "vorgabe") | |
| 166 self.assertFalse("variants" in result["notifications"][0]) | 170 self.assertFalse("variants" in result["notifications"][0]) |
| 167 self.assertFalse("sample" in result["notifications"][0]) | 171 self.assertFalse("sample" in result["notifications"][0]) |
| 168 | 172 |
| 169 def test_no_variant_no_notifications(self): | 173 def test_no_variant_no_notifications(self): |
| 170 self.load_notifications_mock.return_value = [ | 174 self.load_notifications_mock.return_value = [ |
| 171 { | 175 { |
| 172 "id": "a", | 176 "id": "a", |
| 173 "variants": [{"sample": 0}] | 177 "variants": [{"sample": 0}] |
| 174 } | 178 } |
| 175 ] | 179 ] |
| 176 result = json.loads(notification.notification({}, lambda *args: None)) | 180 result = json.loads(notification.notification({}, lambda *args: None)) |
| 177 self.assertEqual(len(result["notifications"]), 0) | 181 self.assertEqual(len(result["notifications"]), 0) |
| 178 | 182 |
| 179 @mock.patch("random.random") | 183 @mock.patch("random.random") |
| 180 def test_probability_distribution_single_group(self, random_call): | 184 def test_probability_distribution_single_group(self, random_call): |
| 181 self.load_notifications_mock.return_value = [ | 185 self.load_notifications_mock.return_value = [ |
| 182 { | 186 { |
| 183 "id": "a", | 187 "id": "a", |
| 184 "variants": [ | 188 "variants": [ |
| 185 { | 189 { |
| 186 "sample": 0.5, | 190 "sample": 0.5, |
| 187 "title.en-GB": "1" | 191 "title": {"en-US": "1"}, |
| 192 "message": {"en-US": ""} | |
| 188 }, | 193 }, |
| 189 { | 194 { |
| 190 "sample": 0.25, | 195 "sample": 0.25, |
| 191 "title.en-GB": "2" | 196 "title": {"en-US": "2"}, |
| 197 "message": {"en-US": ""} | |
| 192 }, | 198 }, |
| 193 { | 199 { |
| 194 "sample": 0.25, | 200 "sample": 0.25, |
| 195 "title.en-GB": "3" | 201 "title": {"en-US": "3"}, |
| 202 "message": {"en-US": ""} | |
| 196 } | 203 } |
| 197 ] | 204 ] |
| 198 } | 205 } |
| 199 ] | 206 ] |
| 200 random_call.return_value = 0 | 207 random_call.return_value = 0 |
| 201 result = json.loads(notification.notification({}, lambda *args: None)) | 208 result = json.loads(notification.notification({}, lambda *args: None)) |
| 202 self.assertEqual(len(result["notifications"]), 1) | 209 self.assertEqual(len(result["notifications"]), 1) |
| 203 self.assertEqual(result["notifications"][0]["title.en-GB"], "1") | 210 self.assertEqual(result["notifications"][0]["title"]["en-US"], "1") |
| 204 self.assertRegexpMatches(result["version"], r"-a/1") | 211 self.assertRegexpMatches(result["version"], r"-a/1") |
| 205 random_call.return_value = 0.5 | 212 random_call.return_value = 0.5 |
| 206 result = json.loads(notification.notification({}, lambda *args: None)) | 213 result = json.loads(notification.notification({}, lambda *args: None)) |
| 207 self.assertEqual(len(result["notifications"]), 1) | 214 self.assertEqual(len(result["notifications"]), 1) |
| 208 self.assertEqual(result["notifications"][0]["title.en-GB"], "1") | 215 self.assertEqual(result["notifications"][0]["title"]["en-US"], "1") |
| 209 self.assertRegexpMatches(result["version"], r"-a/1") | 216 self.assertRegexpMatches(result["version"], r"-a/1") |
| 210 random_call.return_value = 0.51 | 217 random_call.return_value = 0.51 |
| 211 result = json.loads(notification.notification({}, lambda *args: None)) | 218 result = json.loads(notification.notification({}, lambda *args: None)) |
| 212 self.assertEqual(len(result["notifications"]), 1) | 219 self.assertEqual(len(result["notifications"]), 1) |
| 213 self.assertEqual(result["notifications"][0]["title.en-GB"], "2") | 220 self.assertEqual(result["notifications"][0]["title"]["en-US"], "2") |
| 214 self.assertRegexpMatches(result["version"], r"-a/2") | 221 self.assertRegexpMatches(result["version"], r"-a/2") |
| 215 random_call.return_value = 0.75 | 222 random_call.return_value = 0.75 |
| 216 result = json.loads(notification.notification({}, lambda *args: None)) | 223 result = json.loads(notification.notification({}, lambda *args: None)) |
| 217 self.assertEqual(len(result["notifications"]), 1) | 224 self.assertEqual(len(result["notifications"]), 1) |
| 218 self.assertEqual(result["notifications"][0]["title.en-GB"], "2") | 225 self.assertEqual(result["notifications"][0]["title"]["en-US"], "2") |
| 219 self.assertRegexpMatches(result["version"], r"-a/2") | 226 self.assertRegexpMatches(result["version"], r"-a/2") |
| 220 random_call.return_value = 0.751 | 227 random_call.return_value = 0.751 |
| 221 result = json.loads(notification.notification({}, lambda *args: None)) | 228 result = json.loads(notification.notification({}, lambda *args: None)) |
| 222 self.assertEqual(len(result["notifications"]), 1) | 229 self.assertEqual(len(result["notifications"]), 1) |
| 223 self.assertEqual(result["notifications"][0]["title.en-GB"], "3") | 230 self.assertEqual(result["notifications"][0]["title"]["en-US"], "3") |
| 224 self.assertRegexpMatches(result["version"], r"-a/3") | 231 self.assertRegexpMatches(result["version"], r"-a/3") |
| 225 random_call.return_value = 1 | 232 random_call.return_value = 1 |
| 226 result = json.loads(notification.notification({}, lambda *args: None)) | 233 result = json.loads(notification.notification({}, lambda *args: None)) |
| 227 self.assertEqual(len(result["notifications"]), 1) | 234 self.assertEqual(len(result["notifications"]), 1) |
| 228 self.assertEqual(result["notifications"][0]["title.en-GB"], "3") | 235 self.assertEqual(result["notifications"][0]["title"]["en-US"], "3") |
| 229 self.assertRegexpMatches(result["version"], r"-a/3") | 236 self.assertRegexpMatches(result["version"], r"-a/3") |
| 230 | 237 |
| 231 @mock.patch("random.random") | 238 @mock.patch("random.random") |
| 232 def test_probability_distribution_multiple_groups(self, random_call): | 239 def test_probability_distribution_multiple_groups(self, random_call): |
| 233 self.load_notifications_mock.return_value = [ | 240 self.load_notifications_mock.return_value = [ |
| 234 { | 241 { |
| 235 "id": "a", | 242 "id": "a", |
| 236 "variants": [ | 243 "variants": [ |
| 237 { | 244 { |
| 238 "sample": 0.25, | 245 "sample": 0.25, |
| 239 "title.en-GB": "1" | 246 "title": {"en-US": "1"}, |
| 247 "message": {"en-US": ""} | |
| 240 }, | 248 }, |
| 241 { | 249 { |
| 242 "sample": 0.25, | 250 "sample": 0.25, |
| 243 "title.en-GB": "2" | 251 "title": {"en-US": "2"}, |
| 252 "message": {"en-US": ""} | |
| 244 } | 253 } |
| 245 ] | 254 ] |
| 246 }, | 255 }, |
| 247 { | 256 { |
| 248 "id": "b", | 257 "id": "b", |
| 249 "variants": [ | 258 "variants": [ |
| 250 { | 259 { |
| 251 "sample": 0.25, | 260 "sample": 0.25, |
| 252 "title.en-GB": "1" | 261 "title": {"en-US": "1"}, |
| 262 "message": {"en-US": ""} | |
| 253 }, | 263 }, |
| 254 { | 264 { |
| 255 "sample": 0.25, | 265 "sample": 0.25, |
| 256 "title.en-GB": "2" | 266 "title": {"en-US": "2"}, |
| 267 "message": {"en-US": ""} | |
| 257 } | 268 } |
| 258 ] | 269 ] |
| 259 } | 270 } |
| 260 ] | 271 ] |
| 261 random_call.return_value = 0 | 272 random_call.return_value = 0 |
| 262 result = json.loads(notification.notification({}, lambda *args: None)) | 273 result = json.loads(notification.notification({}, lambda *args: None)) |
| 263 self.assertEqual(len(result["notifications"]), 1) | 274 self.assertEqual(len(result["notifications"]), 1) |
| 264 self.assertEqual(result["notifications"][0]["id"], "a") | 275 self.assertEqual(result["notifications"][0]["id"], "a") |
| 265 self.assertEqual(result["notifications"][0]["title.en-GB"], "1") | 276 self.assertEqual(result["notifications"][0]["title"]["en-US"], "1") |
| 266 self.assertRegexpMatches(result["version"], r"-a/1-b/0") | 277 self.assertRegexpMatches(result["version"], r"-a/1-b/0") |
| 267 random_call.return_value = 0.251 | 278 random_call.return_value = 0.251 |
| 268 result = json.loads(notification.notification({}, lambda *args: None)) | 279 result = json.loads(notification.notification({}, lambda *args: None)) |
| 269 self.assertEqual(len(result["notifications"]), 1) | 280 self.assertEqual(len(result["notifications"]), 1) |
| 270 self.assertEqual(result["notifications"][0]["id"], "a") | 281 self.assertEqual(result["notifications"][0]["id"], "a") |
| 271 self.assertEqual(result["notifications"][0]["title.en-GB"], "2") | 282 self.assertEqual(result["notifications"][0]["title"]["en-US"], "2") |
| 272 self.assertRegexpMatches(result["version"], r"-a/2-b/0") | 283 self.assertRegexpMatches(result["version"], r"-a/2-b/0") |
| 273 random_call.return_value = 0.51 | 284 random_call.return_value = 0.51 |
| 274 result = json.loads(notification.notification({}, lambda *args: None)) | 285 result = json.loads(notification.notification({}, lambda *args: None)) |
| 275 self.assertEqual(len(result["notifications"]), 1) | 286 self.assertEqual(len(result["notifications"]), 1) |
| 276 self.assertEqual(result["notifications"][0]["id"], "b") | 287 self.assertEqual(result["notifications"][0]["id"], "b") |
| 277 self.assertEqual(result["notifications"][0]["title.en-GB"], "1") | 288 self.assertEqual(result["notifications"][0]["title"]["en-US"], "1") |
| 278 self.assertRegexpMatches(result["version"], r"-a/0-b/1") | 289 self.assertRegexpMatches(result["version"], r"-a/0-b/1") |
| 279 random_call.return_value = 0.751 | 290 random_call.return_value = 0.751 |
| 280 result = json.loads(notification.notification({}, lambda *args: None)) | 291 result = json.loads(notification.notification({}, lambda *args: None)) |
| 281 self.assertEqual(len(result["notifications"]), 1) | 292 self.assertEqual(len(result["notifications"]), 1) |
| 282 self.assertEqual(result["notifications"][0]["id"], "b") | 293 self.assertEqual(result["notifications"][0]["id"], "b") |
| 283 self.assertEqual(result["notifications"][0]["title.en-GB"], "2") | 294 self.assertEqual(result["notifications"][0]["title"]["en-US"], "2") |
| 284 self.assertRegexpMatches(result["version"], r"-a/0-b/2") | 295 self.assertRegexpMatches(result["version"], r"-a/0-b/2") |
| 285 | 296 |
| 286 def test_invalid_last_version(self): | 297 def test_invalid_last_version(self): |
| 287 self.load_notifications_mock.return_value = [] | 298 self.load_notifications_mock.return_value = [] |
| 288 notification.notification({"QUERY_STRING": "lastVersion="}, | 299 notification.notification({"QUERY_STRING": "lastVersion="}, |
| 289 lambda *args: None) | 300 lambda *args: None) |
| 290 notification.notification({"QUERY_STRING": "lastVersion=-"}, | 301 notification.notification({"QUERY_STRING": "lastVersion=-"}, |
| 291 lambda *args: None) | 302 lambda *args: None) |
| 292 notification.notification({"QUERY_STRING": "lastVersion=-/"}, | 303 notification.notification({"QUERY_STRING": "lastVersion=-/"}, |
| 293 lambda *args: None) | 304 lambda *args: None) |
| 294 notification.notification({"QUERY_STRING": "lastVersion=-//"}, | 305 notification.notification({"QUERY_STRING": "lastVersion=-//"}, |
| 295 lambda *args: None) | 306 lambda *args: None) |
| 296 | 307 |
| 297 def test_version_header_present(self): | 308 def test_version_header_present(self): |
| 298 self.load_notifications_mock.return_value = [{"id": "1"}] | 309 self.load_notifications_mock.return_value = [ |
| 310 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}} | |
| 311 ] | |
| 299 response_header_map = {} | 312 response_header_map = {} |
| 300 def start_response(status, response_headers): | 313 def start_response(status, response_headers): |
| 301 for name, value in response_headers: | 314 for name, value in response_headers: |
| 302 response_header_map[name] = value | 315 response_header_map[name] = value |
| 303 result = json.loads(notification.notification({}, start_response)) | 316 result = json.loads(notification.notification({}, start_response)) |
| 304 self.assertEqual(result["version"], | 317 self.assertEqual(result["version"], |
| 305 response_header_map["ABP-Notification-Version"]) | 318 response_header_map["ABP-Notification-Version"]) |
| 306 | 319 |
| 320 def test_default_group_notification_returned_if_valid(self): | |
| 321 self.load_notifications_mock.return_value = [ | |
| 322 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}}, | |
| 323 { | |
| 324 "id": "a", | |
| 325 "title": {"en-US": "0"}, | |
| 326 "message": {"en-US": "0"}, | |
| 327 "variants": [ | |
| 328 {"title": {"en-US": "1"}, "message": {"en-US": "1"}} | |
| 329 ] | |
| 330 } | |
| 331 ] | |
| 332 result = json.loads(notification.notification({ | |
| 333 "QUERY_STRING": "lastVersion=197001010000-a/0" | |
| 334 }, lambda *args: None)) | |
| 335 self.assertEqual(len(result["notifications"]), 2) | |
| 336 self.assertEqual(result["notifications"][0]["id"], "1") | |
| 337 self.assertEqual(result["notifications"][1]["id"], "a") | |
| 338 self.assertEqual(result["notifications"][1]["title"]["en-US"], "0") | |
| 339 self.assertNotIn("variants", result["notifications"][1]) | |
| 340 self.assertRegexpMatches(result["version"], r"-a/0") | |
| 341 | |
| 342 def test_default_group_notification_not_returned_if_invalid(self): | |
| 343 self.load_notifications_mock.return_value = [ | |
| 344 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}}, | |
| 345 { | |
| 346 "id": "a", | |
| 347 "title": {"en-US": "0"}, | |
| 348 "variants": [ | |
| 349 {"title": {"en-US": "1"}, "message": {"en-US": "1"}} | |
| 350 ] | |
| 351 } | |
| 352 ] | |
| 353 result = json.loads(notification.notification({ | |
| 354 "QUERY_STRING": "lastVersion=197001010000-a/0" | |
| 355 }, lambda *args: None)) | |
| 356 self.assertEqual(len(result["notifications"]), 1) | |
| 357 self.assertEqual(result["notifications"][0]["id"], "1") | |
| 358 self.assertRegexpMatches(result["version"], r"-a/0") | |
| 359 | |
| 360 def test_invalid_notification_not_returned(self): | |
| 361 self.load_notifications_mock.return_value = [ | |
| 362 {"id": "1", "title": {"en-US": ""}, "message": {"en-US": ""}}, | |
| 363 {"id": "2", "title": {"en-US": ""}}, | |
| 364 {"id": "3", "title": {}, "message": {"en-US": ""}}, | |
| 365 {"id": "4", "message": {"en-US": ""}}, | |
| 366 {"id": "5", "title": {"en-US": ""}, "message": {}}, | |
| 367 ] | |
| 368 result = json.loads(notification.notification({}, lambda *args: None)) | |
| 369 self.assertEqual(len(result["notifications"]), 1) | |
| 370 self.assertEqual(result["notifications"][0]["id"], "1") | |
| 371 | |
| 307 if __name__ == '__main__': | 372 if __name__ == '__main__': |
| 308 unittest.main() | 373 unittest.main() |
| OLD | NEW |