django-localeurl / localeurl / tests / tests.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
# -*- coding: utf-8 -*-
"""
Tests for the localeurl application.
"""
import re

from django import template
from django.core import urlresolvers
from django.test import TestCase
from django.utils import translation

from localeurl import middleware, settings as localeurl_settings, utils
from localeurl.models import reverse
from localeurl.sitemaps import LocaleurlSitemap
from localeurl.templatetags import localeurl_tags
from localeurl.tests import test_utils



def settings_fixture(mgr):
    mgr.set(
        USE_I18N = True,
        LANGUAGES = (
            ('en', 'English'),
            ('nl-nl', 'Dutch'),
            ('nl-be', 'Flemish'),
            ('fr', 'French'),
            ('pt', 'Portuguese'),
            ('pt-br', 'Brazilian Portuguese'),
        ),
        LANGUAGE_CODE = 'en-gb',
        LOCALE_INDEPENDENT_PATHS = (
            re.compile('^/$'),
            '^/test/independent/',
        ),
        LOCALE_INDEPENDENT_MEDIA_URL = True,
        MEDIA_URL = '/media/',
        LOCALE_INDEPENDENT_STATIC_URL = True,
        STATIC_URL = '/static/',
        TEMPLATE_CONTEXT_PROCESSORS = (
            'django.core.context_processors.i18n',
        ),
    )



class LocaleurlTestCase(TestCase):
    urls = 'localeurl.tests.test_urls'


    def setUp(self):
        self.settings_manager = test_utils.TestSettingsManager()
        settings_fixture(self.settings_manager)
        translation.activate("en")
        reload(localeurl_settings)


    def tearDown(self):
        self.settings_manager.revert()
        reload(localeurl_settings)



class UtilsTestCase(LocaleurlTestCase):
    def test_is_locale_independent(self):
        self.assertFalse(utils.is_locale_independent('/fr/about'))
        self.assertFalse(utils.is_locale_independent('/about'))
        self.assertTrue(utils.is_locale_independent('/media/img/logo.png'))
        self.assertTrue(utils.is_locale_independent('/static/img/logo.png'))
        self.assertTrue(utils.is_locale_independent('/'))
        self.assertTrue(utils.is_locale_independent(
                '/test/independent/bla/bla'))


    def test_strip_path(self):
        self.assertEqual(('', '/'), utils.strip_path('/'))
        self.assertEqual(('', '/about/'), utils.strip_path('/about/'))
        self.assertEqual(('', '/about/localeurl/'),
                utils.strip_path('/about/localeurl/'))
        self.assertEqual(('fr', '/about/localeurl/'),
                utils.strip_path('/fr/about/localeurl/'))
        self.assertEqual(('nl-be', '/about/localeurl/'),
                utils.strip_path('/nl-be/about/localeurl/'))
        self.assertEqual(('', '/de/about/localeurl/'),
                utils.strip_path('/de/about/localeurl/'))


    def test_strip_path_takes_longer_code_first(self):
        # Refs issue #15.
        self.assertEqual(('pt-br', '/about/localeurl/'),
                utils.strip_path('/pt-br/about/localeurl/'))


    def test_supported_language(self):
        self.assertEqual('fr', utils.supported_language('fr'))
        self.assertEqual('nl-be', utils.supported_language('nl-be'))
        self.assertEqual('en', utils.supported_language('en-gb'))
        self.assertEqual(None, utils.supported_language('de'))


    def test_is_default_locale(self):
        self.assertTrue(utils.is_default_locale('en'))
        self.assertFalse(utils.is_default_locale('en-gb'))
        self.assertFalse(utils.is_default_locale('fr'))
        self.assertFalse(utils.is_default_locale('de'))


    def test_locale_path(self):
        self.assertEqual('/en/about/localeurl/',
                utils.locale_path('/about/localeurl/'))
        self.assertEqual('/en/about/localeurl/',
                utils.locale_path('/about/localeurl/', 'de'))
        self.assertEqual('/en/about/localeurl/',
                utils.locale_path('/about/localeurl/', 'en'))
        self.assertEqual('/en/about/localeurl/',
                utils.locale_path('/about/localeurl/', 'en-us'))
        self.assertEqual('/nl-nl/about/localeurl/',
                utils.locale_path('/about/localeurl/', 'nl-nl'))
        self.assertEqual('/test/independent/bla/bla',
                utils.locale_path('/test/independent/bla/bla', 'en'))


    def test_locale_url(self):
        previous_script_prefix = urlresolvers.get_script_prefix()
        urlresolvers.set_script_prefix("/blah/")
        self.assertEqual('/blah/en/about/localeurl/',
                utils.locale_url('/about/localeurl/'))
        self.assertEqual('/blah/en/about/localeurl/',
                utils.locale_url('/about/localeurl/', 'de'))
        self.assertEqual('/blah/en/about/localeurl/',
                utils.locale_url('/about/localeurl/', 'en'))
        self.assertEqual('/blah/en/about/localeurl/',
                utils.locale_url('/about/localeurl/', 'en-us'))
        self.assertEqual('/blah/nl-nl/about/localeurl/',
                utils.locale_url('/about/localeurl/', 'nl-nl'))
        self.assertEqual('/blah/test/independent/bla/bla',
                utils.locale_url('/test/independent/bla/bla', 'en'))
        urlresolvers.set_script_prefix(previous_script_prefix)



