blob: 52ab9bd825fbe2cde95c067cb3bb2596e90ebf62 [file] [log] [blame]
Jon Wayne Parrott924191c2017-02-15 16:43:23 -08001# Copyright 2017 Google Inc.
2#
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
53 return CredentialsImpl()
54
55
56class TestSigner(object):
57 def test_constructor(self):
58 request = mock.sentinel.request
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070059 credentials = mock.create_autospec(
Bu Sun Kim9eec0912019-10-21 17:04:21 -070060 google.auth.credentials.Credentials, instance=True
61 )
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080062
Bu Sun Kim9eec0912019-10-21 17:04:21 -070063 signer = iam.Signer(request, credentials, mock.sentinel.service_account_email)
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080064
65 assert signer._request == mock.sentinel.request
66 assert signer._credentials == credentials
Bu Sun Kim9eec0912019-10-21 17:04:21 -070067 assert signer._service_account_email == mock.sentinel.service_account_email
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080068
69 def test_key_id(self):
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080070 signer = iam.Signer(
Jon Wayne Parrott254befe2017-02-22 14:37:31 -080071 mock.sentinel.request,
72 mock.sentinel.credentials,
Bu Sun Kim9eec0912019-10-21 17:04:21 -070073 mock.sentinel.service_account_email,
74 )
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080075
Jon Wayne Parrott254befe2017-02-22 14:37:31 -080076 assert signer.key_id is None
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080077
78 def test_sign_bytes(self):
Bu Sun Kim9eec0912019-10-21 17:04:21 -070079 signature = b"DEADBEEF"
80 encoded_signature = base64.b64encode(signature).decode("utf-8")
81 request = make_request(http_client.OK, data={"signature": encoded_signature})
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080082 credentials = make_credentials()
83
Bu Sun Kim9eec0912019-10-21 17:04:21 -070084 signer = iam.Signer(request, credentials, mock.sentinel.service_account_email)
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080085
Bu Sun Kim9eec0912019-10-21 17:04:21 -070086 returned_signature = signer.sign("123")
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080087
88 assert returned_signature == signature
89
90 def test_sign_bytes_failure(self):
91 request = make_request(http_client.UNAUTHORIZED)
92 credentials = make_credentials()
93
Bu Sun Kim9eec0912019-10-21 17:04:21 -070094 signer = iam.Signer(request, credentials, mock.sentinel.service_account_email)
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080095
96 with pytest.raises(exceptions.TransportError):
Bu Sun Kim9eec0912019-10-21 17:04:21 -070097 signer.sign("123")