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 |
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 json | 18 import json |
19 import os | 19 import os |
20 | 20 |
21 import pytest | 21 import pytest |
22 | 22 |
23 from cms.translations.xtm import utils | 23 from cms.translations.xtm import utils |
24 import cms.translations.xtm.constants as const | 24 import cms.translations.xtm.constants as const |
25 from cms.sources import FileSource | 25 from cms.sources import FileSource |
26 from cms.translations.xtm.xtm_api import XTMCloudAPI | 26 from cms.translations.xtm.xtm_api import XTMCloudAPI |
27 from tests.utils import exception_test | 27 from tests.utils import exception_test, XtmMockArgs |
28 | 28 |
29 _API_TOKEN = 'TheXTM-APIToken-VALID' | 29 _API_TOKEN_VALID = 'TheXTM-APIToken-VALID' |
30 _PROJECT_ID = 1234 | 30 _PROJECT_ID = 1234 |
31 | 31 |
32 | 32 |
33 @pytest.fixture | 33 @pytest.fixture |
34 def toydir(tmpdir): | 34 def toydir(tmpdir): |
35 """Toy directory fixture with two locales - 'de' and 'en'.""" | 35 """Toy directory fixture with two locales - 'de' and 'en'.""" |
36 toydir = tmpdir.mkdir('toydir') | 36 toydir = tmpdir.mkdir('toydir') |
37 | 37 |
38 en_dir = toydir.mkdir('en') | 38 en_dir = toydir.mkdir('en') |
39 en_dir.join('file.json').write(json.dumps({'a': 'b'})) | 39 en_dir.join('file.json').write(json.dumps({'a': 'b'})) |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 utils.run_and_wait(func, Exception, exp_msg, retries=1) | 151 utils.run_and_wait(func, Exception, exp_msg, retries=1) |
152 | 152 |
153 assert exp_msg == str(err.value) | 153 assert exp_msg == str(err.value) |
154 | 154 |
155 | 155 |
156 def test_resolve_locales(intercept_populated, temp_site): | 156 def test_resolve_locales(intercept_populated, temp_site): |
157 """Test if locales are resolved correctly. | 157 """Test if locales are resolved correctly. |
158 | 158 |
159 In this environment, no languages have to be added. | 159 In this environment, no languages have to be added. |
160 """ | 160 """ |
161 api = XTMCloudAPI(_API_TOKEN) | 161 api = XTMCloudAPI(_API_TOKEN_VALID) |
162 source = FileSource(str(temp_site)) | 162 source = FileSource(str(temp_site)) |
163 source.write_to_config('XTM', 'project_id', str(_PROJECT_ID)) | 163 source.write_to_config('XTM', 'project_id', str(_PROJECT_ID)) |
164 | 164 |
165 utils.resolve_locales(api, source) | 165 utils.resolve_locales(api, source) |
166 | 166 |
167 | 167 |
168 def test_resolve_locales_adds_langs(intercept, temp_site): | 168 def test_resolve_locales_adds_langs(intercept, temp_site): |
169 """Test if locales are resolved correctly. | 169 """Test if locales are resolved correctly. |
170 | 170 |
171 In this environment, new target languages are added. | 171 In this environment, new target languages are added. |
172 """ | 172 """ |
173 api = XTMCloudAPI(_API_TOKEN) | 173 api = XTMCloudAPI(_API_TOKEN_VALID) |
174 source = FileSource(str(temp_site)) | 174 source = FileSource(str(temp_site)) |
175 source.write_to_config('XTM', 'project_id', str(_PROJECT_ID)) | 175 source.write_to_config('XTM', 'project_id', str(_PROJECT_ID)) |
176 exp_targets = {'de_DE'} | 176 exp_targets = {'de_DE'} |
177 | 177 |
178 utils.resolve_locales(api, source) | 178 utils.resolve_locales(api, source) |
179 | 179 |
180 assert exp_targets == api.get_target_languages(_PROJECT_ID) | 180 assert exp_targets == api.get_target_languages(_PROJECT_ID) |
181 | 181 |
182 | 182 |
183 def test_resolve_locales_raise_exception(intercept_populated, temp_site): | 183 def test_resolve_locales_raise_exception(intercept_populated, temp_site): |
184 """Test if locales are resolved correctly. | 184 """Test if locales are resolved correctly. |
185 | 185 |
186 In this environment, the API has more target languages configured than are | 186 In this environment, the API has more target languages configured than are |
187 available online. We except an exception to be raised. | 187 available online. We except an exception to be raised. |
188 """ | 188 """ |
189 api = XTMCloudAPI(_API_TOKEN) | 189 api = XTMCloudAPI(_API_TOKEN_VALID) |
190 api.add_target_languages(_PROJECT_ID, ['ro_RO']) | 190 api.add_target_languages(_PROJECT_ID, ['ro_RO']) |
191 source = FileSource(str(temp_site)) | 191 source = FileSource(str(temp_site)) |
192 source.write_to_config('XTM', 'project_id', str(_PROJECT_ID)) | 192 source.write_to_config('XTM', 'project_id', str(_PROJECT_ID)) |
193 exp_msg = ('The following languages are enabled in the API, but not ' | 193 exp_msg = ('The following languages are enabled in the API, but not ' |
194 "listed in locales: set(['ro_RO'])! Please remove them manually" | 194 "listed in locales: set(['ro_RO'])! Please remove them manually" |
195 ' from project number 1234 and then re-run the script!') | 195 ' from project number 1234 and then re-run the script!') |
196 | 196 |
197 exception_test(utils.resolve_locales, Exception, exp_msg, api, source) | 197 exception_test(utils.resolve_locales, Exception, exp_msg, api, source) |
198 | 198 |
199 | 199 |
(...skipping 16 matching lines...) Expand all Loading... |
216 @pytest.mark.parametrize('path', ['de/test.json', 'de/dir1/dir2/test.json']) | 216 @pytest.mark.parametrize('path', ['de/test.json', 'de/dir1/dir2/test.json']) |
217 def test_write_data(toydir, path): | 217 def test_write_data(toydir, path): |
218 """Test if writing data to files works as expected.""" | 218 """Test if writing data to files works as expected.""" |
219 data = bytes(json.dumps({'a': 'b'})) | 219 data = bytes(json.dumps({'a': 'b'})) |
220 | 220 |
221 utils.write_to_file(data, str(toydir.join(path))) | 221 utils.write_to_file(data, str(toydir.join(path))) |
222 | 222 |
223 assert toydir.join(path).read('rb') == data | 223 assert toydir.join(path).read('rb') == data |
224 | 224 |
225 | 225 |
| 226 @pytest.mark.parametrize('id_in,name,exp_err,exp_msg,exp_id', [ |
| 227 (1234, 'foo', Exception, |
| 228 const.ErrorMessages.WORKFLOW_NAME_AND_ID_PROVIDED, None), |
| 229 (None, None, Exception, const.ErrorMessages.NO_WORKFLOW_INFO, None), |
| 230 (None, 'foo', Exception, |
| 231 const.ErrorMessages.NO_WORKFLOW_FOR_NAME.format('foo'), None), |
| 232 (1234, None, None, None, 1234), |
| 233 (None, 'workflow1', None, None, 2222), |
| 234 ]) |
| 235 def test_handle_workflows(intercept, id_in, name, exp_err, exp_msg, exp_id): |
| 236 """""" |
| 237 namespace = XtmMockArgs.CreationArgsNamespace() |
| 238 namespace.workflow_id = id_in |
| 239 namespace.workflow_name = name |
| 240 |
| 241 api = XTMCloudAPI(_API_TOKEN_VALID) |
| 242 |
| 243 if exp_err: |
| 244 exception_test(utils.extract_workflow_id, exp_err, exp_msg, api, |
| 245 namespace) |
| 246 else: |
| 247 id_out = utils.extract_workflow_id(api, namespace) |
| 248 |
| 249 assert id_out == exp_id |
| 250 |
| 251 |
226 def test_extract_unicode_strings(temp_site): | 252 def test_extract_unicode_strings(temp_site): |
227 """Test correct extraction of unicode strings for translation.""" | 253 """Test correct extraction of unicode strings for translation.""" |
228 with FileSource(temp_site) as fs: | 254 with FileSource(temp_site) as fs: |
229 strings = utils.extract_strings(fs) | 255 strings = utils.extract_strings(fs) |
230 | 256 |
231 assert '\u0376' in strings['translate-unicode']['simple']['message'] | 257 assert '\u0376' in strings['translate-unicode']['simple']['message'] |
OLD | NEW |