| 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 mock |
| 6 import re | 6 import re |
| 7 import urllib | 7 import urllib |
| 8 | 8 |
| 9 import webapp2 | 9 import webapp2 |
| 10 import webtest | 10 import webtest |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 PermissionLevelHandler.PERMISSION_LEVEL = permission | 102 PermissionLevelHandler.PERMISSION_LEVEL = permission |
| 103 # Simulation of task queue request by setting the header requires admin | 103 # Simulation of task queue request by setting the header requires admin |
| 104 # login. | 104 # login. |
| 105 self._VerifyAuthorizedAccess( | 105 self._VerifyAuthorizedAccess( |
| 106 'test@chromium.org', True, {'X-AppEngine-QueueName': 'task_queue'}) | 106 'test@chromium.org', True, {'X-AppEngine-QueueName': 'task_queue'}) |
| 107 | 107 |
| 108 def testUnknownPermissionLevel(self): | 108 def testUnknownPermissionLevel(self): |
| 109 PermissionLevelHandler.PERMISSION_LEVEL = 80000 # An unknown permission. | 109 PermissionLevelHandler.PERMISSION_LEVEL = 80000 # An unknown permission. |
| 110 self._VerifyUnauthorizedAccess('test@google.com') | 110 self._VerifyUnauthorizedAccess('test@google.com') |
| 111 | 111 |
| 112 def testLoginLinkForGetButForceToUseReferer(self): |
| 113 PermissionLevelHandler.PERMISSION_LEVEL = Permission.CORP_USER |
| 114 PermissionLevelHandler.LOGIN_REDIRECT_TO_DISTINATION_PAGE_FOR_GET = False |
| 115 referer_url = 'http://localhost/referer' |
| 116 login_url = ('https://www.google.com/accounts/Login?continue=%s' % |
| 117 urllib.quote(referer_url)) |
| 118 self.assertRaisesRegexp( |
| 119 webtest.app.AppError, |
| 120 re.compile('.*401 Unauthorized.*%s.*' % re.escape(login_url), |
| 121 re.MULTILINE | re.DOTALL), |
| 122 self.test_app.get, '/permission', headers={'referer': referer_url}) |
| 123 |
| 112 def testLoginLinkForGetWithReferer(self): | 124 def testLoginLinkForGetWithReferer(self): |
| 113 PermissionLevelHandler.PERMISSION_LEVEL = Permission.CORP_USER | 125 PermissionLevelHandler.PERMISSION_LEVEL = Permission.CORP_USER |
| 126 PermissionLevelHandler.LOGIN_REDIRECT_TO_DISTINATION_PAGE_FOR_GET = True |
| 114 referer_url = 'http://localhost/referer' | 127 referer_url = 'http://localhost/referer' |
| 115 login_url = ('https://www.google.com/accounts/Login?continue=%s' % | 128 login_url = ('https://www.google.com/accounts/Login?continue=%s' % |
| 116 urllib.quote('http://localhost/permission')) | 129 urllib.quote('http://localhost/permission')) |
| 117 self.assertRaisesRegexp( | 130 self.assertRaisesRegexp( |
| 118 webtest.app.AppError, | 131 webtest.app.AppError, |
| 119 re.compile('.*401 Unauthorized.*%s.*' % re.escape(login_url), | 132 re.compile('.*401 Unauthorized.*%s.*' % re.escape(login_url), |
| 120 re.MULTILINE | re.DOTALL), | 133 re.MULTILINE | re.DOTALL), |
| 121 self.test_app.get, '/permission', headers={'referer': referer_url}) | 134 self.test_app.get, '/permission', headers={'referer': referer_url}) |
| 122 | 135 |
| 123 def testLoginLinkForGetWithoutReferer(self): | 136 def testLoginLinkForGetWithoutReferer(self): |
| 124 PermissionLevelHandler.PERMISSION_LEVEL = Permission.CORP_USER | 137 PermissionLevelHandler.PERMISSION_LEVEL = Permission.CORP_USER |
| 138 PermissionLevelHandler.LOGIN_REDIRECT_TO_DISTINATION_PAGE_FOR_GET = True |
| 125 login_url = ('https://www.google.com/accounts/Login?continue=%s' % | 139 login_url = ('https://www.google.com/accounts/Login?continue=%s' % |
| 126 urllib.quote('http://localhost/permission')) | 140 urllib.quote('http://localhost/permission')) |
| 127 self.assertRaisesRegexp( | 141 self.assertRaisesRegexp( |
| 128 webtest.app.AppError, | 142 webtest.app.AppError, |
| 129 re.compile('.*401 Unauthorized.*%s.*' % re.escape(login_url), | 143 re.compile('.*401 Unauthorized.*%s.*' % re.escape(login_url), |
| 130 re.MULTILINE | re.DOTALL), | 144 re.MULTILINE | re.DOTALL), |
| 131 self.test_app.get, '/permission') | 145 self.test_app.get, '/permission') |
| 132 | 146 |
| 133 def testLoginLinkForPostWithReferer(self): | 147 def testLoginLinkForPostWithReferer(self): |
| 134 PermissionLevelHandler.PERMISSION_LEVEL = Permission.CORP_USER | 148 PermissionLevelHandler.PERMISSION_LEVEL = Permission.CORP_USER |
| 149 PermissionLevelHandler.LOGIN_REDIRECT_TO_DISTINATION_PAGE_FOR_GET = True |
| 135 referer_url = 'http://localhost/referer' | 150 referer_url = 'http://localhost/referer' |
| 136 login_url = ('https://www.google.com/accounts/Login?continue=%s' % | 151 login_url = ('https://www.google.com/accounts/Login?continue=%s' % |
| 137 urllib.quote(referer_url)) | 152 urllib.quote(referer_url)) |
| 138 self.assertRaisesRegexp( | 153 self.assertRaisesRegexp( |
| 139 webtest.app.AppError, | 154 webtest.app.AppError, |
| 140 re.compile('.*401 Unauthorized.*%s.*' % re.escape(login_url), | 155 re.compile('.*401 Unauthorized.*%s.*' % re.escape(login_url), |
| 141 re.MULTILINE | re.DOTALL), | 156 re.MULTILINE | re.DOTALL), |
| 142 self.test_app.post, '/permission', headers={'referer': referer_url}) | 157 self.test_app.post, '/permission', headers={'referer': referer_url}) |
| 143 | 158 |
| 144 def testLoginLinkWithRequestedUrl(self): | |
| 145 PermissionLevelHandler.PERMISSION_LEVEL = Permission.CORP_USER | |
| 146 request_url = '/permission' | |
| 147 login_url = ('https://www.google.com/accounts/Login?continue=%s' % | |
| 148 urllib.quote('http://localhost/permission')) | |
| 149 self.assertRaisesRegexp( | |
| 150 webtest.app.AppError, | |
| 151 re.compile('.*401 Unauthorized.*%s.*' % re.escape(login_url), | |
| 152 re.MULTILINE | re.DOTALL), | |
| 153 self.test_app.get, request_url) | |
| 154 | |
| 155 | 159 |
| 156 class UnImplementedHandler(BaseHandler): | 160 class UnImplementedHandler(BaseHandler): |
| 157 PERMISSION_LEVEL = Permission.ANYONE | 161 PERMISSION_LEVEL = Permission.ANYONE |
| 158 | 162 |
| 159 | 163 |
| 160 class UnimplementedGetAndPostTest(testing.AppengineTestCase): | 164 class UnimplementedGetAndPostTest(testing.AppengineTestCase): |
| 161 app_module = webapp2.WSGIApplication([ | 165 app_module = webapp2.WSGIApplication([ |
| 162 ('/unimplemented', UnImplementedHandler), | 166 ('/unimplemented', UnImplementedHandler), |
| 163 ], debug=True) | 167 ], debug=True) |
| 164 | 168 |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 app_module = webapp2.WSGIApplication([ | 280 app_module = webapp2.WSGIApplication([ |
| 277 ('/exception', InternalExceptionHandler), | 281 ('/exception', InternalExceptionHandler), |
| 278 ], debug=True) | 282 ], debug=True) |
| 279 | 283 |
| 280 def testInternalException(self): | 284 def testInternalException(self): |
| 281 self.assertRaisesRegexp( | 285 self.assertRaisesRegexp( |
| 282 webtest.app.AppError, | 286 webtest.app.AppError, |
| 283 re.compile('.*500 Internal Server Error.*An internal error occurred.*', | 287 re.compile('.*500 Internal Server Error.*An internal error occurred.*', |
| 284 re.MULTILINE | re.DOTALL), | 288 re.MULTILINE | re.DOTALL), |
| 285 self.test_app.get, '/exception') | 289 self.test_app.get, '/exception') |
| OLD | NEW |