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 logging | 6 import logging |
7 import sys | 7 import sys |
8 import threading | 8 import threading |
9 import time | 9 import time |
10 import unittest | 10 import unittest |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 self.assertEqual({'Now': '100000'}, c.get_authentication_headers()) | 70 self.assertEqual({'Now': '100000'}, c.get_authentication_headers()) |
71 | 71 |
72 # A bit later still using same cached headers. | 72 # A bit later still using same cached headers. |
73 self.mock(time, 'time', lambda: 102000) | 73 self.mock(time, 'time', lambda: 102000) |
74 self.assertEqual({'Now': '100000'}, c.get_authentication_headers()) | 74 self.assertEqual({'Now': '100000'}, c.get_authentication_headers()) |
75 | 75 |
76 # Close to expiration => refreshed. | 76 # Close to expiration => refreshed. |
77 self.mock(time, 'time', lambda: 103500) | 77 self.mock(time, 'time', lambda: 103500) |
78 self.assertEqual({'Now': '103500'}, c.get_authentication_headers()) | 78 self.assertEqual({'Now': '103500'}, c.get_authentication_headers()) |
79 | 79 |
| 80 def test_mint_oauth_token_ok(self): |
| 81 fake_resp = { |
| 82 'service_account': 'blah@example.com', |
| 83 'access_token': 'abc', |
| 84 'expiry': 12345, |
| 85 } |
| 86 |
| 87 c = remote_client.RemoteClientNative('http://localhost:1', None) |
| 88 def mocked_call(url_path, data): |
| 89 self.assertEqual('/swarming/api/v1/bot/oauth_token', url_path) |
| 90 self.assertEqual({ |
| 91 'account_id': 'account_id', |
| 92 'id': 'bot_id', |
| 93 'scopes': ['a', 'b'], |
| 94 'task_id': 'task_id', |
| 95 }, data) |
| 96 return fake_resp |
| 97 self.mock(c, '_url_read_json', mocked_call) |
| 98 |
| 99 resp = c.mint_oauth_token('task_id', 'bot_id', 'account_id', ['a', 'b']) |
| 100 self.assertEqual(fake_resp, resp) |
| 101 |
| 102 def test_mint_oauth_token_transient_err(self): |
| 103 c = remote_client.RemoteClientNative('http://localhost:1', None) |
| 104 def mocked_call(*_args, **_kwargs): |
| 105 return None # that's how net.url_read_json indicates HTTP 500 :-/ |
| 106 self.mock(c, '_url_read_json', mocked_call) |
| 107 with self.assertRaises(remote_client.InternalError): |
| 108 c.mint_oauth_token('task_id', 'bot_id', 'account_id', ['a', 'b']) |
| 109 |
| 110 def test_mint_oauth_token_fatal_err(self): |
| 111 c = remote_client.RemoteClientNative('http://localhost:1', None) |
| 112 def mocked_call(*_args, **_kwargs): |
| 113 return {'error': 'blah'} |
| 114 self.mock(c, '_url_read_json', mocked_call) |
| 115 with self.assertRaises(remote_client.MintOAuthTokenError): |
| 116 c.mint_oauth_token('task_id', 'bot_id', 'account_id', ['a', 'b']) |
| 117 |
80 | 118 |
81 if __name__ == '__main__': | 119 if __name__ == '__main__': |
82 logging.basicConfig( | 120 logging.basicConfig( |
83 level=logging.DEBUG if '-v' in sys.argv else logging.CRITICAL) | 121 level=logging.DEBUG if '-v' in sys.argv else logging.CRITICAL) |
84 unittest.main() | 122 unittest.main() |
OLD | NEW |