I have a view that allows to add tags dynamically with an ajax request. It looks like this:
@require_POST
@login_required
def addtag(request):
"""
a view to create a new tag in the tag database
"""
some logic here
This is what my url.py looks like:
urlpatterns = patterns('',
url(r'^addtag/$',addtag, name='addtag'),
)
And my test is doing that:
def test_addtag(self):
url='^addtag/$'
response = self.client.post(url,{'addtag':'"new tag"'})
self.assertEqual(response.status_code,401)
I expected the returned status code to be a 401, since the testclient is not logged in. So the first decorator, checking if the request is a post is being passed happily. Then i expected the login_required decorator to return a 401, but it didn’t:
AssertionError: 302 != 401
First i thought the login_required decorator would be redirecting to some login page. Checked that, i do not have a settings.LOGIN_REDIRECT_URL specified. So what is login_required doing in this case?
@login_requiredredirects to the login page if the user is not logged in — hence the view returns 302 in such a case. (If you did not setLOGIN_REDIRECT_URLit uses a default value.)