blob: e5d108fd52823d21009707f7268400cfd532e82c [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
Jon Wayne Parrott4382bc12017-01-10 13:35:51 -080030def test_from_client_secrets_file():
31 instance = flow.Flow.from_client_secrets_file(
32 CLIENT_SECRETS_FILE, scopes=mock.sentinel.scopes)
33 assert instance.client_config == CLIENT_SECRETS_INFO['web']
34 assert (instance.oauth2session.client_id ==
35 CLIENT_SECRETS_INFO['web']['client_id'])
36 assert instance.oauth2session.scope == mock.sentinel.scopes
37
38
Jon Wayne Parrott3ff4d552017-02-08 14:43:38 -080039def test_from_client_config_installed():
40 client_config = {'installed': CLIENT_SECRETS_INFO['web']}
41 instance = flow.Flow.from_client_config(
42 client_config, scopes=mock.sentinel.scopes)
43 assert instance.client_config == client_config['installed']
44 assert (instance.oauth2session.client_id ==
45 client_config['installed']['client_id'])
46 assert instance.oauth2session.scope == mock.sentinel.scopes
47
48
49def test_from_client_config_bad_format():
50 with pytest.raises(ValueError):
51 flow.Flow.from_client_config({}, scopes=mock.sentinel.scopes)
52
53
Jon Wayne Parrott4382bc12017-01-10 13:35:51 -080054@pytest.fixture
55def instance():
Jon Wayne Parrott3ff4d552017-02-08 14:43:38 -080056 yield flow.Flow.from_client_config(
57 CLIENT_SECRETS_INFO, scopes=mock.sentinel.scopes)
Jon Wayne Parrott4382bc12017-01-10 13:35:51 -080058
59
60def test_redirect_uri(instance):
61 instance.redirect_uri = mock.sentinel.redirect_uri
62 assert (instance.redirect_uri ==
63 instance.oauth2session.redirect_uri ==
64 mock.sentinel.redirect_uri)
65
66
67def test_authorization_url(instance):
68 scope = 'scope_one'
69 instance.oauth2session.scope = [scope]
70 authorization_url_patch = mock.patch.object(
71 instance.oauth2session, 'authorization_url',
72 wraps=instance.oauth2session.authorization_url)
73
74 with authorization_url_patch as authorization_url_spy:
75 url, _ = instance.authorization_url(prompt='consent')
76
77 assert CLIENT_SECRETS_INFO['web']['auth_uri'] in url
78 assert scope in url
79 authorization_url_spy.assert_called_with(
80 CLIENT_SECRETS_INFO['web']['auth_uri'],
81 access_type='offline',
82 prompt='consent')
83
84
85def test_fetch_token(instance):
86 fetch_token_patch = mock.patch.object(
87 instance.oauth2session, 'fetch_token', autospec=True,
88 return_value=mock.sentinel.token)
89
90 with fetch_token_patch as fetch_token_mock:
91 token = instance.fetch_token(code=mock.sentinel.code)
92
93 assert token == mock.sentinel.token
94 fetch_token_mock.assert_called_with(
95 CLIENT_SECRETS_INFO['web']['token_uri'],
96 client_secret=CLIENT_SECRETS_INFO['web']['client_secret'],
97 code=mock.sentinel.code)
98
99
100def test_credentials(instance):
101 instance.oauth2session.token = {
102 'access_token': mock.sentinel.access_token,
103 'refresh_token': mock.sentinel.refresh_token
104 }
105
106 credentials = instance.credentials
107
108 assert credentials.token == mock.sentinel.access_token
109 assert credentials._refresh_token == mock.sentinel.refresh_token
110 assert credentials._client_id == CLIENT_SECRETS_INFO['web']['client_id']
111 assert (credentials._client_secret ==
112 CLIENT_SECRETS_INFO['web']['client_secret'])
113 assert credentials._token_uri == CLIENT_SECRETS_INFO['web']['token_uri']
114
115
Jon Wayne Parrott4382bc12017-01-10 13:35:51 -0800116def test_authorized_session(instance):
117 instance.oauth2session.token = {
118 'access_token': mock.sentinel.access_token,
119 'refresh_token': mock.sentinel.refresh_token
120 }
121
122 session = instance.authorized_session()
123
124 assert session.credentials.token == mock.sentinel.access_token