簡體   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