test__oauth2client.py 5.4 KB

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