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 collections |
| 6 import logging |
| 7 import webtest |
| 8 |
| 9 from testing_utils import testing |
| 10 from google.appengine.api import users |
| 11 |
| 12 import admin_handler |
| 13 import common |
| 14 import main |
| 15 |
| 16 class AdminTest(testing.AppengineTestCase): |
| 17 |
| 18 @property |
| 19 def app_module(self): |
| 20 return main.app |
| 21 |
| 22 def test_admin_page(self): |
| 23 # Unauthorized GET request: redirect to login page. |
| 24 response = self.test_app.get('/admin/') |
| 25 logging.info('response = %s', response) |
| 26 self.assertEquals(302, response.status_int) |
| 27 # Unauthorized POST request: 403. |
| 28 with self.assertRaises(webtest.AppError) as cm: |
| 29 self.test_app.post('/admin/') |
| 30 logging.info('exception = %s', cm.exception) |
| 31 self.assertIn('403', str(cm.exception)) |
| 32 |
| 33 self.mock(users, 'is_current_user_admin', lambda: True) |
| 34 |
| 35 # Authorized GET request. |
| 36 response = self.test_app.get('/admin/') |
| 37 logging.info('response = %s', response) |
| 38 self.assertEquals(200, response.status_int) |
| 39 # Authorized POST request: 403 (POST not allowed on /admin/). |
| 40 with self.assertRaises(webtest.AppError) as cm: |
| 41 self.test_app.post('/admin/') |
| 42 logging.info('exception = %s', cm.exception) |
| 43 self.assertIn('403', str(cm.exception)) |
| 44 |
| 45 def test_set_credentials(self): |
| 46 class MonAcqDataMock(object): |
| 47 def __init__(self, data): |
| 48 self.data = data |
| 49 |
| 50 def get_by_id(self, _id): |
| 51 return self.data |
| 52 |
| 53 def get_or_insert(self, _id): |
| 54 return self.data |
| 55 |
| 56 class DataMock(object): |
| 57 def __init__(self, credentials=None, url='http://', |
| 58 scopes=None, headers=None): |
| 59 self.credentials = credentials or {} |
| 60 self.url = url |
| 61 self.scopes = scopes or ['a', 'b'] |
| 62 self.headers = headers or {} |
| 63 self.updated = False |
| 64 |
| 65 def to_dict(self): |
| 66 return { |
| 67 'credentials': self.credentials, |
| 68 'url': self.url, |
| 69 'scopes': self.scopes, |
| 70 'headers': self.headers, |
| 71 } |
| 72 |
| 73 def put(self): |
| 74 self.updated = True |
| 75 logging.debug('Saving NDB data: %s', self.to_dict()) |
| 76 |
| 77 # Unauthorized GET request: redirect to login page. |
| 78 response = self.test_app.get('/admin/set-credentials') |
| 79 logging.info('response = %s', response) |
| 80 self.assertEquals(302, response.status_int) |
| 81 |
| 82 # Unauthorized POST request: 403. |
| 83 with self.assertRaises(webtest.AppError) as cm: |
| 84 self.test_app.post('/admin/set-credentials') |
| 85 logging.info('exception = %s', cm.exception) |
| 86 self.assertIn('403', str(cm.exception)) |
| 87 |
| 88 # Authorized GET request, no data in NDB. |
| 89 self.mock(users, 'is_current_user_admin', lambda: True) |
| 90 self.mock(common, 'MonAcqData', MonAcqDataMock(None)) |
| 91 response = self.test_app.get('/admin/set-credentials') |
| 92 self.assertEquals(200, response.status_int) |
| 93 |
| 94 # Authorized GET request, data exists in NDB. |
| 95 self.mock(common, 'MonAcqData', MonAcqDataMock(DataMock())) |
| 96 response = self.test_app.get('/admin/set-credentials') |
| 97 self.assertEquals(200, response.status_int) |
| 98 |
| 99 # POST request with no data (for branch coverage). |
| 100 self.mock(common, 'MonAcqData', MonAcqDataMock(DataMock())) |
| 101 response = self.test_app.post('/admin/set-credentials') |
| 102 self.assertEquals(200, response.status_int) |
| 103 |
| 104 # Valid POST request. |
| 105 data = DataMock() |
| 106 self.mock(common, 'MonAcqData', MonAcqDataMock(data)) |
| 107 params = collections.OrderedDict([ |
| 108 ('url', 'https://new.url'), |
| 109 ('credentials', '{"client_id": "john@doe"}'), |
| 110 ('scopes', 'foo bar'), |
| 111 ]) |
| 112 response = self.test_app.post('/admin/set-credentials', params) |
| 113 self.assertEquals(200, response.status_int) |
| 114 self.assertTrue(data.updated) |
| 115 |
| 116 # Invalid POST request. |
| 117 data = DataMock() |
| 118 self.mock(common, 'MonAcqData', MonAcqDataMock(data)) |
| 119 params = collections.OrderedDict([ |
| 120 ('credentials', '{"client_id": '), # Bad JSON. |
| 121 ]) |
| 122 response = self.test_app.post('/admin/set-credentials', params) |
| 123 self.assertEquals(200, response.status_int) |
| 124 self.assertFalse(data.updated) |
OLD | NEW |