OLD | NEW |
(Empty) | |
| 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 |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import alerts |
| 6 import json |
| 7 import unittest |
| 8 import webtest |
| 9 |
| 10 from google.appengine.api import memcache |
| 11 from google.appengine.ext import testbed |
| 12 |
| 13 |
| 14 class AlertsTest(unittest.TestCase): |
| 15 def setUp(self): |
| 16 self.testbed = testbed.Testbed() |
| 17 self.testbed.activate() |
| 18 self.testbed.init_memcache_stub() |
| 19 self.testapp = webtest.TestApp(alerts.app) |
| 20 |
| 21 def tearDown(self): |
| 22 self.testbed.deactivate() |
| 23 |
| 24 def check_json_headers(self, res): |
| 25 self.assertEqual(res.content_type, 'application/json') |
| 26 # This is necessary for cross-site tools to retrieve alerts |
| 27 self.assertEqual(res.headers['access-control-allow-origin'], '*') |
| 28 |
| 29 def test_get_no_data_cached(self): |
| 30 res = self.testapp.get('/alerts') |
| 31 self.check_json_headers(res) |
| 32 self.assertEqual(res.body, '') |
| 33 |
| 34 def test_happy_path(self): |
| 35 # Set it. |
| 36 params = {'content': '{"alerts": ["hello", "world"]}'} |
| 37 self.testapp.post('/alerts', params) |
| 38 |
| 39 # Get it. |
| 40 res = self.testapp.get('/alerts') |
| 41 self.check_json_headers(res) |
| 42 alerts = json.loads(res.body) |
| 43 |
| 44 # The server should have stuck a 'date' on there. |
| 45 self.assertTrue('date' in alerts) |
| 46 self.assertEqual(type(alerts['date']), int) |
| 47 |
| 48 self.assertEqual(alerts['alerts'], ['hello', 'world']) |
| 49 |
| 50 def test_post_invalid_data_not_reflected(self): |
| 51 params = {'content': '[{"this is not valid JSON'} |
| 52 self.testapp.post('/alerts', params, status=400) |
| 53 res = self.testapp.get('/alerts') |
| 54 self.assertEqual(res.body, '') |
| 55 |
| 56 def test_post_invalid_data_does_not_overwrite_valid_data(self): |
| 57 # Populate the cache with something valid |
| 58 params = {'content': '{"alerts": "everything is OK"}'} |
| 59 self.testapp.post('/alerts', params) |
| 60 self.testapp.post('/alerts', {'content': 'woozlwuzl'}, status=400) |
| 61 res = self.testapp.get('/alerts') |
| 62 self.check_json_headers(res) |
| 63 alerts = json.loads(res.body) |
| 64 self.assertEqual(alerts['alerts'], 'everything is OK') |
OLD | NEW |