class MiddlewareTestCase(LocaleurlTestCase):
    def setUp(self):
        super(MiddlewareTestCase, self).setUp()
        self.request_factory = test_utils.RequestFactory()
        self.middleware = middleware.LocaleURLMiddleware()


    def test_with_locale(self):
        r1 = self.request_factory.get('/fr/test/')
        r2 = self.middleware.process_request(r1)
        self.assertEqual(None, r2)
        self.assertEqual('fr', r1.LANGUAGE_CODE)
        self.assertEqual('/test/', r1.path_info)


    def test_with_sublocale(self):
        r1 = self.request_factory.get('/nl-nl/test/bla/bla/')
        r2 = self.middleware.process_request(r1)
        self.assertEqual(None, r2)
        self.assertEqual('nl-nl', r1.LANGUAGE_CODE)
        self.assertEqual('/test/bla/bla/', r1.path_info)


    def test_locale_independent_url(self):
        r1 = self.request_factory.get('/test/independent/bla/bla/')
        r2 = self.middleware.process_request(r1)
        self.assertEqual(None, r2)
        self.assertEqual('en-gb', r1.LANGUAGE_CODE)
        self.assertEqual('/test/independent/bla/bla/', r1.path_info)


    def test_locale_specified_on_independent_url_with_query_string(self):
        r1 = self.request_factory.get('/nl-be/test/independent/?foo=bar')
        r2 = self.middleware.process_request(r1)
        self.assertEqual(301, r2.status_code)
        self.assertEqual('/test/independent/?foo=bar', r2['Location'])


    def test_redirect_with_script_prefix(self):
        previous_script_prefix = urlresolvers.get_script_prefix()
        urlresolvers.set_script_prefix('/prefix/')

        r1 = self.request_factory.get('/nl-be/test/independent/')
        r2 = self.middleware.process_request(r1)
        self.assertEqual(301, r2.status_code)
        self.assertEqual('/prefix/test/independent/', r2['Location'])

        # no infinite redirects
        r3 = self.request_factory.get('/test/independent/')
        r4 = self.middleware.process_request(r3)
        self.assertEqual(None, r4)

        urlresolvers.set_script_prefix(previous_script_prefix)


    def test_redirect_statuscode_302(self):
        self.settings_manager.set(LOCALE_REDIRECT_PERMANENT=False)
        reload(localeurl_settings)

        r1 = self.request_factory.get('/nl-be/test/independent/?foo=bar')
        r2 = self.middleware.process_request(r1)
        self.assertEqual(302, r2.status_code)


    def test_redirect_statuscode_default(self):
        r1 = self.request_factory.get('/nl-be/test/independent/?foo=bar')
        r2 = self.middleware.process_request(r1)
        self.assertEqual(301, r2.status_code)


    def test_check_accept_lang(self):
        self.settings_manager.set(LOCALEURL_USE_ACCEPT_LANGUAGE=True)
        reload(localeurl_settings)

        r1 = self.request_factory.get('/test/', HTTP_ACCEPT_LANGUAGE='fr, de;q=0.8')
        r2 = self.middleware.process_request(r1)
        self.assertEqual(301, r2.status_code)
        self.assertEqual('/fr/test/', r2['Location'])


    def test_check_session_enabled(self):
        self.settings_manager.set(LOCALEURL_USE_SESSION=True)
        reload(localeurl_settings)

        r1 = self.request_factory.get('/test/')
        r1.session = {'django_language': 'fr'}
        r2 = self.middleware.process_request(r1)
        self.assertEqual(301, r2.status_code)
        self.assertEqual('/fr/test/', r2['Location'])



