Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(798)

Side by Side Diff: appengine/chrome_infra_mon_proxy/test/admin_handler_test.py

Issue 928043005: Monitoring proxy for time series data (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Responded to comments Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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
15 class AdminTest(testing.AppengineTestCase):
16
17 @property
18 def app_module(self):
19 return admin_handler.admin
20
21 def test_admin_page(self):
22 # Not logged in GET request: redirect to login page.
23 self.mock(users, 'get_current_user', lambda: None)
24 response = self.test_app.get('/admin/')
25 logging.info('response = %s', response)
26 self.assertEquals(302, response.status_int)
27
28 # Unauthorized POST request: 403.
29 with self.assertRaises(webtest.AppError) as cm:
30 self.test_app.post('/admin/')
31 logging.info('exception = %s', cm.exception)
32 self.assertIn('403', str(cm.exception))
33
34 # Non-admin user: 403.
35 self.mock(users, 'get_current_user', lambda: 'jack@example.com')
36 self.mock(users, 'is_current_user_admin', lambda: False)
37 with self.assertRaises(webtest.AppError) as cm:
38 self.test_app.get('/admin/')
39 logging.info('exception = %s', cm.exception)
40 self.assertIn('403', str(cm.exception))
41
42 self.mock(users, 'get_current_user', lambda: 'admin@example.com')
43 self.mock(users, 'is_current_user_admin', lambda: True)
44
45 # Authorized GET request.
46 response = self.test_app.get('/admin/')
47 logging.info('response = %s', response)
48 self.assertEquals(200, response.status_int)
49 # Authorized POST request: 403 (POST not allowed on /admin/).
50 with self.assertRaises(webtest.AppError) as cm:
51 self.test_app.post('/admin/')
52 logging.info('exception = %s', cm.exception)
53 self.assertIn('403', str(cm.exception))
54
55 def test_set_credentials(self):
56 class MonAcqDataMock(object):
57 def __init__(self, data):
58 self.data = data
59
60 def get_by_id(self, _id):
61 return self.data
62
63 def get_or_insert(self, _id):
64 return self.data
65
66 class DataMock(object):
67 def __init__(self, credentials=None, url='http://',
68 scopes=None, headers=None):
69 self.credentials = credentials or {}
70 self.url = url
71 self.scopes = scopes or ['a', 'b']
72 self.headers = headers or {}
73 self.updated = False
74
75 def to_dict(self):
76 return {
77 'credentials': self.credentials,
78 'url': self.url,
79 'scopes': self.scopes,
80 'headers': self.headers,
81 }
82
83 def put(self):
84 self.updated = True
85 logging.debug('Saving NDB data: %s', self.to_dict())
86
87 # Unauthorized GET request: redirect to login page.
88 response = self.test_app.get('/admin/set-credentials')
89 logging.info('response = %s', response)
90 self.assertEquals(302, response.status_int)
91
92 # Unauthorized POST request: 403.
93 with self.assertRaises(webtest.AppError) as cm:
94 self.test_app.post('/admin/set-credentials')
95 logging.info('exception = %s', cm.exception)
96 self.assertIn('403', str(cm.exception))
97
98 # Authorized GET request, no data in NDB.
99 self.mock(users, 'get_current_user', lambda: 'admin@example.com')
100 self.mock(users, 'is_current_user_admin', lambda: True)
101 self.mock(common, 'MonAcqData', MonAcqDataMock(None))
102 response = self.test_app.get('/admin/set-credentials')
103 self.assertEquals(200, response.status_int)
104
105 # Authorized GET request, data exists in NDB.
106 self.mock(common, 'MonAcqData', MonAcqDataMock(DataMock()))
107 response = self.test_app.get('/admin/set-credentials')
108 self.assertEquals(200, response.status_int)
109
110 # POST request with no data (for branch coverage).
111 self.mock(common, 'MonAcqData', MonAcqDataMock(DataMock()))
112 response = self.test_app.post('/admin/set-credentials')
113 self.assertEquals(200, response.status_int)
114
115 # Valid POST request.
116 data = DataMock()
117 self.mock(common, 'MonAcqData', MonAcqDataMock(data))
118 params = collections.OrderedDict([
119 ('url', 'https://new.url'),
120 ('credentials', '{"client_id": "john@doe"}'),
121 ('scopes', 'foo \n bar\t'),
122 ])
123 response = self.test_app.post('/admin/set-credentials', params)
124 self.assertEquals(200, response.status_int)
125 self.assertTrue(data.updated)
126 self.assertEquals(data.scopes, ['foo', 'bar'])
127
128 # Invalid POST request.
129 data = DataMock()
130 self.mock(common, 'MonAcqData', MonAcqDataMock(data))
131 params = collections.OrderedDict([
132 ('credentials', '{"client_id": '), # Bad JSON.
133 ])
134 response = self.test_app.post('/admin/set-credentials', params)
135 self.assertEquals(200, response.status_int)
136 self.assertFalse(data.updated)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698