def test_form_bad_data(self): params = { "username": self.user.username, "username2": self.user.username } form = UsernameChangeForm(data=params) self.assertFalse(form.is_valid()) self.assertEqual(form.errors["username"], [u"A user with that username already exists."]) params = { "username": self.user.username + "1", "username2": self.user.username } form = UsernameChangeForm(data=params) self.assertFalse(form.is_valid()) self.assertEqual(form.errors["username2"], [u"The two username fields don't match."]) params = {"username": "******", "username2": "username\x00"} form = UsernameChangeForm(data=params) self.assertFalse(form.is_valid()) self.assertEqual(form.errors["username"], [u"Null characters are not allowed."]) params = {"username": "******", "username2": "usernameß"} form = UsernameChangeForm(data=params) self.assertFalse(form.is_valid()) expected_errors = [ u"Enter a valid username. This value may contain only English letters, numbers, and @/./+/-/_ characters." ] # noqa: E501 self.assertEqual(form.errors["username"], expected_errors)
def test_form_good_data(self): username = self.user.username params = { "username": self.user.username + "1", "username2": self.user.username + "1" } form = UsernameChangeForm(data=params) self.assertTrue(form.is_valid(), form.errors) form.save() new_user = get_user_model().objects.get(pk=form.instance.pk) self.assertEqual(new_user.username, username + "1")