Skip to content
This repository has been archived by the owner on Jan 18, 2025. It is now read-only.

Commit

Permalink
Warn user if storage file is missing
Browse files Browse the repository at this point in the history
Also warn user if the given filename is a directory.
  • Loading branch information
pferate committed Jul 29, 2016
1 parent ae73312 commit a532c78
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
18 changes: 17 additions & 1 deletion oauth2client/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,28 @@

import os
import threading
import warnings

from oauth2client import client


__author__ = 'jcgregorio@google.com (Joe Gregorio)'

_SYM_LINK_MESSAGE = 'File: {0}: Is a symbolic link.'

_IS_DIR_MESSAGE = '{0}: Is a directory'

_MISSING_FILE_MESSAGE = 'Cannot access {0}: No such file or directory'


class CredentialsFileSymbolicLinkError(Exception):
"""Credentials files must not be symbolic links."""


class CredentialsFileDirectoryError(Exception):
"""Credentials files must regular files, not directories."""


class Storage(client.Storage):
"""Store and retrieve a single credential to and from a file."""

Expand All @@ -41,7 +52,12 @@ def __init__(self, filename):
def _validate_file(self):
if os.path.islink(self._filename):
raise CredentialsFileSymbolicLinkError(
'File: {0} is a symbolic link.'.format(self._filename))
_SYM_LINK_MESSAGE.format(self._filename))
elif os.path.isdir(self._filename):
raise CredentialsFileDirectoryError(
_IS_DIR_MESSAGE.format(self._filename))
elif not os.path.isfile(self._filename):
warnings.warn(_MISSING_FILE_MESSAGE.format(self._filename))

def locked_get(self):
"""Retrieve Credential from file.
Expand Down
17 changes: 16 additions & 1 deletion tests/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import pickle
import stat
import tempfile
import warnings

import mock
import six
from six.moves import http_client
import unittest2
Expand Down Expand Up @@ -54,6 +56,7 @@ def tearDown(self):
pass

def setUp(self):
warnings.simplefilter("ignore")
try:
os.unlink(FILENAME)
except OSError:
Expand All @@ -74,11 +77,23 @@ def _create_test_credentials(self, client_id='some_client_id',
user_agent)
return credentials

def test_non_existent_file_storage(self):
@mock.patch('warnings.warn')
def test_non_existent_file_storage(self, warn_mock):
s = file.Storage(FILENAME)
credentials = s.get()
warn_mock.assert_called_with(
file._MISSING_FILE_MESSAGE.format(FILENAME))
self.assertEquals(None, credentials)

def test_directory_file_storage(self):
s = file.Storage(FILENAME)
os.mkdir(FILENAME)
try:
with self.assertRaises(file.CredentialsFileDirectoryError):
s.get()
finally:
os.rmdir(FILENAME)

@unittest2.skipIf(not hasattr(os, 'symlink'), 'No symlink available')
def test_no_sym_link_credentials(self):
SYMFILENAME = FILENAME + '.sym'
Expand Down

0 comments on commit a532c78

Please sign in to comment.