• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2016 Google LLC
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 json
16import os
17
18import pytest
19import six
20
21from google.auth import _service_account_info
22from google.auth import crypt
23
24
25DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
26SERVICE_ACCOUNT_JSON_FILE = os.path.join(DATA_DIR, "service_account.json")
27
28with open(SERVICE_ACCOUNT_JSON_FILE, "r") as fh:
29    SERVICE_ACCOUNT_INFO = json.load(fh)
30
31
32def test_from_dict():
33    signer = _service_account_info.from_dict(SERVICE_ACCOUNT_INFO)
34    assert isinstance(signer, crypt.RSASigner)
35    assert signer.key_id == SERVICE_ACCOUNT_INFO["private_key_id"]
36
37
38def test_from_dict_bad_private_key():
39    info = SERVICE_ACCOUNT_INFO.copy()
40    info["private_key"] = "garbage"
41
42    with pytest.raises(ValueError) as excinfo:
43        _service_account_info.from_dict(info)
44
45    assert excinfo.match(r"key")
46
47
48def test_from_dict_bad_format():
49    with pytest.raises(ValueError) as excinfo:
50        _service_account_info.from_dict({}, require=("meep",))
51
52    assert excinfo.match(r"missing fields")
53
54
55def test_from_filename():
56    info, signer = _service_account_info.from_filename(SERVICE_ACCOUNT_JSON_FILE)
57
58    for key, value in six.iteritems(SERVICE_ACCOUNT_INFO):
59        assert info[key] == value
60
61    assert isinstance(signer, crypt.RSASigner)
62    assert signer.key_id == SERVICE_ACCOUNT_INFO["private_key_id"]
63