def test_account_settings_form(self): form = AccountSettingsForm(instance=self.user, files={ 'avatar': SimpleUploadedFile('test.jpg', self.create_test_image_file().getvalue()), }) self.assertTrue(form.is_valid()) user = form.save() self.assertTrue(user.avatar)
def post(self, request): form = AccountSettingsForm(instance=request.user, data=request.POST, files=request.FILES) if form.is_valid(): form.save() messages.success(request, _('Your profile has been updated.')) return redirect('account.settings') return self.render_to_response({ 'form': form, })
def test_account_settings_form_avatar_dimensions_too_large(self): form = AccountSettingsForm(instance=self.user, files={ 'avatar': SimpleUploadedFile('test.jpg', self.create_test_image_file(width=8001).getvalue()), }) self.assertFalse(form.is_valid()) self.assertEqual(form.errors['avatar'], ['test.jpg is larger than 8000x8000 pixels.'])
def test_account_settings_form_avatar_not_an_image(self): form = AccountSettingsForm(instance=self.user, files={ 'avatar': SimpleUploadedFile('test.jpg', b'0'), }) self.assertFalse(form.is_valid()) self.assertEqual(form.errors['avatar'], ['test.jpg failed to verify as an image file.'])
def test_account_settings_form_avatar_filesize_too_large(self): form = AccountSettingsForm(instance=self.user, files={ 'avatar': SimpleUploadedFile('test.jpg', b'0' * (MAX_IMAGE_FILE_SIZE+1)), }) self.assertFalse(form.is_valid()) self.assertEqual(form.errors['avatar'], ['test.jpg is over 10MB in size.'])