OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import alerts | |
6 import internal_alerts | 5 import internal_alerts |
7 import json | 6 import json |
8 import random | 7 import random |
9 import string | 8 import string |
10 import unittest | 9 import unittest |
11 import webtest | 10 import webtest |
12 | 11 |
13 from google.appengine.api import memcache | 12 from google.appengine.api import memcache |
14 from google.appengine.ext import testbed | 13 from google.appengine.ext import testbed |
15 | 14 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 # Populate the cache with something valid | 73 # Populate the cache with something valid |
75 params = {'content': '{"alerts": "everything is OK"}'} | 74 params = {'content': '{"alerts": "everything is OK"}'} |
76 self.testapp.post('/internal-alerts', params) | 75 self.testapp.post('/internal-alerts', params) |
77 self.testapp.post('/internal-alerts', {'content': 'woozlwuzl'}, | 76 self.testapp.post('/internal-alerts', {'content': 'woozlwuzl'}, |
78 status=400) | 77 status=400) |
79 res = self.testapp.get('/internal-alerts') | 78 res = self.testapp.get('/internal-alerts') |
80 self.check_json_headers(res) | 79 self.check_json_headers(res) |
81 internal_alerts = json.loads(res.body) | 80 internal_alerts = json.loads(res.body) |
82 self.assertEqual(internal_alerts['alerts'], 'everything is OK') | 81 self.assertEqual(internal_alerts['alerts'], 'everything is OK') |
83 | 82 |
84 def test_internal_alerts_stored_in_history_have_correct_type(self): | |
85 test_alerts1 = {'alerts': ['hello', 'world', '1']} | |
86 test_alerts2 = {'alerts': ['hello', 'world', '2']} | |
87 self.testapp.post('/internal-alerts', | |
88 {'content': json.dumps(test_alerts1)}) | |
89 self.testapp.post('/internal-alerts', | |
90 {'content': json.dumps(test_alerts2)}) | |
91 alerts_query = alerts.AlertsJSON.query().order(alerts.AlertsJSON.date) | |
92 stored_alerts = alerts_query.fetch(limit=3) | |
93 self.assertEqual(2, len(stored_alerts)) | |
94 self.assertEqual(stored_alerts[0].type, 'internal-alerts') | |
95 self.assertEqual(stored_alerts[1].type, 'internal-alerts') | |
96 | |
97 def test_internal_alerts_same_as_last_alerts_are_added_to_history(self): | |
98 test_alerts = {'alerts': ['hello', 'world']} | |
99 alerts.AlertsJSON(json=json.dumps(test_alerts), type='alerts').put() | |
100 self.testapp.post('/internal-alerts', | |
101 {'content': json.dumps(test_alerts)}) | |
102 alerts_query = alerts.AlertsJSON.query() | |
103 self.assertEqual(2, alerts_query.count(limit=3)) | |
104 | |
105 def test_large_number_of_internal_alerts(self): | 83 def test_large_number_of_internal_alerts(self): |
106 self.user_helper('tester@google.com', '123') | 84 self.user_helper('tester@google.com', '123') |
107 # This generates ~2.5MB of JSON that compresses to ~750K. Real | 85 # This generates ~2.5MB of JSON that compresses to ~750K. Real |
108 # data compresses about 6x better. | 86 # data compresses about 6x better. |
109 random.seed(0xf00f00) | 87 random.seed(0xf00f00) |
110 put_internal_alerts = self.generate_fake_internal_alerts(4000) | 88 put_internal_alerts = self.generate_fake_internal_alerts(4000) |
111 | 89 |
112 params = {'content': json.dumps(put_internal_alerts)} | 90 params = {'content': json.dumps(put_internal_alerts)} |
113 self.testapp.post('/internal-alerts', params) | 91 self.testapp.post('/internal-alerts', params) |
114 | 92 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 | 159 |
182 def value(): | 160 def value(): |
183 generators = [time, build, revision, test, literal_array, | 161 generators = [time, build, revision, test, literal_array, |
184 literal_map] | 162 literal_map] |
185 return random.choice(generators)() | 163 return random.choice(generators)() |
186 | 164 |
187 alert = {} | 165 alert = {} |
188 for _ in range(random.randint(6, 9)): | 166 for _ in range(random.randint(6, 9)): |
189 alert[label()] = value() | 167 alert[label()] = value() |
190 return alert | 168 return alert |
OLD | NEW |