Jon Wayne Parrott | 4382bc1 | 2017-01-10 13:35:51 -0800 | [diff] [blame^] | 1 | # Copyright 2017 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 json |
| 16 | import os |
| 17 | |
| 18 | import mock |
| 19 | import pytest |
| 20 | |
| 21 | from google.oauth2 import flow |
| 22 | |
| 23 | DATA_DIR = os.path.join(os.path.dirname(__file__), '..', 'data') |
| 24 | CLIENT_SECRETS_FILE = os.path.join(DATA_DIR, 'client_secrets.json') |
| 25 | |
| 26 | with open(CLIENT_SECRETS_FILE, 'r') as fh: |
| 27 | CLIENT_SECRETS_INFO = json.load(fh) |
| 28 | |
| 29 | |
| 30 | def test_constructor_web(): |
| 31 | instance = flow.Flow(CLIENT_SECRETS_INFO, scopes=mock.sentinel.scopes) |
| 32 | assert instance.client_config == CLIENT_SECRETS_INFO['web'] |
| 33 | assert (instance.oauth2session.client_id == |
| 34 | CLIENT_SECRETS_INFO['web']['client_id']) |
| 35 | assert instance.oauth2session.scope == mock.sentinel.scopes |
| 36 | |
| 37 | |
| 38 | def test_constructor_installed(): |
| 39 | info = {'installed': CLIENT_SECRETS_INFO['web']} |
| 40 | instance = flow.Flow(info, scopes=mock.sentinel.scopes) |
| 41 | assert instance.client_config == info['installed'] |
| 42 | assert instance.oauth2session.client_id == info['installed']['client_id'] |
| 43 | assert instance.oauth2session.scope == mock.sentinel.scopes |
| 44 | |
| 45 | |
| 46 | def test_constructor_bad_format(): |
| 47 | with pytest.raises(ValueError): |
| 48 | flow.Flow({}, scopes=[]) |
| 49 | |
| 50 | |
| 51 | def test_constructor_missing_keys(): |
| 52 | with pytest.raises(ValueError): |
| 53 | flow.Flow({'web': {}}, scopes=[]) |
| 54 | |
| 55 | |
| 56 | def test_from_client_secrets_file(): |
| 57 | instance = flow.Flow.from_client_secrets_file( |
| 58 | CLIENT_SECRETS_FILE, scopes=mock.sentinel.scopes) |
| 59 | assert instance.client_config == CLIENT_SECRETS_INFO['web'] |
| 60 | assert (instance.oauth2session.client_id == |
| 61 | CLIENT_SECRETS_INFO['web']['client_id']) |
| 62 | assert instance.oauth2session.scope == mock.sentinel.scopes |
| 63 | |
| 64 | |
| 65 | @pytest.fixture |
| 66 | def instance(): |
| 67 | yield flow.Flow(CLIENT_SECRETS_INFO, scopes=mock.sentinel.scopes) |
| 68 | |
| 69 | |
| 70 | def test_redirect_uri(instance): |
| 71 | instance.redirect_uri = mock.sentinel.redirect_uri |
| 72 | assert (instance.redirect_uri == |
| 73 | instance.oauth2session.redirect_uri == |
| 74 | mock.sentinel.redirect_uri) |
| 75 | |
| 76 | |
| 77 | def test_authorization_url(instance): |
| 78 | scope = 'scope_one' |
| 79 | instance.oauth2session.scope = [scope] |
| 80 | authorization_url_patch = mock.patch.object( |
| 81 | instance.oauth2session, 'authorization_url', |
| 82 | wraps=instance.oauth2session.authorization_url) |
| 83 | |
| 84 | with authorization_url_patch as authorization_url_spy: |
| 85 | url, _ = instance.authorization_url(prompt='consent') |
| 86 | |
| 87 | assert CLIENT_SECRETS_INFO['web']['auth_uri'] in url |
| 88 | assert scope in url |
| 89 | authorization_url_spy.assert_called_with( |
| 90 | CLIENT_SECRETS_INFO['web']['auth_uri'], |
| 91 | access_type='offline', |
| 92 | prompt='consent') |
| 93 | |
| 94 | |
| 95 | def test_fetch_token(instance): |
| 96 | fetch_token_patch = mock.patch.object( |
| 97 | instance.oauth2session, 'fetch_token', autospec=True, |
| 98 | return_value=mock.sentinel.token) |
| 99 | |
| 100 | with fetch_token_patch as fetch_token_mock: |
| 101 | token = instance.fetch_token(code=mock.sentinel.code) |
| 102 | |
| 103 | assert token == mock.sentinel.token |
| 104 | fetch_token_mock.assert_called_with( |
| 105 | CLIENT_SECRETS_INFO['web']['token_uri'], |
| 106 | client_secret=CLIENT_SECRETS_INFO['web']['client_secret'], |
| 107 | code=mock.sentinel.code) |
| 108 | |
| 109 | |
| 110 | def test_credentials(instance): |
| 111 | instance.oauth2session.token = { |
| 112 | 'access_token': mock.sentinel.access_token, |
| 113 | 'refresh_token': mock.sentinel.refresh_token |
| 114 | } |
| 115 | |
| 116 | credentials = instance.credentials |
| 117 | |
| 118 | assert credentials.token == mock.sentinel.access_token |
| 119 | assert credentials._refresh_token == mock.sentinel.refresh_token |
| 120 | assert credentials._client_id == CLIENT_SECRETS_INFO['web']['client_id'] |
| 121 | assert (credentials._client_secret == |
| 122 | CLIENT_SECRETS_INFO['web']['client_secret']) |
| 123 | assert credentials._token_uri == CLIENT_SECRETS_INFO['web']['token_uri'] |
| 124 | |
| 125 | |
| 126 | def test_bad_credentials(instance): |
| 127 | with pytest.raises(ValueError): |
| 128 | assert instance.credentials |
| 129 | |
| 130 | |
| 131 | def test_authorized_session(instance): |
| 132 | instance.oauth2session.token = { |
| 133 | 'access_token': mock.sentinel.access_token, |
| 134 | 'refresh_token': mock.sentinel.refresh_token |
| 135 | } |
| 136 | |
| 137 | session = instance.authorized_session() |
| 138 | |
| 139 | assert session.credentials.token == mock.sentinel.access_token |