Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 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 json |
| 17 | |
| 18 | import mock |
| 19 | import pytest |
| 20 | import six |
| 21 | from six.moves import http_client |
| 22 | from six.moves import urllib |
| 23 | |
| 24 | from google.auth import exceptions |
| 25 | from google.oauth2 import _client |
| 26 | |
| 27 | |
| 28 | def test__handle_error_response(): |
| 29 | response_data = json.dumps({ |
| 30 | 'error': 'help', |
| 31 | 'error_description': 'I\'m alive'}) |
| 32 | |
| 33 | with pytest.raises(exceptions.RefreshError) as excinfo: |
| 34 | _client._handle_error_response(response_data) |
| 35 | |
| 36 | assert excinfo.match(r'help: I\'m alive') |
| 37 | |
| 38 | |
| 39 | def test__handle_error_response_non_json(): |
| 40 | response_data = 'Help, I\'m alive' |
| 41 | |
| 42 | with pytest.raises(exceptions.RefreshError) as excinfo: |
| 43 | _client._handle_error_response(response_data) |
| 44 | |
| 45 | assert excinfo.match(r'Help, I\'m alive') |
| 46 | |
| 47 | |
| 48 | @mock.patch('google.auth._helpers.utcnow', return_value=datetime.datetime.min) |
| 49 | def test__parse_expiry(now_mock): |
| 50 | result = _client._parse_expiry({'expires_in': 500}) |
| 51 | assert result == datetime.datetime.min + datetime.timedelta(seconds=500) |
| 52 | |
| 53 | |
| 54 | def test__parse_expiry_none(): |
| 55 | assert _client._parse_expiry({}) is None |
| 56 | |
| 57 | |
| 58 | def _make_request(response_data): |
| 59 | response = mock.Mock() |
| 60 | response.status = http_client.OK |
| 61 | response.data = json.dumps(response_data).encode('utf-8') |
| 62 | return mock.Mock(return_value=response) |
| 63 | |
| 64 | |
| 65 | def test__token_endpoint_request(): |
| 66 | request = _make_request({'test': 'response'}) |
| 67 | |
| 68 | result = _client._token_endpoint_request( |
| 69 | request, 'http://example.com', {'test': 'params'}) |
| 70 | |
| 71 | # Check request call |
| 72 | request.assert_called_with( |
| 73 | method='POST', |
| 74 | url='http://example.com', |
| 75 | headers={'content-type': 'application/x-www-form-urlencoded'}, |
| 76 | body='test=params') |
| 77 | |
| 78 | # Check result |
| 79 | assert result == {'test': 'response'} |
| 80 | |
| 81 | |
| 82 | def test__token_endpoint_request_error(): |
| 83 | response = mock.Mock() |
| 84 | response.status = http_client.BAD_REQUEST |
| 85 | response.data = b'Error' |
| 86 | request = mock.Mock(return_value=response) |
| 87 | |
| 88 | with pytest.raises(exceptions.RefreshError): |
| 89 | _client._token_endpoint_request(request, 'http://example.com', {}) |
| 90 | |
| 91 | |
| 92 | def _verify_request_params(request, params): |
| 93 | request_body = request.call_args[1]['body'] |
| 94 | request_params = urllib.parse.parse_qs(request_body) |
| 95 | |
| 96 | for key, value in six.iteritems(params): |
| 97 | assert request_params[key][0] == value |
| 98 | |
| 99 | |
| 100 | @mock.patch('google.auth._helpers.utcnow', return_value=datetime.datetime.min) |
| 101 | def test_jwt_grant(now_mock): |
| 102 | request = _make_request({ |
| 103 | 'access_token': 'token', |
| 104 | 'expires_in': 500, |
| 105 | 'extra': 'data'}) |
| 106 | |
| 107 | token, expiry, extra_data = _client.jwt_grant( |
| 108 | request, 'http://example.com', 'assertion_value') |
| 109 | |
| 110 | # Check request call |
| 111 | _verify_request_params(request, { |
| 112 | 'grant_type': _client._JWT_GRANT_TYPE, |
| 113 | 'assertion': 'assertion_value' |
| 114 | }) |
| 115 | |
| 116 | # Check result |
| 117 | assert token == 'token' |
| 118 | assert expiry == datetime.datetime.min + datetime.timedelta(seconds=500) |
| 119 | assert extra_data['extra'] == 'data' |
| 120 | |
| 121 | |
| 122 | def test_jwt_grant_no_access_token(): |
| 123 | request = _make_request({ |
| 124 | # No access token. |
| 125 | 'expires_in': 500, |
| 126 | 'extra': 'data'}) |
| 127 | |
| 128 | with pytest.raises(exceptions.RefreshError): |
| 129 | _client.jwt_grant(request, 'http://example.com', 'assertion_value') |
| 130 | |
| 131 | |
| 132 | @mock.patch('google.auth._helpers.utcnow', return_value=datetime.datetime.min) |
| 133 | def test_refresh_grant(now_mock): |
| 134 | request = _make_request({ |
| 135 | 'access_token': 'token', |
| 136 | 'refresh_token': 'new_refresh_token', |
| 137 | 'expires_in': 500, |
| 138 | 'extra': 'data'}) |
| 139 | |
| 140 | token, refresh_token, expiry, extra_data = _client.refresh_grant( |
| 141 | request, 'http://example.com', 'refresh_token', 'client_id', |
| 142 | 'client_secret') |
| 143 | |
| 144 | # Check request call |
| 145 | _verify_request_params(request, { |
| 146 | 'grant_type': _client._REFRESH_GRANT_TYPE, |
| 147 | 'refresh_token': 'refresh_token', |
| 148 | 'client_id': 'client_id', |
| 149 | 'client_secret': 'client_secret' |
| 150 | }) |
| 151 | |
| 152 | # Check result |
| 153 | assert token == 'token' |
| 154 | assert refresh_token == 'new_refresh_token' |
| 155 | assert expiry == datetime.datetime.min + datetime.timedelta(seconds=500) |
| 156 | assert extra_data['extra'] == 'data' |
| 157 | |
| 158 | |
| 159 | def test_refresh_grant_no_access_token(): |
| 160 | request = _make_request({ |
| 161 | # No access token. |
| 162 | 'refresh_token': 'new_refresh_token', |
| 163 | 'expires_in': 500, |
| 164 | 'extra': 'data'}) |
| 165 | |
| 166 | with pytest.raises(exceptions.RefreshError): |
| 167 | _client.refresh_grant( |
| 168 | request, 'http://example.com', 'refresh_token', 'client_id', |
| 169 | 'client_secret') |