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

Delta Between Two Patch Sets: tests/xtm_mock_api.py

Issue 29886648: Issue #6942 - Add XTM integration in CMS (Closed)
Left Patch Set: Addressed initial comments Created Sept. 25, 2018, 12:24 p.m.
Right Patch Set: Addressed comments from Patch Set #4 Created Oct. 5, 2018, 4:23 p.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
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 flask import Flask, request, jsonify, Response, send_file 16 from flask import Flask, request, jsonify, Response, send_file
17 17
18 import json 18 import json
19 19
20 from tests.utils import create_in_memory_zip 20 from tests.utils import create_in_memory_zip
21 from cms.bin.xtm_translations.constants import SUPPORTED_LOCALES 21 from cms.translations.xtm.constants import SUPPORTED_LOCALES
22 22
23 __all__ = [ 23 __all__ = [
24 'xtm_mock', 24 'xtm_mock',
25 'get_configured_app', 25 'get_configured_app',
26 ] 26 ]
27 27
28 _CREDENTIALS = { 28 _CREDENTIALS = {
29 'token_gen_valid': {'client': 'admin', 'password': 'pass', 'userId': 20}, 29 'token_gen_valid': {'client': 'admin', 'password': 'pass', 'userId': 20},
30 'token_gen_invalid': {'client': 'user', 'password': 'pass', 'userId': 50}, 30 'token_gen_invalid': {'client': 'user', 'password': 'pass', 'userId': 50},
31 'token_valid': 'TheXTM-APIToken-VALID', 31 'token_valid': 'TheXTM-APIToken-VALID',
(...skipping 18 matching lines...) Expand all
50 def get_configured_app(**kwargs): 50 def get_configured_app(**kwargs):
51 xtm_mock.config['rootdir'] = kwargs.pop('rootdir', '') 51 xtm_mock.config['rootdir'] = kwargs.pop('rootdir', '')
52 xtm_mock.config['target_languages'] = kwargs.pop('target_langs', ['de_DE']) 52 xtm_mock.config['target_languages'] = kwargs.pop('target_langs', ['de_DE'])
53 xtm_mock.config['files'] = kwargs.pop('files', []) 53 xtm_mock.config['files'] = kwargs.pop('files', [])
54 xtm_mock.config['source_language'] = kwargs.pop('source_lang', 'en_US') 54 xtm_mock.config['source_language'] = kwargs.pop('source_lang', 'en_US')
55 return xtm_mock.wsgi_app 55 return xtm_mock.wsgi_app
56 56
57 57
58 def _check_auth(): 58 def _check_auth():
59 try: 59 try:
60 auth = request.headers.get('Authorization') 60 auth = request.headers['Authorization']
Vasily Kuznetsov 2018/09/26 15:45:27 Can this just be `request.headers['Authorization']
Tudor Avram 2018/10/04 06:48:15 Done.
61 except Exception: 61 except Exception:
62 return False 62 return False
63 63
64 if auth != 'XTM-Basic ' + _CREDENTIALS['token_valid']: 64 if auth != 'XTM-Basic ' + _CREDENTIALS['token_valid']:
65 return False 65 return False
66 66
67 return True 67 return True
68 68
69 69
70 def _generate_jobs_list(): 70 def _generate_jobs_list():
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 return Response( 155 return Response(
156 status=401, 156 status=401,
157 response=json.dumps({'reason': 'Authentication failed.'}), 157 response=json.dumps({'reason': 'Authentication failed.'}),
158 ) 158 )
159 159
160 reply = [{ 160 reply = [{
161 'coreMetrics': {}, 161 'coreMetrics': {},
162 'jobsMetrics': [], 162 'jobsMetrics': [],
163 'metricsProgress': {}, 163 'metricsProgress': {},
164 'targetLanguage': tl, 164 'targetLanguage': tl,
165 }for tl in xtm_mock.config['target_languages']] 165 } for tl in xtm_mock.config['target_languages']]
Vasily Kuznetsov 2018/09/26 15:45:27 Nit: space before "for"?
Tudor Avram 2018/10/04 06:48:16 Done.
166 166
167 if project_id == _PROJECT_IDS['valid']: 167 if project_id == _PROJECT_IDS['valid']:
168 return jsonify(reply) 168 return jsonify(reply)
169 169
170 return Response(status=404, response=json.dumps({'reason': 'Project not ' 170 return Response(status=404, response=json.dumps({'reason': 'Project not '
171 'found!'})) 171 'found!'}))
172 172
173 173
174 @xtm_mock.route('/rest-api/projects/<int:project_id>/target-languages', 174 @xtm_mock.route('/rest-api/projects/<int:project_id>/target-languages',
175 methods=['POST']) 175 methods=['POST'])
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
264 264
265 zip_file = create_in_memory_zip(names, data) 265 zip_file = create_in_memory_zip(names, data)
266 return send_file( 266 return send_file(
267 zip_file, 267 zip_file,
268 mimetype='application/zip', 268 mimetype='application/zip',
269 ) 269 )
270 270
271 271
272 if __name__ == '__main__': 272 if __name__ == '__main__':
273 xtm_mock.run(debug=True) 273 xtm_mock.run(debug=True)
LEFTRIGHT

Powered by Google App Engine
This is Rietveld