Left: | ||
Right: |
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 """Tests for the XTM API integration.""" | |
Vasily Kuznetsov
2018/09/26 15:45:26
Nit: empty lines
Tudor Avram
2018/10/04 06:48:13
Also switched the order of the imports :D
Done.
| |
16 from __future__ import unicode_literals | |
17 | |
18 import pytest | |
19 import json | |
20 | |
21 import zipfile | |
22 from io import BytesIO | |
23 | |
24 from cms.bin.xtm_translations.xtm_api import (XTMCloudException, XTMCloudAPI, | |
25 get_token) | |
26 from tests.utils import exception_test | |
27 | |
28 _VALID_TOKEN = 'TheXTM-APIToken-VALID' | |
29 _INVALID_TOKEN = 'TheXTM-APIToken-INVALID' | |
30 | |
31 _FILES_UPLOAD = [('test.json', json.dumps({'foo': 'bar'}))] | |
32 _EXPECTED_JOBS = [{'fileName': 'test.json', 'jobId': 1, | |
33 'sourceLanguage': 'en_US', 'targetLanguage': 'de_DE'}] | |
34 | |
35 | |
36 @pytest.mark.parametrize('credentials,is_ok,err_msg, exp_token', [ | |
37 (['admin', 'pass', 20], True, None, 'TheXTM-APIToken-VALID'), | |
38 (['admin', 'wrong_pass', 20], False, 'Invalid credentials.', None), | |
39 ]) | |
40 def test_token_generation(intercept, credentials, is_ok, err_msg, exp_token): | |
41 """Test if the API token generation behaves accordingly.""" | |
42 if is_ok: | |
43 token = get_token(*credentials) | |
44 assert token == exp_token | |
45 else: | |
46 exception_test(get_token, XTMCloudException, err_msg, *credentials) | |
47 | |
48 | |
49 @pytest.mark.parametrize('token,args,files,exp_msg,exp_jobs', [ | |
50 (_VALID_TOKEN, ['name', 'description', 'ref_id', ['en_US'], 10, 20], None, | |
51 None, []), | |
52 (_INVALID_TOKEN, ['name', 'description', 'ref_id', ['en_US'], 10, 20], | |
53 None, 'Authentication failed.', []), | |
54 (_VALID_TOKEN, ['name', 'description', 'ref_id', ['de_DE'], 10, 20], | |
55 _FILES_UPLOAD, None, _EXPECTED_JOBS), | |
56 ]) | |
57 def test_project_creation(intercept, token, files, args, exp_msg, exp_jobs): | |
58 """Test if the creation of files behaves appropriately.""" | |
Vasily Kuznetsov
2018/09/26 15:45:26
Nit: this could be just "Test creation of files" -
Tudor Avram
2018/10/04 06:48:13
Done.
Vasily Kuznetsov
2018/10/05 10:56:26
Nice, but you still left "behaves appropriately" i
| |
59 api = XTMCloudAPI(token) | |
60 | |
61 if exp_msg: | |
62 exception_test(api.create_project, XTMCloudException, exp_msg, *args) | |
63 else: | |
64 project_id, jobs = api.create_project(*args, files=files) | |
65 assert project_id == 1234 | |
66 assert exp_jobs == jobs | |
67 | |
68 | |
69 @pytest.mark.parametrize('token,project_id,exp_langs,exp_msg', [ | |
70 (_VALID_TOKEN, 1234, {'de_DE'}, None), | |
71 (_INVALID_TOKEN, 1234, None, 'Authentication failed.'), | |
72 (_VALID_TOKEN, 1111, None, 'Project not found!'), | |
73 ]) | |
74 def test_extracting_target_languages(intercept, token, project_id, | |
75 exp_langs, exp_msg): | |
76 """Test if the target languages are extracted correctly.""" | |
77 api = XTMCloudAPI(token) | |
78 | |
79 if exp_msg: | |
80 exception_test(api.get_target_languages, XTMCloudException, exp_msg, | |
81 project_id) | |
82 else: | |
83 langs = api.get_target_languages(project_id) | |
84 | |
85 assert exp_langs == langs | |
86 | |
87 | |
88 @pytest.mark.parametrize('token,project_id,new_langs,exp_msg', [ | |
89 (_VALID_TOKEN, 1234, ['en_GB'], None), | |
90 (_INVALID_TOKEN, 1234, ['en_GB'], 'Authentication failed.'), | |
91 (_VALID_TOKEN, 1111, ['en_GB'], 'Project not found!'), | |
92 (_VALID_TOKEN, 1234, ['foo_BAR'], 'Unsupported language: foo_BAR'), | |
93 ]) | |
94 def test_adding_target_language(intercept, token, project_id, new_langs, | |
95 exp_msg): | |
96 """Test if target languages are added correctly.""" | |
97 api = XTMCloudAPI(token) | |
98 | |
99 if exp_msg: | |
100 exception_test(api.add_target_languages, XTMCloudException, exp_msg, | |
101 project_id, new_langs) | |
102 else: | |
103 api.add_target_languages(project_id, new_langs) | |
104 | |
105 | |
106 @pytest.mark.parametrize('token,project_id,exp_msg,', [ | |
107 (_INVALID_TOKEN, 1234, 'Authentication failed'), | |
108 (_VALID_TOKEN, 1111, 'Project not found'), | |
109 (_VALID_TOKEN, 1234, None), | |
110 ]) | |
111 def test_file_download(intercept_populated, token, project_id, exp_msg): | |
112 """Test if file downloading works as expected.""" | |
113 api = XTMCloudAPI(token) | |
114 | |
115 if exp_msg: | |
116 exception_test(api.download_files, XTMCloudException, exp_msg, | |
117 project_id) | |
118 else: | |
119 contents = api.download_files(project_id) | |
120 zipfile.ZipFile(BytesIO(contents)) | |
121 | |
122 | |
123 @pytest.mark.parametrize('token,project_id,files,exp_err,exp_msg,exp_jobs', [ | |
124 (_VALID_TOKEN, 1234, [], Exception, 'No files provided for upload.', []), | |
125 (_INVALID_TOKEN, 1234, _FILES_UPLOAD, XTMCloudException, 'Authentication ' | |
126 'failed', []), | |
127 (_VALID_TOKEN, 1111, _FILES_UPLOAD, XTMCloudException, 'Project not ' | |
128 'found', []), | |
129 (_VALID_TOKEN, 1234, _FILES_UPLOAD, None, None, _EXPECTED_JOBS), | |
130 ]) | |
131 def test_file_upload(intercept, token, project_id, files, exp_err, exp_msg, | |
132 exp_jobs): | |
133 api = XTMCloudAPI(token) | |
134 if exp_msg is not None: | |
135 exception_test(api.upload_files, exp_err, exp_msg, files, project_id) | |
136 else: | |
137 assert exp_jobs == api.upload_files(files, project_id) | |
OLD | NEW |