blob: 97fa90755069f2551ad9ae3467408564ff2f70c7 [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):
31 response = mock.Mock(spec=transport.Response)
32 response.status = status
33
34 if data is not None:
35 response.data = json.dumps(data).encode('utf-8')
36
37 return mock.Mock(return_value=response, spec=transport.Request)
38
39
40def make_credentials():
41 class CredentialsImpl(google.auth.credentials.Credentials):
42 def __init__(self):
43 super(CredentialsImpl, self).__init__()
44 self.token = 'token'
45 # Force refresh
Jon Wayne Parrott7af9f662017-05-08 09:40:56 -070046 self.expiry = datetime.datetime.min + _helpers.CLOCK_SKEW
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080047
48 def refresh(self, request):
49 pass
50
51 return CredentialsImpl()
52
53
54class TestSigner(object):
55 def test_constructor(self):
56 request = mock.sentinel.request
57 credentials = mock.Mock(spec=google.auth.credentials.Credentials)
58
59 signer = iam.Signer(
60 request, credentials, mock.sentinel.service_account_email)
61
62 assert signer._request == mock.sentinel.request
63 assert signer._credentials == credentials
64 assert (signer._service_account_email ==
65 mock.sentinel.service_account_email)
66
67 def test_key_id(self):
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080068 signer = iam.Signer(
Jon Wayne Parrott254befe2017-02-22 14:37:31 -080069 mock.sentinel.request,
70 mock.sentinel.credentials,
71 mock.sentinel.service_account_email)
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080072
Jon Wayne Parrott254befe2017-02-22 14:37:31 -080073 assert signer.key_id is None
Jon Wayne Parrott924191c2017-02-15 16:43:23 -080074
75 def test_sign_bytes(self):
76 signature = b'DEADBEEF'
77 encoded_signature = base64.b64encode(signature).decode('utf-8')
78 request = make_request(
79 http_client.OK, data={'signature': encoded_signature})
80 credentials = make_credentials()
81
82 signer = iam.Signer(
83 request, credentials, mock.sentinel.service_account_email)
84
85 returned_signature = signer.sign('123')
86
87 assert returned_signature == signature
88
89 def test_sign_bytes_failure(self):
90 request = make_request(http_client.UNAUTHORIZED)
91 credentials = make_credentials()
92
93 signer = iam.Signer(
94 request, credentials, mock.sentinel.service_account_email)
95
96 with pytest.raises(exceptions.TransportError):
97 signer.sign('123')