Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(269)

Side by Side Diff: Tools/GardeningServer/alerts_test.py

Issue 664563002: Restructured alerts-history endpoint (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Tools/GardeningServer/alerts.py ('k') | Tools/GardeningServer/app.yaml » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 5 import alerts
6 import json 6 import json
7 import random 7 import random
8 import string 8 import string
9 import unittest 9 import unittest
10 import webtest 10 import webtest
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 self.testapp.post('/alerts', {'content': json.dumps(test_alerts)}) 94 self.testapp.post('/alerts', {'content': json.dumps(test_alerts)})
95 stored_alerts = alerts.AlertsJSON.query().fetch(limit=2) 95 stored_alerts = alerts.AlertsJSON.query().fetch(limit=2)
96 self.assertEqual(1, len(stored_alerts)) 96 self.assertEqual(1, len(stored_alerts))
97 97
98 def test_alerts_jsons_are_retrieved_from_history(self): 98 def test_alerts_jsons_are_retrieved_from_history(self):
99 test_alert = {'alerts': ['hello', 'world', '1']} 99 test_alert = {'alerts': ['hello', 'world', '1']}
100 alerts.AlertsJSON(json=json.dumps(test_alert), type='alerts').put() 100 alerts.AlertsJSON(json=json.dumps(test_alert), type='alerts').put()
101 response = self.testapp.get('/alerts-history') 101 response = self.testapp.get('/alerts-history')
102 self.assertEqual(response.status_int, 200) 102 self.assertEqual(response.status_int, 200)
103 self.assertEqual(response.content_type, 'application/json') 103 self.assertEqual(response.content_type, 'application/json')
104 response_json = json.loads(response.normal_body) 104 parsed_json = json.loads(response.normal_body)
105 self.assertEqual(len(response_json['history']), 1) 105 self.assertEqual(len(parsed_json['history']), 1)
106 self.assertEqual(response_json['history'][0], test_alert)
107 106
108 def test_internal_alerts_in_history_visible_to_internal_users_only(self): 107 entry_id = parsed_json['history'][0]
108 response = self.testapp.get('/alerts-history/%s' % entry_id)
109 self.assertEqual(response.status_int, 200)
110 self.assertEqual(response.content_type, 'application/json')
111 parsed_json = json.loads(response.normal_body)
112 self.assertEqual(parsed_json['alerts'], test_alert['alerts'])
113
114 def test_provides_login_url(self):
115 response = self.testapp.get('/alerts-history')
116 self.assertIn('login-url', response)
117
118 def test_invalid_keys_return_400(self):
119 response = self.testapp.get('/alerts-history/kjhg$%T',
120 expect_errors=True)
121 self.assertEqual(response.status_int, 400)
122 self.assertEqual(response.content_type, 'application/json')
123
124 def test_non_existing_keys_return_404(self):
125 response = self.testapp.get('/alerts-history/5348024557502464',
126 expect_errors=True)
127 self.assertEqual(response.status_int, 404)
128 self.assertEqual(response.content_type, 'application/json')
129
130 def test_internal_alerts_can_only_retrieved_by_internal_users(self):
131 test_alert = {'alerts': ['hello', 'world', '1']}
132 internal_alert = alerts.AlertsJSON(json=json.dumps(test_alert),
133 type='internal-alerts')
134 internal_alert_key = internal_alert.put().integer_id()
135
136 # No signed-in user.
137 response = self.testapp.get('/alerts-history/%s' % internal_alert_key,
138 expect_errors=True)
139 self.assertEqual(response.status_int, 404)
140 self.assertEqual(response.content_type, 'application/json')
141 parsed_json = json.loads(response.normal_body)
142 self.assertNotIn('alerts', parsed_json)
143
144 # Non-internal user.
145 self.testbed.setup_env(USER_EMAIL='test@example.com', USER_ID='1',
146 USER_IS_ADMIN='1', overwrite=True)
147 response = self.testapp.get('/alerts-history/%s' % internal_alert_key,
148 expect_errors=True)
149 self.assertEqual(response.status_int, 404)
150 self.assertEqual(response.content_type, 'application/json')
151 parsed_json = json.loads(response.normal_body)
152 self.assertNotIn('alerts', parsed_json)
153
154 def test_lists_internal_alerts_to_internal_users_only(self):
109 test_alert = {'alerts': ['hello', 'world', '1']} 155 test_alert = {'alerts': ['hello', 'world', '1']}
110 alerts.AlertsJSON(json=json.dumps(test_alert), 156 alerts.AlertsJSON(json=json.dumps(test_alert),
111 type='internal-alerts').put() 157 type='internal-alerts').put()
112 158
113 # No signed-in user. 159 # No signed-in user.
114 response = self.testapp.get('/alerts-history') 160 response = self.testapp.get('/alerts-history')
115 self.assertEqual(response.status_int, 200) 161 self.assertEqual(response.status_int, 200)
116 self.assertEqual(response.content_type, 'application/json') 162 self.assertEqual(response.content_type, 'application/json')
117 response_json = json.loads(response.normal_body) 163 response_json = json.loads(response.normal_body)
118 self.assertEqual(len(response_json['history']), 0) 164 self.assertEqual(len(response_json['history']), 0)
119 165
120 # Non-internal user. 166 # Non-internal user.
121 self.testbed.setup_env(USER_EMAIL='test@example.com', USER_ID='1', 167 self.testbed.setup_env(USER_EMAIL='test@example.com', USER_ID='1',
122 USER_IS_ADMIN='1', overwrite=True) 168 USER_IS_ADMIN='1', overwrite=True)
123 response = self.testapp.get('/alerts-history') 169 response = self.testapp.get('/alerts-history')
124 self.assertEqual(response.status_int, 200) 170 self.assertEqual(response.status_int, 200)
125 self.assertEqual(response.content_type, 'application/json') 171 self.assertEqual(response.content_type, 'application/json')
126 response_json = json.loads(response.normal_body) 172 response_json = json.loads(response.normal_body)
127 self.assertEqual(len(response_json['history']), 0) 173 self.assertEqual(len(response_json['history']), 0)
128 174
129 # Internal user. 175 # Internal user.
130 self.testbed.setup_env(USER_EMAIL='test@google.com', USER_ID='2', 176 self.testbed.setup_env(USER_EMAIL='test@google.com', USER_ID='2',
131 USER_IS_ADMIN='1', overwrite=True) 177 USER_IS_ADMIN='1', overwrite=True)
132 response = self.testapp.get('/alerts-history') 178 response = self.testapp.get('/alerts-history')
133 self.assertEqual(response.status_int, 200) 179 self.assertEqual(response.status_int, 200)
134 self.assertEqual(response.content_type, 'application/json') 180 self.assertEqual(response.content_type, 'application/json')
135 response_json = json.loads(response.normal_body) 181 response_json = json.loads(response.normal_body)
136 self.assertEqual(len(response_json['history']), 1) 182 self.assertEqual(len(response_json['history']), 1)
137 self.assertEqual(response_json['history'][0], test_alert)
138 183
139 def test_returned_alerts_from_history_are_paged(self): 184 def test_returned_alerts_from_history_are_paged(self):
140 for i in range(20): 185 for i in range(20):
141 test_alert = {'alerts': ['hello', 'world', i]} 186 test_alert = {'alerts': ['hello', 'world', i]}
142 alerts.AlertsJSON(json=json.dumps(test_alert), type='alerts').put() 187 alerts.AlertsJSON(json=json.dumps(test_alert), type='alerts').put()
143 188
144 response = self.testapp.get('/alerts-history?limit=15') 189 response = self.testapp.get('/alerts-history?limit=15')
145 self.assertEqual(response.status_int, 200) 190 self.assertEqual(response.status_int, 200)
146 self.assertEqual(response.content_type, 'application/json') 191 self.assertEqual(response.content_type, 'application/json')
147 response_json = json.loads(response.normal_body) 192 response_json = json.loads(response.normal_body)
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 259
215 def value(): 260 def value():
216 generators = [time, build, revision, test, literal_array, 261 generators = [time, build, revision, test, literal_array,
217 literal_map] 262 literal_map]
218 return random.choice(generators)() 263 return random.choice(generators)()
219 264
220 alert = {} 265 alert = {}
221 for _ in range(random.randint(6, 9)): 266 for _ in range(random.randint(6, 9)):
222 alert[label()] = value() 267 alert[label()] = value()
223 return alert 268 return alert
OLDNEW
« no previous file with comments | « Tools/GardeningServer/alerts.py ('k') | Tools/GardeningServer/app.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698