2016-05-19 17:33:30 +02:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
2016-06-04 20:28:02 +02:00
|
|
|
|
from typing import Any
|
|
|
|
|
|
2016-06-01 14:58:54 +02:00
|
|
|
|
import mock
|
2016-05-19 17:33:30 +02:00
|
|
|
|
from django.test import TestCase
|
|
|
|
|
from django.conf import settings
|
2016-06-04 20:28:02 +02:00
|
|
|
|
from django.http import HttpResponse
|
2016-05-19 17:33:30 +02:00
|
|
|
|
from http.cookies import SimpleCookie
|
|
|
|
|
|
2016-06-01 14:58:54 +02:00
|
|
|
|
from zerver.lib.test_helpers import AuthedTestCase
|
|
|
|
|
|
2016-05-19 17:33:30 +02:00
|
|
|
|
|
|
|
|
|
class TranslationTestCase(TestCase):
|
|
|
|
|
"""
|
|
|
|
|
Tranlations strings should change with locale. URLs should be locale
|
|
|
|
|
aware.
|
|
|
|
|
"""
|
|
|
|
|
|
2016-06-04 20:28:02 +02:00
|
|
|
|
# e.g. self.client.post(url) if method is "post"
|
2016-05-19 17:33:30 +02:00
|
|
|
|
def fetch(self, method, url, expected_status, **kwargs):
|
2016-06-04 20:28:02 +02:00
|
|
|
|
# type: (str, str, int, **Any) -> HttpResponse
|
2016-05-19 17:33:30 +02:00
|
|
|
|
response = getattr(self.client, method)(url, **kwargs)
|
|
|
|
|
self.assertEqual(response.status_code, expected_status,
|
|
|
|
|
msg="Expected %d, received %d for %s to %s" % (
|
|
|
|
|
expected_status, response.status_code, method, url))
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
def test_accept_language_header(self):
|
2016-06-04 20:28:02 +02:00
|
|
|
|
# type: () -> None
|
2016-05-19 17:33:30 +02:00
|
|
|
|
languages = [('en', 'Register'),
|
|
|
|
|
('de', 'Registrieren'),
|
|
|
|
|
('sr', 'Региструј се'),
|
|
|
|
|
('zh-cn', '注册'),
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
for lang, word in languages:
|
|
|
|
|
response = self.fetch('get', '/integrations/', 200,
|
|
|
|
|
HTTP_ACCEPT_LANGUAGE=lang)
|
|
|
|
|
self.assertTrue(word in response.content)
|
|
|
|
|
|
|
|
|
|
def test_cookie(self):
|
2016-06-04 20:28:02 +02:00
|
|
|
|
# type: () -> None
|
2016-05-19 17:33:30 +02:00
|
|
|
|
languages = [('en', 'Register'),
|
|
|
|
|
('de', 'Registrieren'),
|
|
|
|
|
('sr', 'Региструј се'),
|
|
|
|
|
('zh-cn', '注册'),
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
for lang, word in languages:
|
|
|
|
|
self.client.cookies = SimpleCookie({settings.LANGUAGE_COOKIE_NAME: lang})
|
|
|
|
|
|
|
|
|
|
response = self.fetch('get', '/integrations/', 200)
|
|
|
|
|
self.assertTrue(word in response.content)
|
|
|
|
|
|
|
|
|
|
def test_i18n_urls(self):
|
2016-06-04 20:28:02 +02:00
|
|
|
|
# type: () -> None
|
2016-05-19 17:33:30 +02:00
|
|
|
|
languages = [('en', 'Register'),
|
|
|
|
|
('de', 'Registrieren'),
|
|
|
|
|
('sr', 'Региструј се'),
|
|
|
|
|
('zh-cn', '注册'),
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
for lang, word in languages:
|
|
|
|
|
response = self.fetch('get', '/{}/integrations/'.format(lang), 200)
|
|
|
|
|
self.assertTrue(word in response.content)
|
2016-06-01 14:58:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class JsonTranslationTestCase(AuthedTestCase):
|
|
|
|
|
@mock.patch('zerver.lib.request._')
|
|
|
|
|
def test_json_error(self, mock_gettext):
|
|
|
|
|
dummy_value = "Some other language '%s'"
|
|
|
|
|
mock_gettext.return_value = dummy_value
|
|
|
|
|
|
|
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
|
result = self.client.post("/json/refer_friend",
|
|
|
|
|
HTTP_ACCEPT_LANGUAGE='de')
|
|
|
|
|
|
|
|
|
|
self.assert_json_error_contains(result,
|
|
|
|
|
dummy_value % 'email',
|
|
|
|
|
status_code=400)
|
|
|
|
|
|
|
|
|
|
@mock.patch('zerver.views._')
|
|
|
|
|
def test_jsonable_error(self, mock_gettext):
|
|
|
|
|
dummy_value = "Some other language"
|
|
|
|
|
mock_gettext.return_value = dummy_value
|
|
|
|
|
|
|
|
|
|
self.login("hamlet@zulip.com")
|
|
|
|
|
result = self.client.get("/de/accounts/login/jwt/")
|
|
|
|
|
|
|
|
|
|
self.assert_json_error_contains(result,
|
|
|
|
|
dummy_value,
|
|
|
|
|
status_code=400)
|