def test_form_bad_data(self): request = utils.MockRequest(self.user) params = {"new_username1": self.user.username, "new_username2": self.user.username} form = UsernameChangeForm(request, data=params) self.assertFalse(form.is_valid()) params = {"new_username1": self.user.username + "1", "new_username2": self.user.username} form = UsernameChangeForm(request, data=params) self.assertFalse(form.is_valid())
def test_form_good_data(self): username = self.user.username params = {"new_username1": self.user.username + "1", "new_username2": self.user.username + "1"} request = utils.MockRequest(self.user) form = UsernameChangeForm(request, data=params) self.assertTrue(form.is_valid()) form.save() new_user = get_user_model().objects.get(pk=form.user.pk) self.assertEqual(new_user.username, username + "1")
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")
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()) if six.PY3: expected_errors = [ u"Enter a valid username. This value may contain only letters, numbers, and @/./+/-/_ characters." ] # noqa: E501 else: 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_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": "******".decode(), "username2": "username\x00".decode() } form = UsernameChangeForm(data=params) self.assertFalse(form.is_valid()) self.assertEqual(form.errors["username"], [u"Null characters are not allowed."]) params = { "username": "******".decode("utf-8"), "username2": "usernameß".decode("utf-8") } form = UsernameChangeForm(data=params) self.assertFalse(form.is_valid()) self.assertEqual(form.errors["username"], [ u"Enter a valid username. This value may contain only English letters, numbers, and @/./+/-/_ characters." ])