def test_comment_form_disallow_empty_comments(self): form = CommentForm(data={ 'content_type': ContentType.objects.get_for_model(self.manga).id, 'object_id': self.manga.id, 'markdown': '\n\n', }) self.assertFalse(form.is_valid()) self.assertEqual(form.errors['markdown'], ['Your comment cannot be blank.'])
def post(self, request): next_url = request.POST.get('next', reverse('manga.list')) form = CommentForm(data=request.POST) if form.is_valid(): form.save(request) messages.success(request, _('Your comment has been posted.')) return redirect(next_url) messages.error(request, '\n'.join(['{}: {}'.format(field.label, error) for field in form for error in field.errors])) return redirect(next_url)
def test_comment_form_save(self): content_type = ContentType.objects.get_for_model(self.manga) form = CommentForm(data={ 'content_type': content_type.id, 'object_id': self.manga.id, 'markdown': 'This is a very simple comment', }) self.assertTrue(form.is_valid()) comment = form.save(self.request) self.assertEqual(comment.content_type, content_type) self.assertEqual(comment.object_id, self.manga.id) self.assertEqual(comment.ip_address, '127.0.0.1') self.assertEqual(comment.created_by, self.user) self.assertTrue(comment.created_on)