def test_userform_valid(self): l_user = PublicUserForm( data={ 'email': self.email, 'first_name': self.first_name, 'last_name': self.last_name, 'company': self.company }) self.assertTrue(l_user.is_valid(), "Form is not valid !") self.assertEqual(User.objects.all().count(), 0, "[DB] an User already exist !") l_user.save() self.assertEqual(User.objects.all().count(), 1, "[DB] User has not been created after save form !") l_user = User.objects.get(email=self.email) self.assertEqual(l_user.email, self.email, "[DB] email invalid !") self.assertEqual(l_user.first_name, self.first_name, "[DB] first_name invalid !") self.assertEqual(l_user.last_name, self.last_name, "[DB] last_name invalid !") self.assertEqual(l_user.company, self.company, "[DB] company invalid !") self.assertEqual(l_user.groups.count(), 0, "[DB] Groups already exist !") self.assertEqual(l_user.user_permissions.count(), 0, "[DB] User_Permissions already exist !") self.assertFalse(l_user.is_staff, "[DB] is_staff is not False !") self.assertTrue(l_user.is_active, "[DB] is_active is not True !") self.assertFalse(l_user.is_superuser, "[DB] is_super_user is not False !")
def test_userform_not_valid_only_email(self): l_user = PublicUserForm(data={'email': self.email}) self.assertFalse(l_user.is_valid(), "Form is valid !") self.assertEqual(len(l_user.errors), 3, "Expected only 5 errors !") self.assertEqual(len(l_user['last_name'].errors), 1, "Expected only 1 error for 'last_name' field !") self.assertEqual(l_user['last_name'].errors[0], "This field is required.", "Error message not expected !") self.assertEqual(len(l_user['first_name'].errors), 1, "Expected only 1 error for 'first_name' field !") self.assertEqual(l_user['first_name'].errors[0], "This field is required.", "Error message not expected !") self.assertEqual(len(l_user['company'].errors), 1, "Expected only 1 error for 'company' field !") self.assertEqual(l_user['company'].errors[0], "This field is required.", "Error message not expected !")
def test_userform_constructor_error_email_exist(self): l_user = PublicUserForm( data={ 'email': self.email, 'first_name': self.first_name, 'last_name': self.last_name, 'company': self.company }) self.assertTrue(l_user.is_valid(), "Form is not valid !") self.assertEqual(User.objects.all().count(), 0, "[DB] an User already exist !") l_user.save() self.assertEqual(User.objects.all().count(), 1, "[DB] User has not been created after save form !") l_user = PublicUserForm( data={ 'email': self.email, 'first_name': self.first_name, 'last_name': self.last_name, 'company': self.company }) self.assertFalse(l_user.is_valid(), "Form is valid !") self.assertEqual(len(l_user.errors), 1, "Expected only 1 errors !") self.assertEqual(len(l_user['email'].errors), 1, "Expected only 1 error for 'email' field !") self.assertEqual(l_user['email'].errors[0], "User with this Email address already exists.", "Error message not expected !")
def test_userform_constructor_error_email(self): l_user = PublicUserForm( data={ 'email': "toto", 'first_name': self.first_name, 'last_name': self.last_name, 'company': self.company }) self.assertFalse(l_user.is_valid(), "Form is valid !") self.assertEqual(len(l_user.errors), 1, "Expected only 1 error !") self.assertEqual(len(l_user['email'].errors), 1, "Expected only 1 error for 'email' field !") self.assertEqual(l_user['email'].errors[0], "Enter a valid email address.", "Error message not expected !") l_user = PublicUserForm( data={ 'email': "toto@titi", 'first_name': self.first_name, 'last_name': self.last_name, 'company': self.company }) self.assertFalse(l_user.is_valid(), "Form is valid !") self.assertEqual(len(l_user.errors), 1, "Expected only 1 error !") self.assertEqual(len(l_user['email'].errors), 1, "Expected only 1 error for 'email' field !") self.assertEqual(l_user['email'].errors[0], "Enter a valid email address.", "Error message not expected !")