1👍
To properly test redirects, use the follow parameter
If you set follow to True the client will follow any redirects and a
redirect_chain attribute will be set in the response object containing
tuples of the intermediate urls and status codes.
Then your code is as simple as
from django.test import TestCase
class Test(TestCase):
def test_login(self):
client = Client()
headers = {'X-OpenAM-Username': 'user', 'X-OpenAM-Password': 'password', 'Content-Type': 'application/json'}
data = {}
response = client.post('/login/', headers=headers, data=data, secure=False)
self.assertRedirects(response,'/destination/',302,200)
Note that it’s self.assertRedirects
rather than assert
or assertRedirects
Also note that the above test will most likely fail because you are posting an empty dictionary as the form data. Django form views do not redirect when the form is invalid and an empty form will probably be invalid here.
1👍
Django have plenty of tools for testing. For this task, you should use test case class from Django, for example django.test.TestCase
.
Then you can use method assertRedirects()
and it will check where you’ve been redirected and with which code. You can find any info you need here.
I’ve tried to write the code for your task:
from django.test import TestCase
class Test(TestCase):
def test_login(self):
data = {'X-OpenAM-Username': 'user', 'X-OpenAM-Password': 'password'}
response = client.post('/login/', data=data, content_type='application/json', secure=false)
assertRedirects(response, '/expected_url/', 200)
Then you can use python3 manage.py test
to run all tests.
- [Answered ]-Django admin: view hangs on field_sets block
- [Answered ]-Django remove duplicates in queryset and access their list of foreign key
- [Answered ]-Django loop over modelchoicefield queryset