blob: 4367fe7a813bf611d7bb6c9dc995a13ddad874cd [file] [log] [blame]
C.J. Collier37141e42020-02-13 13:49:49 -08001# Copyright 2017 Google LLC
Jon Wayne Parrott924191c2017-02-15 16:43:23 -08002#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import base64
16import datetime
17import json
18
19import mock
20import pytest
21from six.moves import http_client
22
Jon Wayne Parrott7af9f662017-05-08 09:40:56 -070023from google.auth import _helpers
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080024from google.auth import exceptions
25from google.auth import iam
26from google.auth import transport
27import google.auth.credentials
28
29
30def make_request(status, data=None):
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070031 response = mock.create_autospec(transport.Response, instance=True)
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080032 response.status = status
33
34 if data is not None:
Bu Sun Kim9eec0912019-10-21 17:04:21 -070035 response.data = json.dumps(data).encode("utf-8")
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080036
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070037 request = mock.create_autospec(transport.Request)
38 request.return_value = response
39 return request
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080040
41
42def make_credentials():
43 class CredentialsImpl(google.auth.credentials.Credentials):
44 def __init__(self):
45 super(CredentialsImpl, self).__init__()
Bu Sun Kim9eec0912019-10-21 17:04:21 -070046 self.token = "token"
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080047 # Force refresh
Jon Wayne Parrott7af9f662017-05-08 09:40:56 -070048 self.expiry = datetime.datetime.min + _helpers.CLOCK_SKEW
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080049
50 def refresh(self, request):
51 pass
52
Bu Sun Kim3dda7b22020-07-09 10:39:39 -070053 def with_quota_project(self, quota_project_id):
54 raise NotImplementedError()
55
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080056 return CredentialsImpl()
57
58
59class TestSigner(object):
60 def test_constructor(self):
61 request = mock.sentinel.request
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070062 credentials = mock.create_autospec(
Bu Sun Kim9eec0912019-10-21 17:04:21 -070063 google.auth.credentials.Credentials, instance=True
64 )
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080065
Bu Sun Kim9eec0912019-10-21 17:04:21 -070066 signer = iam.Signer(request, credentials, mock.sentinel.service_account_email)
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080067
68 assert signer._request == mock.sentinel.request
69 assert signer._credentials == credentials
Bu Sun Kim9eec0912019-10-21 17:04:21 -070070 assert signer._service_account_email == mock.sentinel.service_account_email
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080071
72 def test_key_id(self):
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080073 signer = iam.Signer(
Jon Wayne Parrott254befe2017-02-22 14:37:31 -080074 mock.sentinel.request,
75 mock.sentinel.credentials,
Bu Sun Kim9eec0912019-10-21 17:04:21 -070076 mock.sentinel.service_account_email,
77 )
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080078
Jon Wayne Parrott254befe2017-02-22 14:37:31 -080079 assert signer.key_id is None
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080080
81 def test_sign_bytes(self):
Bu Sun Kim9eec0912019-10-21 17:04:21 -070082 signature = b"DEADBEEF"
83 encoded_signature = base64.b64encode(signature).decode("utf-8")
Bu Sun Kima48b5b92020-07-17 10:24:02 -070084 request = make_request(http_client.OK, data={"signature": encoded_signature})
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080085 credentials = make_credentials()
86
Bu Sun Kim9eec0912019-10-21 17:04:21 -070087 signer = iam.Signer(request, credentials, mock.sentinel.service_account_email)
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080088
Bu Sun Kim9eec0912019-10-21 17:04:21 -070089 returned_signature = signer.sign("123")
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080090
91 assert returned_signature == signature
92
93 def test_sign_bytes_failure(self):
94 request = make_request(http_client.UNAUTHORIZED)
95 credentials = make_credentials()
96
Bu Sun Kim9eec0912019-10-21 17:04:21 -070097 signer = iam.Signer(request, credentials, mock.sentinel.service_account_email)
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080098
99 with pytest.raises(exceptions.TransportError):
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700100 signer.sign("123")