Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Don't write to the fs on module import. #11

Merged
merged 1 commit into from
Feb 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[flake8]
ignore = E203, E266, E501, W503
exclude =
# Standard linting exemptions.
__pycache__,
.git,
*.pyc,
conf.py
1 change: 1 addition & 0 deletions ci/requirements-2.7-0.19.2.pip
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ google-auth-oauthlib
PyCrypto
mock
google-cloud-bigquery
pyfakefs
pytest
pytest-cov
codecov
Expand Down
1 change: 1 addition & 0 deletions ci/requirements-3.5-0.18.1.pip
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
google-auth==1.4.1
google-auth-oauthlib==0.0.1
google-cloud-bigquery==0.32.0
pyfakefs
pytest
pytest-cov
codecov
Expand Down
1 change: 1 addition & 0 deletions ci/requirements-3.6-0.20.1.conda
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
google-auth
google-auth-oauthlib
google-cloud-bigquery==0.32.0
pyfakefs
pytest
pytest-cov
codecov
Expand Down
1 change: 1 addition & 0 deletions ci/requirements-3.6-MASTER.pip
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ google-auth-oauthlib
git+https://github.com/GoogleCloudPlatform/google-cloud-python.git#egg=version_subpkg&subdirectory=api_core
git+https://github.com/GoogleCloudPlatform/google-cloud-python.git#egg=version_subpkg&subdirectory=core
git+https://github.com/GoogleCloudPlatform/google-cloud-python.git#egg=version_subpkg&subdirectory=bigquery
pyfakefs
pytest
pytest-cov
codecov
Expand Down
1 change: 0 additions & 1 deletion pydata_google_auth/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

from .auth import default
from .auth import get_user_credentials
from ._version import get_versions
Expand Down
1 change: 0 additions & 1 deletion pydata_google_auth/_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# This file helps to compute a version number in source trees obtained from
# git-archive tarball (such as those provided by githubs download-from-tag
# feature). Distribution tarballs (built by setup.py sdist) and build
Expand Down
2 changes: 1 addition & 1 deletion pydata_google_auth/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
logger = logging.getLogger(__name__)

CLIENT_ID = (
"262006177488-3425ks60hkk80fssi9vpohv88g6q1iqd" ".apps.googleusercontent.com"
"262006177488-3425ks60hkk80fssi9vpohv88g6q1iqd.apps.googleusercontent.com"
)
CLIENT_SECRET = "JSF-iczmzEgbTR-XK-2xaWAc"

Expand Down
19 changes: 13 additions & 6 deletions pydata_google_auth/cache.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Caching implementations for reading and writing user credentials."""

import errno
import json
import logging
import os
Expand Down Expand Up @@ -30,12 +31,6 @@ def _get_default_credentials_path(credentials_dirname, credentials_filename):
config_path = os.path.join(os.path.expanduser("~"), ".config")

config_path = os.path.join(config_path, credentials_dirname)

# Create a pydata directory in an application-specific hidden
# user folder on the operating system.
if not os.path.exists(config_path):
os.makedirs(config_path)

return os.path.join(config_path, credentials_filename)


Expand Down Expand Up @@ -95,6 +90,18 @@ def _save_user_account_credentials(credentials, credentials_path):
"""
Saves user account credentials to a local file.
"""

# Create the direcory if it doesn't exist.
# https://stackoverflow.com/a/12517490/101923
config_dir = os.path.dirname(credentials_path)
if not os.path.exists(config_dir):
try:
os.makedirs(config_dir)
except OSError as exc: # Guard against race condition.
if exc.errno != errno.EEXIST:
logger.warning("Unable to create credentials directory.")
return

try:
with open(credentials_path, "w") as credentials_file:
credentials_json = {
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ google-cloud-bigquery
nox-automation
pandas
pytest
pyfakefs
setuptools
56 changes: 56 additions & 0 deletions tests/unit/test_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""Test module for pydata_google_auth.cache"""

import json
import os
import os.path

import pytest

import google.oauth2.credentials
from six.moves import reload_module


@pytest.fixture
def module_under_test():
from pydata_google_auth import cache

return cache


def test_import_unwriteable_fs(module_under_test, monkeypatch):
"""Test import with an unwritable filesystem.

See: https://github.com/pydata/pydata-google-auth/issues/10
"""

def raise_unwriteable(path):
raise PermissionError()

monkeypatch.setattr(os.path, "exists", lambda _: False)
monkeypatch.setattr(os, "makedirs", raise_unwriteable)

reload_module(module_under_test)

assert module_under_test.NOOP is not None


def test__save_user_account_credentials_wo_directory(module_under_test, fs):
"""Directories should be created if they don't exist."""

credentials = google.oauth2.credentials.Credentials(
token="access_token",
refresh_token="refresh_token",
id_token="id_token",
token_uri="token_uri",
client_id="client_id",
client_secret="client_secret",
scopes=["scopes"],
)
path = "/home/username/.config/pydata/pydata_google_credentials.json"
assert not os.path.exists("/home/username/.config/pydata/")

module_under_test._save_user_account_credentials(credentials, path)

with open(path) as fp:
serialized_data = json.load(fp)
assert serialized_data["refresh_token"] == "refresh_token"
1 change: 0 additions & 1 deletion versioneer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# Version: 0.18

"""The Versioneer - like a rocketeer, but for versions.
Expand Down