Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: tests/test_xtm_translations_utils.py

Issue 29968558: Issue 7037 - [XTM Integration] Make REST API url customizable
Patch Set: Addressed comments Created Sept. 17, 2019, 12:22 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tests/test_xtm_translate.py ('k') | tests/utils.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 10 matching lines...) Expand all
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, XtmMockArgs 27 from tests.utils import exception_test, XtmMockArgs
28 28
29 _API_TOKEN_VALID = 'TheXTM-APIToken-VALID' 29 _API_TOKEN_VALID = 'TheXTM-APIToken-VALID'
30 _PROJECT_ID = 1234 30 _PROJECT_ID = 1234
31 _API_URL = const.API_URL
31 32
32 33
33 @pytest.fixture 34 @pytest.fixture
34 def toydir(tmpdir): 35 def toydir(tmpdir):
35 """Toy directory fixture with two locales - 'de' and 'en'.""" 36 """Toy directory fixture with two locales - 'de' and 'en'."""
36 toydir = tmpdir.mkdir('toydir') 37 toydir = tmpdir.mkdir('toydir')
37 38
38 en_dir = toydir.mkdir('en') 39 en_dir = toydir.mkdir('en')
39 en_dir.join('file.json').write(json.dumps({'a': 'b'})) 40 en_dir.join('file.json').write(json.dumps({'a': 'b'}))
40 41
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 utils.run_and_wait(func, Exception, exp_msg, retries=1) 151 utils.run_and_wait(func, Exception, exp_msg, retries=1)
151 152
152 assert exp_msg == str(err.value) 153 assert exp_msg == str(err.value)
153 154
154 155
155 def test_resolve_locales(intercept_populated, temp_site): 156 def test_resolve_locales(intercept_populated, temp_site):
156 """Test if locales are resolved correctly. 157 """Test if locales are resolved correctly.
157 158
158 In this environment, no languages have to be added. 159 In this environment, no languages have to be added.
159 """ 160 """
160 api = XTMCloudAPI(_API_TOKEN_VALID) 161 api = XTMCloudAPI(_API_TOKEN_VALID, _API_URL)
161 source = FileSource(str(temp_site)) 162 source = FileSource(str(temp_site))
162 source.write_to_config('XTM', 'project_id', str(_PROJECT_ID)) 163 source.write_to_config('XTM', 'project_id', str(_PROJECT_ID))
163 164
164 utils.resolve_locales(api, source) 165 utils.resolve_locales(api, source)
165 166
166 167
167 def test_resolve_locales_adds_langs(intercept, temp_site): 168 def test_resolve_locales_adds_langs(intercept, temp_site):
168 """Test if locales are resolved correctly. 169 """Test if locales are resolved correctly.
169 170
170 In this environment, new target languages are added. 171 In this environment, new target languages are added.
171 """ 172 """
172 api = XTMCloudAPI(_API_TOKEN_VALID) 173 api = XTMCloudAPI(_API_TOKEN_VALID, _API_URL)
173 source = FileSource(str(temp_site)) 174 source = FileSource(str(temp_site))
174 source.write_to_config('XTM', 'project_id', str(_PROJECT_ID)) 175 source.write_to_config('XTM', 'project_id', str(_PROJECT_ID))
175 exp_targets = {'de_DE'} 176 exp_targets = {'de_DE'}
176 177
177 utils.resolve_locales(api, source) 178 utils.resolve_locales(api, source)
178 179
179 assert exp_targets == api.get_target_languages(_PROJECT_ID) 180 assert exp_targets == api.get_target_languages(_PROJECT_ID)
180 181
181 182
182 def test_resolve_locales_raise_exception(intercept_populated, temp_site): 183 def test_resolve_locales_raise_exception(intercept_populated, temp_site):
183 """Test if locales are resolved correctly. 184 """Test if locales are resolved correctly.
184 185
185 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
186 available online. We except an exception to be raised. 187 available online. We except an exception to be raised.
187 """ 188 """
188 api = XTMCloudAPI(_API_TOKEN_VALID) 189 api = XTMCloudAPI(_API_TOKEN_VALID, _API_URL)
189 api.add_target_languages(_PROJECT_ID, ['ro_RO']) 190 api.add_target_languages(_PROJECT_ID, ['ro_RO'])
190 source = FileSource(str(temp_site)) 191 source = FileSource(str(temp_site))
191 source.write_to_config('XTM', 'project_id', str(_PROJECT_ID)) 192 source.write_to_config('XTM', 'project_id', str(_PROJECT_ID))
192 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 '
193 "listed in locales: set(['ro_RO'])! Please remove them manually" 194 "listed in locales: set(['ro_RO'])! Please remove them manually"
194 ' from project number 1234 and then re-run the script!') 195 ' from project number 1234 and then re-run the script!')
195 196
196 exception_test(utils.resolve_locales, Exception, exp_msg, api, source) 197 exception_test(utils.resolve_locales, Exception, exp_msg, api, source)
197 198
198 199
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 const.ErrorMessages.NO_WORKFLOW_FOR_NAME.format('foo'), None), 231 const.ErrorMessages.NO_WORKFLOW_FOR_NAME.format('foo'), None),
231 (1234, None, None, None, 1234), 232 (1234, None, None, None, 1234),
232 (None, 'workflow1', None, None, 2222), 233 (None, 'workflow1', None, None, 2222),
233 ]) 234 ])
234 def test_handle_workflows(intercept, id_in, name, exp_err, exp_msg, exp_id): 235 def test_handle_workflows(intercept, id_in, name, exp_err, exp_msg, exp_id):
235 """""" 236 """"""
236 namespace = XtmMockArgs.CreationArgsNamespace() 237 namespace = XtmMockArgs.CreationArgsNamespace()
237 namespace.workflow_id = id_in 238 namespace.workflow_id = id_in
238 namespace.workflow_name = name 239 namespace.workflow_name = name
239 240
240 api = XTMCloudAPI(_API_TOKEN_VALID) 241 api = XTMCloudAPI(_API_TOKEN_VALID, _API_URL)
241 242
242 if exp_err: 243 if exp_err:
243 exception_test(utils.extract_workflow_id, exp_err, exp_msg, api, 244 exception_test(utils.extract_workflow_id, exp_err, exp_msg, api,
244 namespace) 245 namespace)
245 else: 246 else:
246 id_out = utils.extract_workflow_id(api, namespace) 247 id_out = utils.extract_workflow_id(api, namespace)
247 248
248 assert id_out == exp_id 249 assert id_out == exp_id
249 250
250 251
251 def test_extract_unicode_strings(temp_site): 252 def test_extract_unicode_strings(temp_site):
252 """Test correct extraction of unicode strings for translation.""" 253 """Test correct extraction of unicode strings for translation."""
253 with FileSource(temp_site) as fs: 254 with FileSource(temp_site) as fs:
254 strings = utils.extract_strings(fs) 255 strings = utils.extract_strings(fs)
255 256
256 assert '\u0376' in strings['translate-unicode']['simple']['message'] 257 assert '\u0376' in strings['translate-unicode']['simple']['message']
258
259
260 def test_get_api_url_from_arguments(temp_site):
261 with FileSource(temp_site) as fs:
262 args = XtmMockArgs.LoginArgsNamespace()
263 args.api_url = 'foo'
264
265 assert utils.get_api_url(args, fs) == 'foo'
266
267
268 def test_get_api_url_from_source(temp_site_url_in_config):
269 with FileSource(temp_site_url_in_config) as fs:
270 url = utils.get_api_url(XtmMockArgs.LoginArgsNamespace, fs)
271 assert url == 'www.bar.com'
272
273
274 def test_get_api_url_hardcoded():
275 assert utils.get_api_url(XtmMockArgs.LoginArgsNamespace, None) == \
276 const.API_URL
OLDNEW
« no previous file with comments | « tests/test_xtm_translate.py ('k') | tests/utils.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld