blob: a16c9044cb13b7e768db1c7f0fa3b764cca9ecad [file] [log] [blame]
Jon Wayne Parrott3ff4d552017-02-08 14:43:38 -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 oauthlib
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_session_from_client_config_web():
31 session, config = oauthlib.session_from_client_config(
32 CLIENT_SECRETS_INFO, scopes=mock.sentinel.scopes)
33
34 assert config == CLIENT_SECRETS_INFO
35 assert session.client_id == CLIENT_SECRETS_INFO['web']['client_id']
36 assert session.scope == mock.sentinel.scopes
37
38
39def test_session_from_client_config_installed():
40 info = {'installed': CLIENT_SECRETS_INFO['web']}
41 session, config = oauthlib.session_from_client_config(
42 info, scopes=mock.sentinel.scopes)
43 assert config == info
44 assert session.client_id == info['installed']['client_id']
45 assert session.scope == mock.sentinel.scopes
46
47
48def test_session_from_client_config_bad_format():
49 with pytest.raises(ValueError):
50 oauthlib.session_from_client_config({}, scopes=[])
51
52
53def test_session_from_client_config_missing_keys():
54 with pytest.raises(ValueError):
55 oauthlib.session_from_client_config({'web': {}}, scopes=[])
56
57
58def test_session_from_client_secrets_file():
59 session, config = oauthlib.session_from_client_secrets_file(
60 CLIENT_SECRETS_FILE, scopes=mock.sentinel.scopes)
61 assert config == CLIENT_SECRETS_INFO
62 assert session.client_id == CLIENT_SECRETS_INFO['web']['client_id']
63 assert session.scope == mock.sentinel.scopes
64
65
66@pytest.fixture
67def session():
68 session, _ = oauthlib.session_from_client_config(
69 CLIENT_SECRETS_INFO, scopes=mock.sentinel.scopes)
70 yield session
71
72
73def test_credentials_from_session(session):
74 session.token = {
75 'access_token': mock.sentinel.access_token,
76 'refresh_token': mock.sentinel.refresh_token
77 }
78
79 credentials = oauthlib.credentials_from_session(
80 session, CLIENT_SECRETS_INFO['web'])
81
82 assert credentials.token == mock.sentinel.access_token
83 assert credentials._refresh_token == mock.sentinel.refresh_token
84 assert credentials._client_id == CLIENT_SECRETS_INFO['web']['client_id']
85 assert (credentials._client_secret ==
86 CLIENT_SECRETS_INFO['web']['client_secret'])
87 assert credentials._token_uri == CLIENT_SECRETS_INFO['web']['token_uri']
88
89
90def test_bad_credentials(session):
91 with pytest.raises(ValueError):
92 oauthlib.credentials_from_session(session)