I'm new to Django and I have some difficulties starting testing my code. I want to test that the login function I wrote can refuse connection if the form is invalid and log in if it valid.
Here is the code I want to test :
def login_view(request):
title = "Login"
form = UserLoginForm(request.POST or None)
context = {"form":form, "title":title}
if form.is_valid():
username = form.cleaned_data.get("username")
password = form.cleaned_data.get("password")
user = authenticate(username=username,password=password)
login(request,user)
return redirect('/production')
return render(request, "registration/form.html", context)
All tutorials I saw only test that posting a valid form to the correct url works or that an invalid form don't. Something like :
def test_login_with_no_username(self):
form = UserLoginForm({'username': "JohnDoe",'password': "DoeJohn",})
self.assertFalse(form.is_valid())
response = self.client.post("login", form)
self.assertEqual(response.status_code, 302)
I trying to pass in argument of login_view
a HttpRequest
with a QuerySet
but I can't find a way to make it works.
Is is at least possible ? Is the test that I've described is enough ?
Aucun commentaire:
Enregistrer un commentaire