Raise errors and warnings for invalid locales (bug 836147)

This commit is contained in:
Matt Basta 2013-07-10 16:37:35 -07:00
Родитель f4b2e1056f
Коммит 68106bfe6f
4 изменённых файлов: 159 добавлений и 0 удалений

Просмотреть файл

@ -39,6 +39,23 @@ PERMISSIONS = {
]),
}
SHORT_LOCALES = {
'en': 'en-US', 'ga': 'ga-IE', 'pt': 'pt-PT', 'sv': 'sv-SE', 'zh': 'zh-CN'
}
# Taken from Fireplace's hearth/media/js/l10n.js
SUPPORTED_LOCALES = [
'de', 'en-US', 'es', 'fr', 'pl', 'pt-BR',
# List of languages from AMO's settings (excluding mkt's active locales).
'af', 'ar', 'bg', 'ca', 'cs', 'da', 'el', 'eu', 'fa',
'fi', 'ga-IE', 'he', 'hu', 'id', 'it', 'ja', 'ko', 'mn', 'nl',
'pt-PT', 'ro', 'ru', 'sk', 'sl', 'sq', 'sv-SE', 'uk', 'vi',
'zh-CN', 'zh-TW',
# The hidden list from AMO's settings:
'cy', 'sr', 'tr',
]
# Graciously provided by @kumar in bug 614574
if (not SPIDERMONKEY_INSTALLATION or
not os.path.exists(SPIDERMONKEY_INSTALLATION)):

Просмотреть файл

@ -1,3 +1,4 @@
import testcases.content
import testcases.locales
import testcases.packagelayout
import testcases.webappbase

Просмотреть файл

@ -0,0 +1,72 @@
from . import register_test
from appvalidator.constants import SHORT_LOCALES, SUPPORTED_LOCALES
def canonicalize(locale):
# Format the locale properly.
if "-" in locale:
language, region = locale.split('-', 1)
else:
language = locale
region = ""
language = language.lower()
region = region.upper()
locale = '%s-%s' % (language, region)
if locale in SUPPORTED_LOCALES:
return locale
if language in SUPPORTED_LOCALES:
return language
if language in SHORT_LOCALES:
return SHORT_LOCALES[language]
return locale
@register_test(tier=2)
def validate_locales(err, package=None):
# Double check that the validation hasn't failed. We don't want to get
# invalid object types throwing tracebacks from the JSON.
if err.failed(fail_on_warnings=False):
return
manifest = err.get_resource("manifest")
if not manifest:
return
locales = set()
if "default_locale" in manifest:
locales.add(manifest["default_locale"])
if "locales" in manifest:
for locale in manifest["locales"]:
locales.add(locale)
err.save_resource("locales", locales)
if not locales:
return
if not any(canonicalize(loc) in SUPPORTED_LOCALES for loc in locales):
err.error(
err_id=("locales", "none_supported"),
error="No supported locales provided.",
description=["None of the locales provided in the manifest are "
"supported by the Firefox Marketplace. At least one "
"supported locale must be provided in the manifest.",
"Provided locales: %s" % ", ".join(locales)])
return
for locale in locales:
if canonicalize(locale) not in SUPPORTED_LOCALES:
err.warning(
err_id=("locales", "not_supported"),
warning="Unsupported locale provided.",
description=["An locale which is not supported by the Firefox "
"Marketplace was specified in the manifest. The "
"information listed in this locale will not be "
"stored or displayed to users.",
"Unsupported locale: %s" % locale])

69
tests/test_locales.py Normal file
Просмотреть файл

@ -0,0 +1,69 @@
from nose.tools import eq_
import appvalidator.testcases.locales as locales
from appvalidator.errorbundle import ErrorBundle
from helper import TestCase
def test_canonicalize():
def test(locale, expected_locale):
eq_(locales.canonicalize(locale), expected_locale)
yield test, "en-US", "en-US"
yield test, "EN-us", "en-US"
yield test, "EN", "en-US"
yield test, "en", "en-US"
# pt-BR is a supported locale, so keep it at that.
yield test, "pt-BR", "pt-BR"
yield test, "pt-PT", "pt-PT"
yield test, "pt-FOO", "pt-PT"
class TestLocales(TestCase):
def setUp(self):
self.setup_err()
self.manifest = {
"default_locale": "en-US",
"locales": {},
}
self.err.save_resource("manifest", self.manifest)
def run(self):
locales.validate_locales(self.err, None)
def test_passes(self):
self.run()
self.assert_silent()
def test_passes_no_default(self):
del self.manifest["default_locale"]
self.run()
self.assert_silent()
def test_passes_locales(self):
self.manifest["locales"]["pt-BR"] = {}
self.run()
self.assert_silent()
def test_warns_locales(self):
self.manifest["locales"]["foo"] = {}
self.run()
self.assert_failed(with_warnings=True)
def test_warns_bad_default_locale(self):
self.manifest["default_locale"] = "foobar"
self.manifest["locales"]["pt-BR"] = {}
self.run()
self.assert_failed(with_warnings=True)
def test_default_locale_invalid(self):
self.manifest["default_locale"] = "asdf"
self.run()
self.assert_failed(with_errors=True)
def test_locales_locale_invalid(self):
self.manifest["default_locale"] = "asdf"
self.manifest["locales"]["foo"] = {}
self.run()
self.assert_failed(with_errors=True)