Refactor common code from google.oauth2.flow to google.oauth2.oauthlib (#106)
diff --git a/tests/oauth2/test_flow.py b/tests/oauth2/test_flow.py
index 7fc268c..e5d108f 100644
--- a/tests/oauth2/test_flow.py
+++ b/tests/oauth2/test_flow.py
@@ -27,32 +27,6 @@
CLIENT_SECRETS_INFO = json.load(fh)
-def test_constructor_web():
- instance = flow.Flow(CLIENT_SECRETS_INFO, scopes=mock.sentinel.scopes)
- assert instance.client_config == CLIENT_SECRETS_INFO['web']
- assert (instance.oauth2session.client_id ==
- CLIENT_SECRETS_INFO['web']['client_id'])
- assert instance.oauth2session.scope == mock.sentinel.scopes
-
-
-def test_constructor_installed():
- info = {'installed': CLIENT_SECRETS_INFO['web']}
- instance = flow.Flow(info, scopes=mock.sentinel.scopes)
- assert instance.client_config == info['installed']
- assert instance.oauth2session.client_id == info['installed']['client_id']
- assert instance.oauth2session.scope == mock.sentinel.scopes
-
-
-def test_constructor_bad_format():
- with pytest.raises(ValueError):
- flow.Flow({}, scopes=[])
-
-
-def test_constructor_missing_keys():
- with pytest.raises(ValueError):
- flow.Flow({'web': {}}, scopes=[])
-
-
def test_from_client_secrets_file():
instance = flow.Flow.from_client_secrets_file(
CLIENT_SECRETS_FILE, scopes=mock.sentinel.scopes)
@@ -62,9 +36,25 @@
assert instance.oauth2session.scope == mock.sentinel.scopes
+def test_from_client_config_installed():
+ client_config = {'installed': CLIENT_SECRETS_INFO['web']}
+ instance = flow.Flow.from_client_config(
+ client_config, scopes=mock.sentinel.scopes)
+ assert instance.client_config == client_config['installed']
+ assert (instance.oauth2session.client_id ==
+ client_config['installed']['client_id'])
+ assert instance.oauth2session.scope == mock.sentinel.scopes
+
+
+def test_from_client_config_bad_format():
+ with pytest.raises(ValueError):
+ flow.Flow.from_client_config({}, scopes=mock.sentinel.scopes)
+
+
@pytest.fixture
def instance():
- yield flow.Flow(CLIENT_SECRETS_INFO, scopes=mock.sentinel.scopes)
+ yield flow.Flow.from_client_config(
+ CLIENT_SECRETS_INFO, scopes=mock.sentinel.scopes)
def test_redirect_uri(instance):
@@ -123,11 +113,6 @@
assert credentials._token_uri == CLIENT_SECRETS_INFO['web']['token_uri']
-def test_bad_credentials(instance):
- with pytest.raises(ValueError):
- assert instance.credentials
-
-
def test_authorized_session(instance):
instance.oauth2session.token = {
'access_token': mock.sentinel.access_token,
diff --git a/tests/oauth2/test_oauthlib.py b/tests/oauth2/test_oauthlib.py
new file mode 100644
index 0000000..a16c904
--- /dev/null
+++ b/tests/oauth2/test_oauthlib.py
@@ -0,0 +1,92 @@
+# Copyright 2017 Google Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import json
+import os
+
+import mock
+import pytest
+
+from google.oauth2 import oauthlib
+
+DATA_DIR = os.path.join(os.path.dirname(__file__), '..', 'data')
+CLIENT_SECRETS_FILE = os.path.join(DATA_DIR, 'client_secrets.json')
+
+with open(CLIENT_SECRETS_FILE, 'r') as fh:
+ CLIENT_SECRETS_INFO = json.load(fh)
+
+
+def test_session_from_client_config_web():
+ session, config = oauthlib.session_from_client_config(
+ CLIENT_SECRETS_INFO, scopes=mock.sentinel.scopes)
+
+ assert config == CLIENT_SECRETS_INFO
+ assert session.client_id == CLIENT_SECRETS_INFO['web']['client_id']
+ assert session.scope == mock.sentinel.scopes
+
+
+def test_session_from_client_config_installed():
+ info = {'installed': CLIENT_SECRETS_INFO['web']}
+ session, config = oauthlib.session_from_client_config(
+ info, scopes=mock.sentinel.scopes)
+ assert config == info
+ assert session.client_id == info['installed']['client_id']
+ assert session.scope == mock.sentinel.scopes
+
+
+def test_session_from_client_config_bad_format():
+ with pytest.raises(ValueError):
+ oauthlib.session_from_client_config({}, scopes=[])
+
+
+def test_session_from_client_config_missing_keys():
+ with pytest.raises(ValueError):
+ oauthlib.session_from_client_config({'web': {}}, scopes=[])
+
+
+def test_session_from_client_secrets_file():
+ session, config = oauthlib.session_from_client_secrets_file(
+ CLIENT_SECRETS_FILE, scopes=mock.sentinel.scopes)
+ assert config == CLIENT_SECRETS_INFO
+ assert session.client_id == CLIENT_SECRETS_INFO['web']['client_id']
+ assert session.scope == mock.sentinel.scopes
+
+
+@pytest.fixture
+def session():
+ session, _ = oauthlib.session_from_client_config(
+ CLIENT_SECRETS_INFO, scopes=mock.sentinel.scopes)
+ yield session
+
+
+def test_credentials_from_session(session):
+ session.token = {
+ 'access_token': mock.sentinel.access_token,
+ 'refresh_token': mock.sentinel.refresh_token
+ }
+
+ credentials = oauthlib.credentials_from_session(
+ session, CLIENT_SECRETS_INFO['web'])
+
+ assert credentials.token == mock.sentinel.access_token
+ assert credentials._refresh_token == mock.sentinel.refresh_token
+ assert credentials._client_id == CLIENT_SECRETS_INFO['web']['client_id']
+ assert (credentials._client_secret ==
+ CLIENT_SECRETS_INFO['web']['client_secret'])
+ assert credentials._token_uri == CLIENT_SECRETS_INFO['web']['token_uri']
+
+
+def test_bad_credentials(session):
+ with pytest.raises(ValueError):
+ oauthlib.credentials_from_session(session)