test__oauth2client.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 datetime
  15. import importlib
  16. import os
  17. import sys
  18. import mock
  19. import pytest # type: ignore
  20. try:
  21. import oauth2client.client # type: ignore
  22. import oauth2client.contrib.gce # type: ignore
  23. import oauth2client.service_account # type: ignore
  24. except ImportError: # pragma: NO COVER
  25. pytest.skip(
  26. "Skipping oauth2client tests since oauth2client is not installed.",
  27. allow_module_level=True,
  28. )
  29. from google.auth import _oauth2client
  30. import yatest.common as yc
  31. DATA_DIR = os.path.join(os.path.dirname(yc.source_path(__file__)), "data")
  32. SERVICE_ACCOUNT_JSON_FILE = os.path.join(DATA_DIR, "service_account.json")
  33. def test__convert_oauth2_credentials():
  34. old_credentials = oauth2client.client.OAuth2Credentials(
  35. "access_token",
  36. "client_id",
  37. "client_secret",
  38. "refresh_token",
  39. datetime.datetime.min,
  40. "token_uri",
  41. "user_agent",
  42. scopes="one two",
  43. )
  44. new_credentials = _oauth2client._convert_oauth2_credentials(old_credentials)
  45. assert new_credentials.token == old_credentials.access_token
  46. assert new_credentials._refresh_token == old_credentials.refresh_token
  47. assert new_credentials._client_id == old_credentials.client_id
  48. assert new_credentials._client_secret == old_credentials.client_secret
  49. assert new_credentials._token_uri == old_credentials.token_uri
  50. assert new_credentials.scopes == old_credentials.scopes
  51. def test__convert_service_account_credentials():
  52. old_class = oauth2client.service_account.ServiceAccountCredentials
  53. old_credentials = old_class.from_json_keyfile_name(SERVICE_ACCOUNT_JSON_FILE)
  54. new_credentials = _oauth2client._convert_service_account_credentials(
  55. old_credentials
  56. )
  57. assert (
  58. new_credentials.service_account_email == old_credentials.service_account_email
  59. )
  60. assert new_credentials._signer.key_id == old_credentials._private_key_id
  61. assert new_credentials._token_uri == old_credentials.token_uri
  62. def test__convert_service_account_credentials_with_jwt():
  63. old_class = oauth2client.service_account._JWTAccessCredentials
  64. old_credentials = old_class.from_json_keyfile_name(SERVICE_ACCOUNT_JSON_FILE)
  65. new_credentials = _oauth2client._convert_service_account_credentials(
  66. old_credentials
  67. )
  68. assert (
  69. new_credentials.service_account_email == old_credentials.service_account_email
  70. )
  71. assert new_credentials._signer.key_id == old_credentials._private_key_id
  72. assert new_credentials._token_uri == old_credentials.token_uri
  73. def test__convert_gce_app_assertion_credentials():
  74. old_credentials = oauth2client.contrib.gce.AppAssertionCredentials(
  75. email="some_email"
  76. )
  77. new_credentials = _oauth2client._convert_gce_app_assertion_credentials(
  78. old_credentials
  79. )
  80. assert (
  81. new_credentials.service_account_email == old_credentials.service_account_email
  82. )
  83. @pytest.fixture
  84. def mock_oauth2client_gae_imports(mock_non_existent_module):
  85. mock_non_existent_module("google.appengine.api.app_identity")
  86. mock_non_existent_module("google.appengine.ext.ndb")
  87. mock_non_existent_module("google.appengine.ext.webapp.util")
  88. mock_non_existent_module("webapp2")
  89. @mock.patch("google.auth.app_engine.app_identity")
  90. def _test__convert_appengine_app_assertion_credentials(
  91. app_identity, mock_oauth2client_gae_imports
  92. ):
  93. import oauth2client.contrib.appengine # type: ignore
  94. service_account_id = "service_account_id"
  95. old_credentials = oauth2client.contrib.appengine.AppAssertionCredentials(
  96. scope="one two", service_account_id=service_account_id
  97. )
  98. new_credentials = _oauth2client._convert_appengine_app_assertion_credentials(
  99. old_credentials
  100. )
  101. assert new_credentials.scopes == ["one", "two"]
  102. assert new_credentials._service_account_id == old_credentials.service_account_id
  103. class FakeCredentials(object):
  104. pass
  105. def test_convert_success():
  106. convert_function = mock.Mock(spec=["__call__"])
  107. conversion_map_patch = mock.patch.object(
  108. _oauth2client, "_CLASS_CONVERSION_MAP", {FakeCredentials: convert_function}
  109. )
  110. credentials = FakeCredentials()
  111. with conversion_map_patch:
  112. result = _oauth2client.convert(credentials)
  113. convert_function.assert_called_once_with(credentials)
  114. assert result == convert_function.return_value
  115. def test_convert_not_found():
  116. with pytest.raises(ValueError) as excinfo:
  117. _oauth2client.convert("a string is not a real credentials class")
  118. assert excinfo.match("Unable to convert")
  119. @pytest.fixture
  120. def reset__oauth2client_module():
  121. """Reloads the _oauth2client module after a test."""
  122. importlib.reload(_oauth2client)
  123. def _test_import_has_app_engine(
  124. mock_oauth2client_gae_imports, reset__oauth2client_module
  125. ):
  126. importlib.reload(_oauth2client)
  127. assert _oauth2client._HAS_APPENGINE
  128. def test_import_without_oauth2client(monkeypatch, reset__oauth2client_module):
  129. monkeypatch.setitem(sys.modules, "oauth2client", None)
  130. with pytest.raises(ImportError) as excinfo:
  131. importlib.reload(_oauth2client)
  132. assert excinfo.match("oauth2client")