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

Delta Between Two Patch Sets: tests/test_xtm_translate.py

Issue 29886648: Issue #6942 - Add XTM integration in CMS (Closed)
Left Patch Set: Addressed comments on patch set #2 Created Oct. 3, 2018, 2:50 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 __future__ import unicode_literals 16 from __future__ import unicode_literals
17 17
18 import os 18 import os
19 from ConfigParser import SafeConfigParser 19 from ConfigParser import SafeConfigParser
20 import io 20 import io
21 import sys 21 import sys
22 import json 22 import json
23 23
24 import pytest 24 import pytest
25 25
26 26 from cms.translations.xtm import constants as const
27 from cms.bin.xtm_translations import constants as const 27 from cms.translations.xtm.projects_handler import (
28 from cms.bin.xtm_translations.projects_handler import (
29 create_project, upload_files, download_files, 28 create_project, upload_files, download_files,
30 ) 29 )
31 from cms.bin.xtm_translations.translate_xtm_cloud import ( 30 from cms.translations.xtm.cli import (
32 generate_token, handle_projects as main_project_handler, 31 generate_token, handle_projects as main_project_handler,
33 ) 32 )
34 33
35 _CMD_START = ['python', '-m', 'cms.bin.xtm_translations'] 34 _CMD_START = ['python', '-m', 'cms.bin.xtm_translations']
36 35
37 _ENV_NO_TOKEN = dict(os.environ) 36 _ENV_NO_TOKEN = dict(os.environ)
38 _ENV_NO_TOKEN.pop(const.Token.ENV_VAR, None) 37 _ENV_NO_TOKEN.pop(const.Token.ENV_VAR, None)
39 38
40 _ENV_TOKEN_VALID = dict(os.environ) 39 _ENV_TOKEN_VALID = dict(os.environ)
41 _ENV_TOKEN_VALID[const.Token.ENV_VAR] = 'TheXTM-APIToken-VALID' 40 _ENV_TOKEN_VALID[const.Token.ENV_VAR] = 'TheXTM-APIToken-VALID'
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 @pytest.fixture 88 @pytest.fixture
90 def env_valid_token(): 89 def env_valid_token():
91 old_env = os.environ 90 old_env = os.environ
92 os.environ = _ENV_TOKEN_VALID 91 os.environ = _ENV_TOKEN_VALID
93 yield True 92 yield True
94 os.environ = old_env 93 os.environ = old_env
95 94
96 95
97 @pytest.mark.script_launch_mode('subprocess') 96 @pytest.mark.script_launch_mode('subprocess')
98 @pytest.mark.parametrize('args,exp_msg', [ 97 @pytest.mark.parametrize('args,exp_msg', [
99 (['-h'], 'usage: __main__.py [-h] [-v] {login,create,upload,download} ' 98 (['-h'], 'usage: xtm_translations.py [-h] [-v] '
100 '...'), 99 '{login,create,upload,download} ...'),
101 (['create', '-h'], 'usage: __main__.py create [-h] --name NAME --desc ' 100 (['create', '-h'], 'usage: xtm_translations.py create [-h] --name NAME '
102 'DESC --client-id CLIENT_ID --ref-id REF_ID ' 101 '--desc DESC --client-id CLIENT_ID --ref-id REF_ID '
103 '--workflow-id WORKFLOW_ID [--source-lang SOURCE_LANG] ' 102 '--workflow-id WORKFLOW_ID [--source-lang SOURCE_LANG] '
104 '[--save-id] [source_dir]'), 103 '[--save-id] [source_dir]'),
105 (['upload', '-h'], 'usage: __main__.py upload [-h] [--no-overwrite] ' 104 (['upload', '-h'], 'usage: xtm_translations.py upload [-h] '
106 '[source_dir]'), 105 '[--no-overwrite] [source_dir]'),
107 (['download', '-h'], 'usage: __main__.py download [-h] [source_dir]'), 106 (['download', '-h'], 'usage: xtm_translations.py download [-h] '
107 '[source_dir]'),
108 ]) 108 ])
109 def test_usage_messages(args, exp_msg, script_runner): 109 def test_usage_messages(args, exp_msg, script_runner):
110 """Test if appropriate usage messages are displayed.""" 110 """Test if appropriate usage messages are displayed."""
111 cmd = list(_CMD_START) 111 cmd = list(_CMD_START)
112 cmd.extend(args) 112 cmd.extend(args)
113 ret = script_runner.run(*cmd) 113 ret = script_runner.run(*cmd)
114 114
115 usg_msg = ret.stdout.replace('\n', '').replace(' ', '') 115 usg_msg = ret.stdout.replace('\n', '').replace(' ', '')
116 116
117 assert exp_msg.replace(' ', '') in usg_msg 117 assert exp_msg.replace(' ', '') in usg_msg
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 cnf.remove_option(const.Config.XTM_SECTION, 190 cnf.remove_option(const.Config.XTM_SECTION,
191 const.Config.PROJECT_OPTION) 191 const.Config.PROJECT_OPTION)
192 cnf.write(open(os.path.join(temp_site, 'settings.ini'), 'w')) 192 cnf.write(open(os.path.join(temp_site, 'settings.ini'), 'w'))
193 193
194 194
195 def test_login(intercept, monkeypatch, capsys): 195 def test_login(intercept, monkeypatch, capsys):
196 """Test if the login functionality works as expected.""" 196 """Test if the login functionality works as expected."""
197 exp_output = const.Token.SAVE_COMMAND.format(const.Token.ENV_VAR, 197 exp_output = const.Token.SAVE_COMMAND.format(const.Token.ENV_VAR,
198 'TheXTM-APIToken-VALID') 198 'TheXTM-APIToken-VALID')
199 monkeypatch.setattr( 199 monkeypatch.setattr(
200 'cms.bin.xtm_translations.translate_xtm_cloud.input_fn', 200 'cms.translations.xtm.cli.input_fn',
201 lambda inp: 'admin' if 'username' in inp.lower() else '20', 201 lambda inp: 'admin' if 'username' in inp.lower() else '20',
202 ) 202 )
203 monkeypatch.setattr('getpass.getpass', lambda prompt: 'pass') 203 monkeypatch.setattr('getpass.getpass', lambda prompt: 'pass')
204 204
205 generate_token(None) 205 generate_token(None)
206 out, err = capsys.readouterr() 206 out, err = capsys.readouterr()
207 207
208 assert err == '' 208 assert err == ''
209 assert exp_output in out 209 assert exp_output in out
210 210
211 211
212 def test_login_wrong_credentials(intercept, monkeypatch, capsys): 212 def test_login_wrong_credentials(intercept, monkeypatch, capsys):
213 """Test exception handling when generating the tokens.""" 213 """Test exception handling when generating the tokens."""
214 monkeypatch.setattr( 214 monkeypatch.setattr(
215 'cms.bin.xtm_translations.translate_xtm_cloud.input_fn', 215 'cms.translations.xtm.cli.input_fn',
216 lambda inp: 'foo' if 'username' in inp.lower() else '50', 216 lambda inp: 'foo' if 'username' in inp.lower() else '50',
217 ) 217 )
218 monkeypatch.setattr('getpass.getpass', lambda prompt: 'pass') 218 monkeypatch.setattr('getpass.getpass', lambda prompt: 'pass')
219 monkeypatch.setattr('sys.exit', lambda x: sys.stderr.write(str(x))) 219 monkeypatch.setattr('sys.exit', lambda x: sys.stderr.write(str(x)))
220 220
221 generate_token(None) 221 generate_token(None)
222 out, err = capsys.readouterr() 222 out, err = capsys.readouterr()
223 223
224 assert 'Invalid credentials' in err 224 assert 'Invalid credentials' in err
225 assert out == '' 225 assert out == ''
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
304 304
305 main_project_handler(namespace) 305 main_project_handler(namespace)
306 306
307 with open(os.path.join(temp_site_valid_project, 'locales', 'de', 307 with open(os.path.join(temp_site_valid_project, 'locales', 'de',
308 'file.json')) as f: 308 'file.json')) as f:
309 assert json.dumps({'foo': 'bar', 'faz': 'baz'}) == f.read() 309 assert json.dumps({'foo': 'bar', 'faz': 'baz'}) == f.read()
310 310
311 with open(os.path.join(temp_site_valid_project, 'locales', 'de', 'foo', 311 with open(os.path.join(temp_site_valid_project, 'locales', 'de', 'foo',
312 'file-utf8.json'), 'rb') as f: 312 'file-utf8.json'), 'rb') as f:
313 assert json.loads(f.read()) == {'foo': '\u1234'} 313 assert json.loads(f.read()) == {'foo': '\u1234'}
LEFTRIGHT

Powered by Google App Engine
This is Rietveld