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