Index: Tools/GardeningServer/alerts_test.py |
diff --git a/Tools/GardeningServer/alerts_test.py b/Tools/GardeningServer/alerts_test.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..d99cf2d3a2a263eac61a2182a803c87d8789dcf3 |
--- /dev/null |
+++ b/Tools/GardeningServer/alerts_test.py |
@@ -0,0 +1,64 @@ |
+# Copyright 2014 The Chromium Authors. All rights reserved. |
+# Use of this source code is governed by a BSD-style license that can be |
+# found in the LICENSE file. |
+ |
+import alerts |
+import json |
+import unittest |
+import webtest |
+ |
+from google.appengine.api import memcache |
+from google.appengine.ext import testbed |
+ |
+ |
+class AlertsTest(unittest.TestCase): |
+ def setUp(self): |
+ self.testbed = testbed.Testbed() |
+ self.testbed.activate() |
+ self.testbed.init_memcache_stub() |
+ self.testapp = webtest.TestApp(alerts.app) |
+ |
+ def tearDown(self): |
+ self.testbed.deactivate() |
+ |
+ def check_json_headers(self, res): |
+ self.assertEqual(res.content_type, 'application/json') |
+ # This is necessary for cross-site tools to retrieve alerts |
+ self.assertEqual(res.headers['access-control-allow-origin'], '*') |
+ |
+ def testGet_NoDataCached(self): |
eseidel
2014/08/13 05:44:22
btw, I think blink follows pep8, so this should be
|
+ res = self.testapp.get('/alerts') |
+ self.check_json_headers(res) |
+ self.assertEqual(res.body, '') |
+ |
+ def testHappyPath(self): |
+ # Set it. |
+ params = {'content': '{"alerts": ["hello", "world"]}'} |
+ self.testapp.post('/alerts', params) |
+ |
+ # Get it. |
+ res = self.testapp.get('/alerts') |
+ self.check_json_headers(res) |
+ alerts = json.loads(res.body) |
+ |
+ # The server should have stuck a 'date' on there. |
+ self.assertTrue('date' in alerts) |
+ self.assertEqual(type(alerts['date']), int) |
+ |
+ self.assertEqual(alerts['alerts'], ['hello', 'world']) |
+ |
+ def testPost_InvalidData_NotReflected(self): |
+ params = {'content': '[{"this is not valid JSON'} |
+ self.testapp.post('/alerts', params, status=400) |
+ res = self.testapp.get('/alerts') |
+ self.assertEqual(res.body, '') |
+ |
+ def testPost_InvalidData_DoesNotOverwriteValidData(self): |
+ # Populate the cache with something valid |
+ params = {'content': '{"alerts": "everything is OK"}'} |
+ self.testapp.post('/alerts', params) |
+ self.testapp.post('/alerts', {'content': 'woozlwuzl'}, status=400) |
+ res = self.testapp.get('/alerts') |
+ self.check_json_headers(res) |
+ alerts = json.loads(res.body) |
+ self.assertEqual(alerts['alerts'], 'everything is OK') |