LEFT | RIGHT |
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 22 matching lines...) Expand all Loading... |
33 | 33 |
34 _ENV_NO_TOKEN = dict(os.environ) | 34 _ENV_NO_TOKEN = dict(os.environ) |
35 _ENV_NO_TOKEN.pop(const.Token.ENV_VAR, None) | 35 _ENV_NO_TOKEN.pop(const.Token.ENV_VAR, None) |
36 | 36 |
37 _ENV_TOKEN_VALID = dict(os.environ) | 37 _ENV_TOKEN_VALID = dict(os.environ) |
38 _ENV_TOKEN_VALID[const.Token.ENV_VAR] = 'TheXTM-APIToken-VALID' | 38 _ENV_TOKEN_VALID[const.Token.ENV_VAR] = 'TheXTM-APIToken-VALID' |
39 | 39 |
40 _ENV_TOKEN_INVALID = dict(os.environ) | 40 _ENV_TOKEN_INVALID = dict(os.environ) |
41 _ENV_TOKEN_INVALID[const.Token.ENV_VAR] = 'TheXTM-APIToken-INVALID' | 41 _ENV_TOKEN_INVALID[const.Token.ENV_VAR] = 'TheXTM-APIToken-INVALID' |
42 | 42 |
43 _CREATION_ARGS_DEFAULT = ['--name', 'bar', '--desc', 'foo', '--client-id', | 43 _CREATION_ARGS_DEFAULT = ['--name', 'bar', '--desc', 'foo', '--customer-id', |
44 '10', '--ref-id', 'faz', '--workflow-id', '20'] | 44 '10', '--ref-id', 'faz', '--workflow-id', '20'] |
45 _CREATION_ARGS_INVALID_TYPE_1 = ['--name', 'bar', '--desc', 'foo', | 45 _CREATION_ARGS_INVALID_TYPE_1 = ['--name', 'bar', '--desc', 'foo', |
46 '--client-id', 'foo', '--ref-id', 'faz', | 46 '--customer-id', 'foo', '--ref-id', 'faz', |
47 '--workflow-id', '3'] | 47 '--workflow-id', '3'] |
48 _CREATION_ARGS_INVALID_TYPE_2 = ['--name', 'bar', '--desc', 'foo', | 48 _CREATION_ARGS_INVALID_TYPE_2 = ['--name', 'bar', '--desc', 'foo', |
49 '--client-id', '23', '--ref-id', 'faz', | 49 '--customer-id', '23', '--ref-id', 'faz', |
50 '--workflow-id', 'foo'] | 50 '--workflow-id', 'foo'] |
51 | 51 |
52 _UploadArgsNamespace = XtmMockArgs.UploadArgsNamespace | 52 _UploadArgsNamespace = XtmMockArgs.UploadArgsNamespace |
53 _CreationArgsNamespace = XtmMockArgs.CreationArgsNamespace | 53 _CreationArgsNamespace = XtmMockArgs.CreationArgsNamespace |
54 _DownloadArgsNamespace = XtmMockArgs.DownloadArgsNamespace | 54 _DownloadArgsNamespace = XtmMockArgs.DownloadArgsNamespace |
55 | 55 |
56 | 56 |
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] [--api-urlAPI_URL]' | 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 --customer-id CUSTOMER_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]'), |
78 ]) | 78 ]) |
79 def test_usage_messages(args, exp_msg, script_runner): | 79 def test_usage_messages(args, exp_msg, script_runner): |
80 """Test if appropriate usage messages are displayed.""" | 80 """Test if appropriate usage messages are displayed.""" |
81 cmd = list(_CMD_START) | 81 cmd = list(_CMD_START) |
82 cmd.extend(args) | 82 cmd.extend(args) |
83 ret = script_runner.run(*cmd) | 83 ret = script_runner.run(*cmd) |
84 | 84 |
85 usg_msg = ret.stdout.replace('\n', '').replace(' ', '') | 85 usg_msg = ret.stdout.replace('\n', '').replace(' ', '') |
86 | 86 |
87 assert exp_msg.replace(' ', '') in usg_msg | 87 assert exp_msg.replace(' ', '') in usg_msg |
88 | 88 |
89 | 89 |
90 @pytest.mark.script_launch_mode('subprocess') | 90 @pytest.mark.script_launch_mode('subprocess') |
91 @pytest.mark.parametrize('args', [ | 91 @pytest.mark.parametrize('args', [ |
92 ['create', '--name', 'bar', '--desc', 'foo', '--client-id', '1', | 92 ['create', '--name', 'bar', '--desc', 'foo', '--customer-id', '1', |
93 '--ref-id', 'faz', '--workflow-id', '3'], | 93 '--ref-id', 'faz', '--workflow-id', '3'], |
94 ['upload'], | 94 ['upload'], |
95 ['download'], | 95 ['download'], |
96 ]) | 96 ]) |
97 def test_default_source_directory(args, script_runner): | 97 def test_default_source_directory(args, script_runner): |
98 """Test if the source directory if set to default if not provided.""" | 98 """Test if the source directory if set to default if not provided.""" |
99 exp = const.ErrorMessages.NO_TOKEN_PROVIDED.split('\n')[0] | 99 exp = const.ErrorMessages.NO_TOKEN_PROVIDED.split('\n')[0] |
100 cmd = list(_CMD_START) | 100 cmd = list(_CMD_START) |
101 cmd.extend(args) | 101 cmd.extend(args) |
102 | 102 |
103 ret = script_runner.run(*cmd) | 103 ret = script_runner.run(*cmd) |
104 | 104 |
105 assert not ret.success | 105 assert not ret.success |
106 assert exp in ret.stderr | 106 assert exp in ret.stderr |
107 | 107 |
108 | 108 |
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 "--customer-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_no_project)', _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, temp_site_no_project): | 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 | |
135 assert not ret.success | 132 assert not ret.success |
136 assert exp_msg in ret.stderr | 133 assert exp_msg in ret.stderr |
137 | 134 |
138 | 135 |
139 def test_creation_correct(temp_site, intercept, env_valid_token): | 136 def test_creation_correct(temp_site, intercept, env_valid_token): |
140 """Test if a project is created correctly, given the appropriate args.""" | 137 """Test if a project is created correctly, given the appropriate args.""" |
141 namespace = _CreationArgsNamespace() | 138 namespace = _CreationArgsNamespace() |
142 namespace.source_dir = str(temp_site) | 139 namespace.source_dir = str(temp_site) |
143 main_project_handler(namespace) | 140 main_project_handler(namespace) |
144 | 141 |
(...skipping 19 matching lines...) Expand all Loading... |
164 const.Config.PROJECT_OPTION) | 161 const.Config.PROJECT_OPTION) |
165 cnf.write(open(os.path.join(temp_site, 'settings.ini'), 'w')) | 162 cnf.write(open(os.path.join(temp_site, 'settings.ini'), 'w')) |
166 | 163 |
167 | 164 |
168 def test_login(intercept, monkeypatch, capsys): | 165 def test_login(intercept, monkeypatch, capsys): |
169 """Test if the login functionality works as expected.""" | 166 """Test if the login functionality works as expected.""" |
170 exp_output = const.Token.SAVE_COMMAND.format(const.Token.ENV_VAR, | 167 exp_output = const.Token.SAVE_COMMAND.format(const.Token.ENV_VAR, |
171 'TheXTM-APIToken-VALID') | 168 'TheXTM-APIToken-VALID') |
172 monkeypatch.setattr( | 169 monkeypatch.setattr( |
173 'cms.translations.xtm.cli.input_fn', | 170 'cms.translations.xtm.cli.input_fn', |
174 lambda inp: 'admin' if 'username' in inp.lower() else '20', | 171 lambda inp: 'admin' if 'client name' in inp.lower() else '20', |
175 ) | 172 ) |
176 monkeypatch.setattr('getpass.getpass', lambda prompt: 'pass') | 173 monkeypatch.setattr('getpass.getpass', lambda prompt: 'pass') |
177 | 174 |
178 generate_token(XtmMockArgs.LoginArgsNamespace) | 175 generate_token(XtmMockArgs.LoginArgsNamespace) |
179 out, err = capsys.readouterr() | 176 out, err = capsys.readouterr() |
180 | 177 |
181 assert err == '' | 178 assert err == '' |
182 assert exp_output in out | 179 assert exp_output in out |
183 | 180 |
184 | 181 |
185 def test_login_wrong_credentials(intercept, monkeypatch, capsys): | 182 def test_login_wrong_credentials(intercept, monkeypatch, capsys): |
186 """Test exception handling when generating the tokens.""" | 183 """Test exception handling when generating the tokens.""" |
187 monkeypatch.setattr( | 184 monkeypatch.setattr( |
188 'cms.translations.xtm.cli.input_fn', | 185 'cms.translations.xtm.cli.input_fn', |
189 lambda inp: 'foo' if 'username' in inp.lower() else '50', | 186 lambda inp: 'foo' if 'client name' in inp.lower() else '50', |
190 ) | 187 ) |
191 monkeypatch.setattr('getpass.getpass', lambda prompt: 'pass') | 188 monkeypatch.setattr('getpass.getpass', lambda prompt: 'pass') |
192 monkeypatch.setattr('sys.exit', lambda x: sys.stderr.write(str(x))) | 189 monkeypatch.setattr('sys.exit', lambda x: sys.stderr.write(str(x))) |
193 | 190 |
194 generate_token(XtmMockArgs.LoginArgsNamespace) | 191 generate_token(XtmMockArgs.LoginArgsNamespace) |
195 out, err = capsys.readouterr() | 192 out, err = capsys.readouterr() |
196 | 193 |
197 assert 'Invalid credentials' in err | 194 assert 'Invalid credentials' in err |
198 assert out == '' | 195 assert out == '' |
199 | 196 |
(...skipping 15 matching lines...) Expand all Loading... |
215 | 212 |
216 assert not ret.success | 213 assert not ret.success |
217 assert exp_msg in ret.stderr | 214 assert exp_msg in ret.stderr |
218 | 215 |
219 | 216 |
220 def test_upload_too_many_languages(intercept_too_many_targets, | 217 def test_upload_too_many_languages(intercept_too_many_targets, |
221 env_valid_token, temp_site_valid_project): | 218 env_valid_token, temp_site_valid_project): |
222 namespace = _UploadArgsNamespace() | 219 namespace = _UploadArgsNamespace() |
223 namespace.source_dir = str(temp_site_valid_project) | 220 namespace.source_dir = str(temp_site_valid_project) |
224 | 221 |
225 with pytest.raises(Exception) as err: | 222 with pytest.raises(SystemExit) as err: |
226 main_project_handler(namespace) | 223 main_project_handler(namespace) |
227 | 224 |
228 assert 'languages are enabled in the API, but not listed in locales' in \ | 225 assert ('languages are enabled in the API, but not listed in locales' in |
229 str(err.value) | 226 str(err.value)) |
230 | 227 |
231 | 228 |
232 def test_upload_successful(intercept, env_valid_token, | 229 def test_upload_successful(intercept, env_valid_token, |
233 temp_site_valid_project, monkeypatch, capsys): | 230 temp_site_valid_project, monkeypatch, capsys): |
234 namespace = _UploadArgsNamespace() | 231 namespace = _UploadArgsNamespace() |
235 namespace.source_dir = str(temp_site_valid_project) | 232 namespace.source_dir = str(temp_site_valid_project) |
236 monkeypatch.setattr('logging.info', lambda x: sys.stderr.write(x)) | 233 monkeypatch.setattr('logging.info', lambda x: sys.stderr.write(x)) |
237 | 234 |
238 main_project_handler(namespace) | 235 main_project_handler(namespace) |
239 _, err = capsys.readouterr() | 236 _, err = capsys.readouterr() |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 | 274 |
278 main_project_handler(namespace) | 275 main_project_handler(namespace) |
279 | 276 |
280 with open(os.path.join(temp_site_valid_project, 'locales', 'de', | 277 with open(os.path.join(temp_site_valid_project, 'locales', 'de', |
281 'file.json')) as f: | 278 'file.json')) as f: |
282 assert json.dumps({'foo': 'bar', 'faz': 'baz'}) == f.read() | 279 assert json.dumps({'foo': 'bar', 'faz': 'baz'}) == f.read() |
283 | 280 |
284 with open(os.path.join(temp_site_valid_project, 'locales', 'de', 'foo', | 281 with open(os.path.join(temp_site_valid_project, 'locales', 'de', 'foo', |
285 'file-utf8.json'), 'rb') as f: | 282 'file-utf8.json'), 'rb') as f: |
286 assert json.loads(f.read()) == {'foo': '\u1234'} | 283 assert json.loads(f.read()) == {'foo': '\u1234'} |
LEFT | RIGHT |