| 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 json | 6 import json |
| 7 import logging | 7 import logging |
| 8 import os | 8 import os |
| 9 import sys | 9 import sys |
| 10 import tempfile | 10 import tempfile |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 with open(path, 'w') as f: | 50 with open(path, 'w') as f: |
| 51 json.dump(auth_params._asdict(), f) | 51 json.dump(auth_params._asdict(), f) |
| 52 return path | 52 return path |
| 53 | 53 |
| 54 def test_bot_auth_works(self): | 54 def test_bot_auth_works(self): |
| 55 # If 'task_service_account' is 'bot', local HTTP server returns bot tokens. | 55 # If 'task_service_account' is 'bot', local HTTP server returns bot tokens. |
| 56 exp = int(time.time() + 3600) | 56 exp = int(time.time() + 3600) |
| 57 auth_params_path = self.write_auth_params(bot_auth.AuthParams( | 57 auth_params_path = self.write_auth_params(bot_auth.AuthParams( |
| 58 swarming_http_headers={'Authorization': 'Bearer bot-own-token'}, | 58 swarming_http_headers={'Authorization': 'Bearer bot-own-token'}, |
| 59 swarming_http_headers_exp=exp, | 59 swarming_http_headers_exp=exp, |
| 60 system_service_account='none', |
| 60 task_service_account='bot')) | 61 task_service_account='bot')) |
| 61 with bot_auth.AuthSystem(auth_params_path) as auth_sys: | 62 with bot_auth.AuthSystem(auth_params_path) as auth_sys: |
| 62 self.assertEqual( | 63 self.assertEqual( |
| 63 ({'Authorization': 'Bearer bot-own-token'}, exp), | 64 ({'Authorization': 'Bearer bot-own-token'}, exp), |
| 64 auth_sys.get_bot_headers()) | 65 auth_sys.get_bot_headers()) |
| 65 self.assertEqual( | 66 self.assertEqual( |
| 66 ['rpc_port', 'secret'], | 67 ['rpc_port', 'secret'], |
| 67 sorted(auth_sys.local_auth_context)) | 68 sorted(auth_sys.local_auth_context)) |
| 68 # Try to actually use the local RPC service to grab a token. | 69 # Try to actually use the local RPC service to grab a token. |
| 69 resp = call_rpc(auth_sys.local_auth_context, ['A', 'B', 'C']) | 70 resp = call_rpc(auth_sys.local_auth_context, ['A', 'B', 'C']) |
| 70 self.assertEqual([u'access_token', u'expiry'], sorted(resp)) | 71 self.assertEqual([u'access_token', u'expiry'], sorted(resp)) |
| 71 self.assertEqual(u'bot-own-token', resp['access_token']) | 72 self.assertEqual(u'bot-own-token', resp['access_token']) |
| 72 self.assertEqual(exp, resp['expiry']) | 73 self.assertEqual(exp, resp['expiry']) |
| 73 | 74 |
| 74 def test_no_auth_works(self): | 75 def test_no_auth_works(self): |
| 75 # If 'task_service_account' is empty, doesn't launch local HTTP server. | 76 # If 'task_service_account' is empty, doesn't launch local HTTP server. |
| 76 auth_params_path = self.write_auth_params(bot_auth.AuthParams( | 77 auth_params_path = self.write_auth_params(bot_auth.AuthParams( |
| 77 swarming_http_headers={'Authorization': 'Bearer bot-own-token'}, | 78 swarming_http_headers={'Authorization': 'Bearer bot-own-token'}, |
| 78 swarming_http_headers_exp=0, | 79 swarming_http_headers_exp=0, |
| 80 system_service_account='none', |
| 79 task_service_account='none')) | 81 task_service_account='none')) |
| 80 with bot_auth.AuthSystem(auth_params_path) as auth_sys: | 82 with bot_auth.AuthSystem(auth_params_path) as auth_sys: |
| 81 self.assertIsNone(auth_sys.local_auth_context) | 83 self.assertIsNone(auth_sys.local_auth_context) |
| 82 | 84 |
| 83 | 85 |
| 84 if __name__ == '__main__': | 86 if __name__ == '__main__': |
| 85 fix_encoding.fix_encoding() | 87 fix_encoding.fix_encoding() |
| 86 logging.basicConfig( | 88 logging.basicConfig( |
| 87 level=logging.DEBUG if '-v' in sys.argv else logging.CRITICAL) | 89 level=logging.DEBUG if '-v' in sys.argv else logging.CRITICAL) |
| 88 unittest.main() | 90 unittest.main() |
| OLD | NEW |