| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2016 The LUCI Authors. All rights reserved. | 2 # Copyright 2016 The LUCI Authors. All rights reserved. |
| 3 # Use of this source code is governed under the Apache License, Version 2.0 | 3 # Use of this source code is governed under the Apache License, Version 2.0 |
| 4 # that can be found in the LICENSE file. | 4 # that can be found in the LICENSE file. |
| 5 | 5 |
| 6 import contextlib | 6 import contextlib |
| 7 import json | 7 import json |
| 8 import logging | 8 import logging |
| 9 import os | 9 import os |
| 10 import socket | 10 import socket |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 del calls[:] | 109 del calls[:] |
| 110 | 110 |
| 111 # First token has expired. Generated new one. | 111 # First token has expired. Generated new one. |
| 112 self.mock_time(300) | 112 self.mock_time(300) |
| 113 resp = call_rpc('acc_1', ['A', 'B', 'C']) | 113 resp = call_rpc('acc_1', ['A', 'B', 'C']) |
| 114 self.assertEqual( | 114 self.assertEqual( |
| 115 {u'access_token': u'tok_acc_1', u'expiry': self.epoch + 600}, resp) | 115 {u'access_token': u'tok_acc_1', u'expiry': self.epoch + 600}, resp) |
| 116 self.assertEqual([('acc_1', ('A', 'B', 'C'))], calls) | 116 self.assertEqual([('acc_1', ('A', 'B', 'C'))], calls) |
| 117 | 117 |
| 118 def test_handles_token_errors(self): | 118 def test_handles_token_errors(self): |
| 119 fatal = False | 119 calls = [] |
| 120 code = 123 | |
| 121 def token_gen(_account_id, _scopes): | 120 def token_gen(_account_id, _scopes): |
| 122 raise auth_server.TokenError(code, 'error message', fatal=fatal) | 121 calls.append(1) |
| 122 raise auth_server.TokenError(123, 'error message') |
| 123 | 123 |
| 124 with local_auth_server(token_gen, 'acc_1'): | 124 with local_auth_server(token_gen, 'acc_1'): |
| 125 self.assertEqual( | 125 self.assertEqual( |
| 126 {u'error_code': 123, u'error_message': u'error message'}, | 126 {u'error_code': 123, u'error_message': u'error message'}, |
| 127 call_rpc('acc_1', ['B', 'B', 'A', 'C'])) | 127 call_rpc('acc_1', ['B', 'B', 'A', 'C'])) |
| 128 self.assertEqual(1, len(calls)) |
| 128 | 129 |
| 129 # Non-fatal errors aren't cached. | 130 # Errors are cached. Same error is returned. |
| 130 code = 456 | |
| 131 self.assertEqual( | 131 self.assertEqual( |
| 132 {u'error_code': 456, u'error_message': u'error message'}, | 132 {u'error_code': 123, u'error_message': u'error message'}, |
| 133 call_rpc('acc_1', ['B', 'B', 'A', 'C'])) | 133 call_rpc('acc_1', ['B', 'B', 'A', 'C'])) |
| 134 | 134 self.assertEqual(1, len(calls)) |
| 135 # Fatal errors are cached. | |
| 136 fatal = True | |
| 137 code = 789 | |
| 138 self.assertEqual( | |
| 139 {u'error_code': 789, u'error_message': u'error message'}, | |
| 140 call_rpc('acc_1', ['B', 'B', 'A', 'C'])) | |
| 141 | |
| 142 # Same cached error. | |
| 143 code = 111 | |
| 144 self.assertEqual( | |
| 145 {u'error_code': 789, u'error_message': u'error message'}, | |
| 146 call_rpc('acc_1', ['B', 'B', 'A', 'C'])) | |
| 147 | 135 |
| 148 def test_http_level_errors(self): | 136 def test_http_level_errors(self): |
| 149 def token_gen(_account_id, _scopes): | 137 def token_gen(_account_id, _scopes): |
| 150 self.fail('must not be called') | 138 self.fail('must not be called') |
| 151 | 139 |
| 152 with local_auth_server(token_gen, 'acc_1'): | 140 with local_auth_server(token_gen, 'acc_1'): |
| 153 # Wrong URL. | 141 # Wrong URL. |
| 154 ctx = luci_context.read('local_auth') | 142 ctx = luci_context.read('local_auth') |
| 155 r = requests.post( | 143 r = requests.post( |
| 156 url='http://127.0.0.1:%d/blah/LuciLocalAuthService.GetOAuthToken' % | 144 url='http://127.0.0.1:%d/blah/LuciLocalAuthService.GetOAuthToken' % |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 375 service = self.mocked_http_service(perform_request=handle_request) | 363 service = self.mocked_http_service(perform_request=handle_request) |
| 376 self.assertEqual(service.request(request_url, data={}).read(), response) | 364 self.assertEqual(service.request(request_url, data={}).read(), response) |
| 377 | 365 |
| 378 | 366 |
| 379 if __name__ == '__main__': | 367 if __name__ == '__main__': |
| 380 fix_encoding.fix_encoding() | 368 fix_encoding.fix_encoding() |
| 381 logging.basicConfig( | 369 logging.basicConfig( |
| 382 level=logging.DEBUG if '-v' in sys.argv else logging.CRITICAL) | 370 level=logging.DEBUG if '-v' in sys.argv else logging.CRITICAL) |
| 383 global_test_setup() | 371 global_test_setup() |
| 384 unittest.main() | 372 unittest.main() |
| OLD | NEW |