ordigi/elodie/tests/plugins/googlephotos/googlephotos_test.py
Jaisen Mathai 12c17c9cac Add a plugin to upload photos to Google Photos (#319)
Fixes #315.

This PR aims to address the [recent changes](https://www.blog.google/products/photos/simplifying-google-photos-and-google-drive/) in Google Photos + Google Drive where syncing between the two is no longer supported.

It works by uploading photos as part of the import process to add a copy of every photo in your library to Google Photos. Google Drive is not required for this plugin to work.

This plugin lets you have all your photos in Google Photos without relying on Google Drive. You can use another cloud storage service like iCloud or Dropbox or no cloud storage at all.

- [x] Add tests for `after()` plugin methods.
- [x] Add support for storage/async support.
- [x] Include plugins into code coverage.
- [x] Sweep code and clean up and add comments.
2019-07-12 01:44:57 -07:00

186 lines
5.9 KiB
Python

from __future__ import absolute_import
# Project imports
import mock
import os
import sys
from tempfile import gettempdir
sys.path.insert(0, os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))))
sys.path.insert(0, os.path.abspath(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))
import helper
from elodie.config import load_config
from elodie.plugins.googlephotos.googlephotos import GooglePhotos
from elodie.media.audio import Audio
from elodie.media.photo import Photo
# Globals to simplify mocking configs
auth_file = helper.get_file('plugins/googlephotos/auth_file.json')
secrets_file = helper.get_file('plugins/googlephotos/secrets_file.json')
config_string = """
[Plugins]
plugins=GooglePhotos
[PluginGooglePhotos]
auth_file={}
secrets_file={}
"""
config_string_fmt = config_string.format(
auth_file,
secrets_file
)
sample_photo = Photo(helper.get_file('plain.jpg'))
sample_metadata = sample_photo.get_metadata()
sample_metadata['original_name'] = 'foobar'
@mock.patch('elodie.config.config_file', '%s/config.ini-googlephotos-set-session' % gettempdir())
def test_googlephotos_set_session():
with open('%s/config.ini-googlephotos-set-session' % gettempdir(), 'w') as f:
f.write(config_string_fmt)
if hasattr(load_config, 'config'):
del load_config.config
gp = GooglePhotos()
if hasattr(load_config, 'config'):
del load_config.config
assert gp.session is None, gp.session
gp.set_session()
assert gp.session is not None, gp.session
@mock.patch('elodie.config.config_file', '%s/config.ini-googlephotos-after-supported' % gettempdir())
def test_googlephotos_after_supported():
with open('%s/config.ini-googlephotos-after-supported' % gettempdir(), 'w') as f:
f.write(config_string_fmt)
if hasattr(load_config, 'config'):
del load_config.config
final_file_path = helper.get_file('plain.jpg')
gp = GooglePhotos()
gp.after('', '', final_file_path, sample_metadata)
db_row = gp.db.get(final_file_path)
if hasattr(load_config, 'config'):
del load_config.config
assert db_row == 'foobar', db_row
@mock.patch('elodie.config.config_file', '%s/config.ini-googlephotos-after-unsupported' % gettempdir())
def test_googlephotos_after_unsupported():
with open('%s/config.ini-googlephotos-after-unsupported' % gettempdir(), 'w') as f:
f.write(config_string_fmt)
if hasattr(load_config, 'config'):
del load_config.config
final_file_path = helper.get_file('audio.m4a')
sample_photo = Audio(final_file_path)
sample_metadata = sample_photo.get_metadata()
sample_metadata['original_name'] = 'foobar'
gp = GooglePhotos()
gp.after('', '', final_file_path, sample_metadata)
db_row = gp.db.get(final_file_path)
if hasattr(load_config, 'config'):
del load_config.config
assert db_row == None, db_row
@mock.patch('elodie.config.config_file', '%s/config.ini-googlephotos-upload' % gettempdir())
def test_googlephotos_upload():
with open('%s/config.ini-googlephotos-upload' % gettempdir(), 'w') as f:
f.write(config_string_fmt)
if hasattr(load_config, 'config'):
del load_config.config
gp = GooglePhotos()
if hasattr(load_config, 'config'):
del load_config.config
gp.set_session()
status = gp.upload(helper.get_file('plain.jpg'))
assert status is not None, status
@mock.patch('elodie.config.config_file', '%s/config.ini-googlephotos-upload-session-fail' % gettempdir())
def test_googlephotos_upload_session_fail():
with open('%s/config.ini-googlephotos-upload-session-fail' % gettempdir(), 'w') as f:
f.write(config_string)
if hasattr(load_config, 'config'):
del load_config.config
gp = GooglePhotos()
if hasattr(load_config, 'config'):
del load_config.config
gp.set_session()
status = gp.upload(helper.get_file('plain.jpg'))
assert status is None, status
@mock.patch('elodie.config.config_file', '%s/config.ini-googlephotos-upload-invalid-empty' % gettempdir())
def test_googlephotos_upload_invalid_empty():
with open('%s/config.ini-googlephotos-upload-invalid-empty' % gettempdir(), 'w') as f:
f.write(config_string_fmt)
if hasattr(load_config, 'config'):
del load_config.config
gp = GooglePhotos()
if hasattr(load_config, 'config'):
del load_config.config
gp.set_session()
status = gp.upload(helper.get_file('invalid.jpg'))
assert status is None, status
@mock.patch('elodie.config.config_file', '%s/config.ini-googlephotos-upload-dne' % gettempdir())
def test_googlephotos_upload_dne():
with open('%s/config.ini-googlephotos-upload-dne' % gettempdir(), 'w') as f:
f.write(config_string_fmt)
if hasattr(load_config, 'config'):
del load_config.config
gp = GooglePhotos()
if hasattr(load_config, 'config'):
del load_config.config
gp.set_session()
status = gp.upload('/file/does/not/exist')
assert status is None, status
@mock.patch('elodie.config.config_file', '%s/config.ini-googlephotos-batch' % gettempdir())
def test_googlephotos_batch():
with open('%s/config.ini-googlephotos-batch' % gettempdir(), 'w') as f:
f.write(config_string_fmt)
if hasattr(load_config, 'config'):
del load_config.config
final_file_path = helper.get_file('plain.jpg')
gp = GooglePhotos()
gp.after('', '', final_file_path, sample_metadata)
db_row = gp.db.get(final_file_path)
assert db_row == 'foobar', db_row
status, count = gp.batch()
db_row_after = gp.db.get(final_file_path)
assert status == True, status
assert count == 1, count
assert db_row_after is None, db_row_after
if hasattr(load_config, 'config'):
del load_config.config
gp.set_session()
status = gp.upload(helper.get_file('invalid.jpg'))
assert status is None, status