如何创建可以添加会话的灯具?

时间:2019-08-31 15:08:13

标签: python django api pytest pytest-django

我有我的API,在这里我开发了登录和注销视图。现在我想编写pytests,我有一个问题,idk如何创建固定装置,使我的用户可以被授权并在会话中设置一些ID(注销时需要)。

我通过Fixture创建new_user(以避免使用我的数据库)。

观看次数

@csrf_exempt
def login(request):
    if request.method == "POST":
        data = json.loads(request.body.decode('utf-8'))
        if not is_data_valid_for_login(data):
            return HttpResponseBadRequest()
        user = authenticate(email=data["email"], password=data["password"])
        if user:
            auth_login(request, user)
            response = HttpResponse(status=200, content_type='application/json')
            request.session['id'] = user.id
            return response
        return HttpResponseBadRequest()
    return HttpResponseBadRequest()


@csrf_exempt
def logout(request):
    if request.method == "GET":
        auth_logout(request)
        response = HttpResponse(status=200)
        if 'id' in request.session:
            del request.session['id']
        return response
    return HttpResponseBadRequest

pytest

import pytest
from user.models import User

PASSWORD = "testPassword"


@pytest.fixture()
def user():
    return User.create(first_name="Name", last_name="Last", email="test@test.com", password=PASSWORD)

0 个答案:

没有答案