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

Side by Side Diff: tests/test_xtm_translate.py

Issue 29886648: Issue #6942 - Add XTM integration in CMS (Closed)
Patch Set: Created Sept. 20, 2018, 4:24 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(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 from __future__ import unicode_literals
16
17 import pytest
18
19 import os
20 from ConfigParser import SafeConfigParser
21 import io
22 import sys
23 import json
24
25 from cms.bin.xtm_translations import constants as cnts
26 from cms.bin.xtm_translations.projects_handler import (
27 main as main_project_handler,
28 create_project, upload_files, download_files,
29 )
30 from cms.bin.xtm_translations.translate_xtm_cloud import generate_token
31
32 _CMD_START = ['python', '-m', 'cms.bin.xtm_translations']
33
34 _ENV_NO_TOKEN = dict(os.environ)
35 _ENV_NO_TOKEN.pop(cnts.TOKEN['env_var'], None)
36
37 _ENV_TOKEN_VALID = dict(os.environ)
38 _ENV_TOKEN_VALID[cnts.TOKEN['env_var']] = 'TheXTM-APIToken-VALID'
39
40 _ENV_TOKEN_INVALID = dict(os.environ)
41 _ENV_TOKEN_INVALID[cnts.TOKEN['env_var']] = 'TheXTM-APIToken-INVALID'
42
43
44 class _CreationArgsNamespace:
45 def __init__(self):
46 pass
47
48 name = 'bar'
49 desc = 'foo'
50 client_id = 10
51 ref_id = 'faz'
52 workflow_id = 20
53 save_id = False
54 source_dir = None
55 projects_func = staticmethod(create_project)
56 source_lang = 'en_US'
57
58
59 class _UploadArgsNamespace:
60 def __init__(self):
61 pass
62
63 source_dir = None
64 projects_func = staticmethod(upload_files)
65 no_overwrite = False
66
67
68 class _DownloadArgsNamespace:
69 def __init__(self):
70 pass
71
72 source_dir = None
73 projects_func = staticmethod(download_files)
74
75
76 _CREATION_ARGS_DEFAULT = ['--name', 'bar', '--desc', 'foo', '--client-id',
77 '10', '--ref-id', 'faz', '--workflow-id', '20']
78 _CREATION_ARGS_INVALID_TYPE_1 = ['--name', 'bar', '--desc', 'foo',
79 '--client-id', 'foo', '--ref-id', 'faz',
80 '--workflow-id', '3']
81 _CREATION_ARGS_INVALID_TYPE_2 = ['--name', 'bar', '--desc', 'foo',
82 '--client-id', '23', '--ref-id', 'faz',
83 '--workflow-id', 'foo']
84
85
86 @pytest.fixture
87 def env_valid_token():
88 old_env = os.environ
89 os.environ = _ENV_TOKEN_VALID
90 yield True
91 os.environ = old_env
92
93
94 @pytest.mark.script_launch_mode('subprocess')
95 @pytest.mark.parametrize('args,exp_msg', [
96 (['-h'], 'usage: __main__.py [-h] [-v] {login,create,upload,download} '
97 '...'),
98 (['create', '-h'], 'usage: __main__.py create [-h] --name NAME --desc '
99 'DESC --client-id CLIENT_ID --ref-id REF_ID '
100 '--workflow-id WORKFLOW_ID [--source-lang SOURCE_LANG] '
101 '[--save-id] [source_dir]'),
102 (['upload', '-h'], 'usage: __main__.py upload [-h] [--no-overwrite] '
103 '[source_dir]'),
104 (['download', '-h'], 'usage: __main__.py download [-h] [source_dir]'),
105 ])
106 def test_usage_messages(args, exp_msg, script_runner):
107 """Test if appropriate usage messages are displayed."""
108 cmd = list(_CMD_START)
109 cmd.extend(args)
110 ret = script_runner.run(*cmd)
111
112 usg_msg = ret.stdout.replace('\n', '').replace(' ', '')
113
114 assert exp_msg.replace(' ', '') in usg_msg
115
116
117 @pytest.mark.script_launch_mode('subprocess')
118 @pytest.mark.parametrize('args', [
119 ['create', '--name', 'bar', '--desc', 'foo', '--client-id', '1',
120 '--ref-id', 'faz', '--workflow-id', '3'],
121 ['upload'],
122 ['download'],
123 ])
124 def test_default_source_directory(args, script_runner):
125 """Test if the source directory if set to default if not provided."""
126 exp = cnts.ErrorMessages.NO_TOKEN_PROVIDED.split('\n')[0]
127 cmd = list(_CMD_START)
128 cmd.extend(args)
129
130 ret = script_runner.run(*cmd)
131
132 assert not ret.success
133 assert exp in ret.stderr
134
135
136 @pytest.mark.script_launch_mode('subprocess')
137 @pytest.mark.parametrize('source_dir,args,env,exp_msg', [
138 ('str(temp_site)', _CREATION_ARGS_INVALID_TYPE_1, _ENV_NO_TOKEN,
139 "--client-id: invalid int value: 'foo'"),
140 ('str(temp_site)', _CREATION_ARGS_INVALID_TYPE_2, _ENV_NO_TOKEN,
141 "--workflow-id: invalid int value: 'foo'"),
142 ('str(temp_site)', _CREATION_ARGS_DEFAULT, _ENV_NO_TOKEN,
143 cnts.ErrorMessages.NO_TOKEN_PROVIDED.split('\n')[0]),
144 ('str(temp_site_valid_project)', _CREATION_ARGS_DEFAULT, _ENV_TOKEN_VALID,
145 cnts.ErrorMessages.EXISTENT_PROJECT.format(1234)),
146 ('str(temp_site)', _CREATION_ARGS_DEFAULT, _ENV_TOKEN_INVALID,
147 'Authentication failed'),
148 ])
149 def test_creation_error_messages(temp_site, intercept, script_runner, args,
150 source_dir, temp_site_valid_project, env,
151 exp_msg):
152 """Test if error cases are treated correctly when creating a project."""
153 cmd = list(_CMD_START)
154 cmd.extend(['create', eval(source_dir)])
155 cmd.extend(args)
156
157 ret = script_runner.run(*cmd, env=env)
158
159 assert not ret.success
160 assert exp_msg in ret.stderr
161
162
163 def test_creation_correct(temp_site, intercept, env_valid_token):
164 """Test if a project is created correctly, given the appropriate args."""
165 namespace = _CreationArgsNamespace()
166 namespace.source_dir = str(temp_site)
167 main_project_handler(namespace)
168
169
170 def test_creation_save_id(temp_site, intercept, env_valid_token):
171 """Test the project id is saved after successfully creating a project."""
172 namespace = _CreationArgsNamespace()
173 namespace.source_dir = str(temp_site)
174 namespace.save_id = True
175 main_project_handler(namespace)
176 cnf = SafeConfigParser()
177 try:
178 with io.open(os.path.join(temp_site, 'settings.ini'),
179 encoding='utf-8') as f:
180 cnf_data = f.read()
181 cnf.readfp(io.StringIO(cnf_data))
182 project_id = cnf.get(cnts.CONFIG['XTM_section'],
183 cnts.CONFIG['project_option'])
184
185 assert int(project_id) == 1234
186 finally:
187 cnf.remove_option(cnts.CONFIG['XTM_section'],
188 cnts.CONFIG['project_option'])
189 cnf.write(open(os.path.join(temp_site, 'settings.ini'), 'w'))
190
191
192 def test_login(intercept, monkeypatch, capsys):
193 """Test if the login functionality works as expected."""
194 exp_output = cnts.TOKEN['save_cmd'].format(cnts.TOKEN['env_var'],
195 'TheXTM-APIToken-VALID')
196 monkeypatch.setattr('cms.bin.xtm_translations.translate_xtm_cloud.input',
197 lambda inp: 'admin' if 'username' in inp.lower()
198 else '20')
199 monkeypatch.setattr('getpass.getpass', lambda prompt: 'pass')
200
201 generate_token(None)
202 out, err = capsys.readouterr()
203
204 assert err == ''
205 assert exp_output in out
206
207
208 def test_login_wrong_credentials(intercept, monkeypatch, capsys):
209 """Test exception handling when generating the tokens."""
210 monkeypatch.setattr('cms.bin.xtm_translations.translate_xtm_cloud.input',
211 lambda inp: 'foo' if 'username' in inp.lower() else
212 '50')
213 monkeypatch.setattr('getpass.getpass', lambda prompt: 'pass')
214 monkeypatch.setattr('sys.exit', lambda x: sys.stderr.write(str(x)))
215
216 generate_token(None)
217 out, err = capsys.readouterr()
218
219 assert 'Invalid credentials' in err
220 assert out == ''
221
222
223 @pytest.mark.script_launch_mode('subprocess')
224 @pytest.mark.parametrize('args,env,exp_msg', [
225 (['str(temp_site)'], _ENV_NO_TOKEN,
226 cnts.ErrorMessages.NO_TOKEN_PROVIDED.split('\n')[0]),
227 (['str(temp_site)'], _ENV_TOKEN_VALID, 'No project configured'),
228 ])
229 def test_upload_error_messages_as_script(temp_site, script_runner, args, env,
230 exp_msg):
231 cmd = list(_CMD_START)
232 cmd.append('upload')
233
234 cmd.extend(list(map(eval, args)))
235
236 ret = script_runner.run(*cmd, env=env)
237
238 assert not ret.success
239 assert exp_msg in ret.stderr
240
241
242 def test_upload_too_many_languages(intercept_too_many_targets,
243 env_valid_token, temp_site_valid_project):
244 namespace = _UploadArgsNamespace()
245 namespace.source_dir = str(temp_site_valid_project)
246
247 with pytest.raises(Exception) as err:
248 main_project_handler(namespace)
249
250 assert 'languages are enabled in the API, but not listed in locales' in \
251 str(err.value)
252
253
254 def test_upload_successful(intercept, env_valid_token,
255 temp_site_valid_project, monkeypatch, capsys):
256 namespace = _UploadArgsNamespace()
257 namespace.source_dir = str(temp_site_valid_project)
258 monkeypatch.setattr('logging.info', lambda x: sys.stderr.write(x))
259
260 main_project_handler(namespace)
261 _, err = capsys.readouterr()
262
263 assert cnts.InfoMessages.FILES_UPLOADED in err
264 assert 'Created job 1' in err
265
266
267 @pytest.mark.script_launch_mode('subprocess')
268 @pytest.mark.parametrize('env,exp_msg', [
269 (_ENV_NO_TOKEN, cnts.ErrorMessages.NO_TOKEN_PROVIDED.split('\n')[0]),
270 (_ENV_TOKEN_VALID, 'No project configured'),
271 ])
272 def test_download_error_messages_as_script(temp_site, script_runner, env,
273 exp_msg):
274 cmd = list(_CMD_START)
275 cmd.extend(['download', str(temp_site)])
276
277 ret = script_runner.run(*cmd, env=env)
278
279 assert not ret.success
280 assert exp_msg in ret.stderr
281
282
283 def test_download_no_target_files(temp_site_no_target_files, env_valid_token,
284 intercept, monkeypatch, capsys):
285 namespace = _DownloadArgsNamespace()
286 namespace.source_dir = str(temp_site_no_target_files)
287 monkeypatch.setattr('sys.exit', lambda x: sys.stderr.write(x))
288
289 main_project_handler(namespace)
290 out, err = capsys.readouterr()
291
292 assert cnts.ErrorMessages.NO_TARGET_FILES_FOUND in err
293
294
295 def test_download_saves_to_disk(temp_site_valid_project, env_valid_token,
296 intercept_populated):
297 namespace = _DownloadArgsNamespace()
298 namespace.source_dir = str(temp_site_valid_project)
299
300 main_project_handler(namespace)
301
302 with open(os.path.join(temp_site_valid_project, 'locales', 'de',
303 'file.json')) as f:
304 assert json.dumps({'foo': 'bar', 'faz': 'baz'}) == f.read()
305
306 with open(os.path.join(temp_site_valid_project, 'locales', 'de', 'foo',
307 'file-utf8.json'), 'rb') as f:
308 assert json.loads(f.read()) == {'foo': '\u1234'}
OLDNEW

Powered by Google App Engine
This is Rietveld