Мне нужна помощь django-pytest invalid token error due to fixtures or token generator
fixtures.py, здесь расположены мои светильники
from django.test import Client
from pearmonie.auth.models import User
from pearmonie.settings.models import BusinessSettings
import pytest
@pytest.fixture
def BASE_URL():
return "http://localhost:8000"
@pytest.fixture
def user_sub_info():
return {"email": "a@gmail.com", "fcm_token": ""}
@pytest.fixture
def user_info():
return {
"phone": "+234955443322",
"password": "Password.1",
}
@pytest.fixture
def user_info_mail():
return {
"phone": "+234955443322",
"password": "Password.1",
"email": "a@gmail.com",
}
@pytest.fixture(scope="session", autouse=True)
def HTTP_AUTHORIZATION():
return {"HTTP_AUTHORIZATION": ""}
@pytest.mark.django_db()
def user(user_info, user_sub_info):
user = User.objects.create(email=user_sub_info["email"], **user_info)
BusinessSettings.objects.create(business_name="my store", user=user)
return user
@pytest.fixture(scope="session", autouse=True)
def client():
return Client()
@pytest.fixture
def codes():
return {
"otp": "000000",
"transaction_pin1": "000000",
"transaction_pin2": "111111",
"pinId": "222222",
}
auth/tests.py
import pytest
from pearmonie.auth.models import Otp, User
from pearmonie.auth.lambdas import gen_token
from utils.fixtures import (
client,
HTTP_AUTHORIZATION,
BASE_URL,
user_info,
user_sub_info,
codes,
user_fixture,
user_info_mail
)
@pytest.mark.django_db
def test_auth(
client, BASE_URL, HTTP_AUTHORIZATION, user_fixture, user_info, user_sub_info, user_info_mail, codes
):
client.login(**user_info)
otp = Otp.objects.create(code=codes["otp"], pinId=codes["pinId"], user=user_fixture, is_verified=True)
assert otp.code == codes["otp"]
assert otp.is_verified
token = gen_token(data = user_info_mail)
HTTP_AUTHORIZATION["Authorization"] = token
assert HTTP_AUTHORIZATION["Authorization"] == token
password_reset_response = client.post(
f"{BASE_URL}/auth/password/reset/",
{
"phone": user_info["phone"],
"code": otp.code,
"new_password": "HardPass@2022..",
"fcm_token": user_sub_info["fcm_token"],
},
**HTTP_AUTHORIZATION,
content_type="application/json",
)
assert password_reset_response.status_code == 200
assert password_reset_response.headers["Authorization"]
@pytest.mark.django_db
def test_transaction_pin(client, HTTP_AUTHORIZATION, BASE_URL, codes):
check_pin_response = client.get(
f"{BASE_URL}/auth/pin/check/",
**HTTP_AUTHORIZATION,
content_type="application/json",
)
assert check_pin_response.status_code == 200
create_pin_response = client.post(
f"{BASE_URL}/auth/pin/create/",
{"pin": codes["transaction_pin1"]},
**HTTP_AUTHORIZATION,
content_type="application/json",
)
assert create_pin_response.status_code == 201
verify_transaction_pin_response = client.post(
f"{BASE_URL}/auth/pin/verify/",
{"pin": codes["transaction_pin1"]},
**HTTP_AUTHORIZATION,
content_type="application/json",
)
assert verify_transaction_pin_response.status_code == 200
change_transaction_pin_response = client.post(
f"{BASE_URL}/auth/pin/change/",
{"old_pin": codes["transaction_pin1"], "new_pin": codes["transaction_pin2"]},
**HTTP_AUTHORIZATION,
content_type="application/json",
)
assert change_transaction_pin_response.status_code == 200
otp = Otp.objects.create(
code=codes["otp"], pinId=codes["pinId"], user=user_fixture, is_verified=True
)
assert otp.code == codes["otp"]
assert otp.is_verified
reset_transaction_pin_response = client.post(
f"{BASE_URL}/auth/pin/reset/",
{
"new_pin": codes["transaction_code1"],
"old_pin": codes["transaction_pin2"],
"code": otp.code,
},
**HTTP_AUTHORIZATION,
content_type="application/json",
)
assert reset_transaction_pin_response.status_code == 200
Итак, когда я запускаю тест, он проходит успешно для первого теста, а затем генерирует недействительный токен для остальных. GET /auth/pin/check/ 400: недействительный токен {}
Так что это единственная проблема с моим кодом, я думаю, что это может быть из-за фиксов или генераторов токенов, я полагаю