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 """Tests for the XTM API integration.""" | 16 """Tests for the XTM API integration.""" |
17 | 17 |
18 from __future__ import unicode_literals | 18 from __future__ import unicode_literals |
19 | 19 |
20 import json | 20 import json |
21 import zipfile | 21 import zipfile |
22 from io import BytesIO | 22 from io import BytesIO |
23 | 23 |
24 import pytest | 24 import pytest |
25 | 25 |
26 from cms.translations.xtm.xtm_api import (XTMCloudException, XTMCloudAPI, | 26 from cms.translations.xtm.xtm_api import (XTMCloudException, XTMCloudAPI, |
27 get_token) | 27 get_token) |
| 28 from cms.translations.xtm.constants import API_URL |
28 from tests.utils import exception_test | 29 from tests.utils import exception_test |
29 | 30 |
30 _VALID_TOKEN = 'TheXTM-APIToken-VALID' | 31 _VALID_TOKEN = 'TheXTM-APIToken-VALID' |
31 _INVALID_TOKEN = 'TheXTM-APIToken-INVALID' | 32 _INVALID_TOKEN = 'TheXTM-APIToken-INVALID' |
32 | 33 |
33 _FILES_UPLOAD = {'test.json': json.dumps({'foo': 'bar'})} | 34 _FILES_UPLOAD = {'test.json': json.dumps({'foo': 'bar'})} |
34 _EXPECTED_JOBS = [{'fileName': 'test.json', 'jobId': 1, | 35 _EXPECTED_JOBS = [{'fileName': 'test.json', 'jobId': 1, |
35 'sourceLanguage': 'en_US', 'targetLanguage': 'de_DE'}] | 36 'sourceLanguage': 'en_US', 'targetLanguage': 'de_DE'}] |
36 | 37 |
37 | 38 |
38 @pytest.mark.parametrize('credentials,is_ok,err_msg, exp_token', [ | 39 @pytest.mark.parametrize('credentials,is_ok,err_msg, exp_token', [ |
39 (['admin', 'pass', 20], True, None, 'TheXTM-APIToken-VALID'), | 40 (['admin', 'pass', 20], True, None, 'TheXTM-APIToken-VALID'), |
40 (['admin', 'wrong_pass', 20], False, 'Invalid credentials.', None), | 41 (['admin', 'wrong_pass', 20], False, 'Invalid credentials.', None), |
41 ]) | 42 ]) |
42 def test_token_generation(intercept, credentials, is_ok, err_msg, exp_token): | 43 def test_token_generation(intercept, credentials, is_ok, err_msg, exp_token): |
43 """Test the API token generation.""" | 44 """Test the API token generation.""" |
| 45 args = credentials + [API_URL] |
44 if is_ok: | 46 if is_ok: |
45 token = get_token(*credentials) | 47 token = get_token(*args) |
46 assert token == exp_token | 48 assert token == exp_token |
47 else: | 49 else: |
48 exception_test(get_token, XTMCloudException, err_msg, *credentials) | 50 exception_test(get_token, XTMCloudException, err_msg, *args) |
49 | 51 |
50 | 52 |
51 @pytest.mark.parametrize('token,args,files,exp_msg,exp_jobs', [ | 53 @pytest.mark.parametrize('token,args,files,exp_msg,exp_jobs', [ |
52 (_VALID_TOKEN, ['name', 'description', 'ref_id', ['en_US'], 10, 20], None, | 54 (_VALID_TOKEN, ['name', 'description', 'ref_id', ['en_US'], 10, 20], None, |
53 None, []), | 55 None, []), |
54 (_INVALID_TOKEN, ['name', 'description', 'ref_id', ['en_US'], 10, 20], | 56 (_INVALID_TOKEN, ['name', 'description', 'ref_id', ['en_US'], 10, 20], |
55 None, 'Authentication failed.', []), | 57 None, 'Authentication failed.', []), |
56 (_VALID_TOKEN, ['name', 'description', 'ref_id', ['de_DE'], 10, 20], | 58 (_VALID_TOKEN, ['name', 'description', 'ref_id', ['de_DE'], 10, 20], |
57 _FILES_UPLOAD, None, _EXPECTED_JOBS), | 59 _FILES_UPLOAD, None, _EXPECTED_JOBS), |
58 ]) | 60 ]) |
59 def test_project_creation(intercept, token, files, args, exp_msg, exp_jobs): | 61 def test_project_creation(intercept, token, files, args, exp_msg, exp_jobs): |
60 """Test creation of files behaves appropriately.""" | 62 """Test creation of files behaves appropriately.""" |
61 api = XTMCloudAPI(token) | 63 api = XTMCloudAPI(token, API_URL) |
62 | 64 |
63 if exp_msg: | 65 if exp_msg: |
64 exception_test(api.create_project, XTMCloudException, exp_msg, *args) | 66 exception_test(api.create_project, XTMCloudException, exp_msg, *args) |
65 else: | 67 else: |
66 project_id, jobs = api.create_project(*args, files=files) | 68 project_id, jobs = api.create_project(*args, files=files) |
67 assert project_id == 1234 | 69 assert project_id == 1234 |
68 assert exp_jobs == jobs | 70 assert exp_jobs == jobs |
69 | 71 |
70 | 72 |
71 @pytest.mark.parametrize('token,project_id,exp_langs,exp_msg', [ | 73 @pytest.mark.parametrize('token,project_id,exp_langs,exp_msg', [ |
72 (_VALID_TOKEN, 1234, {'de_DE'}, None), | 74 (_VALID_TOKEN, 1234, {'de_DE'}, None), |
73 (_INVALID_TOKEN, 1234, None, 'Authentication failed.'), | 75 (_INVALID_TOKEN, 1234, None, 'Authentication failed.'), |
74 (_VALID_TOKEN, 1111, None, 'Project not found!'), | 76 (_VALID_TOKEN, 1111, None, 'Project not found!'), |
75 ]) | 77 ]) |
76 def test_extracting_target_languages(intercept, token, project_id, | 78 def test_extracting_target_languages(intercept, token, project_id, |
77 exp_langs, exp_msg): | 79 exp_langs, exp_msg): |
78 """Test extraction of target languages.""" | 80 """Test extraction of target languages.""" |
79 api = XTMCloudAPI(token) | 81 api = XTMCloudAPI(token, API_URL) |
80 | 82 |
81 if exp_msg: | 83 if exp_msg: |
82 exception_test(api.get_target_languages, XTMCloudException, exp_msg, | 84 exception_test(api.get_target_languages, XTMCloudException, exp_msg, |
83 project_id) | 85 project_id) |
84 else: | 86 else: |
85 langs = api.get_target_languages(project_id) | 87 langs = api.get_target_languages(project_id) |
86 | 88 |
87 assert exp_langs == langs | 89 assert exp_langs == langs |
88 | 90 |
89 | 91 |
90 @pytest.mark.parametrize('token,project_id,new_langs,exp_msg', [ | 92 @pytest.mark.parametrize('token,project_id,new_langs,exp_msg', [ |
91 (_VALID_TOKEN, 1234, ['en_GB'], None), | 93 (_VALID_TOKEN, 1234, ['en_GB'], None), |
92 (_INVALID_TOKEN, 1234, ['en_GB'], 'Authentication failed.'), | 94 (_INVALID_TOKEN, 1234, ['en_GB'], 'Authentication failed.'), |
93 (_VALID_TOKEN, 1111, ['en_GB'], 'Project not found!'), | 95 (_VALID_TOKEN, 1111, ['en_GB'], 'Project not found!'), |
94 (_VALID_TOKEN, 1234, ['foo_BAR'], 'Unsupported language: foo_BAR'), | 96 (_VALID_TOKEN, 1234, ['foo_BAR'], 'Unsupported language: foo_BAR'), |
95 ]) | 97 ]) |
96 def test_adding_target_language(intercept, token, project_id, new_langs, | 98 def test_adding_target_language(intercept, token, project_id, new_langs, |
97 exp_msg): | 99 exp_msg): |
98 """Test adding target languages.""" | 100 """Test adding target languages.""" |
99 api = XTMCloudAPI(token) | 101 api = XTMCloudAPI(token, API_URL) |
100 | 102 |
101 if exp_msg: | 103 if exp_msg: |
102 exception_test(api.add_target_languages, XTMCloudException, exp_msg, | 104 exception_test(api.add_target_languages, XTMCloudException, exp_msg, |
103 project_id, new_langs) | 105 project_id, new_langs) |
104 else: | 106 else: |
105 api.add_target_languages(project_id, new_langs) | 107 api.add_target_languages(project_id, new_langs) |
106 | 108 |
107 | 109 |
108 @pytest.mark.parametrize('token,project_id,exp_msg,', [ | 110 @pytest.mark.parametrize('token,project_id,exp_msg,', [ |
109 (_INVALID_TOKEN, 1234, 'Authentication failed'), | 111 (_INVALID_TOKEN, 1234, 'Authentication failed'), |
110 (_VALID_TOKEN, 1111, 'Project not found'), | 112 (_VALID_TOKEN, 1111, 'Project not found'), |
111 (_VALID_TOKEN, 1234, None), | 113 (_VALID_TOKEN, 1234, None), |
112 ]) | 114 ]) |
113 def test_file_download(intercept_populated, token, project_id, exp_msg): | 115 def test_file_download(intercept_populated, token, project_id, exp_msg): |
114 """Test file downloading.""" | 116 """Test file downloading.""" |
115 api = XTMCloudAPI(token) | 117 api = XTMCloudAPI(token, API_URL) |
116 | 118 |
117 if exp_msg: | 119 if exp_msg: |
118 exception_test(api.download_files, XTMCloudException, exp_msg, | 120 exception_test(api.download_files, XTMCloudException, exp_msg, |
119 project_id) | 121 project_id) |
120 else: | 122 else: |
121 contents = api.download_files(project_id) | 123 contents = api.download_files(project_id) |
122 zipfile.ZipFile(BytesIO(contents)) | 124 zipfile.ZipFile(BytesIO(contents)) |
123 | 125 |
124 | 126 |
125 @pytest.mark.parametrize('token,project_id,files,exp_err,exp_msg,exp_jobs', [ | 127 @pytest.mark.parametrize('token,project_id,files,exp_err,exp_msg,exp_jobs', [ |
126 (_VALID_TOKEN, 1234, [], Exception, 'No files provided for upload.', []), | 128 (_VALID_TOKEN, 1234, [], Exception, 'No files provided for upload.', []), |
127 (_INVALID_TOKEN, 1234, _FILES_UPLOAD, XTMCloudException, 'Authentication ' | 129 (_INVALID_TOKEN, 1234, _FILES_UPLOAD, XTMCloudException, 'Authentication ' |
128 'failed', []), | 130 'failed', []), |
129 (_VALID_TOKEN, 1111, _FILES_UPLOAD, XTMCloudException, 'Project not ' | 131 (_VALID_TOKEN, 1111, _FILES_UPLOAD, XTMCloudException, 'Project not ' |
130 'found', []), | 132 'found', []), |
131 (_VALID_TOKEN, 1234, _FILES_UPLOAD, None, None, _EXPECTED_JOBS), | 133 (_VALID_TOKEN, 1234, _FILES_UPLOAD, None, None, _EXPECTED_JOBS), |
132 ]) | 134 ]) |
133 def test_file_upload(intercept, token, project_id, files, exp_err, exp_msg, | 135 def test_file_upload(intercept, token, project_id, files, exp_err, exp_msg, |
134 exp_jobs): | 136 exp_jobs): |
135 """Test file uploading.""" | 137 """Test file uploading.""" |
136 api = XTMCloudAPI(token) | 138 api = XTMCloudAPI(token, API_URL) |
137 if exp_msg is not None: | 139 if exp_msg is not None: |
138 exception_test(api.upload_files, exp_err, exp_msg, files, project_id) | 140 exception_test(api.upload_files, exp_err, exp_msg, files, project_id) |
139 else: | 141 else: |
140 assert exp_jobs == api.upload_files(files, project_id) | 142 assert exp_jobs == api.upload_files(files, project_id) |
141 | 143 |
142 | 144 |
143 @pytest.mark.parametrize('token,name,exp_err,exp_msg,exp_ids', [ | 145 @pytest.mark.parametrize('token,name,exp_err,exp_msg,exp_ids', [ |
144 (_INVALID_TOKEN, 'foo', XTMCloudException, 'Authentication failed', None), | 146 (_INVALID_TOKEN, 'foo', XTMCloudException, 'Authentication failed', None), |
145 (_VALID_TOKEN, 'workflow', None, None, []), | 147 (_VALID_TOKEN, 'workflow', None, None, []), |
146 (_VALID_TOKEN, 'workflow1', None, None, [2222]), | 148 (_VALID_TOKEN, 'workflow1', None, None, [2222]), |
147 ]) | 149 ]) |
148 def test_workflow_id_extraction(intercept, token, name, exp_err, | 150 def test_workflow_id_extraction(intercept, token, name, exp_err, |
149 exp_msg, exp_ids): | 151 exp_msg, exp_ids): |
150 api = XTMCloudAPI(token) | 152 api = XTMCloudAPI(token, API_URL) |
151 if exp_msg is not None: | 153 if exp_msg is not None: |
152 exception_test(api.get_workflows_by_name, exp_err, exp_msg, name) | 154 exception_test(api.get_workflows_by_name, exp_err, exp_msg, name) |
153 else: | 155 else: |
154 assert exp_ids == api.get_workflows_by_name(name) | 156 assert exp_ids == api.get_workflows_by_name(name) |
OLD | NEW |