Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 # This file is part of the Adblock Plus web scripts, | |
2 # Copyright (C) 2006-present eyeo GmbH | |
3 # | |
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 | |
6 # published by the Free Software Foundation. | |
7 # | |
8 # Adblock Plus is distributed in the hope that it will be useful, | |
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 # GNU General Public License for more details. | |
12 # | |
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/>. | |
15 | |
16 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 | |
18 import json | |
19 from wsgi_intercept.interceptor import Httplib2Interceptor | |
20 | |
21 import sitescripts.oauth2dl.bin.constants as cnts | |
22 from sitescripts.oauth2dl.utils.dummy_wsgi_app import main as intercept_app | |
23 from sitescripts.oauth2dl.bin.oauth2dl import download_file, write_to_file | |
24 | |
25 | |
26 def get_intercept_app(): | |
27 """Return the intercepting WSGI application.""" | |
28 return intercept_app | |
29 | |
30 | |
31 def write_to_json(data, path): | |
32 """Write data to JSON.""" | |
33 with open(str(path), 'w') as f: | |
34 json.dump(data, f) | |
35 | |
36 | |
37 @pytest.fixture | |
38 def rootdir(tmpdir): | |
39 """Directory with prepared key and downloadable files.""" | |
40 rootdir = tmpdir.join('root') | |
41 rootdir.mkdir() | |
42 | |
43 # Keyfile missing a key - private_key_id | |
44 invalid_keyfile_path = rootdir.join('keyfile_missing_key.json') | |
45 data = { | |
46 'private_key': cnts.DUMMY_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)) | |
52 | |
53 # Keyfile with invalid private key | |
54 invalid_keyfile_path = rootdir.join('keyfile_invalid_private_key.json') | |
55 data = { | |
56 'private_key_id': 6, | |
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)) | |
63 | |
64 # Keyfile with wrong value for 'type' | |
65 invalid_keyfile_path = rootdir.join('keyfile_invalid_type.json') | |
66 data = { | |
67 'private_key_id': 6, | |
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)) | |
74 | |
75 # Valid (dummy) keyfile | |
76 valid_keyfile_path = rootdir.join('good_keyfile.json') | |
77 data = { | |
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 | |
86 # Downloadable file | |
87 rootdir.join('file_to_download').write('Success!') | |
88 | |
89 # Downloadable file with utf-8 characters | |
90 rootdir.join('file_to_download_utf8').write('Ok! \u1234'.encode('utf-8'), | |
91 mode='wb') | |
92 | |
93 return rootdir | |
94 | |
95 | |
96 @pytest.fixture | |
97 def dstfile(tmpdir): | |
98 """Destination file for saving the downloaded whitelist.""" | |
99 return tmpdir.join('dst') | |
100 | |
101 | |
102 def run_script(*args, **kw): | |
103 """Run download script with given arguments and return its output.""" | |
104 try: | |
105 cmd = kw.pop('cmd') | |
106 except KeyError: | |
107 cmd = 'python -m sitescripts.oauth2dl.bin.oauth2dl' | |
108 | |
109 cmd = [cmd] + list(args) | |
110 cmd = ' '.join(cmd) | |
111 proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, | |
112 stderr=subprocess.PIPE, shell=True, **kw) | |
113 | |
114 stdout, stderr = proc.communicate() | |
115 | |
116 return proc.returncode, stderr.decode('utf-8'), stdout.decode('utf-8') | |
117 | |
118 | |
119 @pytest.mark.parametrize('args_in, expected_stderr, expected_code', [ | |
120 ((), 'usage: oauth2dl.py [-h] [-k KEY] [-s SCOPE] [-o O] url', 2), | |
121 (('www.test.com',), cnts.KEYFILE_NOT_FOUND_ERROR, 1), | |
122 (('www.test.com', '-k', 'test.json'), cnts.SCOPE_NOT_FOUND_ERROR, 1), | |
123 (('www.test.com', '-k', 'test.json', '-s', 'test'), | |
124 "No such file or directory: 'test.json'", 1), | |
125 ]) | |
126 def test_error_messages(args_in, expected_stderr, expected_code): | |
127 """Testing that appropriate error messages are provided.""" | |
128 code, stderr, _ = run_script(*args_in) | |
129 | |
130 assert code == expected_code | |
131 assert expected_stderr in stderr | |
132 | |
133 | |
134 def test_extracting_from_environment_vars(): | |
135 """Test if it uses the environment variables if none are provided.""" | |
136 test_env = {'OAUTH2_KEY': 'env_test.json', | |
137 'OAUTH2_SCOPE': 'env_test_scope'} | |
138 _, stderr, _ = run_script('www.test.com', env=test_env) | |
139 | |
140 assert cnts.KEYFILE_NOT_FOUND_ERROR not in stderr | |
141 assert cnts.SCOPE_NOT_FOUND_ERROR not in stderr | |
142 | |
143 | |
144 @pytest.mark.parametrize('key, expected_stderr, expected_code', [ | |
145 ('keyfile_missing_key.json', 'Invalid key file format!', 1), | |
146 ('keyfile_invalid_private_key.json', 'invalid_client: The OAuth ' | |
147 'client was not found.', 1), | |
148 ('keyfile_invalid_type.json', "('Unexpected credentials type', u'invalid'," | |
149 " 'Expected', 'service_account')", 1), | |
150 ('good_keyfile.json', 'invalid_client: The OAuth client was not found.', | |
151 1), | |
152 ]) | |
153 def test_keyfile_errors(rootdir, key, expected_stderr, expected_code): | |
154 """Testing how the script handles key file-related error messages. | |
155 | |
156 Connects to the actual google API, using set of dummy key files. | |
157 """ | |
158 keyfile_path = rootdir.join(key) | |
159 | |
160 code, stderr, _ = run_script('www.test.com', '-k', str(keyfile_path), '-s', | |
161 'test') | |
162 | |
163 assert code == expected_code | |
164 assert expected_stderr in stderr | |
165 | |
166 | |
167 @pytest.mark.parametrize('file, expected', [ | |
168 ('file_to_download', 'Success!'), | |
169 ('file_to_download_utf8', '\u1234'), | |
170 ]) | |
171 def test_download(rootdir, file, expected): | |
172 """Test authenticating and downloading a file. | |
173 | |
174 Uses a local server that simulates the interaction with the google API | |
175 """ | |
176 keyfile_path = str(rootdir.join('good_keyfile.json')) | |
177 url = 'https://www.googleapis.com/download?path={0}'.format( | |
178 str(rootdir.join(file)), | |
179 ) | |
180 scope = 'www.googleapis.com' | |
181 | |
182 with Httplib2Interceptor(get_intercept_app, host='www.googleapis.com', | |
183 port=443): | |
184 _, data = download_file(url, keyfile_path, scope) | |
185 | |
186 assert expected in data | |
187 | |
188 | |
189 def test_download_wrong_url(rootdir): | |
190 """Test authenticating and trying to download a file from an invalid url. | |
191 | |
192 Uses a local server that simulates the interaction with the google API. | |
193 """ | |
194 keyfile_path = str(rootdir.join('good_keyfile.json')) | |
195 url = 'https://www.googleapis.com/download?path={0}'.format( | |
196 str(rootdir.join('file_to_downlaaoad_utf8'))) | |
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' | |
198 | |
199 with Httplib2Interceptor(get_intercept_app, host='www.googleapis.com', | |
200 port=443): | |
201 headers, data = download_file(url, keyfile_path, scope) | |
202 | |
203 assert 'NOT FOUND' in data.upper() | |
204 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') | |
OLD | NEW |