I got this login and logout views on my views.py :
class Login(View):
template_name = ['cost_control_app/login.html', 'cost_control_app/home.html']
def get(self, request, *args, **kwargs):
form = UsersForm()
return render(request, self.template_name[0],{"form":form,})
def post(self, request, *args, **kwargs):
#import pdb; pdb.set_trace()
username = request.POST['username']
password = request.POST['password']
#import pdb; pdb.set_trace()
user = authenticate(username=username, password=password)
if user is not None:
login(request, user)
form_group = GroupsForm()
lista = definetree(request.user.groups.all()[0].name, request.user.id)
return render_to_response(self.template_name[1], {"form_group" : form_group,
"groups":lista[0],
"subgroups":lista[1] ,
"sub_subgroups":lista[2],
"cost_items":lista[3],
"count_requests":lista[4],
},RequestContext(request))
else:
#messages.error(request, "Usuario o password incorrecto")
return HttpResponseRedirect(reverse('cost_control_app:login'))
def logout_view(request):
logout(request)
return HttpResponseRedirect(reverse('cost_control_app:login'))
And im using coverage to make unitary test on them, this is my authentication_test.py :
from django.test import TestCase
from cost_control_app.models import *
from cost_control_app.form import *
from cost_control_app.views.views_authentication import *
from django.http import HttpRequest
from django.conf import settings
from django.utils.importlib import import_module
class test_login(TestCase):
fixtures = ['test_data_cost_control_app.json']
def test_login_get(self):
request = HttpRequest()
request.method = 'GET'
Login.as_view()(request)
def test_login_post(self):
request = HttpRequest()
request.method = 'POST'
request.POST['username'] = "jsanchezs"
request.POST['password'] = "pbkdf2_sha256$20000$PFQCunwR7Rzz$rhTeSCRUCz8hqwYGf8Uprj+B/5yAmYMzjc6jamY9eCw="
Login.as_view()(request)
Thing is, test_login doesn't work when user authenticates and i dont know why (user gets None), user and password are correct and they do login inside my app, but not in this test...also, i don't know how to test the logout view.
Users are created in a json fixtures data :
[
{"pk":1,
"model":"auth.user",
"fields":{
"password":,
"last_login":"2015-12-30 14:17:39.413827",
"is_superuser":1,
"username":"jsanchezs",
"first_name":"Juan David",
"last_name":"Sanchez",
"email":"anything@gmail.com",
"is_staff":1,
"is_active":1,
"date_joined":"2015-12-30 14:17:29.150420"
}
}
]
Any help ?, thanks in advance.
I suggest to try the client as follows,
from django.test import Client
class test_login(TestCase):
fixtures = ['test_data_cost_control_app.json']
def test_login_post(self):
c = Client()
response = c.post('url to login', {'username': 'jsanchezs', 'password': 'password in plain text'}) # you can use here reverse for urls
self.assertEqual(response.status, 200) # or any other value
It is a prototype, I'm just explaining the idea. Check this https://docs.djangoproject.com/en/1.8/topics/testing/tools/
You can check if the user has successfully logged in by self.assertIn('_auth_user_id', c.session)
or specific user by self.assertEqual(int(c.session['_auth_user_id']), user.pk)
In setUp
method you should create the user in the first place as the unittests create its own db.
Update This is an update to show how to create a user and test login, authenticate
class test_login(TestCase):
fixtures = ['test_data_cost_control_app.json']
def setUp(self):
self.user = User.objects.create_user(
username='jsanchezs', email='jacob@test.com', password='password in plain text')
def test_login_post_success(self):
c = Client()
response = c.post('url to login', {'username': self.user.username, 'password': 'password in plain text'}) # you can use here reverse for urls
self.assertEqual(response.status, 200) # or any other value
def test_authenticate_success(self):
result = authenticate(username=self.user.username, password='password in plain text')
self.assertTrue(result is not None)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.