mercredi 28 octobre 2015

Testing url redirection in Django

If I go to http://localhost:8000/login/, login form will be displayed and you have to input username and password. If the credentials are right then you will be redirected to http://localhost:8000/dashboard/. The problem I am facing while testing is even though my username and password are correct it is not redirected to http://localhost:8000/dashboard/ but rather it goes to http://testserver/admin/login/?next=/dashboard/. I am using the below code to rest redirection functionality in django:

class UrlTests(TestCase):

    def test_client_cart(self):
        response = self.client.get('/login/')
        self.assertEqual(200, response.status_code)

    def test_login_client(self):
        User.objects.create_user('rakesh', 'rrs402@nyu.edu', 'ranjan')
        self.client.get('/login/')
        self.client.login(username='rakesh', password='sukla')
        print self.client.get('/dashboard/')

Could anyone tell me why i am redirected to http://testserver/admin/login/?next=/dashboard/ instead http://localhost:8000/dashboard/.

In my settings.py file:

LOGIN_URL = '/'
LOGOUT_URL = '/logout/'
LOGIN_REDIRECT_URL = '/dashboard/'
LOGOUT_REDIRECT_URL = '/'

My application is working fine but I am not able to test redirection thing. Could anyone give me some pointers where I am going wrong?

If I print out print self.client.login(username='rakesh', password='sukla') it is coming out to be True which means I am able to login but why not redirection ?

Aucun commentaire:

Enregistrer un commentaire