Ticket #15179: tests.py

File tests.py, 1.6 KB (added by Ciantic, 5 years ago)

Test against Client.login request middlewares and params

Line 
1from django.conf import settings
2from django.conf.urls.defaults import * #@UnusedWildImport
3from django.contrib.auth.models import User
4from django.http import HttpResponse
5from django.test import TestCase, Client
6from django.contrib.auth.signals import user_logged_in
7from django.dispatch import receiver
8
9urlpatterns = patterns('',
10    (r'^$', 'testing.tests.http_host_view'),
11)
12
13def http_host_view(request):
14    return HttpResponse(request.get_host() + request.middlewares)
15   
16class MyMiddleware:
17    def process_request(self, request):
18        request.middlewares = '+middlewares'
19
20class DjangoClientRequestTests(TestCase):
21    urls = 'testing.tests'
22
23    def setUp(self):
24        u = User.objects.create(username='tester')
25        u.set_password('1234')
26        u.save()
27        self.MIDDLEWARE_CLASSES = settings.MIDDLEWARE_CLASSES
28        settings.MIDDLEWARE_CLASSES = ['testing.tests.MyMiddleware']
29       
30    def tearDown(self):
31        settings.MIDDLEWARE_CLASSES = self.MIDDLEWARE_CLASSES
32       
33    def test_client_request(self):
34        c = Client(HTTP_HOST='myhost.com')
35        self.assertEqual(c.get("/").content, 'myhost.com+middlewares')
36
37    def test_client_login(self):
38        c = Client(HTTP_HOST='myhost.com')
39       
40        @receiver(user_logged_in)
41        def login(sender, signal, **kwargs):
42            request = kwargs.get('request', None)
43            if request:
44                self.assertEqual(request.get_host() + request.middlewares, 'myhost.com+middlewares') # <------ This fails
45           
46        c.login(username='tester', password='1234')
47
Back to Top