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