Ejemplo n.º 1
0
 def test_user_form_when_password_is_incorrect(self):
     user_form_data = {'username': self.random_name,
                       'email': self.email,
                       'password': '******',
                       'password_again': '12345678',
                       }
     user_form = UserForm(user_form_data)
     self.assertFalse(user_form.is_valid())
     self.assertEqual(user_form.errors['__all__'][0], 'Passwords mismatch')
Ejemplo n.º 2
0
 def test_user_form_when_email_is_incorrect(self):
     user_form_data = {'username': self.random_name,
                       'email': self.random_name + 'm.ru',
                       'password': self.password,
                       'password_again': self.password,
                       }
     user_form = UserForm(user_form_data)
     self.assertFalse(user_form.is_valid())
     self.assertEqual(user_form.errors['email'][0], 'Enter a valid email address.')
Ejemplo n.º 3
0
 def test_user_form_when_username_is_too_short(self):
     name = 'XX'
     user_form_data = {'username': name,
                       'email': self.email,
                       'password': self.password,
                       'password_again': self.password,
                       }
     user_form = UserForm(user_form_data)
     self.assertFalse(user_form.is_valid())
     self.assertIn('Ensure this value has at least 3 characters', user_form.errors['username'][0])
Ejemplo n.º 4
0
 def test_user_form(self):
     img_file, content_type = create_test_image()
     avatar = SimpleUploadedFile('myavatar.bmp', img_file.read(), content_type)
     user_form_data = {'username': self.random_name,
                       'email': self.email,
                       'password': self.password,
                       'password_again': self.password,
                       }
     user_form = UserForm(user_form_data, {'avatar': avatar})
     self.assertTrue(user_form.is_valid())
Ejemplo n.º 5
0
 def test_user_form_when_avatar_is_too_large(self):
     img_file, content_type = create_test_image(3000)
     avatar = SimpleUploadedFile('myavatar.bmp', img_file.read(), content_type)
     user_form_data = {'username': self.random_name,
                       'email': self.email,
                       'password': self.password,
                       'password_again': self.password,
                       }
     user_form = UserForm(user_form_data, {'avatar': avatar})
     self.assertFalse(user_form.is_valid())
     self.assertEqual(user_form.errors['__all__'][0], 'You have only 640Kb for all purposes!')
Ejemplo n.º 6
0
 def test_user_form_when_username_had_wrong_symbols(self):
     name = 'X.X'
     user_form_data = {'username': name,
                       'email': self.email,
                       'password': self.password,
                       'password_again': self.password,
                       }
     user_form = UserForm(user_form_data)
     self.assertFalse(user_form.is_valid())
     self.assertIn('Username should contain only letters, digits, underscores, and dashes',
                   user_form.errors['username'][0])
Ejemplo n.º 7
0
def save_user_by_form(request, context):
    valid = False
    user_form = UserForm(request.POST, request.FILES)

    if user_form.is_valid():
        valid = True
        try:
            user = user_form.save()
        except UserException as error:
            logging.error(
                f"Error occured while saving user from form: {error}")
            user_form.valid = False
            # add error to user form
            user_form.errors.update({"__all__": str(error)})
            return user_form, False

        context['user'] = user
    return user_form, valid