Как мне написать тестовый пример для входа в систему с помощью электронной почты

В функциях test_login и test_get_add_page я хотел бы войти в систему, используя email, pw, а затем для одной из них перенаправить на страницу добавления и проверить, использует ли она правильный шаблон.

Выполнение следующих действий дает мне:

Traceback (most recent call last):
  File "C:\Users\arund\Desktop\Code\Instawork-Fall-Internship-2022\env\lib\site-packages\django\utils\datastructures.py", line 84, in __getitem__
    list_ = super().__getitem__(key)
KeyError: 'username'

Во время обработки вышеуказанного исключения произошло другое исключение:

Traceback (most recent call last):
  File "C:\Users\arund\Desktop\Code\Instawork-Fall-Internship-2022\instawork\Internship\tests.py", line 89, in test_login
    user_login = self.client.post(self.login_url, {'email': self.user.email, 'password': self.user.password})
  File "C:\Users\arund\Desktop\Code\Instawork-Fall-Internship-2022\env\lib\site-packages\django\test\client.py", line 852, in post
    response = super().post(
  File "C:\Users\arund\Desktop\Code\Instawork-Fall-Internship-2022\env\lib\site-packages\django\test\client.py", line 44    return self.generic(
  File "C:\Users\arund\Desktop\Code\Instawork-Fall-Internship-2022\env\lib\site-packages\django\test\client.py", line 541, in generic
    return self.request(**r)
  File "C:\Users\arund\Desktop\Code\Instawork-Fall-Internship-2022\env\lib\site-packages\django\test\client.py", line 810, in request
    self.check_exception(response)
  File "C:\Users\arund\Desktop\Code\Instawork-Fall-Internship-2022\env\lib\site-packages\django\test\client.py", line 663, in check_exception
    raise exc_value
  File "C:\Users\arund\Desktop\Code\Instawork-Fall-Internship-2022\env\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
  File "C:\Users\arund\Desktop\Code\Instawork-Fall-Internship-2022\env\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\arund\Desktop\Code\Instawork-Fall-Internship-2022\instawork\Internship\views.py", line 21, in login
    username = request.POST['username']
  File "C:\Users\arund\Desktop\Code\Instawork-Fall-Internship-2022\env\lib\site-packages\django\utils\datastructures.py", line 86, in __getitem__
    raise MultiValueDictKeyError(key)
django.utils.datastructures.MultiValueDictKeyError: 'username'

Я пытаюсь использовать электронную почту вместо имени пользователя для входа по умолчанию.

from django.test import TestCase, Client
from django.urls import reverse

from .models import Profile, Team
from .forms import ProfileForm
# Create your tests here.
 
class Instawork(TestCase):

    #fields are first_name, last_name, is_edit, email
    def setUp(self):
        self.client = Client()
        self.user = Profile.objects.create(first_name='A', last_name='C',email='a@hotmail.com',is_edit=True,phone_number="+17777777777")
        self.user.set_password('password')
        self.user.save()
        self.login_url =  reverse('login')
        self.add_url = reverse('add')


class TestLogin(Instawork):
    def test_get_login_page(self):
        response = self.client.get(self.login_url)
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response,"login.html") 
    def test_login(self):
        user_login = self.client.post(self.login_url, {'email': self.user.email, 'password': self.user.password})
        #self.assertTrue(user_login.context['user'].is_authenticated)
        self.assertTemplateUsed(user_login,"home.html")
    def test_get_add_page(self):
        response=self.client.post(self.login_url, {'email': self.user.email, 'password': self.user.password})
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response,"add.html")

class TestForms(Instawork):
    def test_form_validity(self):
        """ form_data = {
            'email': 'a@hotmail.com',
            'is_edit': True,
            'first_name':'c',
            'last_name':'a',
            'phone_number':'+17777777777'
        }
        form = ProfileForm(data=form_data)
        print(form)
        self.assertTrue(form.is_valid()) """
Вернуться на верх