| OLD | NEW | 
|---|
| (Empty) |  | 
|  | 1 # This file is part of the Adblock Plus web scripts, | 
|  | 2 # Copyright (C) 2006-present eyeo GmbH | 
|  | 3 # | 
|  | 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 | 
|  | 6 # published by the Free Software Foundation. | 
|  | 7 # | 
|  | 8 # Adblock Plus is distributed in the hope that it will be useful, | 
|  | 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 11 # GNU General Public License for more details. | 
|  | 12 # | 
|  | 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/>. | 
|  | 15 | 
|  | 16 import json | 
|  | 17 import os | 
|  | 18 | 
|  | 19 import pytest | 
|  | 20 | 
|  | 21 from cms.bin.xtm_translations import utils | 
|  | 22 import cms.bin.xtm_translations.constants as const | 
|  | 23 from cms.sources import FileSource | 
|  | 24 from cms.bin.xtm_translations.xtm_api import XTMCloudAPI | 
|  | 25 from tests.utils import exception_test | 
|  | 26 | 
|  | 27 _API_TOKEN = 'TheXTM-APIToken-VALID' | 
|  | 28 _PROJECT_ID = 1234 | 
|  | 29 | 
|  | 30 | 
|  | 31 @pytest.fixture | 
|  | 32 def toydir(tmpdir): | 
|  | 33     """Toy directory fixture with two locales - 'de' and 'en'.""" | 
|  | 34     toydir = tmpdir.mkdir('toydir') | 
|  | 35 | 
|  | 36     en_dir = toydir.mkdir('en') | 
|  | 37     en_dir.join('file.json').write(json.dumps({'a': 'b'})) | 
|  | 38 | 
|  | 39     de_dir = toydir.mkdir('de') | 
|  | 40     de_dir.join('file.json').write(json.dumps({'a': 'b'})) | 
|  | 41     de_dir.join('file.txt').write('test') | 
|  | 42 | 
|  | 43     return toydir | 
|  | 44 | 
|  | 45 | 
|  | 46 def test_get_files_to_upload(temp_site): | 
|  | 47     """Generation of correct datatype for the XTMCloudAPI class.""" | 
|  | 48     files = utils.get_files_to_upload(FileSource(temp_site)) | 
|  | 49 | 
|  | 50     assert 'translate.json' in files | 
|  | 51     assert 'translate-not-enough.json' in files | 
|  | 52 | 
|  | 53 | 
|  | 54 def test_read_token_invalid(): | 
|  | 55     """Test if appropriate exception is raised when no token is found.""" | 
|  | 56     exp_msg = const.ErrorMessages.NO_TOKEN_PROVIDED.format( | 
|  | 57         const.Token.CREATION_CMD, | 
|  | 58     ) | 
|  | 59     with pytest.raises(Exception) as err: | 
|  | 60         utils.read_token() | 
|  | 61     assert exp_msg in str(err.value) | 
|  | 62 | 
|  | 63 | 
|  | 64 def test_read_token_valid(): | 
|  | 65     """Test if the token is read correctly when the env is setup.""" | 
|  | 66     env_var = const.Token.ENV_VAR | 
|  | 67     token = 'test_token' | 
|  | 68     os.environ[env_var] = token | 
|  | 69 | 
|  | 70     try: | 
|  | 71         assert token == utils.read_token() | 
|  | 72     finally: | 
|  | 73         del os.environ[env_var] | 
|  | 74 | 
|  | 75 | 
|  | 76 def test_sanitize_project_name_characters(): | 
|  | 77     """Test if invalid name characters are replaced as expected.""" | 
|  | 78     test_in = '{0}{1}{0}'.format( | 
|  | 79         'test', ''.join(const.ProjectName.INVALID_CHARS), | 
|  | 80     ) | 
|  | 81     exp_out = '{0}{1}{0}'.format( | 
|  | 82         'test', | 
|  | 83         const.ProjectName.NAME_WILDCARD * | 
|  | 84         len(const.ProjectName.INVALID_CHARS), | 
|  | 85     ) | 
|  | 86 | 
|  | 87     assert exp_out == utils.sanitize_project_name(test_in) | 
|  | 88 | 
|  | 89 | 
|  | 90 def test_sanitize_project_name_length(): | 
|  | 91     """Test if names that are too long are truncated as expected.""" | 
|  | 92     test_in = 'a' * (const.ProjectName.MAX_LENGTH + 10) | 
|  | 93     exp_out = 'a' * const.ProjectName.MAX_LENGTH | 
|  | 94 | 
|  | 95     assert exp_out == utils.sanitize_project_name(test_in) | 
|  | 96 | 
|  | 97 | 
|  | 98 def test_map_locales(temp_site): | 
|  | 99     """Test if a local website's languages are mapped to XTM's format.""" | 
|  | 100     test_source = FileSource(str(temp_site)) | 
|  | 101     exp_out = {'de_DE'} | 
|  | 102 | 
|  | 103     assert exp_out == utils.map_locales(test_source) | 
|  | 104 | 
|  | 105 | 
|  | 106 def test_remote_to_local_good(temp_site): | 
|  | 107     """Test if a remote filename is resolved to a valid OS path.""" | 
|  | 108     test_in = '___foo___bar___faz___test.json' | 
|  | 109     exp_out = os.path.join(str(temp_site), 'foo', 'bar', 'faz', 'test.json') | 
|  | 110 | 
|  | 111     assert exp_out == utils.remote_to_local(test_in, str(temp_site), | 
|  | 112                                             locales=['foo']) | 
|  | 113 | 
|  | 114 | 
|  | 115 def test_remote_to_local_exception(temp_site): | 
|  | 116     test_in = '___foo___bar___faz___test.json' | 
|  | 117     exp_msg = const.ErrorMessages.CANT_RESOLVE_REMOTE_LANG.format('foo') | 
|  | 118 | 
|  | 119     exception_test(utils.remote_to_local, Exception, exp_msg, test_in, | 
|  | 120                    str(temp_site), locales=['bar']) | 
|  | 121 | 
|  | 122 | 
|  | 123 def test_run_and_wait_no_err(): | 
|  | 124     exp_out = 'test' | 
|  | 125 | 
|  | 126     def func(): | 
|  | 127         return exp_out | 
|  | 128 | 
|  | 129     assert exp_out == utils.run_and_wait(func, Exception, '') | 
|  | 130 | 
|  | 131 | 
|  | 132 def test_run_and_wait_with_params(): | 
|  | 133     test_in = 'test' | 
|  | 134 | 
|  | 135     def func(a): | 
|  | 136         return a | 
|  | 137 | 
|  | 138     assert test_in == utils.run_and_wait(func, Exception, '', a=test_in) | 
|  | 139 | 
|  | 140 | 
|  | 141 @pytest.mark.slow_test | 
|  | 142 def test_run_and_wait_infinite(): | 
|  | 143     exp_msg = 'ERROR' | 
|  | 144 | 
|  | 145     def func(): | 
|  | 146         raise Exception(exp_msg) | 
|  | 147 | 
|  | 148     with pytest.raises(Exception) as err: | 
|  | 149         utils.run_and_wait(func, Exception, exp_msg, retries=1) | 
|  | 150 | 
|  | 151     assert exp_msg == str(err.value) | 
|  | 152 | 
|  | 153 | 
|  | 154 def test_resolve_locales(intercept_populated, temp_site): | 
|  | 155     """Test if locales are resolved correctly. | 
|  | 156 | 
|  | 157     In this environment, no languages have to be added. | 
|  | 158     """ | 
|  | 159     api = XTMCloudAPI(_API_TOKEN) | 
|  | 160     source = FileSource(str(temp_site)) | 
|  | 161     source.write_to_config('XTM', 'project_id', str(_PROJECT_ID)) | 
|  | 162 | 
|  | 163     utils.resolve_locales(api, source) | 
|  | 164 | 
|  | 165 | 
|  | 166 def test_resolve_locales_adds_langs(intercept, temp_site): | 
|  | 167     """Test if locales are resolved correctly. | 
|  | 168 | 
|  | 169     In this environment, new target languages are added. | 
|  | 170     """ | 
|  | 171     api = XTMCloudAPI(_API_TOKEN) | 
|  | 172     source = FileSource(str(temp_site)) | 
|  | 173     source.write_to_config('XTM', 'project_id', str(_PROJECT_ID)) | 
|  | 174     exp_targets = {'de_DE'} | 
|  | 175 | 
|  | 176     utils.resolve_locales(api, source) | 
|  | 177 | 
|  | 178     assert exp_targets == api.get_target_languages(_PROJECT_ID) | 
|  | 179 | 
|  | 180 | 
|  | 181 def test_resolve_locales_raise_exception(intercept_populated, temp_site): | 
|  | 182     """Test if locales are resolved correctly. | 
|  | 183 | 
|  | 184     In this environment, the API has more target languages configured than are | 
|  | 185     available online. We except an exception to be raised. | 
|  | 186     """ | 
|  | 187     api = XTMCloudAPI(_API_TOKEN) | 
|  | 188     api.add_target_languages(_PROJECT_ID, ['ro_RO']) | 
|  | 189     source = FileSource(str(temp_site)) | 
|  | 190     source.write_to_config('XTM', 'project_id', str(_PROJECT_ID)) | 
|  | 191     exp_msg = ('The following languages are enabled in the API, but not ' | 
|  | 192                "listed in locales: set(['ro_RO'])! Please remove them manually" | 
|  | 193                ' from project number 1234 and then re-run the script!') | 
|  | 194 | 
|  | 195     exception_test(utils.resolve_locales, Exception, exp_msg, api, source) | 
|  | 196 | 
|  | 197 | 
|  | 198 def test_list_locales(toydir): | 
|  | 199     """Test if the the locales are listed correctly.""" | 
|  | 200     assert utils.get_locales(str(toydir), 'en') == ['de'] | 
|  | 201 | 
|  | 202 | 
|  | 203 def test_clear_files(toydir): | 
|  | 204     """Test if local translation files are deleted, as expected.""" | 
|  | 205     required_locales = ['de'] | 
|  | 206 | 
|  | 207     utils.clear_files(str(toydir), required_locales) | 
|  | 208 | 
|  | 209     assert os.path.exists(str(toydir.join('en/file.json'))) | 
|  | 210     assert os.path.isfile(str(toydir.join('de/file.txt'))) | 
|  | 211     assert not os.path.exists(str(toydir.join('de/file.json'))) | 
|  | 212 | 
|  | 213 | 
|  | 214 @pytest.mark.parametrize('path', ['de/test.json', 'de/dir1/dir2/test.json']) | 
|  | 215 def test_write_data(toydir, path): | 
|  | 216     """Test if writing data to files works as expected.""" | 
|  | 217     data = bytes(json.dumps({'a': 'b'})) | 
|  | 218 | 
|  | 219     utils.write_to_file(data, str(toydir.join(path))) | 
|  | 220 | 
|  | 221     assert toydir.join(path).read('rb') == data | 
| OLD | NEW | 
|---|