def setUp(self):
     super(MiddlewareTest, self).setUp()
     self.rf = RequestFactory()
     self.middleware = LocaleAndAppURLMiddleware()
class MiddlewareTest(BaseTestCase):
    """Tests that the locale and app redirection work properly."""

    def setUp(self):
        super(MiddlewareTest, self).setUp()
        self.rf = RequestFactory()
        self.middleware = LocaleAndAppURLMiddleware()

    def test_redirection(self):
        redirections = {
            '/': '/en-US/firefox/',
            '/en-US': '/en-US/firefox/',
            '/firefox': '/en-US/firefox/',
            '/android': '/en-US/android/',

            # Make sure we don't mess with trailing slashes.
            '/addon/1/': '/en-US/firefox/addon/1/',
            '/addon/1': '/en-US/firefox/addon/1',

            # Check an invalid locale.
            '/sda/firefox/addon/1': '/en-US/firefox/addon/1',

            # Check a consolidated language (e.g. es-* -> es).
            '/es-ES/firefox/addon/1': '/es/firefox/addon/1',
            '/es-PE/firefox/addon/1': '/es/firefox/addon/1',

            # /developers doesn't get an app.
            '/developers': '/en-US/developers',

            # Check basic use-cases with a 'lang' GET parameter:
            '/?lang=fr': '/fr/firefox/',
            '/addon/1/?lang=fr': '/fr/firefox/addon/1/',
            '/addon/1?lang=fr': '/fr/firefox/addon/1',
            '/firefox?lang=fr': '/fr/firefox/',
            '/developers?lang=fr': '/fr/developers',
        }

        for path, location in redirections.items():
            response = self.middleware.process_request(self.rf.get(path))
            assert response.status_code == 301
            assert response['Location'] == location

    def process(self, *args, **kwargs):
        self.request = self.rf.get(*args, **kwargs)
        return self.middleware.process_request(self.request)

    def test_no_redirect(self):
        # /services doesn't get an app or locale.
        response = self.process('/services')
        assert response is None

        # Things in settings.SUPPORTED_NONAPPS_NONLOCALES_PREFIX don't get
        # a redirect either, even if they have a lang GET parameter.
        with self.settings(SUPPORTED_NONAPPS_NONLOCALES_PREFIX=('lol',)):
            response = self.process('/lol?lang=fr')
            assert self.request.LANG == 'fr'
            assert response is None

        with self.settings(SUPPORTED_NONAPPS_NONLOCALES_PREFIX=('lol',)):
            response = self.process('/lol')
            assert self.request.LANG == 'en-US'
            assert response is None

    def test_api_no_redirect(self):
        response = self.process('/api/v3/some/endpoint/')
        assert response is None

    def test_vary(self):
        response = self.process('/')
        assert response['Vary'] == 'Accept-Language, User-Agent'

        response = self.process('/firefox')
        assert response['Vary'] == 'Accept-Language'

        response = self.process('/en-US')
        assert response['Vary'] == 'User-Agent'

        response = self.process('/en-US/thunderbird')
        assert 'Vary' not in response

    def test_no_redirect_with_script(self):
        response = self.process('/services', SCRIPT_NAME='/oremj')
        assert response is None

    def test_get_app(self):
        def check(url, expected, ua):
            response = self.process(url, HTTP_USER_AGENT=ua)
            assert response['Location'] == expected

        check('/en-US/', '/en-US/firefox/', 'Firefox')
        check('/de/', '/de/mobile/', 'Fennec')

        # Mobile gets priority because it has both strings in its UA...
        check('/de/', '/de/mobile/', 'Firefox Fennec')

        # SeaMonkey gets priority because it has both strings in its UA...
        check('/en-US/', '/en-US/seamonkey/', 'Firefox SeaMonkey')

        # Android can found by its user agent.
        check('/en-US/', '/en-US/android/', 'Fennec/12.0.1')
        check('/en-US/', '/en-US/android/', 'Fennec/12')
        check('/en-US/', '/en-US/android/', 'Fennec/11.0')
        check('/en-US/', '/en-US/mobile/', 'Fennec/10.9.1')
        check('/en-US/', '/en-US/mobile/', 'Fennec/10.9')

        # And the user agent changed again.
        check('/en-US/', '/en-US/android/',
              'Mozilla/5.0 (Android; Mobile; rv:17.0) Gecko/17.0 Firefox/17.0')

        # And the user agent yet changed again.
        check('/en-US/', '/en-US/android/',
              'Mozilla/5.0 (Mobile; rv:18.0) Gecko/18.0 Firefox/18.0')

        # And the tablet user agent yet changed again!
        check('/en-US/', '/en-US/android/',
              'Mozilla/5.0 (Android; Tablet; rv:18.0) Gecko/18.0 Firefox/18.0')

    def test_get_lang(self):
        def check(url, expected):
            response = self.process(url)
            self.assertUrlEqual(response['Location'], expected)

        check('/services?lang=fr', '/services')
        check('/en-US/firefox?lang=fr', '/fr/firefox/')
        check('/de/admin/?lang=fr&foo=bar', '/fr/admin/?foo=bar')
        check('/en-US/firefox/?lang=fake', '/en-US/firefox/')
        check('/firefox/?lang=fr', '/fr/firefox/')
        check('/firefox/?lang=fake', '/en-US/firefox/')
        check('/en-US/extensions/?foo=fooval&bar=barval&lang=fr',
              '/fr/firefox/extensions/?foo=fooval&bar=barval')
        check('/en-US/firefox?lang=es-PE', '/es/firefox/')
