OLD | NEW |
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 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 @pytest.fixture | 57 @pytest.fixture |
58 def env_valid_token(): | 58 def env_valid_token(): |
59 old_env = os.environ | 59 old_env = os.environ |
60 os.environ = _ENV_TOKEN_VALID | 60 os.environ = _ENV_TOKEN_VALID |
61 yield True | 61 yield True |
62 os.environ = old_env | 62 os.environ = old_env |
63 | 63 |
64 | 64 |
65 @pytest.mark.script_launch_mode('subprocess') | 65 @pytest.mark.script_launch_mode('subprocess') |
66 @pytest.mark.parametrize('args,exp_msg', [ | 66 @pytest.mark.parametrize('args,exp_msg', [ |
67 (['-h'], 'usage: xtm_translations.py [-h] [-v] ' | 67 (['-h'], 'usage: xtm_translations.py [-h] [-v] [--api-urlAPI_URL]' |
68 '{login,create,upload,download} ...'), | 68 '{login,create,upload,download} ...'), |
69 (['create', '-h'], 'usage: xtm_translations.py create [-h] --name NAME ' | 69 (['create', '-h'], 'usage: xtm_translations.py create [-h] --name NAME ' |
70 '--desc DESC --client-id CLIENT_ID --ref-id REF_ID ' | 70 '--desc DESC --client-id CLIENT_ID --ref-id REF_ID ' |
71 '[--workflow-id WORKFLOW_ID] [--source-lang ' | 71 '[--workflow-id WORKFLOW_ID] [--source-lang ' |
72 'SOURCE_LANG] [--save-id] [--workflow-name ' | 72 'SOURCE_LANG] [--save-id] [--workflow-name ' |
73 'WORKFLOW_NAME] [source_dir]'), | 73 'WORKFLOW_NAME] [source_dir]'), |
74 (['upload', '-h'], 'usage: xtm_translations.py upload [-h] ' | 74 (['upload', '-h'], 'usage: xtm_translations.py upload [-h] ' |
75 '[--no-overwrite] [source_dir]'), | 75 '[--no-overwrite] [source_dir]'), |
76 (['download', '-h'], 'usage: xtm_translations.py download [-h] ' | 76 (['download', '-h'], 'usage: xtm_translations.py download [-h] ' |
77 '[source_dir]'), | 77 '[source_dir]'), |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 @pytest.mark.script_launch_mode('subprocess') | 109 @pytest.mark.script_launch_mode('subprocess') |
110 @pytest.mark.parametrize('source_dir,args,env,exp_msg', [ | 110 @pytest.mark.parametrize('source_dir,args,env,exp_msg', [ |
111 ('str(temp_site)', _CREATION_ARGS_INVALID_TYPE_1, _ENV_NO_TOKEN, | 111 ('str(temp_site)', _CREATION_ARGS_INVALID_TYPE_1, _ENV_NO_TOKEN, |
112 "--client-id: invalid int value: 'foo'"), | 112 "--client-id: invalid int value: 'foo'"), |
113 ('str(temp_site)', _CREATION_ARGS_INVALID_TYPE_2, _ENV_NO_TOKEN, | 113 ('str(temp_site)', _CREATION_ARGS_INVALID_TYPE_2, _ENV_NO_TOKEN, |
114 "--workflow-id: invalid int value: 'foo'"), | 114 "--workflow-id: invalid int value: 'foo'"), |
115 ('str(temp_site)', _CREATION_ARGS_DEFAULT, _ENV_NO_TOKEN, | 115 ('str(temp_site)', _CREATION_ARGS_DEFAULT, _ENV_NO_TOKEN, |
116 const.ErrorMessages.NO_TOKEN_PROVIDED.split('\n')[0]), | 116 const.ErrorMessages.NO_TOKEN_PROVIDED.split('\n')[0]), |
117 ('str(temp_site_valid_project)', _CREATION_ARGS_DEFAULT, _ENV_TOKEN_VALID, | 117 ('str(temp_site_valid_project)', _CREATION_ARGS_DEFAULT, _ENV_TOKEN_VALID, |
118 const.ErrorMessages.PROJECT_EXISTS.format(1234)), | 118 const.ErrorMessages.PROJECT_EXISTS.format(1234)), |
119 ('str(temp_site)', _CREATION_ARGS_DEFAULT, _ENV_TOKEN_INVALID, | 119 ('str(temp_site_no_project)', _CREATION_ARGS_DEFAULT, _ENV_TOKEN_INVALID, |
120 'Authentication failed'), | 120 'Authentication failed'), |
121 ]) | 121 ]) |
122 def test_creation_error_messages(temp_site, intercept, script_runner, args, | 122 def test_creation_error_messages(temp_site, intercept, script_runner, args, |
123 source_dir, temp_site_valid_project, env, | 123 source_dir, temp_site_valid_project, env, |
124 exp_msg): | 124 exp_msg, temp_site_no_project): |
125 """Test if error cases are treated correctly when creating a project.""" | 125 """Test if error cases are treated correctly when creating a project.""" |
126 cmd = list(_CMD_START) | 126 cmd = list(_CMD_START) |
127 cmd.extend(['create', eval(source_dir)]) | 127 cmd.extend(['create', eval(source_dir)]) |
128 cmd.extend(args) | 128 cmd.extend(args) |
129 | 129 |
130 ret = script_runner.run(*cmd, env=env) | 130 ret = script_runner.run(*cmd, env=env) |
131 | 131 |
| 132 print(ret.stdout) |
| 133 print(ret.returncode) |
| 134 |
132 assert not ret.success | 135 assert not ret.success |
133 assert exp_msg in ret.stderr | 136 assert exp_msg in ret.stderr |
134 | 137 |
135 | 138 |
136 def test_creation_correct(temp_site, intercept, env_valid_token): | 139 def test_creation_correct(temp_site, intercept, env_valid_token): |
137 """Test if a project is created correctly, given the appropriate args.""" | 140 """Test if a project is created correctly, given the appropriate args.""" |
138 namespace = _CreationArgsNamespace() | 141 namespace = _CreationArgsNamespace() |
139 namespace.source_dir = str(temp_site) | 142 namespace.source_dir = str(temp_site) |
140 main_project_handler(namespace) | 143 main_project_handler(namespace) |
141 | 144 |
(...skipping 23 matching lines...) Expand all Loading... |
165 def test_login(intercept, monkeypatch, capsys): | 168 def test_login(intercept, monkeypatch, capsys): |
166 """Test if the login functionality works as expected.""" | 169 """Test if the login functionality works as expected.""" |
167 exp_output = const.Token.SAVE_COMMAND.format(const.Token.ENV_VAR, | 170 exp_output = const.Token.SAVE_COMMAND.format(const.Token.ENV_VAR, |
168 'TheXTM-APIToken-VALID') | 171 'TheXTM-APIToken-VALID') |
169 monkeypatch.setattr( | 172 monkeypatch.setattr( |
170 'cms.translations.xtm.cli.input_fn', | 173 'cms.translations.xtm.cli.input_fn', |
171 lambda inp: 'admin' if 'username' in inp.lower() else '20', | 174 lambda inp: 'admin' if 'username' in inp.lower() else '20', |
172 ) | 175 ) |
173 monkeypatch.setattr('getpass.getpass', lambda prompt: 'pass') | 176 monkeypatch.setattr('getpass.getpass', lambda prompt: 'pass') |
174 | 177 |
175 generate_token(None) | 178 generate_token(XtmMockArgs.LoginArgsNamespace) |
176 out, err = capsys.readouterr() | 179 out, err = capsys.readouterr() |
177 | 180 |
178 assert err == '' | 181 assert err == '' |
179 assert exp_output in out | 182 assert exp_output in out |
180 | 183 |
181 | 184 |
182 def test_login_wrong_credentials(intercept, monkeypatch, capsys): | 185 def test_login_wrong_credentials(intercept, monkeypatch, capsys): |
183 """Test exception handling when generating the tokens.""" | 186 """Test exception handling when generating the tokens.""" |
184 monkeypatch.setattr( | 187 monkeypatch.setattr( |
185 'cms.translations.xtm.cli.input_fn', | 188 'cms.translations.xtm.cli.input_fn', |
186 lambda inp: 'foo' if 'username' in inp.lower() else '50', | 189 lambda inp: 'foo' if 'username' in inp.lower() else '50', |
187 ) | 190 ) |
188 monkeypatch.setattr('getpass.getpass', lambda prompt: 'pass') | 191 monkeypatch.setattr('getpass.getpass', lambda prompt: 'pass') |
189 monkeypatch.setattr('sys.exit', lambda x: sys.stderr.write(str(x))) | 192 monkeypatch.setattr('sys.exit', lambda x: sys.stderr.write(str(x))) |
190 | 193 |
191 generate_token(None) | 194 generate_token(XtmMockArgs.LoginArgsNamespace) |
192 out, err = capsys.readouterr() | 195 out, err = capsys.readouterr() |
193 | 196 |
194 assert 'Invalid credentials' in err | 197 assert 'Invalid credentials' in err |
195 assert out == '' | 198 assert out == '' |
196 | 199 |
197 | 200 |
198 @pytest.mark.script_launch_mode('subprocess') | 201 @pytest.mark.script_launch_mode('subprocess') |
199 @pytest.mark.parametrize('args,env,exp_msg', [ | 202 @pytest.mark.parametrize('args,env,exp_msg', [ |
200 (['str(temp_site)'], _ENV_NO_TOKEN, | 203 (['str(temp_site)'], _ENV_NO_TOKEN, |
201 const.ErrorMessages.NO_TOKEN_PROVIDED.split('\n')[0]), | 204 const.ErrorMessages.NO_TOKEN_PROVIDED.split('\n')[0]), |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 | 277 |
275 main_project_handler(namespace) | 278 main_project_handler(namespace) |
276 | 279 |
277 with open(os.path.join(temp_site_valid_project, 'locales', 'de', | 280 with open(os.path.join(temp_site_valid_project, 'locales', 'de', |
278 'file.json')) as f: | 281 'file.json')) as f: |
279 assert json.dumps({'foo': 'bar', 'faz': 'baz'}) == f.read() | 282 assert json.dumps({'foo': 'bar', 'faz': 'baz'}) == f.read() |
280 | 283 |
281 with open(os.path.join(temp_site_valid_project, 'locales', 'de', 'foo', | 284 with open(os.path.join(temp_site_valid_project, 'locales', 'de', 'foo', |
282 'file-utf8.json'), 'rb') as f: | 285 'file-utf8.json'), 'rb') as f: |
283 assert json.loads(f.read()) == {'foo': '\u1234'} | 286 assert json.loads(f.read()) == {'foo': '\u1234'} |
OLD | NEW |