Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(260)

Side by Side Diff: appengine/swarming/swarming_bot/bot_code/remote_client_test.py

Issue 2958413002: Add 'mint_oauth_token' method to RPC client. (Closed)
Patch Set: Add 'mint_oauth_token' method to RPC client. Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « appengine/swarming/swarming_bot/bot_code/remote_client_grpc.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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()
OLDNEW
« no previous file with comments | « appengine/swarming/swarming_bot/bot_code/remote_client_grpc.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698