繁体   English   中英

Django coverage测试登录和注销视图不起作用

[英]Django coverage testing login and logout views is not working

我在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'))

我使用coverage对它们进行统一测试,这是我的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)

问题是,当用户进行身份验证时,test_login不起作用,我不知道为什么(用户未获得密码),用户名和密码正确,并且他们确实在我的应用程序中登录,但在此测试中不起作用...而且,我也不知道如何测试注销视图。

用户是在json夹具数据中创建的:

[
    {"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"
             }

    }
]

任何帮助吗?,在此先感谢。

我建议按以下方式尝试客户端,

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

这是一个原型,我只是在解释这个想法。 检查此https://docs.djangoproject.com/en/1.8/topics/testing/tools/

您可以通过self.assertIn('_auth_user_id', c.session)检查用户是否成功登录,或者通过self.assertEqual(int(c.session['_auth_user_id']), user.pk)特定用户是否成功登录。

setUp方法中,您应该首先创建用户,因为单元测试将创建其自己的数据库。

更新这是一个更新,显示如何创建用户并测试登录名,身份验证

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM