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

Delta Between Two Patch Sets: tests/test_xtm_api.py

Issue 29886648: Issue #6942 - Add XTM integration in CMS (Closed)
Left Patch Set: Addressed initial comments Created Sept. 25, 2018, 12:24 p.m.
Right Patch Set: Addressed comments from Patch Set #4 Created Oct. 5, 2018, 4:23 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
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 """Tests for the XTM API integration.""" 16 """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.
17
16 from __future__ import unicode_literals 18 from __future__ import unicode_literals
17 19
18 import pytest
19 import json 20 import json
20
21 import zipfile 21 import zipfile
22 from io import BytesIO 22 from io import BytesIO
23 23
24 from cms.bin.xtm_translations.xtm_api import (XTMCloudException, XTMCloudAPI, 24 import pytest
25 get_token) 25
26 from cms.translations.xtm.xtm_api import (XTMCloudException, XTMCloudAPI,
27 get_token)
26 from tests.utils import exception_test 28 from tests.utils import exception_test
27 29
28 _VALID_TOKEN = 'TheXTM-APIToken-VALID' 30 _VALID_TOKEN = 'TheXTM-APIToken-VALID'
29 _INVALID_TOKEN = 'TheXTM-APIToken-INVALID' 31 _INVALID_TOKEN = 'TheXTM-APIToken-INVALID'
30 32
31 _FILES_UPLOAD = [('test.json', json.dumps({'foo': 'bar'}))] 33 _FILES_UPLOAD = {'test.json': json.dumps({'foo': 'bar'})}
32 _EXPECTED_JOBS = [{'fileName': 'test.json', 'jobId': 1, 34 _EXPECTED_JOBS = [{'fileName': 'test.json', 'jobId': 1,
33 'sourceLanguage': 'en_US', 'targetLanguage': 'de_DE'}] 35 'sourceLanguage': 'en_US', 'targetLanguage': 'de_DE'}]
34 36
35 37
36 @pytest.mark.parametrize('credentials,is_ok,err_msg, exp_token', [ 38 @pytest.mark.parametrize('credentials,is_ok,err_msg, exp_token', [
37 (['admin', 'pass', 20], True, None, 'TheXTM-APIToken-VALID'), 39 (['admin', 'pass', 20], True, None, 'TheXTM-APIToken-VALID'),
38 (['admin', 'wrong_pass', 20], False, 'Invalid credentials.', None), 40 (['admin', 'wrong_pass', 20], False, 'Invalid credentials.', None),
39 ]) 41 ])
40 def test_token_generation(intercept, credentials, is_ok, err_msg, exp_token): 42 def test_token_generation(intercept, credentials, is_ok, err_msg, exp_token):
41 """Test if the API token generation behaves accordingly.""" 43 """Test the API token generation."""
42 if is_ok: 44 if is_ok:
43 token = get_token(*credentials) 45 token = get_token(*credentials)
44 assert token == exp_token 46 assert token == exp_token
45 else: 47 else:
46 exception_test(get_token, XTMCloudException, err_msg, *credentials) 48 exception_test(get_token, XTMCloudException, err_msg, *credentials)
47 49
48 50
49 @pytest.mark.parametrize('token,args,files,exp_msg,exp_jobs', [ 51 @pytest.mark.parametrize('token,args,files,exp_msg,exp_jobs', [
50 (_VALID_TOKEN, ['name', 'description', 'ref_id', ['en_US'], 10, 20], None, 52 (_VALID_TOKEN, ['name', 'description', 'ref_id', ['en_US'], 10, 20], None,
51 None, []), 53 None, []),
52 (_INVALID_TOKEN, ['name', 'description', 'ref_id', ['en_US'], 10, 20], 54 (_INVALID_TOKEN, ['name', 'description', 'ref_id', ['en_US'], 10, 20],
53 None, 'Authentication failed.', []), 55 None, 'Authentication failed.', []),
54 (_VALID_TOKEN, ['name', 'description', 'ref_id', ['de_DE'], 10, 20], 56 (_VALID_TOKEN, ['name', 'description', 'ref_id', ['de_DE'], 10, 20],
55 _FILES_UPLOAD, None, _EXPECTED_JOBS), 57 _FILES_UPLOAD, None, _EXPECTED_JOBS),
56 ]) 58 ])
57 def test_project_creation(intercept, token, files, args, exp_msg, exp_jobs): 59 def test_project_creation(intercept, token, files, args, exp_msg, exp_jobs):
58 """Test if the creation of files behaves appropriately.""" 60 """Test 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) 61 api = XTMCloudAPI(token)
60 62
61 if exp_msg: 63 if exp_msg:
62 exception_test(api.create_project, XTMCloudException, exp_msg, *args) 64 exception_test(api.create_project, XTMCloudException, exp_msg, *args)
63 else: 65 else:
64 project_id, jobs = api.create_project(*args, files=files) 66 project_id, jobs = api.create_project(*args, files=files)
65 assert project_id == 1234 67 assert project_id == 1234
66 assert exp_jobs == jobs 68 assert exp_jobs == jobs
67 69
68 70
69 @pytest.mark.parametrize('token,project_id,exp_langs,exp_msg', [ 71 @pytest.mark.parametrize('token,project_id,exp_langs,exp_msg', [
70 (_VALID_TOKEN, 1234, {'de_DE'}, None), 72 (_VALID_TOKEN, 1234, {'de_DE'}, None),
71 (_INVALID_TOKEN, 1234, None, 'Authentication failed.'), 73 (_INVALID_TOKEN, 1234, None, 'Authentication failed.'),
72 (_VALID_TOKEN, 1111, None, 'Project not found!'), 74 (_VALID_TOKEN, 1111, None, 'Project not found!'),
73 ]) 75 ])
74 def test_extracting_target_languages(intercept, token, project_id, 76 def test_extracting_target_languages(intercept, token, project_id,
75 exp_langs, exp_msg): 77 exp_langs, exp_msg):
76 """Test if the target languages are extracted correctly.""" 78 """Test extraction of target languages."""
77 api = XTMCloudAPI(token) 79 api = XTMCloudAPI(token)
78 80
79 if exp_msg: 81 if exp_msg:
80 exception_test(api.get_target_languages, XTMCloudException, exp_msg, 82 exception_test(api.get_target_languages, XTMCloudException, exp_msg,
81 project_id) 83 project_id)
82 else: 84 else:
83 langs = api.get_target_languages(project_id) 85 langs = api.get_target_languages(project_id)
84 86
85 assert exp_langs == langs 87 assert exp_langs == langs
86 88
87 89
88 @pytest.mark.parametrize('token,project_id,new_langs,exp_msg', [ 90 @pytest.mark.parametrize('token,project_id,new_langs,exp_msg', [
89 (_VALID_TOKEN, 1234, ['en_GB'], None), 91 (_VALID_TOKEN, 1234, ['en_GB'], None),
90 (_INVALID_TOKEN, 1234, ['en_GB'], 'Authentication failed.'), 92 (_INVALID_TOKEN, 1234, ['en_GB'], 'Authentication failed.'),
91 (_VALID_TOKEN, 1111, ['en_GB'], 'Project not found!'), 93 (_VALID_TOKEN, 1111, ['en_GB'], 'Project not found!'),
92 (_VALID_TOKEN, 1234, ['foo_BAR'], 'Unsupported language: foo_BAR'), 94 (_VALID_TOKEN, 1234, ['foo_BAR'], 'Unsupported language: foo_BAR'),
93 ]) 95 ])
94 def test_adding_target_language(intercept, token, project_id, new_langs, 96 def test_adding_target_language(intercept, token, project_id, new_langs,
95 exp_msg): 97 exp_msg):
96 """Test if target languages are added correctly.""" 98 """Test adding target languages."""
97 api = XTMCloudAPI(token) 99 api = XTMCloudAPI(token)
98 100
99 if exp_msg: 101 if exp_msg:
100 exception_test(api.add_target_languages, XTMCloudException, exp_msg, 102 exception_test(api.add_target_languages, XTMCloudException, exp_msg,
101 project_id, new_langs) 103 project_id, new_langs)
102 else: 104 else:
103 api.add_target_languages(project_id, new_langs) 105 api.add_target_languages(project_id, new_langs)
104 106
105 107
106 @pytest.mark.parametrize('token,project_id,exp_msg,', [ 108 @pytest.mark.parametrize('token,project_id,exp_msg,', [
107 (_INVALID_TOKEN, 1234, 'Authentication failed'), 109 (_INVALID_TOKEN, 1234, 'Authentication failed'),
108 (_VALID_TOKEN, 1111, 'Project not found'), 110 (_VALID_TOKEN, 1111, 'Project not found'),
109 (_VALID_TOKEN, 1234, None), 111 (_VALID_TOKEN, 1234, None),
110 ]) 112 ])
111 def test_file_download(intercept_populated, token, project_id, exp_msg): 113 def test_file_download(intercept_populated, token, project_id, exp_msg):
112 """Test if file downloading works as expected.""" 114 """Test file downloading."""
113 api = XTMCloudAPI(token) 115 api = XTMCloudAPI(token)
114 116
115 if exp_msg: 117 if exp_msg:
116 exception_test(api.download_files, XTMCloudException, exp_msg, 118 exception_test(api.download_files, XTMCloudException, exp_msg,
117 project_id) 119 project_id)
118 else: 120 else:
119 contents = api.download_files(project_id) 121 contents = api.download_files(project_id)
120 zipfile.ZipFile(BytesIO(contents)) 122 zipfile.ZipFile(BytesIO(contents))
121 123
122 124
123 @pytest.mark.parametrize('token,project_id,files,exp_err,exp_msg,exp_jobs', [ 125 @pytest.mark.parametrize('token,project_id,files,exp_err,exp_msg,exp_jobs', [
124 (_VALID_TOKEN, 1234, [], Exception, 'No files provided for upload.', []), 126 (_VALID_TOKEN, 1234, [], Exception, 'No files provided for upload.', []),
125 (_INVALID_TOKEN, 1234, _FILES_UPLOAD, XTMCloudException, 'Authentication ' 127 (_INVALID_TOKEN, 1234, _FILES_UPLOAD, XTMCloudException, 'Authentication '
126 'failed', []), 128 'failed', []),
127 (_VALID_TOKEN, 1111, _FILES_UPLOAD, XTMCloudException, 'Project not ' 129 (_VALID_TOKEN, 1111, _FILES_UPLOAD, XTMCloudException, 'Project not '
128 'found', []), 130 'found', []),
129 (_VALID_TOKEN, 1234, _FILES_UPLOAD, None, None, _EXPECTED_JOBS), 131 (_VALID_TOKEN, 1234, _FILES_UPLOAD, None, None, _EXPECTED_JOBS),
130 ]) 132 ])
131 def test_file_upload(intercept, token, project_id, files, exp_err, exp_msg, 133 def test_file_upload(intercept, token, project_id, files, exp_err, exp_msg,
132 exp_jobs): 134 exp_jobs):
135 """Test file uploading."""
133 api = XTMCloudAPI(token) 136 api = XTMCloudAPI(token)
134 if exp_msg is not None: 137 if exp_msg is not None:
135 exception_test(api.upload_files, exp_err, exp_msg, files, project_id) 138 exception_test(api.upload_files, exp_err, exp_msg, files, project_id)
136 else: 139 else:
137 assert exp_jobs == api.upload_files(files, project_id) 140 assert exp_jobs == api.upload_files(files, project_id)
LEFTRIGHT

Powered by Google App Engine
This is Rietveld