Skip to content
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
51 changes: 17 additions & 34 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,28 @@ env:
- PATH="$PYENV_ROOT/bin:$PATH"
- PIP_CACHE_DIR="$HOME/.cache/pip" # unify pip cache location for all platforms
stages:
# - check
# - test
- test
- build
# Travis doesn't support testing python on Mac yet, so we need
# to workaround it by installing it directly with brew.
#
# Note that we specify language as ruby-2.4.3 to help us satisfy
# fpm dependencies, as travis runs el capitan with an old ruby
# version, that causes fpm to crash.
#
# Each job will use the previous job's `stage` tag if not specified.
jobs:
include:
# patch formatting checks
# - name: "Check patch formatting"
# stage: check
# os: linux
# language: python
# python: 3.7
# before_install:
# install:
# script: ./scripts/ci/check_patch.sh
# test jobs
# - name: "3.7 on Windows"
# stage: test
# os: windows # Windows 10.0.17134 N/A Build 17134
# language: shell # 'language: python' is an error on Travis CI Windows
# - name: "3.5 on Linux"
# language: python
# python: 3.5
# - name: "3.6 on Linux"
# language: python
# python: 3.6
# - name: "3.7 on Linux"
# language: python
# python: 3.7
# - name: "3.7 on Mac"
# os: osx
# osx_image: xcode11.3
- name: "3.7 on Windows"
stage: test
os: windows
language: shell
- name: "3.5 on Linux"
language: python
python: 3.5
- name: "3.6 on Linux"
language: python
python: 3.6
- name: "3.7 on Linux"
language: python
python: 3.7
- name: "3.7 on Mac"
os: osx
osx_image: xcode11.3
# build jobs
- name: "Pypi pkgs"
if: tag is present
Expand Down
20 changes: 8 additions & 12 deletions pydrive2/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def _decorated(self, *args, **kwargs):
self.auth = GoogleAuth()
# Re-create access token if it expired.
if self.auth.access_token_expired:
if getattr(self, 'auth_method', False) == 'service':
if getattr(self.auth, 'auth_method', False) == 'service':
self.auth.ServiceAuth()
else:
self.auth.LocalWebserverAuth()
Expand Down Expand Up @@ -93,14 +93,9 @@ def _decorated(self, *args, **kwargs):
decoratee(self, *args, **kwargs)
self.Authorize()
dirty = True
else:
if self.access_token_expired:
if self.credentials.refresh_token is not None:
self.Refresh()
else:
decoratee(self, *args, **kwargs)
self.Authorize()
dirty = True
elif self.access_token_expired:
self.Refresh()
dirty = True
if dirty and save_credentials:
self.SaveCredentials()
return _decorated
Expand Down Expand Up @@ -472,7 +467,7 @@ def Refresh(self):
"""
if self.credentials is None:
raise RefreshError('No credential to refresh.')
if self.credentials.refresh_token is None:
if self.credentials.refresh_token is None and self.auth_method != 'service':
raise RefreshError('No refresh_token found.'
'Please set access_type of OAuth to offline.')
if self.http is None:
Expand Down Expand Up @@ -521,10 +516,11 @@ def Authorize(self):

:raises: AuthenticationError
"""
if self.http is None:
self.http = httplib2.Http(timeout=self.http_timeout)
if self.access_token_expired:
raise AuthenticationError('No valid credentials provided to authorize')

if self.http is None:
self.http = httplib2.Http(timeout=self.http_timeout)
self.http = self.credentials.authorize(self.http)
self.service = build('drive', 'v2', http=self.http, cache_discovery=False)

