blob: 7fc268ccb9a7a7a443dee8c0a2101536f6c00849 [file] [log] [blame]
Jon Wayne Parrott4382bc12017-01-10 13:35:51 -08001# 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
15import json
16import os
17
18import mock
19import pytest
20
21from google.oauth2 import flow
22
23DATA_DIR = os.path.join(os.path.dirname(__file__), '..', 'data')
24CLIENT_SECRETS_FILE = os.path.join(DATA_DIR, 'client_secrets.json')
25
26with open(CLIENT_SECRETS_FILE, 'r') as fh:
27 CLIENT_SECRETS_INFO = json.load(fh)
28
29
30def 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
38def 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
46def test_constructor_bad_format():
47 with pytest.raises(ValueError):
48 flow.Flow({}, scopes=[])
49
50
51def test_constructor_missing_keys():
52 with pytest.raises(ValueError):
53 flow.Flow({'web': {}}, scopes=[])
54
55
56def 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
66def instance():
67 yield flow.Flow(CLIENT_SECRETS_INFO, scopes=mock.sentinel.scopes)
68
69
70def 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
77def 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
95def 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
110def 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
126def test_bad_credentials(instance):
127 with pytest.raises(ValueError):
128 assert instance.credentials
129
130
131def 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