class DefaultPrefixMiddlewareTestCase(MiddlewareTestCase):
    def setUp(self):
        super(DefaultPrefixMiddlewareTestCase, self).setUp()


    def test_no_locale(self):
        r1 = self.request_factory.get('/test/')
        r2 = self.middleware.process_request(r1)
        self.assertEqual(301, r2.status_code)
        self.assertEqual('/en/test/', r2['Location'])


    def test_non_ascii(self):
        r1 = self.request_factory.get('/plaît/')
        r2 = self.middleware.process_request(r1)
        self.assertEqual(301, r2.status_code)
        self.assertEqual('/en/pla%C3%AEt/', r2['Location'])


    def test_with_query_string(self):
        r1 = self.request_factory.get('/test/?somevar=someval')
        r2 = self.middleware.process_request(r1)
        self.assertEqual(301, r2.status_code)
        self.assertEqual('/en/test/?somevar=someval', r2['Location'])

    def test_with_unescaped_query_string(self):
        # This contains an ISO-88591-2 latin small letter C with cedilla,
        # received in a request declared as windows-1251:
        r1 = self.request_factory.get('/test/?somevar=Mudan\xe7as_recentes')
        r2 = self.middleware.process_request(r1)
        self.assertEqual(301, r2.status_code)
        self.assertEqual('/en/test/?somevar=Mudan%E7as_recentes', r2['Location'])


    def test_check_session_disabled(self):
        self.settings_manager.set(LOCALEURL_USE_SESSION=False)
        reload(localeurl_settings)

        r1 = self.request_factory.get('/test/')
        r1.session = {'locale': 'fr'}
        r2 = self.middleware.process_request(r1)
        self.assertEqual(301, r2.status_code)
        self.assertEqual('/en/test/', r2['Location'])





class NoDefaultPrefixMiddlewareTestCase(MiddlewareTestCase):
    def setUp(self):
        super(NoDefaultPrefixMiddlewareTestCase, self).setUp()
        self.settings_manager.set(PREFIX_DEFAULT_LOCALE=False)
        reload(localeurl_settings)


    def test_default_locale(self):
        r1 = self.request_factory.get('/test/foo/')
        r2 = self.middleware.process_request(r1)
        self.assertEqual(None, r2)
        self.assertEqual('en-gb', r1.LANGUAGE_CODE)
        self.assertEqual('/test/foo/', r1.path_info)


    def test_default_locale_specified(self):
        r1 = self.request_factory.get('/en/test/foo/')
        r2 = self.middleware.process_request(r1)
        self.assertEqual(301, r2.status_code)
        self.assertEqual('/test/foo/', r2['Location'])


    def test_alternate_locale(self):
        r1 = self.request_factory.get('/fr/test/foo/')
        r2 = self.middleware.process_request(r1)
        self.assertEqual(None, r2)
        self.assertEqual('fr', r1.LANGUAGE_CODE)
        self.assertEqual('/test/foo/', r1.path_info)

    def test_check_session_disabled(self):
        self.settings_manager.set(LOCALEURL_USE_SESSION=False)
        reload(localeurl_settings)

        r1 = self.request_factory.get('/test/')
        r1.session = {'locale': 'fr'}
        r2 = self.middleware.process_request(r1)
        self.assertEqual(None, r2)



class TagsTestCase(LocaleurlTestCase):
    def render_template(self, text):
        t = test_utils.TestTemplate(text, libraries=[localeurl_tags.register])
        c = template.Context()
        return t.render(c)


    def test_locale_url_tag(self):
        self.assertRaises(ValueError, self.render_template,
                '{% locale_url "nl" dummy0 %}')

        self.assertEqual('/en/dummy/', self.render_template(
                '{% locale_url "en-us" dummy0 %}'))

        self.assertEqual('/fr/dummy/4', self.render_template(
            '{% locale_url "fr" dummy1 test=4 %}'))

        self.assertEqual('/en/dummy/4', self.render_template(
            '{% locale_url "en" dummy1 test=4 as testvar %}{{ testvar }}'))


    def test_chlocale_filter(self):
        self.assertEqual('/fr/dummy/', self.render_template(
                '{{ "/dummy/"|chlocale:"fr" }}'))

        self.assertEqual('/en/dummy/', self.render_template(
                '{{"/fr/dummy/"|chlocale:"en-gb"}}'))


    def test_rmlocale_filter(self):
        self.assertEqual('/dummy/', self.render_template(
                '{{ "/dummy/"|rmlocale }}'))

        self.assertEqual('/dummy/', self.render_template(
                '{{ "/nl-be/dummy/"|rmlocale }}'))

        self.assertEqual('/dummy/', self.render_template(
                '{{ "/en/dummy/"|rmlocale }}'))


    def test_locale_url_tag_no_default_prefix(self):
        self.settings_manager.set(PREFIX_DEFAULT_LOCALE=False)
        reload(localeurl_settings)

        self.assertEqual('/dummy/', self.render_template(
                '{% locale_url "en-us" dummy0 %}'))

        self.assertEqual('/fr/dummy/', self.render_template(
            '{% locale_url "fr" dummy0 %}'))


    def test_chlocale_filter_no_default_prefix(self):
        self.settings_manager.set(PREFIX_DEFAULT_LOCALE=False)
        reload(localeurl_settings)

        self.assertEqual('/dummy/', self.render_template(
                '{{ "/nl-nl/dummy/"|chlocale:"en-gb" }}'))

        self.assertEqual('/fr/dummy/', self.render_template(
                '{{"/nl-nl/dummy/"|chlocale:"fr"}}'))