Example #3
0
 def setUp(self):
     super(MiddlewareTest, self).setUp()
     self.rf = RequestFactory()
     self.middleware = LocaleAndAppURLMiddleware()
Example #4
0
class MiddlewareTest(BaseTestCase):
    """Tests that the locale and app redirection work properly."""

    def setUp(self):
        super(MiddlewareTest, self).setUp()
        self.rf = RequestFactory()
        self.middleware = LocaleAndAppURLMiddleware()

    def test_redirection(self):
        redirections = {
            '/': '/en-US/firefox/',
            '/en-US': '/en-US/firefox/',
            '/firefox': '/en-US/firefox/',
            '/android': '/en-US/android/',

            # Make sure we don't mess with trailing slashes.
            '/addon/1/': '/en-US/firefox/addon/1/',
            '/addon/1': '/en-US/firefox/addon/1',

            # Check an invalid locale.
            '/sda/firefox/addon/1': '/en-US/firefox/addon/1',

            # Check a consolidated language (e.g. es-* -> es).
            '/es-ES/firefox/addon/1': '/es/firefox/addon/1',
            '/es-PE/firefox/addon/1': '/es/firefox/addon/1',

            # /developers doesn't get an app.
            '/developers': '/en-US/developers',

            # Check basic use-cases with a 'lang' GET parameter:
            '/?lang=fr': '/fr/firefox/',
            '/addon/1/?lang=fr': '/fr/firefox/addon/1/',
            '/addon/1?lang=fr': '/fr/firefox/addon/1',
            '/firefox?lang=fr': '/fr/firefox/',
            '/developers?lang=fr': '/fr/developers',
        }

        for path, location in redirections.items():
            response = self.middleware.process_request(self.rf.get(path))
            assert response.status_code == 301
            assert response['Location'] == location

    def process(self, *args, **kwargs):
        self.request = self.rf.get(*args, **kwargs)
        return self.middleware.process_request(self.request)

    def test_no_redirect(self):
        # /services doesn't get an app or locale.
        response = self.process('/services')
        assert response is None

        # Things in settings.SUPPORTED_NONAPPS_NONLOCALES_PREFIX don't get
        # a redirect either, even if they have a lang GET parameter.
        with self.settings(SUPPORTED_NONAPPS_NONLOCALES_PREFIX=('lol',)):
            response = self.process('/lol?lang=fr')
            assert self.request.LANG == 'fr'
            assert response is None

        with self.settings(SUPPORTED_NONAPPS_NONLOCALES_PREFIX=('lol',)):
            response = self.process('/lol')
            assert self.request.LANG == 'en-US'
            assert response is None

    def test_api_no_redirect(self):
        response = self.process('/api/v3/some/endpoint/')
        assert response is None

    def test_vary(self):
        response = self.process('/')
        assert response['Vary'] == 'Accept-Language, User-Agent'

        response = self.process('/firefox')
        assert response['Vary'] == 'Accept-Language'

        response = self.process('/en-US')
        assert response['Vary'] == 'User-Agent'

        response = self.process('/en-US/thunderbird')
        assert 'Vary' not in response

    def test_no_redirect_with_script(self):
        response = self.process('/services', SCRIPT_NAME='/oremj')
        assert response is None

    def test_get_app(self):
        def check(url, expected, ua):
            response = self.process(url, HTTP_USER_AGENT=ua)
            assert response['Location'] == expected

        check('/en-US/', '/en-US/firefox/', 'Firefox')

        # SeaMonkey gets priority because it has both strings in its UA...
        check('/en-US/', '/en-US/seamonkey/', 'Firefox SeaMonkey')

        # Android can found by its user agent.
        check('/en-US/', '/en-US/android/', 'Fennec/12.0.1')
        check('/en-US/', '/en-US/android/', 'Fennec/12')
        check('/en-US/', '/en-US/android/', 'Fennec/11.0')

        # And the user agent changed again.
        check('/en-US/', '/en-US/android/',
              'Mozilla/5.0 (Android; Mobile; rv:17.0) Gecko/17.0 Firefox/17.0')

        # And the user agent yet changed again.
        check('/en-US/', '/en-US/android/',
              'Mozilla/5.0 (Mobile; rv:18.0) Gecko/18.0 Firefox/18.0')

        # And the tablet user agent yet changed again!
        check('/en-US/', '/en-US/android/',
              'Mozilla/5.0 (Android; Tablet; rv:18.0) Gecko/18.0 Firefox/18.0')

    def test_get_lang(self):
        def check(url, expected):
            response = self.process(url)
            self.assertUrlEqual(response['Location'], expected)

        check('/services?lang=fr', '/services')
        check('/en-US/firefox?lang=fr', '/fr/firefox/')
        check('/de/admin/?lang=fr&foo=bar', '/fr/admin/?foo=bar')
        check('/en-US/firefox/?lang=fake', '/en-US/firefox/')
        check('/firefox/?lang=fr', '/fr/firefox/')
        check('/firefox/?lang=fake', '/en-US/firefox/')
        check('/en-US/extensions/?foo=fooval&bar=barval&lang=fr',
              '/fr/firefox/extensions/?foo=fooval&bar=barval')
        check('/en-US/firefox?lang=es-PE', '/es/firefox/')
