| OLD | NEW |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import mock |
| 5 import re | 6 import re |
| 6 import urllib | 7 import urllib |
| 7 | 8 |
| 8 import webapp2 | 9 import webapp2 |
| 9 import webtest | 10 import webtest |
| 10 | 11 |
| 12 from google.appengine.api import users |
| 13 |
| 11 from testing_utils import testing | 14 from testing_utils import testing |
| 12 | 15 |
| 13 from common import base_handler | 16 from common import base_handler |
| 14 from common.base_handler import BaseHandler | 17 from common.base_handler import BaseHandler |
| 15 from common.base_handler import Permission | 18 from common.base_handler import Permission |
| 16 | 19 |
| 17 | 20 |
| 18 class PermissionLevelHandler(BaseHandler): | 21 class PermissionLevelHandler(BaseHandler): |
| 19 PERMISSION_LEVEL = Permission.ANYONE | 22 PERMISSION_LEVEL = Permission.ANYONE |
| 20 | 23 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 PermissionLevelHandler.PERMISSION_LEVEL = Permission.CORP_USER | 74 PermissionLevelHandler.PERMISSION_LEVEL = Permission.CORP_USER |
| 72 | 75 |
| 73 # Non-member has no access. | 76 # Non-member has no access. |
| 74 self._VerifyUnauthorizedAccess('test@gmail.com') | 77 self._VerifyUnauthorizedAccess('test@gmail.com') |
| 75 self._VerifyUnauthorizedAccess('test@chromium.org') | 78 self._VerifyUnauthorizedAccess('test@chromium.org') |
| 76 | 79 |
| 77 # Corp users and admin has access. | 80 # Corp users and admin has access. |
| 78 self._VerifyAuthorizedAccess('test@google.com') | 81 self._VerifyAuthorizedAccess('test@google.com') |
| 79 self._VerifyAuthorizedAccess('test@chromium.org', True) | 82 self._VerifyAuthorizedAccess('test@chromium.org', True) |
| 80 | 83 |
| 84 @mock.patch.object(users, 'is_current_user_admin', return_value=True) |
| 85 def testShowDebugInfoForAdmin(self, _): |
| 86 self.assertTrue(BaseHandler()._ShowDebugInfo()) |
| 87 |
| 88 @mock.patch.object(users, 'is_current_user_admin', return_value=False) |
| 89 def testShowDebugInfoForNonAdmin(self, _): |
| 90 handler = BaseHandler() |
| 91 handler.request = {} |
| 92 self.assertFalse(handler._ShowDebugInfo()) |
| 93 |
| 94 @mock.patch.object(users, 'is_current_user_admin', return_value=False) |
| 95 def testShowDebugInfoWithDebugFlag(self, _): |
| 96 handler = BaseHandler() |
| 97 handler.request = {'debug': '1'} |
| 98 self.assertTrue(handler._ShowDebugInfo()) |
| 99 |
| 81 def testAccessByTaskQueue(self): | 100 def testAccessByTaskQueue(self): |
| 82 for permission in (Permission.ANYONE, Permission.CORP_USER, | 101 for permission in (Permission.ANYONE, Permission.CORP_USER, |
| 83 Permission.ADMIN): | 102 Permission.ADMIN): |
| 84 PermissionLevelHandler.PERMISSION_LEVEL = permission | 103 PermissionLevelHandler.PERMISSION_LEVEL = permission |
| 85 # Simulation of task queue request by setting the header requires admin | 104 # Simulation of task queue request by setting the header requires admin |
| 86 # login. | 105 # login. |
| 87 self._VerifyAuthorizedAccess( | 106 self._VerifyAuthorizedAccess( |
| 88 'test@chromium.org', True, {'X-AppEngine-QueueName': 'task_queue'}) | 107 'test@chromium.org', True, {'X-AppEngine-QueueName': 'task_queue'}) |
| 89 | 108 |
| 90 def testUnknownPermissionLevel(self): | 109 def testUnknownPermissionLevel(self): |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 response = self.test_app.get('/format?format=json&pretty=1') | 237 response = self.test_app.get('/format?format=json&pretty=1') |
| 219 self.assertEquals(200, response.status_int) | 238 self.assertEquals(200, response.status_int) |
| 220 self.assertEquals('application/json', response.content_type) | 239 self.assertEquals('application/json', response.content_type) |
| 221 expected_body = ('{\n "a": "b", \n "z": [\n 1, \n 2, \n 3\n ], ' | 240 expected_body = ('{\n "a": "b", \n "z": [\n 1, \n 2, \n 3\n ], ' |
| 222 '\n "b": "%s"\n}' % ('1' * 200)) | 241 '\n "b": "%s"\n}' % ('1' * 200)) |
| 223 self.assertEquals(response.body, expected_body) | 242 self.assertEquals(response.body, expected_body) |
| 224 | 243 |
| 225 def testToJson(self): | 244 def testToJson(self): |
| 226 self.assertEqual('{}', base_handler.ToJson({})) | 245 self.assertEqual('{}', base_handler.ToJson({})) |
| 227 | 246 |
| 247 |
| 228 class InternalExceptionHandler(BaseHandler): | 248 class InternalExceptionHandler(BaseHandler): |
| 229 PERMISSION_LEVEL = Permission.ANYONE | 249 PERMISSION_LEVEL = Permission.ANYONE |
| 230 | 250 |
| 231 def HandleGet(self): | 251 def HandleGet(self): |
| 232 raise Exception('abc') | 252 raise Exception('abc') |
| 233 | 253 |
| 234 | 254 |
| 235 class InternalExceptionTest(testing.AppengineTestCase): | 255 class InternalExceptionTest(testing.AppengineTestCase): |
| 236 app_module = webapp2.WSGIApplication([ | 256 app_module = webapp2.WSGIApplication([ |
| 237 ('/exception', InternalExceptionHandler), | 257 ('/exception', InternalExceptionHandler), |
| 238 ], debug=True) | 258 ], debug=True) |
| 239 | 259 |
| 240 def testInternalException(self): | 260 def testInternalException(self): |
| 241 self.assertRaisesRegexp( | 261 self.assertRaisesRegexp( |
| 242 webtest.app.AppError, | 262 webtest.app.AppError, |
| 243 re.compile('.*500 Internal Server Error.*An internal error occurred.*', | 263 re.compile('.*500 Internal Server Error.*An internal error occurred.*', |
| 244 re.MULTILINE | re.DOTALL), | 264 re.MULTILINE | re.DOTALL), |
| 245 self.test_app.get, '/exception') | 265 self.test_app.get, '/exception') |
| OLD | NEW |