Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: sitescripts/oauth2dl/test/test_oauth2dl.py

Issue 29833582: Issue 4954 - Implement a downloader script supporting OAuth2 authentication/authorization (Closed)
Left Patch Set: Created July 18, 2018, 1:41 p.m.
Right Patch Set: removed .Python from .gitignore and added .DS_Store to .hgignore Created Aug. 2, 2018, 9:24 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « sitescripts/oauth2dl/test/dummy_wsgi_app.py ('k') | tox.ini » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 # This file is part of the Adblock Plus web scripts, 1 # This file is part of the Adblock Plus web scripts,
2 # Copyright (C) 2006-present eyeo GmbH 2 # Copyright (C) 2006-present eyeo GmbH
3 # 3 #
4 # Adblock Plus is free software: you can redistribute it and/or modify 4 # Adblock Plus is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License version 3 as 5 # it under the terms of the GNU General Public License version 3 as
6 # published by the Free Software Foundation. 6 # published by the Free Software Foundation.
7 # 7 #
8 # Adblock Plus is distributed in the hope that it will be useful, 8 # Adblock Plus is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details. 11 # GNU General Public License for more details.
12 # 12 #
13 # You should have received a copy of the GNU General Public License 13 # You should have received a copy of the GNU General Public License
14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
15 15
16 from __future__ import unicode_literals
17
16 import subprocess 18 import subprocess
Vasily Kuznetsov 2018/07/20 21:00:01 Same comment about ordering as in oauth2dl.py
Tudor Avram 2018/07/23 19:29:03 Acknowledged.
Tudor Avram 2018/07/31 09:20:17 Done.
17 import pytest 19 import pytest
18 import json 20 import json
21
19 from wsgi_intercept.interceptor import Httplib2Interceptor 22 from wsgi_intercept.interceptor import Httplib2Interceptor
20 23
21 import sitescripts.oauth2dl.bin.constants as cnts 24 import sitescripts.oauth2dl.bin.constants as cnts
22 from sitescripts.oauth2dl.utils.dummy_wsgi_app import main as intercept_app 25 from sitescripts.oauth2dl.test.dummy_wsgi_app import main as intercept_app
23 from sitescripts.oauth2dl.bin.oauth2dl import download_file, write_to_file 26 from sitescripts.oauth2dl.bin.oauth2dl import download_file
27
28
29 def get_valid_keyfile():
30 return {
31 'private_key_id': 6,
32 'private_key': cnts.DUMMY_PRIVATE_KEY,
33 'client_email': 'firstpart@secondpart.com',
34 'client_id': '8',
35 'type': 'service_account',
36 }
24 37
25 38
26 def get_intercept_app(): 39 def get_intercept_app():
27 """Return the intercepting WSGI application.""" 40 """Return the intercepting WSGI application."""
28 return intercept_app 41 return intercept_app
29 42
30 43
31 def write_to_json(data, path): 44 def write_to_json(data, path):
32 """Write data to JSON.""" 45 """Write data to JSON."""
33 with open(str(path), 'w') as f: 46 with open(str(path), 'w') as f:
34 json.dump(data, f) 47 json.dump(data, f)
35 48
36 49
37 @pytest.fixture 50 @pytest.fixture
38 def rootdir(tmpdir): 51 def rootdir(tmpdir):
39 """Directory with prepared key and downloadable files.""" 52 """Directory with prepared key and downloadable files."""
40 rootdir = tmpdir.join('root') 53 rootdir = tmpdir.join('root')
41 rootdir.mkdir() 54 rootdir.mkdir()
42 55
43 # Keyfile missing a key - private_key_id 56 # Keyfile missing a key - private_key_id
44 invalid_keyfile_path = rootdir.join('keyfile_missing_key.json') 57 invalid_keyfile_path = rootdir.join('keyfile_missing_key.json')
45 data = { 58 data = get_valid_keyfile()
46 'private_key': cnts.DUMMY_PRIVATE_KEY, 59 data.pop('private_key')
47 'client_email': 'firstpart@secondpart.com',
48 'client_id': '8',
49 'type': 'service_account',
50 }
51 write_to_json(data, str(invalid_keyfile_path)) 60 write_to_json(data, str(invalid_keyfile_path))
52 61
53 # Keyfile with invalid private key 62 # Keyfile with invalid private key
54 invalid_keyfile_path = rootdir.join('keyfile_invalid_private_key.json') 63 invalid_keyfile_path = rootdir.join('keyfile_invalid_private_key.json')
55 data = { 64 data = get_valid_keyfile()
56 'private_key_id': 6, 65 data['private_key'] = data['private_key'][:-10]
57 'private_key': cnts.DUMMY_PRIVATE_KEY[:-10],
58 'client_email': 'firstpart@secondpart.com',
59 'client_id': '8',
60 'type': 'service_account',
61 }
62 write_to_json(data, str(invalid_keyfile_path)) 66 write_to_json(data, str(invalid_keyfile_path))
63 67
64 # Keyfile with wrong value for 'type' 68 # Keyfile with wrong value for 'type'
65 invalid_keyfile_path = rootdir.join('keyfile_invalid_type.json') 69 invalid_keyfile_path = rootdir.join('keyfile_invalid_type.json')
66 data = { 70 data = get_valid_keyfile()
67 'private_key_id': 6, 71 data['type'] = 'invalid'
68 'private_key': cnts.DUMMY_PRIVATE_KEY,
69 'client_email': 'firstpart@secondpart.com',
70 'client_id': '8',
71 'type': 'invalid',
72 }
73 write_to_json(data, str(invalid_keyfile_path)) 72 write_to_json(data, str(invalid_keyfile_path))
74 73
75 # Valid (dummy) keyfile 74 # Valid (dummy) keyfile
76 valid_keyfile_path = rootdir.join('good_keyfile.json') 75 valid_keyfile_path = rootdir.join('good_keyfile.json')
77 data = { 76 write_to_json(get_valid_keyfile(), str(valid_keyfile_path))
Vasily Kuznetsov 2018/07/20 21:00:01 There seems to be some repetition here, maybe we c
Tudor Avram 2018/07/31 09:20:17 Done.
78 'private_key_id': 6,
79 'private_key': cnts.DUMMY_PRIVATE_KEY,
80 'client_email': 'firstpart@secondpart.com',
81 'client_id': '8',
82 'type': 'service_account',
83 }
84 write_to_json(data, str(valid_keyfile_path))
85 77
86 # Downloadable file 78 # Downloadable file
87 rootdir.join('file_to_download').write('Success!') 79 rootdir.join('file_to_download').write('Success!')
88 80
89 # Downloadable file with utf-8 characters 81 # Downloadable file with utf-8 characters
90 rootdir.join('file_to_download_utf8').write('Ok! \u1234'.encode('utf-8'), 82 rootdir.join('file_to_download_utf8').write('Ok \u1234'.encode('utf-8'),
91 mode='wb') 83 mode='wb')
92 84
93 return rootdir 85 return rootdir
94 86
95 87
96 @pytest.fixture 88 @pytest.fixture
97 def dstfile(tmpdir): 89 def dstfile(tmpdir):
98 """Destination file for saving the downloaded whitelist.""" 90 """Destination file for saving the downloaded whitelist."""
99 return tmpdir.join('dst') 91 return tmpdir.join('dst')
100 92
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 assert expected in data 178 assert expected in data
187 179
188 180
189 def test_download_wrong_url(rootdir): 181 def test_download_wrong_url(rootdir):
190 """Test authenticating and trying to download a file from an invalid url. 182 """Test authenticating and trying to download a file from an invalid url.
191 183
192 Uses a local server that simulates the interaction with the google API. 184 Uses a local server that simulates the interaction with the google API.
193 """ 185 """
194 keyfile_path = str(rootdir.join('good_keyfile.json')) 186 keyfile_path = str(rootdir.join('good_keyfile.json'))
195 url = 'https://www.googleapis.com/download?path={0}'.format( 187 url = 'https://www.googleapis.com/download?path={0}'.format(
196 str(rootdir.join('file_to_downlaaoad_utf8'))) 188 str(rootdir.join('inexistent_file')))
Vasily Kuznetsov 2018/07/20 21:00:01 ..._donwlaaoad_... -- typo or on purpose? :)
Tudor Avram 2018/07/23 19:29:03 It was on purpose. I wanted to test the behaviour
Tudor Avram 2018/07/31 09:20:18 Done.
197 scope = 'www.googleapis.com' 189 scope = 'www.googleapis.com'
198 190
199 with Httplib2Interceptor(get_intercept_app, host='www.googleapis.com', 191 with Httplib2Interceptor(get_intercept_app, host='www.googleapis.com',
200 port=443): 192 port=443):
201 headers, data = download_file(url, keyfile_path, scope) 193 headers, data = download_file(url, keyfile_path, scope)
202 194
203 assert 'NOT FOUND' in data.upper() 195 assert 'NOT FOUND' in data.upper()
204 assert headers['status'] == '404' 196 assert headers['status'] == '404'
205
206
207 def test_script_run_as_file():
208 """Test the script's handling of relative imports."""
209 command = 'python sitescripts/oauth2dl/bin/oauth2dl.py '
210 _, stderr, _ = run_script(cmd=command)
211
212 assert 'ValueError' not in stderr
213 assert 'ImportError' not in stderr
214
215
216 @pytest.mark.parametrize('file, expected', [
217 ('file_to_download', 'Success!'),
218 ('file_to_download_utf8', '\u1234'),
219 ])
220 def test_write_to_file(rootdir, dstfile, file, expected):
221 """Test if the script writes the data correctly."""
222 data = rootdir.join(file).read().encode('utf-8')
223 write_to_file(data, str(dstfile))
224
225 assert expected in dstfile.read(mode='rb').decode('utf-8')
LEFTRIGHT

Powered by Google App Engine
This is Rietveld