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