try:
    from localeurl.templatetags import localeurl_future
except ImportError:
    localeurl_future = None


# @@@ use proper test skipping once Django 1.2 is minimum version
if localeurl_future is not None:
    class FutureTagsTestCase(LocaleurlTestCase):
        def render_template(self, text):
            t = test_utils.TestTemplate(text, libraries=[localeurl_future.register])
            c = template.Context()
            return t.render(c)


        def test_locale_url_tag(self):
            self.assertRaises(ValueError, self.render_template,
                    '{% locale_url "nl" "dummy0" %}')

            self.assertEqual('/en/dummy/', self.render_template(
                    '{% locale_url "en-us" "dummy0" %}'))

            self.assertEqual('/fr/dummy/4', self.render_template(
                '{% locale_url "fr" "dummy1" test=4 %}'))

            self.assertEqual('/en/dummy/4', self.render_template(
                '{% locale_url "en" "dummy1" test=4 as testvar %}{{ testvar }}'))


        def test_locale_url_tag_no_default_prefix(self):
            self.settings_manager.set(PREFIX_DEFAULT_LOCALE=False)
            reload(localeurl_settings)

            self.assertEqual('/dummy/', self.render_template(
                    '{% locale_url "en-us" "dummy0" %}'))

            self.assertEqual('/fr/dummy/', self.render_template(
                '{% locale_url "fr" "dummy0" %}'))



class DummyModel(object):
    def __init__(self, num):
        self.num = num


    def get_absolute_url(self):
        return '/dummy/%s/' % self.num



class DummySitemap(LocaleurlSitemap):
    def items(self):
        return [DummyModel(i) for i in range(3)]



class SitemapTestCase(LocaleurlTestCase):
    def setUp(self):
        super(SitemapTestCase, self).setUp()
        class DummySite(object):
            domain = 'www.example.com'
        from django.contrib.sites.models import Site
        self._orig_get_current = Site.objects.get_current
        Site.objects.get_current = lambda: DummySite()


    def test_localeurl_sitemap(self):
        sitemap = DummySitemap('fr')
        self.assertEqual(sitemap.get_urls()[0]['location'],
                         'http://www.example.com/fr/dummy/0/')


    def tearDown(self):
        super(SitemapTestCase, self).tearDown()
        from django.contrib.sites.models import Site
        Site.objects.get_current = self._orig_get_current



class ReverseTestCase(LocaleurlTestCase):
    def test_args_kwargs_none(self):
        url = reverse("dummy0", args=None, kwargs=None)
        self.assertEqual(url, "/en/dummy/")


    def test_args_none(self):
        url = reverse("dummy1", args=None, kwargs=dict(test="test"))
        self.assertEqual(url, "/en/dummy/test")


    def test_kwargs_none(self):
        url = reverse("dummy1", args=["test"], kwargs=None)
        self.assertEqual(url, "/en/dummy/test")


class ViewsTestCase(LocaleurlTestCase):

    urls = 'localeurl.urls'

    def setUp(self):
        super(LocaleurlTestCase, self).setUp()
        self.settings_manager = test_utils.TestSettingsManager()

    def test_change_locale_check_session_enabled(self):
        self.settings_manager.set(LOCALEURL_USE_SESSION=True)
        reload(localeurl_settings)
        self.client.post('/change/', data={'locale': 'de', 'next': '/foo'})
        self.assertEqual("de", self.client.session['django_language'])

    def test_change_locale_check_session_disabled(self):
        self.settings_manager.set(LOCALEURL_USE_SESSION=False)
        reload(localeurl_settings)
        self.client.post('/change/', data={'locale': 'de', 'next': '/foo'})
        self.assertNotEqual("de", self.client.session.get('django_language'))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.