class MiddlewareTest(BaseTestCase):
    """Tests that the locale and app redirection work properly."""

    def setUp(self):
        super(MiddlewareTest, self).setUp()
        self.rf = RequestFactory()
        self.middleware = LocaleAndAppURLMiddleware()

    def test_redirection(self):
        redirections = {
            "/": "/en-US/firefox/",
            "/en-US": "/en-US/firefox/",
            "/firefox": "/en-US/firefox/",
            "/android": "/en-US/android/",
            # Make sure we don't mess with trailing slashes.
            "/addon/1/": "/en-US/firefox/addon/1/",
            "/addon/1": "/en-US/firefox/addon/1",
            # Check an invalid locale.
            "/sda/firefox/addon/1": "/en-US/firefox/addon/1",
            # Check a consolidated language (e.g. es-* -> es).
            "/es-ES/firefox/addon/1": "/es/firefox/addon/1",
            "/es-PE/firefox/addon/1": "/es/firefox/addon/1",
            # /admin doesn't get an app.
            "/developers": "/en-US/developers",
        }

        for path, location in redirections.items():
            response = self.middleware.process_request(self.rf.get(path))
            eq_(response.status_code, 301)
            eq_(response["Location"], location)

    def process(self, *args, **kwargs):
        request = self.rf.get(*args, **kwargs)
        return self.middleware.process_request(request)

    def test_no_redirect(self):
        # /services doesn't get an app or locale.
        response = self.process("/services")
        assert response is None

    def test_api_no_redirect(self):
        response = self.process("/api/v3/some/endpoint/")
        assert response is None

    def test_vary(self):
        response = self.process("/")
        eq_(response["Vary"], "Accept-Language, User-Agent")

        response = self.process("/firefox")
        eq_(response["Vary"], "Accept-Language")

        response = self.process("/en-US")
        eq_(response["Vary"], "User-Agent")

        response = self.process("/en-US/thunderbird")
        assert "Vary" not in response

    def test_no_redirect_with_script(self):
        response = self.process("/services", SCRIPT_NAME="/oremj")
        assert response is None

    def test_get_app(self):
        def check(url, expected, ua):
            response = self.process(url, HTTP_USER_AGENT=ua)
            eq_(response["Location"], expected)

        check("/en-US/", "/en-US/firefox/", "Firefox")
        check("/de/", "/de/mobile/", "Fennec")

        # Mobile gets priority because it has both strings in its UA...
        check("/de/", "/de/mobile/", "Firefox Fennec")

        # SeaMonkey gets priority because it has both strings in its UA...
        check("/en-US/", "/en-US/seamonkey/", "Firefox SeaMonkey")

        # Android can found by its user agent.
        check("/en-US/", "/en-US/android/", "Fennec/12.0.1")
        check("/en-US/", "/en-US/android/", "Fennec/12")
        check("/en-US/", "/en-US/android/", "Fennec/11.0")
        check("/en-US/", "/en-US/mobile/", "Fennec/10.9.1")
        check("/en-US/", "/en-US/mobile/", "Fennec/10.9")

        # And the user agent changed again.
        check("/en-US/", "/en-US/android/", "Mozilla/5.0 (Android; Mobile; rv:17.0) Gecko/17.0 Firefox/17.0")

        # And the user agent yet changed again.
        check("/en-US/", "/en-US/android/", "Mozilla/5.0 (Mobile; rv:18.0) Gecko/18.0 Firefox/18.0")

        # And the tablet user agent yet changed again!
        check("/en-US/", "/en-US/android/", "Mozilla/5.0 (Android; Tablet; rv:18.0) Gecko/18.0 Firefox/18.0")

    def test_get_lang(self):
        def check(url, expected):
            response = self.process(url)
            self.assertUrlEqual(response["Location"], expected)

        check("/services?lang=fr", "/services")
        check("/en-US/firefox?lang=fr", "/fr/firefox/")
        check("/de/admin/?lang=fr&foo=bar", "/fr/admin/?foo=bar")
        check("/en-US/firefox/?lang=fake", "/en-US/firefox/")
        check("/firefox/?lang=fr", "/fr/firefox/")
        check("/firefox/?lang=fake", "/en-US/firefox/")
        check("/en-US/extensions/?foo=fooval&bar=barval&lang=fr", "/fr/firefox/extensions/?foo=fooval&bar=barval")
        check("/en-US/firefox?lang=es-PE", "/es/firefox/")
 def setUp(self):
     super().setUp()
     self.request_factory = RequestFactory()
     self.middleware = LocaleAndAppURLMiddleware()