blob: cc090850f13fb45011a42246ce6950d7e15b119a [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:
35 response.data = json.dumps(data).encode('utf-8')
36
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__()
46 self.token = 'token'
47 # 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(
60 google.auth.credentials.Credentials, instance=True)
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080061
62 signer = iam.Signer(
63 request, credentials, mock.sentinel.service_account_email)
64
65 assert signer._request == mock.sentinel.request
66 assert signer._credentials == credentials
67 assert (signer._service_account_email ==
68 mock.sentinel.service_account_email)
69
70 def test_key_id(self):
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080071 signer = iam.Signer(
Jon Wayne Parrott254befe2017-02-22 14:37:31 -080072 mock.sentinel.request,
73 mock.sentinel.credentials,
74 mock.sentinel.service_account_email)
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):
79 signature = b'DEADBEEF'
80 encoded_signature = base64.b64encode(signature).decode('utf-8')
81 request = make_request(
82 http_client.OK, data={'signature': encoded_signature})
83 credentials = make_credentials()
84
85 signer = iam.Signer(
86 request, credentials, mock.sentinel.service_account_email)
87
88 returned_signature = signer.sign('123')
89
90 assert returned_signature == signature
91
92 def test_sign_bytes_failure(self):
93 request = make_request(http_client.UNAUTHORIZED)
94 credentials = make_credentials()
95
96 signer = iam.Signer(
97 request, credentials, mock.sentinel.service_account_email)
98
99 with pytest.raises(exceptions.TransportError):
100 signer.sign('123')