def test_update_user_password_when_password_is_too_large(self): newpass = '******' * (MAX_PAGE_SIZE + 1) update_form = UserUpdateForm({'password': newpass, 'password_again': newpass}, instance=self.user) self.assertFalse(update_form.is_valid()) self.assertEqual(update_form.errors['__all__'][0], 'You have only 640Kb for all purposes!')
def test_update_user_password_when_passwords_are_differ(self): newpass = '******' update_form = UserUpdateForm({'password': newpass, 'password_again': newpass + "occasional symbols"}, instance=self.user) self.assertFalse(update_form.is_valid()) self.assertEqual(update_form.errors['__all__'][0], 'Passwords mismatch')
def test_update_user_password_when_password_is_short(self): newpass = '******' * 7 update_form = UserUpdateForm({'password': newpass, 'password_again': newpass}, instance=self.user) self.assertFalse(update_form.is_valid()) self.assertEqual(update_form.errors['__all__'][0], 'Password length must be at least 8 symbols')
def test_update_user_avatar(self): img_file, content_type = create_test_image(100) avatar = {'avatar': SimpleUploadedFile('newavatar.bmp', img_file.read(), content_type)} update_form = UserUpdateForm({}, avatar, instance=self.user) self.assertTrue(update_form.is_valid()) update_form.save() self.assertEqual(avatar['avatar'].size, update_form.cleaned_data['avatar'].size) self.assertEqual('newavatar.bmp', update_form.cleaned_data['avatar'].name)
def test_update_user_password(self): newpass = '******' update_form = UserUpdateForm({'password': newpass, 'password_again': newpass}, instance=self.user) self.assertTrue(update_form.is_valid()) update_form.save() self.user.refresh_from_db() self.assertTrue(self.user.check_password(newpass))
def test_update_basic_user_text_data(self): newfirstname = 'new firstname' newlastname = 'new lastname' newpatronymic = 'new patronymic' newbirth_date = '10.04.1986' oldpass = self.user.password update_form = UserUpdateForm({'firstname': newfirstname, 'lastname': newlastname, 'patronymic': newpatronymic, 'birth_date': newbirth_date}, instance=self.user) update_form.is_valid() self.assertTrue(update_form.is_valid()) update_form.save() self.user.refresh_from_db() self.assertEqual(self.user.firstname, newfirstname) self.assertEqual(self.user.lastname, newlastname) self.assertEqual(self.user.patronymic, newpatronymic) self.assertEqual(self.user.password, oldpass) self.assertEqual(timezone.datetime.strftime(self.user.birth_date, DATE_FORMAT), newbirth_date)
def update_user_by_form(request, context): user = context['user'] valid = False user_update_form = UserUpdateForm(request.POST, request.FILES, instance=user) if user_update_form.is_valid(): user_update = user_update_form.save(commit=False) if user_update.avatar: user_update.avatar_size = user_update.avatar.size user_update.save() context['user'] = user_update valid = True else: # This is strange but user instance is changed after form validation context['user'].refresh_from_db() return user_update_form, valid
def test_update_user_avatar_when_pic_is_too_large(self): img_file, content_type = create_test_image(3000) avatar = {'avatar': SimpleUploadedFile('newavatar.bmp', img_file.read(), content_type)} update_form = UserUpdateForm({}, avatar, instance=self.user) self.assertFalse(update_form.is_valid()) self.assertEqual(update_form.errors['__all__'][0], 'You have only 640Kb for all purposes!')