def test_form_email_field_raises_validation_error_when_user_not_found( self): self.assertFalse( get_user_model().objects.filter(email='*****@*****.**').exists()) form = forms.UsersPasswordResetRequestForm( {'email': '*****@*****.**'}) self.assertEqual( form.errors['email'], ['No account associated with the email you submitted!'])
# Test a Form # #-------------# from django.contrib.auth import get_user_model from random import randrange from users import forms # create a user random_number = randrange(100000) email = f'{random_number}[email protected]' password = '******' user = get_user_model().objects.create_user(email=email, password=password) # create form object blank_form = forms.UsersPasswordResetRequestForm() form = forms.UsersPasswordResetRequestForm( {'email': f'{random_number}[email protected]'}) #-------------------# # Test a view class # #-------------------# from django.test import RequestFactory from django.urls import reverse from users import views request = RequestFactory().get(reverse('users:password_request_reset_link')) response = views.UsersForgotPasswordResetRequestView.as_view()(request) response.template_name[0] #---------------------------#
def test_has_correct_fields(self): form = forms.UsersPasswordResetRequestForm() self.assertEqual(list(form.fields.keys()), ['email'])
def test_form_converts_email_to_lowercase(self): form = forms.UsersPasswordResetRequestForm({'email': '*****@*****.**'}) form.cleaned_data = {'email': '*****@*****.**'} self.assertTrue(form.is_valid()) self.assertEqual(form.cleaned_data['email'], '*****@*****.**')
def test_renders_expected_instance(self): form = forms.UsersPasswordResetRequestForm() self.assertIsInstance(form, forms.UsersPasswordResetRequestForm)