def test_get_access_token_error_code(self): with self.luci_ctx({'local_auth': _LOCAL_AUTH}) as environ: with self.assertRaises(luci_auth.LUCIAuthError): luci_auth.get_access_token(scopes=['a', 'b'], environ=environ, http=self.mocked_http( {'error_code': 1}, status=200))
def test_get_access_token_happy_without_exp(self): with self.luci_ctx({'local_auth': _LOCAL_AUTH}) as environ: tok, exp = luci_auth.get_access_token(scopes=['a', 'b'], environ=environ, http=self.mocked_http({ 'access_token': 'new-token', })) self.assertEqual(tok, 'new-token') self.assertIsNone(exp)
def test_get_access_token_happy_with_exp(self): with self.luci_ctx({'local_auth': _LOCAL_AUTH}) as environ: expiry = int(time.time()) + 3600 http = self.mocked_http({ 'access_token': 'new-token', 'expiry': expiry, }) tok, exp = luci_auth.get_access_token(scopes=['a', 'b'], environ=environ, http=http) self.assertEqual(tok, 'new-token') self.assertEqual(exp, datetime.datetime.utcfromtimestamp(expiry)) self.assertEqual(http.requests_made, [ http.HttpCall( uri='http://127.0.0.1:%s/rpc/' 'LuciLocalAuthService.GetOAuthToken' % _PORT, method='POST', body= '{"scopes": ["a", "b"], "secret": "zzz", "account_id": "task"}', headers={'Content-Type': 'application/json'}, ) ])
def test_get_access_token_no_ctx(self): with self.luci_ctx({}) as environ: with self.assertRaises(luci_auth.LUCIAuthError): luci_auth.get_access_token(scopes=['a', 'b'], environ=environ, http=self.mocked_http({}))
def test_get_access_token_bad_scopes_2(self): with self.luci_ctx({'local_auth': _LOCAL_AUTH}) as environ: with self.assertRaises(TypeError): luci_auth.get_access_token(scopes=[1, 2, 3], environ=environ, http=self.mocked_http({}))