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

Powered by Google App Engine
This is Rietveld