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 'es-mx', 'ur' is not supported languages=['ur', 'es-mx', 'fr'] t = client.gp_translation(bundleId=common.bundleId2, languages=languages) _ = t.gettext value = _('exit') common.my_assert_equal(self, u'Adiós', value, 'incorrect translated value - should have used es-mx 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')
def common_test_caching(self, cacheTimeout=None): """Shared code between the various caching tests """ acc = common.get_gpserviceaccount() if cacheTimeout is None: client = GPClient(acc) else: client = GPClient(acc, cacheTimeout=cacheTimeout) languages=['fr_CA'] t = client.gp_translation(bundleId=common.bundleId1, languages=languages) _ = t.gettext # key and translated value used for testing below # makes it easier to change key = 'greet' tValue = 'Bonjour' # translated value # check that the translated value is correct value = _(key) common.my_assert_equal(self, tValue, value, 'incorrect translated value') return (t, _, key, tValue)
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, 'Bonjour', value, 'incorrect value; should have returned gp translation')
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')
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')
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,))
def common_test_caching(self, cacheTimeout=None): """Shared code between the various caching tests """ acc = common.get_gpserviceaccount() if cacheTimeout is None: client = GPClient(acc) else: client = GPClient(acc, cacheTimeout=cacheTimeout) languages=['fr_CA'] t = client.gp_translation(bundleId=common.bundleId1, languages=languages) _ = t.gettext # key and translated value used for testing below # makes it easier to change key = 'greet' tValue = 'Salut' # translated value # check that the translated value is correct value = _(key) common.my_assert_equal(self, tValue, value, 'incorrect translated value') return (t, _, key, tValue)
def test_caching_disabled(self): """Test when the caching feature in GPTranslations is disabled""" (t, _, key, tValue) = self.common_test_caching( cacheTimeout=0) # check that the cache map is empty expectedCacheMap = {} common.my_assert_equal(self, expectedCacheMap, t._GPTranslations__cachedMap, 'incorrect cache map')
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')
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')
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, "ERROR", tresp["status"], 'a bundle which does not exist can not be deleted')
def test_reader__get_bundles(self): """Verify bundles can not be obtained with reader acc""" acc = common.get_gpserviceaccount() client = GPClient(acc) expectedBundles = None actualBundles = client._GPClient__get_bundles_data() common.my_assert_equal(self, expectedBundles, actualBundles, 'reader acc can not get bundles list')
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')
def test_update_resource_entries(self): """Test to update resource entries""" acc = common.get_admin_gpserviceaccount(True) 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')
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')
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')
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, ))
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')
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')
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=['es-mx'] t = client.gp_translation(bundleId=common.bundleId2, languages=languages) _ = t.gettext value = _('show') common.my_assert_equal(self, u'El físico', value, 'incorrect translated value')
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) languages=['es-mx'] t = client.gp_translation(bundleId=common.bundleId2, languages=languages) _ = t.gettext value = _('show') common.my_assert_equal(self, 'show', value, 'admin acc can not use basic auth')
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')
def test_example_2(self): """Test example 2 used in the docs""" acc = common.get_gpserviceaccount() client = GPClient(acc) languages=['es-mx'] # languages=[locale.getdefaultlocale()[0]] t = client.gp_translation(bundleId=common.bundleId2, languages=languages) _ = t.gettext value = _('exit') # 'exit' key will be localized/translated to Spanish common.my_assert_equal(self, u'Adiós', value, 'incorrect translated value')
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')
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')
def test_example_1(self): """Test example 1 used in the docs""" common.set_vcap_env_vars() acc = GPServiceAccount() client = GPClient(acc) languages=['fr_CA'] # 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, 'Bonjour', value, 'incorrect translated value')
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')
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')
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 = {'Date': 'Mon, 30 Jun 2014 00:00:00 GMT', 'Authorization': 'GaaS-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')
def test_caching_enabled_timeout_disabled(self): """Test when the caching feature in GPTranslations is enabled and there is no cache timeout """ (t, _, key, tValue) = self.common_test_caching( cacheTimeout=-1) # check that the cache map is as expected expectedCacheMap = {'greet': 'Salut', 'weather': "Il neige"} common.my_assert_equal(self, expectedCacheMap, t._GPTranslations__cachedMap, 'incorrect cache map') # modify the cachedMap and verify that the value is in fact obtained # from the cache and not directly from GP modifiedTValue = tValue + 'modified' # modified translated value t._GPTranslations__cachedMap[key] = modifiedTValue value = _(key) common.my_assert_equal(self, modifiedTValue, value, 'incorrect translated value - should have returned cached value') # test cache is not expired 10 hours before timeout value tenHoursAfterTimeout = t._GPTranslations__cacheMapTimestamp - \ datetime.timedelta(hours=10) t._GPTranslations__cacheMapTimestamp = tenHoursAfterTimeout value = _(key) common.my_assert_equal(self, modifiedTValue, value, 'incorrect translated value - should have returned cached value' + ' (no cache timeout)')
def test_caching_enabled_timeout_disabled(self): """Test when the caching feature in GPTranslations is enabled and there is no cache timeout """ (t, _, key, tValue) = self.common_test_caching( cacheTimeout=-1) # check that the cache map is as expected expectedCacheMap = {'greet': 'Bonjour', 'weather': "C'est snowing"} common.my_assert_equal(self, expectedCacheMap, t._GPTranslations__cachedMap, 'incorrect cache map') # modify the cachedMap and verify that the value is in fact obtained # from the cache and not directly from GP modifiedTValue = tValue + 'modified' # modified translated value t._GPTranslations__cachedMap[key] = modifiedTValue value = _(key) common.my_assert_equal(self, modifiedTValue, value, 'incorrect translated value - should have returned cached value') # test cache is not expired 10 hours before timeout value tenHoursAfterTimeout = t._GPTranslations__cacheMapTimestamp - \ datetime.timedelta(hours=10) t._GPTranslations__cacheMapTimestamp = tenHoursAfterTimeout value = _(key) common.my_assert_equal(self, modifiedTValue, value, 'incorrect translated value - should have returned cached value' + ' (no cache timeout)')
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')
def test_good_init(self): """Test the various valid ways to create a GPServiceAccount instance """ # used to distinguish values for different GPServiceAccount init calls USER_KEY = 'userEnvVar' VCAP_KEY = 'vcap' common.set_user_env_vars(suffix=USER_KEY) common.set_vcap_env_vars(suffix=VCAP_KEY) # test init method with params acc = common.get_gpserviceaccount() common.my_assert_equal(self, common.url, acc.get_url(), 'incorrect url from method params') common.my_assert_equal(self, common.instanceId, acc.get_instance_id(), 'incorrect instanceId from method params') common.my_assert_equal(self, common.userId, acc.get_user_id(), 'incorrect userId from method params') common.my_assert_equal(self, common.password, acc.get_password(), 'incorrect password from method params') # test init method with user defined env vars acc = GPServiceAccount() common.my_assert_equal(self, common.url + USER_KEY, acc.get_url(), 'incorrect url from user defined env vars') common.my_assert_equal(self, common.instanceId + USER_KEY, acc.get_instance_id(), 'incorrect instanceId from user defined env vars') common.my_assert_equal(self, common.userId + USER_KEY, acc.get_user_id(), 'incorrect userId from user defined env vars') common.my_assert_equal(self, common.password + USER_KEY, acc.get_password(), 'incorrect password from user defined env vars') # test init method with vcap env var common.unset_user_env_vars() acc = None acc = GPServiceAccount() common.my_assert_equal(self, common.url + VCAP_KEY, acc.get_url(), 'incorrect url from vcap env var') common.my_assert_equal(self, common.instanceId + VCAP_KEY, acc.get_instance_id(), 'incorrect instanceId from vcap env var') common.my_assert_equal(self, common.userId + VCAP_KEY, acc.get_user_id(), 'incorrect userId from vcap env var') common.my_assert_equal(self, common.password + VCAP_KEY, acc.get_password(), 'incorrect password from vcap env var')
def test_good_init(self): """Test the various valid ways to create a GPServiceAccount instance """ # used to distinguish values for different GPServiceAccount init calls USER_KEY = 'userEnvVar' VCAP_KEY = 'vcap' common.set_user_env_vars(suffix=USER_KEY) common.set_vcap_env_vars(suffix=VCAP_KEY) # test init method with params acc = common.get_gpserviceaccount() common.my_assert_equal(self, common.url, acc.get_url(), 'incorrect url from method params') common.my_assert_equal(self, common.instanceId, acc.get_instance_id(), 'incorrect instanceId from method params') common.my_assert_equal(self, common.userId, acc.get_user_id(), 'incorrect userId from method params') common.my_assert_equal(self, common.password, acc.get_password(), 'incorrect password from method params') # test init method with user defined env vars acc = GPServiceAccount() common.my_assert_equal(self, common.url + USER_KEY, acc.get_url(), 'incorrect url from user defined env vars') common.my_assert_equal( self, common.instanceId + USER_KEY, acc.get_instance_id(), 'incorrect instanceId from user defined env vars') common.my_assert_equal(self, common.userId + USER_KEY, acc.get_user_id(), 'incorrect userId from user defined env vars') common.my_assert_equal( self, common.password + USER_KEY, acc.get_password(), 'incorrect password from user defined env vars') # test init method with vcap env var common.unset_user_env_vars() acc = GPServiceAccount(serviceInstanceName=common.gpInstanceName) common.my_assert_equal(self, common.url + VCAP_KEY, acc.get_url(), 'incorrect url from vcap env var') common.my_assert_equal(self, common.instanceId + VCAP_KEY, acc.get_instance_id(), 'incorrect instanceId from vcap env var') common.my_assert_equal(self, common.userId + VCAP_KEY, acc.get_user_id(), 'incorrect userId from vcap env var') common.my_assert_equal(self, common.password + VCAP_KEY, acc.get_password(), 'incorrect password from vcap env var')