Expand Down
2 changes: 1 addition & 1 deletion pydrive2/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ def _DeletePermission(self, permission_id):
if 'permissions' in self and 'permissions' in self.metadata:
permissions = self['permissions']
is_not_current_permission = lambda per: per['id'] == permission_id
permissions = filter(is_not_current_permission, permissions)
permissions = list(filter(is_not_current_permission, permissions))
self['permissions'] = permissions
self.metadata['permissions'] = permissions
return True
Expand Down
13 changes: 8 additions & 5 deletions pydrive2/test/settings/default.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
client_config_backend: 'file'
client_config_file: 'configs/client_secrets.json'
client_config_backend: file
service_config:
client_service_email: your-service-account-email
client_pkcs12_file_path: your-file-path.p12
client_user_email:

save_credentials: True
save_credentials_backend: 'file'
save_credentials_file: 'credentials/1.dat'
save_credentials_backend: file
save_credentials_file: credentials/default.dat

oauth_scope:
- 'https://www.googleapis.com/auth/drive'
- https://www.googleapis.com/auth/drive
9 changes: 9 additions & 0 deletions pydrive2/test/settings/test_oauth_test_01.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
client_config_backend: 'file'
client_config_file: 'configs/client_secrets.json'

save_credentials: True
save_credentials_backend: 'file'
save_credentials_file: 'credentials/1.dat'

oauth_scope:
- 'https://www.googleapis.com/auth/drive'
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ service_config:

save_credentials: True
save_credentials_backend: 'file'
save_credentials_file: 'credentials/5.dat'
save_credentials_file: 'credentials/6.dat'

oauth_scope:
- "https://www.googleapis.com/auth/drive"
22 changes: 13 additions & 9 deletions pydrive2/test/test_apiattr.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
import unittest
import pytest

from pydrive2.auth import GoogleAuth
from pydrive2.drive import GoogleDrive
from pydrive2.test.test_util import pydrive_retry, setup_credentials


class ApiAttributeTest(unittest.TestCase):
"""Test ApiAttr functions.
"""
ga = GoogleAuth('settings/test1.yaml')
ga.LocalWebserverAuth()
first_file = 'a.png'
second_file = 'b.png'

@classmethod
def setup_class(cls):
setup_credentials()

def test_UpdateMetadataNotInfinitelyNesting(self):
# Verify 'metadata' field present.
self.assertTrue(self.file1.metadata is not None)
self.file1.UpdateMetadata()
self.file1.UpdateMetadata()
pydrive_retry(self.file1.UpdateMetadata)

# Verify 'metadata' field still present.
self.assertTrue(self.file1.metadata is not None)
# Ensure no 'metadata' field in 'metadata' (i.e. nested).
self.assertTrue('metadata' not in self.file1.metadata)

def setUp(self):
self.drive = GoogleDrive(self.ga)
ga = GoogleAuth('pydrive2/test/settings/default.yaml')
ga.ServiceAuth()
self.drive = GoogleDrive(ga)
self.file1 = self.drive.CreateFile()
self.file1.Upload()
pydrive_retry(self.file1.Upload)

def tearDown(self):
self.file1.Delete()
pydrive_retry(self.file1.Delete)

if __name__ == '__main__':
unittest.main()
13 changes: 10 additions & 3 deletions pydrive2/test/test_drive.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
# -*- coding: utf-8 -*-
import unittest
import pytest

from pydrive2.auth import GoogleAuth
from pydrive2.drive import GoogleDrive
from pydrive2.test.test_util import pydrive_retry, setup_credentials


class GoogleDriveTest(unittest.TestCase):
"""Tests basic operations on meta-data information of the linked Google Drive account.
"""

ga = GoogleAuth('settings/test1.yaml')
ga.LocalWebserverAuth()
@classmethod
def setup_class(cls):
setup_credentials()

cls.ga = GoogleAuth('pydrive2/test/settings/default.yaml')
cls.ga.ServiceAuth()


def test_01_About_Request(self):
drive = GoogleDrive(self.ga)

about_object = drive.GetAbout()
about_object = pydrive_retry(drive.GetAbout)
self.assertTrue(about_object is not None, "About object not loading.")


Expand Down
Loading