1 | from __future__ import absolute_import |
||
2 | # Project imports |
||
3 | import mock |
||
4 | import os |
||
5 | import sys |
||
6 | from tempfile import gettempdir |
||
7 | |||
8 | sys.path.insert(0, os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))) |
||
9 | sys.path.insert(0, os.path.abspath(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))) |
||
10 | |||
11 | import helper |
||
12 | from elodie.config import load_config |
||
13 | from elodie.plugins.googlephotos.googlephotos import GooglePhotos |
||
14 | from elodie.media.audio import Audio |
||
15 | from elodie.media.photo import Photo |
||
16 | |||
17 | # Globals to simplify mocking configs |
||
18 | auth_file = helper.get_file('plugins/googlephotos/auth_file.json') |
||
19 | secrets_file = helper.get_file('plugins/googlephotos/secrets_file.json') |
||
20 | config_string = """ |
||
21 | [Plugins] |
||
22 | plugins=GooglePhotos |
||
23 | |||
24 | [PluginGooglePhotos] |
||
25 | auth_file={} |
||
26 | secrets_file={} |
||
27 | """ |
||
28 | config_string_fmt = config_string.format( |
||
29 | auth_file, |
||
30 | secrets_file |
||
31 | ) |
||
32 | |||
33 | sample_photo = Photo(helper.get_file('plain.jpg')) |
||
34 | sample_metadata = sample_photo.get_metadata() |
||
35 | sample_metadata['original_name'] = 'foobar' |
||
36 | |||
37 | @mock.patch('elodie.config.config_file', '%s/config.ini-googlephotos-set-session' % gettempdir()) |
||
38 | def test_googlephotos_set_session(): |
||
39 | with open('%s/config.ini-googlephotos-set-session' % gettempdir(), 'w') as f: |
||
40 | f.write(config_string_fmt) |
||
41 | if hasattr(load_config, 'config'): |
||
42 | del load_config.config |
||
43 | |||
44 | gp = GooglePhotos() |
||
45 | |||
46 | if hasattr(load_config, 'config'): |
||
47 | del load_config.config |
||
48 | |||
49 | assert gp.session is None, gp.session |
||
50 | gp.set_session() |
||
51 | assert gp.session is not None, gp.session |
||
52 | |||
53 | @mock.patch('elodie.config.config_file', '%s/config.ini-googlephotos-after-supported' % gettempdir()) |
||
54 | def test_googlephotos_after_supported(): |
||
55 | with open('%s/config.ini-googlephotos-after-supported' % gettempdir(), 'w') as f: |
||
56 | f.write(config_string_fmt) |
||
57 | if hasattr(load_config, 'config'): |
||
58 | del load_config.config |
||
59 | |||
60 | final_file_path = helper.get_file('plain.jpg') |
||
61 | gp = GooglePhotos() |
||
62 | gp.after('', '', final_file_path, sample_metadata) |
||
63 | db_row = gp.db.get(final_file_path) |
||
64 | |||
65 | if hasattr(load_config, 'config'): |
||
66 | del load_config.config |
||
67 | |||
68 | assert db_row == 'foobar', db_row |
||
69 | |||
70 | @mock.patch('elodie.config.config_file', '%s/config.ini-googlephotos-after-unsupported' % gettempdir()) |
||
71 | def test_googlephotos_after_unsupported(): |
||
72 | with open('%s/config.ini-googlephotos-after-unsupported' % gettempdir(), 'w') as f: |
||
73 | f.write(config_string_fmt) |
||
74 | if hasattr(load_config, 'config'): |
||
75 | del load_config.config |
||
76 | |||
77 | final_file_path = helper.get_file('audio.m4a') |
||
78 | sample_photo = Audio(final_file_path) |
||
79 | sample_metadata = sample_photo.get_metadata() |
||
80 | sample_metadata['original_name'] = 'foobar' |
||
81 | gp = GooglePhotos() |
||
82 | gp.after('', '', final_file_path, sample_metadata) |
||
83 | db_row = gp.db.get(final_file_path) |
||
84 | |||
85 | if hasattr(load_config, 'config'): |
||
86 | del load_config.config |
||
87 | |||
88 | assert db_row == None, db_row |
||
89 | |||
90 | View Code Duplication | @mock.patch('elodie.config.config_file', '%s/config.ini-googlephotos-upload' % gettempdir()) |
|
0 ignored issues
–
show
Duplication
introduced
by
Loading history...
|
|||
91 | def test_googlephotos_upload(): |
||
92 | with open('%s/config.ini-googlephotos-upload' % gettempdir(), 'w') as f: |
||
93 | f.write(config_string_fmt) |
||
94 | if hasattr(load_config, 'config'): |
||
95 | del load_config.config |
||
96 | |||
97 | gp = GooglePhotos() |
||
98 | |||
99 | if hasattr(load_config, 'config'): |
||
100 | del load_config.config |
||
101 | |||
102 | gp.set_session() |
||
103 | status = gp.upload(helper.get_file('plain.jpg')) |
||
104 | |||
105 | assert status is not None, status |
||
106 | |||
107 | @mock.patch('elodie.config.config_file', '%s/config.ini-googlephotos-upload-session-fail' % gettempdir()) |
||
108 | def test_googlephotos_upload_session_fail(): |
||
109 | with open('%s/config.ini-googlephotos-upload-session-fail' % gettempdir(), 'w') as f: |
||
110 | f.write(config_string) |
||
111 | if hasattr(load_config, 'config'): |
||
112 | del load_config.config |
||
113 | |||
114 | gp = GooglePhotos() |
||
115 | |||
116 | if hasattr(load_config, 'config'): |
||
117 | del load_config.config |
||
118 | |||
119 | gp.set_session() |
||
120 | status = gp.upload(helper.get_file('plain.jpg')) |
||
121 | |||
122 | assert status is None, status |
||
123 | |||
124 | View Code Duplication | @mock.patch('elodie.config.config_file', '%s/config.ini-googlephotos-upload-invalid-empty' % gettempdir()) |
|
0 ignored issues
–
show
|
|||
125 | def test_googlephotos_upload_invalid_empty(): |
||
126 | with open('%s/config.ini-googlephotos-upload-invalid-empty' % gettempdir(), 'w') as f: |
||
127 | f.write(config_string_fmt) |
||
128 | if hasattr(load_config, 'config'): |
||
129 | del load_config.config |
||
130 | |||
131 | gp = GooglePhotos() |
||
132 | |||
133 | if hasattr(load_config, 'config'): |
||
134 | del load_config.config |
||
135 | |||
136 | gp.set_session() |
||
137 | status = gp.upload(helper.get_file('invalid.jpg')) |
||
138 | |||
139 | assert status is None, status |
||
140 | |||
141 | @mock.patch('elodie.config.config_file', '%s/config.ini-googlephotos-upload-dne' % gettempdir()) |
||
142 | def test_googlephotos_upload_dne(): |
||
143 | with open('%s/config.ini-googlephotos-upload-dne' % gettempdir(), 'w') as f: |
||
144 | f.write(config_string_fmt) |
||
145 | if hasattr(load_config, 'config'): |
||
146 | del load_config.config |
||
147 | |||
148 | gp = GooglePhotos() |
||
149 | |||
150 | if hasattr(load_config, 'config'): |
||
151 | del load_config.config |
||
152 | |||
153 | gp.set_session() |
||
154 | status = gp.upload('/file/does/not/exist') |
||
155 | |||
156 | assert status is None, status |
||
157 | |||
158 | @mock.patch('elodie.config.config_file', '%s/config.ini-googlephotos-batch' % gettempdir()) |
||
159 | def test_googlephotos_batch(): |
||
160 | with open('%s/config.ini-googlephotos-batch' % gettempdir(), 'w') as f: |
||
161 | f.write(config_string_fmt) |
||
162 | if hasattr(load_config, 'config'): |
||
163 | del load_config.config |
||
164 | |||
165 | final_file_path = helper.get_file('plain.jpg') |
||
166 | gp = GooglePhotos() |
||
167 | gp.after('', '', final_file_path, sample_metadata) |
||
168 | db_row = gp.db.get(final_file_path) |
||
169 | assert db_row == 'foobar', db_row |
||
170 | |||
171 | status, count = gp.batch() |
||
172 | db_row_after = gp.db.get(final_file_path) |
||
173 | assert status == True, status |
||
174 | assert count == 1, count |
||
175 | assert db_row_after is None, db_row_after |
||
176 | |||
177 | |||
178 | if hasattr(load_config, 'config'): |
||
179 | del load_config.config |
||
180 | |||
181 | |||
182 | gp.set_session() |
||
183 | status = gp.upload(helper.get_file('invalid.jpg')) |
||
184 | |||
185 | assert status is None, status |
||
186 |