Получение ответа 200 вместо 302 в тесте django
Я использую django-pytest в urls есть маршрут signup/
, при обращении к которому в браузере отображается форма регистрации, которая ожидает поля ниже
['first_name', 'last_name', 'username', 'email', 'password1', 'password2']
Вот SignUpView
и SignupForm
import uuid
from django import forms
from django.contrib import messages
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django.core.mail import send_mail
from django.shortcuts import redirect
from django.urls import reverse
from django.views.generic import FormView
class SignUpForm(UserCreationForm):
first_name = forms.CharField(
widget=forms.TextInput(
attrs={'class': 'form-control', 'placeholder': 'First name'}
),
label='',
)
last_name = forms.CharField(
widget=forms.TextInput(
attrs={'class': 'form-control', 'placeholder': 'Last name'}
),
label='',
)
username = forms.CharField(
widget=forms.TextInput(
attrs={'class': 'form-control', 'placeholder': 'Username'}
),
label='',
)
email = forms.EmailField(
widget=forms.EmailInput(
attrs={'class': 'form-control', 'placeholder': 'Email'}
),
label='',
)
password1 = forms.CharField(
widget=forms.PasswordInput(
attrs={'class': 'form-control', 'placeholder': 'Password'}
),
label='',
)
password2 = forms.CharField(
widget=forms.PasswordInput(
attrs={'class': 'form-control', 'placeholder': 'Confirm password'}
),
label='',
)
class SignUpView(FormView):
template_name = 'center-form.html'
form_class = SignUpForm
def form_valid(self, form):
if form.is_valid():
user = User.objects.create_user(
first_name=form.cleaned_data['first_name'],
last_name=form.cleaned_data['last_name'],
username=form.cleaned_data['username'],
email=form.cleaned_data['email'],
password=form.cleaned_data['password1'],
is_active=False,
)
activation_code = uuid.uuid4().hex
Activation.objects.create(code=activation_code, user=user)
activation_url = self.request.build_absolute_uri(
reverse('activate', kwargs={'code': activation_code})
)
send_mail(
'Activate translation account',
f'To activate your account, please follow the link below\n{activation_url}',
'Translation services',
[form.cleaned_data['email']],
fail_silently=False,
)
messages.success(
self.request,
'Please check your email for a message with the activation code.',
)
return redirect('index')
При доступе в браузере и создании пользователя все работает как ожидалось. Вот тест, который, как я ожидаю, пройдет.
import pytest
from django.urls import reverse
@pytest.mark.django_db
class TestSignup:
def test_valid(self, client, valid_user_data, django_user_model):
url = reverse('signup')
client.get(url)
response = client.post(url, valid_user_data)
assert django_user_model.objects.count() and response.status_code == 302
Это не так. Код состояния ответа равен 200, и django_user_model.objects.count()
все еще 0
после ответа.
Вот valid_user_data
{
'first_name': 'First',
'last_name': 'Last',
'username': 'username',
'email': 'email@domain.com',
'password1': 'HJGcutcu23TYCUhtxfy5ex',
'password2': 'HJGcutcu23TYCUhtxfy5ex',
}
<TemplateResponse status_code=200, "text/html; charset=utf-8">