forked from IBM-Cloud/gp-python-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_gpclient.py
393 lines (304 loc) · 14.2 KB
/
test_gpclient.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# -*- coding: utf-8 -*-
# Copyright IBM Corp. 2015, 2017
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
from gpclient import GPClient
from test import common
class TestGPClient(unittest.TestCase):
@classmethod
def setUpClass(self):
"""Setting up the globalization pipeline for testing"""
acc = common.get_admin_gpserviceaccount()
client = GPClient(acc)
try:
client.delete_bundle(common.bundleId1)
client.delete_bundle(common.bundleId2)
data = {}
data['sourceLanguage'] = "en"
#data['targetLanguages'] = ["fr","es-mx"]
data['targetLanguages'] =[]
data['notes']=["string"]
data['metadata']={}
data['partner']=''
data['segmentSeparatorPattern']='string'
data['noTranslationPattern']='string'
client.create_bundle(common.bundleId1, data=data)
bundle_entries = {}
bundle_entries['greet'] = "Hello"
bundle_entries['weather'] = "It is snowing"
client.upload_resource_entries(common.bundleId1, "en", data=bundle_entries)
bundle1_entries = {}
bundle1_entries['greet'] = "Salut"
bundle1_entries['weather'] = "Il neige"
client.upload_resource_entries(common.bundleId1, "fr", data=bundle1_entries)
bundle3_entries = {}
bundle3_entries['greet'] = "Salut"
bundle3_entries['weather'] = "Il neige"
client.upload_resource_entries(common.bundleId1, "es-mx", data=bundle3_entries)
client.create_bundle(common.bundleId2, data=data)
bundle0_entries = {}
bundle0_entries['exit'] = "Goodbye"
bundle0_entries['show'] = "The Wire"
client.upload_resource_entries(common.bundleId2, "en", data=bundle0_entries)
bundle2_entries = {}
bundle2_entries['exit']= u'Au revoir'
bundle2_entries['show']= u'Le Fil'
client.upload_resource_entries(common.bundleId2, "fr", data=bundle2_entries)
except:
pass
@classmethod
def tearDownClass(self):
pass
def setUp(self):
pass
def tearDown(self):
pass
#@unittest.skip("skipping")
def test_admin_basic_auth(self):
"""Verify basic auth fails with admin account"""
acc = common.get_admin_gpserviceaccount()
client = GPClient(acc, auth=GPClient.BASIC_AUTH)
ids = client.get_bundles()
self.assertEqual(0, len(ids), "Admin account can not use basic authentication")
#@unittest.skip("skipping")
def test_basic_auth_translation(self):
"""Test if translation works with basic auth"""
acc = common.get_gpserviceaccount()
client = GPClient(acc, auth=GPClient.BASIC_AUTH)
languages=['fr']
t = client.gp_translation(bundleId=common.bundleId2,
languages=languages)
_ = t.gettext
value = _('show')
common.my_assert_equal(self, u'Le Fil', value,
'incorrect translated value')
#@unittest.skip("skipping")
def test_create_bundle(self):
"""Test to create a new bundle"""
acc = common.get_admin_gpserviceaccount()
client = GPClient(acc)
tresp = client.create_bundle("test-bundle")
common.my_assert_equal(self, "SUCCESS", tresp["status"],
'bundle could not be created')
#@unittest.skip("skipping")
def test_delete_bundle_fail(self):
"""Test to delete a specific bundle which doesn't exist"""
acc = common.get_admin_gpserviceaccount()
client = GPClient(acc)
tresp = client.delete_bundle("test-bundle-notexists")
common.my_assert_equal(self, "SUCCESS", tresp["status"],
'a bundle which does not exist can not be deleted')
#@unittest.skip("skipping")
def test_delete_bundle_success(self):
"""Test to delete a specific bundle which exists"""
acc = common.get_admin_gpserviceaccount()
client = GPClient(acc)
tresp = client.delete_bundle("test-bundle")
common.my_assert_equal(self, "SUCCESS", tresp["status"],
'bundle could not be deleted')
#@unittest.skip("skipping")
def test_english_values(self):
"""Verify English values are returned when asked for"""
acc = common.get_gpserviceaccount()
client = GPClient(acc)
languages=['en']
t = client.gp_translation(bundleId=common.bundleId1,
languages=languages)
_ = t.gettext
value = _('greet')
common.my_assert_equal(self, 'Hello', value,
'incorrect value')
#@unittest.skip("skipping")
def test_example_1(self):
"""Test example 1 used in the docs"""
#common.set_vcap_env_vars()
acc = common.get_gpserviceaccount()
client = GPClient(acc)
languages=['fr'] # languages=[locale.getdefaultlocale()[0]]
t = client.gp_translation(bundleId=common.bundleId1,
languages=languages)
_ = t.gettext
value = _('greet') # 'greet' key will be localized/translated to French
common.my_assert_equal(self, 'Salut', value,
'incorrect translated value')
#@unittest.skip("skipping")
def test_example_2(self):
"""Test example 2 used in the docs"""
acc = common.get_gpserviceaccount()
client = GPClient(acc)
languages=['fr'] # languages=[locale.getdefaultlocale()[0]]
t = client.gp_translation(bundleId=common.bundleId2,
languages=languages)
_ = t.gettext
value = _('exit') # 'exit' key will be localized/translated to French
common.my_assert_equal(self, u'Au revoir', value,
'incorrect translated value')
#@unittest.skip("skipping")
def test_get_gaas_hmac_headers(self):
"""Test if the GaaS HMAC header generation is correct """
method = 'POST'
url = 'https://example.com/gaas'
date = 'Mon, 30 Jun 2014 00:00:00 GMT'
body = '{"param":"value"}'
userId = 'MyUser'
secret = 'MySecret'
expectedHeaders = {'GP-Date': 'Mon, 30 Jun 2014 00:00:00 GMT',
'Authorization': 'GP-HMAC MyUser:ONBJapYEveDZfsPFdqZHQ64GDgc='}
acc = common.get_gpserviceaccount()
client = GPClient(acc)
headers = client._GPClient__get_gaas_hmac_headers( method=method,
url=url, date=date, body=body, secret=secret, userId=userId)
common.my_assert_equal(self, expectedHeaders, headers,
'incorrect GaaS HMAC headers')
#@unittest.skip("skipping")
def test_get_language_match(self):
"""Test the matching of langauge codes to supported langauges"""
# supported languages in GP
supportedLangs = ['en','de','es','fr','it', 'ja','ko', 'pt-BR',
'zh-Hans', 'zh-Hant']
acc = common.get_gpserviceaccount()
client = GPClient(acc)
get_language_match = client._GPClient__get_language_match
expectedMatches = {
'en': 'en', 'en_US': 'en', 'en-US': 'en',
'de': 'de', 'de_at': 'de', 'de-at': 'de',
'es': 'es', 'es_mx': 'es', 'es-mx': 'es',
'fr': 'fr', 'fr_FR': 'fr', 'fr-Fr': 'fr', 'fr_CA': 'fr',
'it': 'it', 'it_ch': 'it', 'it-ch': 'it', 'it-IT': 'it',
'ja': 'ja', 'ja_JA': 'ja', 'ja-JA': 'ja',
'ko': 'ko', 'ko_KO': 'ko', 'ko-KO': 'ko',
'pt-BR': 'pt-BR', 'pt': None,
'zh': 'zh-Hans', 'zh-tw': 'zh-Hant', 'zh-cn': 'zh-Hans',
'zh-hk': 'zh-Hant', 'zh-sg': 'zh-Hans',
}
for langCode in expectedMatches:
match = get_language_match(langCode, supportedLangs)
expectedMatch = expectedMatches[langCode]
common.my_assert_equal(self, expectedMatch, match,
'incorrect langauge match (Input= %s)' % (langCode,))
#@unittest.skip("skipping")
def test_gp_fallback(self):
"""Test the fallback feature, i.e. when a translated value is not found
for a language, the fallback language should be used - if there is no
fallback language then the source value should be returned.
If the key is not found, the key should be returned.
"""
acc = common.get_gpserviceaccount()
client = GPClient(acc)
# should fallbcak to 'fr', 'ur' is not supported
languages=['ur', 'fr']
t = client.gp_translation(bundleId=common.bundleId2,
languages=languages)
_ = t.gettext
value = _('exit')
common.my_assert_equal(self, u'Au revoir', value,
'incorrect translated value - should have used fr fallback')
# should return key back, key doesn't exist
languages=['es-mx']
t = client.gp_translation(bundleId=common.bundleId2,
languages=languages)
_ = t.gettext
key = 'badKey'
value = _(key)
common.my_assert_equal(self, key, value,
'incorrect translated value - key doesn\'t exist')
#@unittest.skip("skipping")
def test_local_fallback(self):
"""Verify local translations are used with expected"""
acc = common.get_gpserviceaccount()
client = GPClient(acc)
languages=['fo', 'fr']
t = client.translation(bundleId=common.bundleId1,
languages=languages, priority='local', domain='messages',
localedir='test/data/translations', class_=None, codeset=None)
_ = t.gettext
value = _('greet')
common.my_assert_equal(self, 'greet in French (local)', value,
'incorrect value; should have returned local translation')
#@unittest.skip("skipping")
def test_local_translations(self):
"""Verify local translations are used with expected"""
acc = common.get_gpserviceaccount()
client = GPClient(acc)
languages=['fr']
t = client.translation(bundleId=common.bundleId1,
languages=languages, priority='local', domain='messages',
localedir='test/data/translations', class_=None, codeset=None)
_ = t.gettext
value = _('greet')
common.my_assert_equal(self, 'greet in French (local)', value,
'incorrect value; should have returned local translation')
#@unittest.skipIf(common.isReaderCredNotAvailable(),"Reader credentials not available")
def test_reader_get_bundles(self):
"""Verify bundles can not be obtained with reader acc"""
acc = common.get_gpserviceaccount()
client = GPClient(acc)
expectedBundles = []
actualBundles = client.get_bundles()
common.my_assert_equal(self, expectedBundles, actualBundles,
'reader acc can not get bundles list')
#@unittest.skip("skipping")
def test_translation_priority(self):
"""Verify that the priority option in GPClient.translation works"""
acc = common.get_gpserviceaccount()
client = GPClient(acc)
languages=['fr']
# prioritize local
t = client.translation(bundleId=common.bundleId1,
languages=languages, priority='local', domain='messages',
localedir='test/data/translations', class_=None, codeset=None)
_ = t.gettext
value = _('greet')
common.my_assert_equal(self, 'greet in French (local)', value,
'incorrect value; should have returned local translation')
# prioritize gp
t = client.translation(bundleId=common.bundleId1,
languages=languages, priority='gp', domain='messages',
localedir='test/data/translations', class_=None, codeset=None)
_ = t.gettext
value = _('greet')
common.my_assert_equal(self, 'Salut', value,
'incorrect value; should have returned gp translation')
#@unittest.skip("skipping")
def test_update_resource_entry(self):
"""Test to update a resource entry"""
acc = common.get_admin_gpserviceaccount()
client = GPClient(acc)
data = {}
data['value'] = "weather in spanish"
tresp = client.update_resource_entry(common.bundleId1,"es-mx","weather", data=data)
common.my_assert_equal(self, "SUCCESS", tresp["status"],
'bundle resource entry for the language could not be updated')
#@unittest.skip("skipping")
def test_update_resource_entries(self):
"""Test to update resource entries"""
acc = common.get_admin_gpserviceaccount()
client = GPClient(acc)
data = {}
data["welcome"]="Welcome"
tresp = client.update_resource_entries(common.bundleId1,"en", data=data)
common.my_assert_equal(self, "SUCCESS", tresp["status"],
'bundle resource entries for the language could not be updated')
#@unittest.skip("skipping")
def test_upload_resource_entries(self):
"""Test to upload resource entries"""
acc = common.get_admin_gpserviceaccount()
client = GPClient(acc)
data = {}
data["welcome"]="Hello"
tresp = client.upload_resource_entries(common.bundleId1,"en", data=data)
common.my_assert_equal(self, "SUCCESS", tresp["status"],
'bundle resource entries could not be uploaded')
if __name__ == '__main__':
unittest.main()