OLD | NEW |
(Empty) | |
| 1 # Copyright 2015 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 json |
| 6 import logging |
| 7 import os |
| 8 import urllib2 |
| 9 import webtest |
| 10 |
| 11 from google.appengine.api import users |
| 12 from testing_utils import testing |
| 13 |
| 14 import common |
| 15 import test_module |
| 16 from components import auth |
| 17 |
| 18 |
| 19 class TestHandlerTest(testing.AppengineTestCase): |
| 20 @property |
| 21 def app_module(self): |
| 22 return test_module.test |
| 23 |
| 24 def setUp(self): |
| 25 super(TestHandlerTest, self).setUp() |
| 26 # Disable auth module checks. |
| 27 # self.mock(users, 'get_current_user', |
| 28 # lambda: users.User('test@user.com', 'auth_domain')) |
| 29 # self.mock(test_module.TestHandler, 'xsrf_token_enforce_on', []) |
| 30 # self.mock(auth, 'is_group_member', lambda _: True) |
| 31 |
| 32 def tearDown(self): |
| 33 super(TestHandlerTest, self).tearDown() |
| 34 |
| 35 def test_get(self): |
| 36 response = self.test_app.get('/test') |
| 37 logging.info('response = %s', response) |
| 38 self.assertEquals(200, response.status_int) |
| 39 |
| 40 def test_post(self): |
| 41 response = self.test_app.post('/test', 'deadbeefdata') |
| 42 logging.info('response = %s', response) |
| 43 self.assertEquals(200, response.status_int) |
| 44 |
| 45 |
| 46 class LoadTestHandlerTest(testing.AppengineTestCase): |
| 47 @property |
| 48 def app_module(self): |
| 49 return test_module.test |
| 50 |
| 51 def test_get(self): |
| 52 class LoadTestDataMock(object): |
| 53 def __init__(self, data): |
| 54 self.data = data |
| 55 |
| 56 def get_or_insert(self, _id): |
| 57 return self.data |
| 58 |
| 59 class DataMock(object): |
| 60 def __init__(self, qps=0): |
| 61 self.qps = qps |
| 62 self.updated = False |
| 63 |
| 64 def to_dict(self): |
| 65 return {'qps': self.qps} |
| 66 |
| 67 def put(self): |
| 68 self.updated = True |
| 69 logging.debug('Saving NDB data: %s', self.to_dict()) |
| 70 |
| 71 # Read data. |
| 72 data_0 = DataMock(0) |
| 73 self.mock(test_module, 'LoadTestData', LoadTestDataMock(data_0)) |
| 74 response = self.test_app.get('/loadtest/') |
| 75 logging.info('response = %s', response) |
| 76 self.assertEquals(200, response.status_int) |
| 77 self.assertEquals(data_0.to_dict(), json.loads(response.body)) |
| 78 |
| 79 # Write data, unauthorized. |
| 80 self.mock(users, 'is_current_user_admin', lambda: False) |
| 81 response = self.test_app.get('/loadtest/5') |
| 82 logging.info('response = %s', response) |
| 83 self.assertEquals(302, response.status_int) |
| 84 |
| 85 # Write qps data, authorized. |
| 86 self.mock(users, 'is_current_user_admin', lambda: True) |
| 87 data_1 = DataMock(0) |
| 88 self.mock(test_module, 'LoadTestData', LoadTestDataMock(data_1)) |
| 89 response = self.test_app.get('/loadtest/5') |
| 90 logging.info('response = %s', response) |
| 91 self.assertEquals(200, response.status_int) |
| 92 self.assertTrue(data_1.updated) |
| 93 self.assertEquals(data_1.to_dict(), json.loads(response.body)) |
| 94 |
| 95 # Write qps + size data, authorized. |
| 96 self.mock(users, 'is_current_user_admin', lambda: True) |
| 97 data_1 = DataMock(0) |
| 98 self.mock(test_module, 'LoadTestData', LoadTestDataMock(data_1)) |
| 99 response = self.test_app.get('/loadtest/5?size=2048') |
| 100 logging.info('response = %s', response) |
| 101 self.assertEquals(200, response.status_int) |
| 102 self.assertTrue(data_1.updated) |
| 103 self.assertEquals(data_1.to_dict(), json.loads(response.body)) |
| 104 |
| 105 # Attempt to write bad data, authorized. |
| 106 self.mock(users, 'is_current_user_admin', lambda: True) |
| 107 data_2 = DataMock(0) |
| 108 self.mock(test_module, 'LoadTestData', LoadTestDataMock(data_2)) |
| 109 response = self.test_app.get('/loadtest/-5') |
| 110 logging.info('response = %s', response) |
| 111 self.assertEquals(200, response.status_int) |
| 112 self.assertFalse(data_2.updated) |
OLD | NEW |