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

Side by Side Diff: tests/test_xtm_api.py

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

Powered by Google App Engine
This is Rietveld