Skip to content

Commit

Permalink
comment tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nitely committed May 15, 2016
1 parent acbd91f commit 6513fec
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
4 changes: 2 additions & 2 deletions spirit/comment/forms.py
Expand Up @@ -22,13 +22,13 @@ class CommentForm(forms.ModelForm):
max_length=settings.ST_COMMENT_MAX_LEN,
widget=forms.Textarea)
comment_hash = forms.CharField(
max_length=128,
max_length=32,
widget=forms.HiddenInput,
required=False)

class Meta:
model = Comment
fields = ['comment', ]
fields = ['comment']

def __init__(self, user=None, topic=None, *args, **kwargs):
super(CommentForm, self).__init__(*args, **kwargs)
Expand Down
5 changes: 4 additions & 1 deletion spirit/comment/models.py
Expand Up @@ -87,4 +87,7 @@ def create_moderation_action(cls, user, topic, action):

@classmethod
def get_last_for_topic(cls, topic_id):
return cls.objects.filter(topic_id=topic_id).last()
return (cls.objects
.filter(topic_id=topic_id)
.order_by('pk')
.last())
69 changes: 69 additions & 0 deletions spirit/comment/tests.py
Expand Up @@ -5,6 +5,7 @@
import os
import json
import shutil
import hashlib

from django.test import TestCase, RequestFactory
from django.core.cache import cache
Expand Down Expand Up @@ -89,6 +90,42 @@ def mocked_comment_posted(comment, mentions):
finally:
views.comment_posted = org_comment_posted

@override_settings(ST_DOUBLE_POST_THRESHOLD_MINUTES=10)
def test_comment_publish_double_post(self):
"""
Should prevent double posts
"""
utils.login(self)
comment_txt = 'foobar'
utils.create_comment(topic=self.topic)
self.assertEqual(len(Comment.objects.all()), 1)

# First post
self.client.post(
reverse('spirit:comment:publish', kwargs={'topic_id': self.topic.pk}),
{'comment': comment_txt})
self.assertEqual(len(Comment.objects.all()), 2)

# Double post
cache.clear() # Clear rate limit
response = self.client.post(
reverse('spirit:comment:publish', kwargs={'topic_id': self.topic.pk}),
{'comment': comment_txt})
self.assertEqual(len(Comment.objects.all()), 2) # Prevented!

self.assertRedirects(
response,
expected_url=Comment.get_last_for_topic(self.topic.pk).get_absolute_url(),
status_code=302,
target_status_code=302)

# New post
cache.clear() # Clear rate limit
self.client.post(
reverse('spirit:comment:publish', kwargs={'topic_id': self.topic.pk}),
{'comment': 'not a foobar'})
self.assertEqual(len(Comment.objects.all()), 3)

def test_comment_publish_on_private(self):
"""
create comment on private topic
Expand Down Expand Up @@ -438,6 +475,14 @@ def test_comment_create_moderation_action(self):
Comment.create_moderation_action(user=self.user, topic=self.topic, action=1)
self.assertEqual(Comment.objects.filter(user=self.user, topic=self.topic, action=1).count(), 1)

def test_comment_get_last_for_topic(self):
"""
Should return last comment for a given topic
"""
utils.create_comment(topic=self.topic)
comment_last = utils.create_comment(topic=self.topic)
self.assertEqual(Comment.get_last_for_topic(self.topic.pk), comment_last)


class CommentTemplateTagTests(TestCase):

Expand Down Expand Up @@ -511,6 +556,30 @@ def test_comment_markdown_no_follow(self):
comment2 = form.save()
self.assertEqual(comment2.comment_html, '<p><a href="http://foo.com">http://foo.com</a></p>')

def test_comment_get_comment_hash(self):
"""
Should return the comment hash
"""
comment_txt = 'foo'
form_data = {'comment': comment_txt}
form = CommentForm(data=form_data, topic=self.topic)
self.assertTrue(form.is_valid())

comment_txt_to_hash = '{}thread-{}'.format(comment_txt, self.topic.pk)
self.assertEqual(
form.get_comment_hash(),
hashlib.md5(comment_txt_to_hash.encode('utf-8')).hexdigest())

def test_comment_get_comment_hash_from_field(self):
"""
Should return the comment hash from field
"""
comment_hash = '1' * 32
form_data = {'comment': 'foo', 'comment_hash': comment_hash}
form = CommentForm(data=form_data, topic=self.topic)
self.assertTrue(form.is_valid())
self.assertEqual(form.get_comment_hash(), comment_hash)

def test_comments_move(self):
comment = utils.create_comment(user=self.user, topic=self.topic)
comment2 = utils.create_comment(user=self.user, topic=self.topic)
Expand Down

0 comments on commit 6513fec

Please sign in to comment.