test__service_account_info.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. import json
  15. import os
  16. import pytest
  17. import six
  18. from google.auth import _service_account_info
  19. from google.auth import crypt
  20. import yatest.common
  21. DATA_DIR = os.path.join(yatest.common.test_source_path(), "data")
  22. SERVICE_ACCOUNT_JSON_FILE = os.path.join(DATA_DIR, "service_account.json")
  23. with open(SERVICE_ACCOUNT_JSON_FILE, "r") as fh:
  24. SERVICE_ACCOUNT_INFO = json.load(fh)
  25. def test_from_dict():
  26. signer = _service_account_info.from_dict(SERVICE_ACCOUNT_INFO)
  27. assert isinstance(signer, crypt.RSASigner)
  28. assert signer.key_id == SERVICE_ACCOUNT_INFO["private_key_id"]
  29. def test_from_dict_bad_private_key():
  30. info = SERVICE_ACCOUNT_INFO.copy()
  31. info["private_key"] = "garbage"
  32. with pytest.raises(ValueError) as excinfo:
  33. _service_account_info.from_dict(info)
  34. assert excinfo.match(r"key")
  35. def test_from_dict_bad_format():
  36. with pytest.raises(ValueError) as excinfo:
  37. _service_account_info.from_dict({}, require=("meep",))
  38. assert excinfo.match(r"missing fields")
  39. def test_from_filename():
  40. info, signer = _service_account_info.from_filename(SERVICE_ACCOUNT_JSON_FILE)
  41. for key, value in six.iteritems(SERVICE_ACCOUNT_INFO):
  42. assert info[key] == value
  43. assert isinstance(signer, crypt.RSASigner)
  44. assert signer.key_id == SERVICE_ACCOUNT_INFO["private_key_id"]