test__service_account_info.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 # type: ignore
  17. from google.auth import _service_account_info
  18. from google.auth import crypt
  19. import yatest.common as yc
  20. DATA_DIR = os.path.join(os.path.dirname(yc.source_path(__file__)), "data")
  21. SERVICE_ACCOUNT_JSON_FILE = os.path.join(DATA_DIR, "service_account.json")
  22. GDCH_SERVICE_ACCOUNT_JSON_FILE = os.path.join(DATA_DIR, "gdch_service_account.json")
  23. with open(SERVICE_ACCOUNT_JSON_FILE, "r") as fh:
  24. SERVICE_ACCOUNT_INFO = json.load(fh)
  25. with open(GDCH_SERVICE_ACCOUNT_JSON_FILE, "r") as fh:
  26. GDCH_SERVICE_ACCOUNT_INFO = json.load(fh)
  27. def test_from_dict():
  28. signer = _service_account_info.from_dict(SERVICE_ACCOUNT_INFO)
  29. assert isinstance(signer, crypt.RSASigner)
  30. assert signer.key_id == SERVICE_ACCOUNT_INFO["private_key_id"]
  31. def test_from_dict_es256_signer():
  32. signer = _service_account_info.from_dict(
  33. GDCH_SERVICE_ACCOUNT_INFO, use_rsa_signer=False
  34. )
  35. assert isinstance(signer, crypt.ES256Signer)
  36. assert signer.key_id == GDCH_SERVICE_ACCOUNT_INFO["private_key_id"]
  37. def test_from_dict_bad_private_key():
  38. info = SERVICE_ACCOUNT_INFO.copy()
  39. info["private_key"] = "garbage"
  40. with pytest.raises(ValueError) as excinfo:
  41. _service_account_info.from_dict(info)
  42. assert excinfo.match(r"key")
  43. def test_from_dict_bad_format():
  44. with pytest.raises(ValueError) as excinfo:
  45. _service_account_info.from_dict({}, require=("meep",))
  46. assert excinfo.match(r"missing fields")
  47. def test_from_filename():
  48. info, signer = _service_account_info.from_filename(SERVICE_ACCOUNT_JSON_FILE)
  49. for key, value in SERVICE_ACCOUNT_INFO.items():
  50. assert info[key] == value
  51. assert isinstance(signer, crypt.RSASigner)
  52. assert signer.key_id == SERVICE_ACCOUNT_INFO["private_key_id"]
  53. def test_from_filename_es256_signer():
  54. _, signer = _service_account_info.from_filename(
  55. GDCH_SERVICE_ACCOUNT_JSON_FILE, use_rsa_signer=False
  56. )
  57. assert isinstance(signer, crypt.ES256Signer)
  58. assert signer.key_id == GDCH_SERVICE_ACCOUNT_INFO["private_key_id"]