summaryrefslogtreecommitdiff
path: root/tests/regressiontests/comment_tests
diff options
context:
space:
mode:
authorJacob Kaplan-Moss <jacob@jacobian.org>2008-08-25 22:14:22 +0000
committerJacob Kaplan-Moss <jacob@jacobian.org>2008-08-25 22:14:22 +0000
commitcba91997a24f3cb154c7c51029c6dd91471f8800 (patch)
tree54481fbef77e08851f83a047cae2b9273feb4c19 /tests/regressiontests/comment_tests
parentb46e736c9ae2d16e4743e783d03954b1424bbf2b (diff)
Refactored Django's comment system.
Much of this work was done by Thejaswi Puthraya as part of Google's Summer of Code project; much thanks to him for the work, and to them for the program. This is a backwards-incompatible change; see the upgrading guide in docs/ref/contrib/comments/upgrade.txt for instructions if you were using the old comments system. git-svn-id: http://code.djangoproject.com/svn/django/trunk@8557 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests/regressiontests/comment_tests')
-rw-r--r--tests/regressiontests/comment_tests/__init__.py0
-rw-r--r--tests/regressiontests/comment_tests/fixtures/comment_tests.json43
-rw-r--r--tests/regressiontests/comment_tests/models.py22
-rw-r--r--tests/regressiontests/comment_tests/tests/__init__.py90
-rw-r--r--tests/regressiontests/comment_tests/tests/app_api_tests.py30
-rw-r--r--tests/regressiontests/comment_tests/tests/comment_form_tests.py81
-rw-r--r--tests/regressiontests/comment_tests/tests/comment_view_tests.py166
-rw-r--r--tests/regressiontests/comment_tests/tests/model_tests.py48
-rw-r--r--tests/regressiontests/comment_tests/tests/moderation_view_tests.py181
-rw-r--r--tests/regressiontests/comment_tests/tests/templatetag_tests.py65
10 files changed, 726 insertions, 0 deletions
diff --git a/tests/regressiontests/comment_tests/__init__.py b/tests/regressiontests/comment_tests/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/tests/regressiontests/comment_tests/__init__.py
diff --git a/tests/regressiontests/comment_tests/fixtures/comment_tests.json b/tests/regressiontests/comment_tests/fixtures/comment_tests.json
new file mode 100644
index 0000000000..b10326930e
--- /dev/null
+++ b/tests/regressiontests/comment_tests/fixtures/comment_tests.json
@@ -0,0 +1,43 @@
+[
+ {
+ "model" : "comment_tests.author",
+ "pk" : 1,
+ "fields" : {
+ "first_name" : "John",
+ "last_name" : "Smith"
+ }
+ },
+ {
+ "model" : "comment_tests.author",
+ "pk" : 2,
+ "fields" : {
+ "first_name" : "Peter",
+ "last_name" : "Jones"
+ }
+ },
+ {
+ "model" : "comment_tests.article",
+ "pk" : 1,
+ "fields" : {
+ "author" : 1,
+ "headline" : "Man Bites Dog"
+ }
+ },
+ {
+ "model" : "comment_tests.article",
+ "pk" : 2,
+ "fields" : {
+ "author" : 2,
+ "headline" : "Dog Bites Man"
+ }
+ },
+
+ {
+ "model" : "auth.user",
+ "pk" : 100,
+ "fields" : {
+ "username" : "normaluser",
+ "password" : "34ea4aaaf24efcbb4b30d27302f8657f"
+ }
+ }
+]
diff --git a/tests/regressiontests/comment_tests/models.py b/tests/regressiontests/comment_tests/models.py
new file mode 100644
index 0000000000..28022e2848
--- /dev/null
+++ b/tests/regressiontests/comment_tests/models.py
@@ -0,0 +1,22 @@
+"""
+Comments may be attached to any object. See the comment documentation for
+more information.
+"""
+
+from django.db import models
+from django.test import TestCase
+
+class Author(models.Model):
+ first_name = models.CharField(max_length=30)
+ last_name = models.CharField(max_length=30)
+
+ def __str__(self):
+ return '%s %s' % (self.first_name, self.last_name)
+
+class Article(models.Model):
+ author = models.ForeignKey(Author)
+ headline = models.CharField(max_length=100)
+
+ def __str__(self):
+ return self.headline
+
diff --git a/tests/regressiontests/comment_tests/tests/__init__.py b/tests/regressiontests/comment_tests/tests/__init__.py
new file mode 100644
index 0000000000..ac460d4d0b
--- /dev/null
+++ b/tests/regressiontests/comment_tests/tests/__init__.py
@@ -0,0 +1,90 @@
+from django.contrib.auth.models import User
+from django.contrib.comments.forms import CommentForm
+from django.contrib.comments.models import Comment
+from django.contrib.contenttypes.models import ContentType
+from django.contrib.sites.models import Site
+from django.test import TestCase
+from regressiontests.comment_tests.models import Article, Author
+
+# Shortcut
+CT = ContentType.objects.get_for_model
+
+# Helper base class for comment tests that need data.
+class CommentTestCase(TestCase):
+ fixtures = ["comment_tests"]
+
+ def setUp(self):
+ settings.ROOT_URLCONF = "django.contrib.comments.urls"
+
+ def createSomeComments(self):
+ # Two anonymous comments on two different objects
+ c1 = Comment.objects.create(
+ content_type = CT(Article),
+ object_pk = "1",
+ user_name = "Joe Somebody",
+ user_email = "jsomebody@example.com",
+ user_url = "http://example.com/~joe/",
+ comment = "First!",
+ site = Site.objects.get_current(),
+ )
+ c2 = Comment.objects.create(
+ content_type = CT(Author),
+ object_pk = "1",
+ user_name = "Joe Somebody",
+ user_email = "jsomebody@example.com",
+ user_url = "http://example.com/~joe/",
+ comment = "First here, too!",
+ site = Site.objects.get_current(),
+ )
+
+ # Two authenticated comments: one on the same Article, and
+ # one on a different Author
+ user = User.objects.create(
+ username = "frank_nobody",
+ first_name = "Frank",
+ last_name = "Nobody",
+ email = "fnobody@example.com",
+ password = "",
+ is_staff = False,
+ is_active = True,
+ is_superuser = False,
+ )
+ c3 = Comment.objects.create(
+ content_type = CT(Article),
+ object_pk = "1",
+ user = user,
+ user_url = "http://example.com/~frank/",
+ comment = "Damn, I wanted to be first.",
+ site = Site.objects.get_current(),
+ )
+ c4 = Comment.objects.create(
+ content_type = CT(Author),
+ object_pk = "2",
+ user = user,
+ user_url = "http://example.com/~frank/",
+ comment = "You get here first, too?",
+ site = Site.objects.get_current(),
+ )
+
+ return c1, c2, c3, c4
+
+ def getData(self):
+ return {
+ 'name' : 'Jim Bob',
+ 'email' : 'jim.bob@example.com',
+ 'url' : '',
+ 'comment' : 'This is my comment',
+ }
+
+ def getValidData(self, obj):
+ f = CommentForm(obj)
+ d = self.getData()
+ d.update(f.initial)
+ return d
+
+from regressiontests.comment_tests.tests.app_api_tests import *
+from regressiontests.comment_tests.tests.model_tests import *
+from regressiontests.comment_tests.tests.comment_form_tests import *
+from regressiontests.comment_tests.tests.templatetag_tests import *
+from regressiontests.comment_tests.tests.comment_view_tests import *
+from regressiontests.comment_tests.tests.moderation_view_tests import *
diff --git a/tests/regressiontests/comment_tests/tests/app_api_tests.py b/tests/regressiontests/comment_tests/tests/app_api_tests.py
new file mode 100644
index 0000000000..d4a4488ab5
--- /dev/null
+++ b/tests/regressiontests/comment_tests/tests/app_api_tests.py
@@ -0,0 +1,30 @@
+from django.conf import settings
+from django.contrib import comments
+from django.contrib.comments.models import Comment
+from django.contrib.comments.forms import CommentForm
+from regressiontests.comment_tests.tests import CommentTestCase
+
+class CommentAppAPITests(CommentTestCase):
+ """Tests for the "comment app" API"""
+
+ def testGetCommentApp(self):
+ self.assertEqual(comments.get_comment_app(), comments)
+
+ def testGetForm(self):
+ self.assertEqual(comments.get_form(), CommentForm)
+
+ def testGetFormTarget(self):
+ self.assertEqual(comments.get_form_target(), "/post/")
+
+ def testGetFlagURL(self):
+ c = Comment(id=12345)
+ self.assertEqual(comments.get_flag_url(c), "/flag/12345/")
+
+ def getGetDeleteURL(self):
+ c = Comment(id=12345)
+ self.assertEqual(comments.get_delete_url(c), "/delete/12345/")
+
+ def getGetApproveURL(self):
+ c = Comment(id=12345)
+ self.assertEqual(comments.get_approve_url(c), "/approve/12345/")
+
diff --git a/tests/regressiontests/comment_tests/tests/comment_form_tests.py b/tests/regressiontests/comment_tests/tests/comment_form_tests.py
new file mode 100644
index 0000000000..142931bfd6
--- /dev/null
+++ b/tests/regressiontests/comment_tests/tests/comment_form_tests.py
@@ -0,0 +1,81 @@
+import time
+from django.conf import settings
+from django.contrib.comments.models import Comment
+from django.contrib.comments.forms import CommentForm
+from regressiontests.comment_tests.models import Article
+from regressiontests.comment_tests.tests import CommentTestCase
+
+class CommentFormTests(CommentTestCase):
+
+ def testInit(self):
+ f = CommentForm(Article.objects.get(pk=1))
+ self.assertEqual(f.initial['content_type'], str(Article._meta))
+ self.assertEqual(f.initial['object_pk'], "1")
+ self.failIfEqual(f.initial['security_hash'], None)
+ self.failIfEqual(f.initial['timestamp'], None)
+
+ def testValidPost(self):
+ a = Article.objects.get(pk=1)
+ f = CommentForm(a, data=self.getValidData(a))
+ self.assert_(f.is_valid(), f.errors)
+ return f
+
+ def tamperWithForm(self, **kwargs):
+ a = Article.objects.get(pk=1)
+ d = self.getValidData(a)
+ d.update(kwargs)
+ f = CommentForm(Article.objects.get(pk=1), data=d)
+ self.failIf(f.is_valid())
+ return f
+
+ def testHoneypotTampering(self):
+ self.tamperWithForm(honeypot="I am a robot")
+
+ def testTimestampTampering(self):
+ self.tamperWithForm(timestamp=str(time.time() - 28800))
+
+ def testSecurityHashTampering(self):
+ self.tamperWithForm(security_hash="Nobody expects the Spanish Inquisition!")
+
+ def testContentTypeTampering(self):
+ self.tamperWithForm(content_type="auth.user")
+
+ def testObjectPKTampering(self):
+ self.tamperWithForm(object_pk="3")
+
+ def testSecurityErrors(self):
+ f = self.tamperWithForm(honeypot="I am a robot")
+ self.assert_("honeypot" in f.security_errors())
+
+ def testGetCommentObject(self):
+ f = self.testValidPost()
+ c = f.get_comment_object()
+ self.assert_(isinstance(c, Comment))
+ self.assertEqual(c.content_object, Article.objects.get(pk=1))
+ self.assertEqual(c.comment, "This is my comment")
+ c.save()
+ self.assertEqual(Comment.objects.count(), 1)
+
+ def testProfanities(self):
+ """Test COMMENTS_ALLOW_PROFANITIES and PROFANITIES_LIST settings"""
+ a = Article.objects.get(pk=1)
+ d = self.getValidData(a)
+
+ # Save settings in case other tests need 'em
+ saved = settings.PROFANITIES_LIST, settings.COMMENTS_ALLOW_PROFANITIES
+
+ # Don't wanna swear in the unit tests if we don't have to...
+ settings.PROFANITIES_LIST = ["rooster"]
+
+ # Try with COMMENTS_ALLOW_PROFANITIES off
+ settings.COMMENTS_ALLOW_PROFANITIES = False
+ f = CommentForm(a, data=dict(d, comment="What a rooster!"))
+ self.failIf(f.is_valid())
+
+ # Now with COMMENTS_ALLOW_PROFANITIES on
+ settings.COMMENTS_ALLOW_PROFANITIES = True
+ f = CommentForm(a, data=dict(d, comment="What a rooster!"))
+ self.failUnless(f.is_valid())
+
+ # Restore settings
+ settings.PROFANITIES_LIST, settings.COMMENTS_ALLOW_PROFANITIES = saved
diff --git a/tests/regressiontests/comment_tests/tests/comment_view_tests.py b/tests/regressiontests/comment_tests/tests/comment_view_tests.py
new file mode 100644
index 0000000000..87f5fd372b
--- /dev/null
+++ b/tests/regressiontests/comment_tests/tests/comment_view_tests.py
@@ -0,0 +1,166 @@
+from django.conf import settings
+from django.contrib.comments import signals
+from django.contrib.comments.models import Comment
+from regressiontests.comment_tests.models import Article
+from regressiontests.comment_tests.tests import CommentTestCase
+
+class CommentViewTests(CommentTestCase):
+
+ def testPostCommentHTTPMethods(self):
+ a = Article.objects.get(pk=1)
+ data = self.getValidData(a)
+ response = self.client.get("/post/", data)
+ self.assertEqual(response.status_code, 405)
+ self.assertEqual(response["Allow"], "POST")
+
+ def testPostCommentMissingCtype(self):
+ a = Article.objects.get(pk=1)
+ data = self.getValidData(a)
+ del data["content_type"]
+ response = self.client.post("/post/", data)
+ self.assertEqual(response.status_code, 400)
+
+ def testPostCommentBadCtype(self):
+ a = Article.objects.get(pk=1)
+ data = self.getValidData(a)
+ data["content_type"] = "Nobody expects the Spanish Inquisition!"
+ response = self.client.post("/post/", data)
+ self.assertEqual(response.status_code, 400)
+
+ def testPostCommentMissingObjectPK(self):
+ a = Article.objects.get(pk=1)
+ data = self.getValidData(a)
+ del data["object_pk"]
+ response = self.client.post("/post/", data)
+ self.assertEqual(response.status_code, 400)
+
+ def testPostCommentBadObjectPK(self):
+ a = Article.objects.get(pk=1)
+ data = self.getValidData(a)
+ data["object_pk"] = "14"
+ response = self.client.post("/post/", data)
+ self.assertEqual(response.status_code, 400)
+
+ def testCommentPreview(self):
+ a = Article.objects.get(pk=1)
+ data = self.getValidData(a)
+ data["submit"] = "preview"
+ response = self.client.post("/post/", data)
+ self.assertEqual(response.status_code, 200)
+ self.assertTemplateUsed(response, "comments/preview.html")
+
+ def testHashTampering(self):
+ a = Article.objects.get(pk=1)
+ data = self.getValidData(a)
+ data["security_hash"] = "Nobody expects the Spanish Inquisition!"
+ response = self.client.post("/post/", data)
+ self.assertEqual(response.status_code, 400)
+
+ def testDebugCommentErrors(self):
+ """The debug error template should be shown only if DEBUG is True"""
+ olddebug = settings.DEBUG
+
+ settings.DEBUG = True
+ a = Article.objects.get(pk=1)
+ data = self.getValidData(a)
+ data["security_hash"] = "Nobody expects the Spanish Inquisition!"
+ response = self.client.post("/post/", data)
+ self.assertEqual(response.status_code, 400)
+ self.assertTemplateUsed(response, "comments/400-debug.html")
+
+ settings.DEBUG = False
+ response = self.client.post("/post/", data)
+ self.assertEqual(response.status_code, 400)
+ self.assertTemplateNotUsed(response, "comments/400-debug.html")
+
+ settings.DEBUG = olddebug
+
+ def testCreateValidComment(self):
+ a = Article.objects.get(pk=1)
+ data = self.getValidData(a)
+ self.response = self.client.post("/post/", data, REMOTE_ADDR="1.2.3.4")
+ self.assertEqual(self.response.status_code, 302)
+ self.assertEqual(Comment.objects.count(), 1)
+ c = Comment.objects.all()[0]
+ self.assertEqual(c.ip_address, "1.2.3.4")
+ self.assertEqual(c.comment, "This is my comment")
+
+ def testPreventDuplicateComments(self):
+ """Prevent posting the exact same comment twice"""
+ a = Article.objects.get(pk=1)
+ data = self.getValidData(a)
+ self.client.post("/post/", data)
+ self.client.post("/post/", data)
+ self.assertEqual(Comment.objects.count(), 1)
+
+ # This should not trigger the duplicate prevention
+ self.client.post("/post/", dict(data, comment="My second comment."))
+ self.assertEqual(Comment.objects.count(), 2)
+
+ def testCommentSignals(self):
+ """Test signals emitted by the comment posting view"""
+
+ # callback
+ def receive(sender, **kwargs):
+ self.assertEqual(sender.comment, "This is my comment")
+ # TODO: Get the two commented tests below to work.
+# self.assertEqual(form_data["comment"], "This is my comment")
+# self.assertEqual(request.method, "POST")
+ received_signals.append(kwargs.get('signal'))
+
+ # Connect signals and keep track of handled ones
+ received_signals = []
+ excepted_signals = [signals.comment_will_be_posted, signals.comment_was_posted]
+ for signal in excepted_signals:
+ signal.connect(receive)
+
+ # Post a comment and check the signals
+ self.testCreateValidComment()
+ self.assertEqual(received_signals, excepted_signals)
+
+ def testWillBePostedSignal(self):
+ """
+ Test that the comment_will_be_posted signal can prevent the comment from
+ actually getting saved
+ """
+ def receive(sender, **kwargs): return False
+ signals.comment_will_be_posted.connect(receive)
+ a = Article.objects.get(pk=1)
+ data = self.getValidData(a)
+ response = self.client.post("/post/", data)
+ self.assertEqual(response.status_code, 400)
+ self.assertEqual(Comment.objects.count(), 0)
+
+ def testWillBePostedSignalModifyComment(self):
+ """
+ Test that the comment_will_be_posted signal can modify a comment before
+ it gets posted
+ """
+ def receive(sender, **kwargs):
+ sender.is_public = False # a bad but effective spam filter :)...
+
+ signals.comment_will_be_posted.connect(receive)
+ self.testCreateValidComment()
+ c = Comment.objects.all()[0]
+ self.failIf(c.is_public)
+
+ def testCommentNext(self):
+ """Test the different "next" actions the comment view can take"""
+ a = Article.objects.get(pk=1)
+ data = self.getValidData(a)
+ response = self.client.post("/post/", data)
+ self.assertEqual(response["Location"], "http://testserver/posted/?c=1")
+
+ data["next"] = "/somewhere/else/"
+ data["comment"] = "This is another comment"
+ response = self.client.post("/post/", data)
+ self.assertEqual(response["Location"], "http://testserver/somewhere/else/?c=2")
+
+ def testCommentDoneView(self):
+ a = Article.objects.get(pk=1)
+ data = self.getValidData(a)
+ response = self.client.post("/post/", data)
+ response = self.client.get("/posted/", {'c':1})
+ self.assertTemplateUsed(response, "comments/posted.html")
+ self.assertEqual(response.context[0]["comment"], Comment.objects.get(pk=1))
+
diff --git a/tests/regressiontests/comment_tests/tests/model_tests.py b/tests/regressiontests/comment_tests/tests/model_tests.py
new file mode 100644
index 0000000000..17797bb7a6
--- /dev/null
+++ b/tests/regressiontests/comment_tests/tests/model_tests.py
@@ -0,0 +1,48 @@
+from django.contrib.comments.models import Comment
+from regressiontests.comment_tests.models import Author, Article
+from regressiontests.comment_tests.tests import CommentTestCase
+
+class CommentModelTests(CommentTestCase):
+
+ def testSave(self):
+ for c in self.createSomeComments():
+ self.failIfEqual(c.submit_date, None)
+
+ def testUserProperties(self):
+ c1, c2, c3, c4 = self.createSomeComments()
+ self.assertEqual(c1.name, "Joe Somebody")
+ self.assertEqual(c2.email, "jsomebody@example.com")
+ self.assertEqual(c3.name, "Frank Nobody")
+ self.assertEqual(c3.url, "http://example.com/~frank/")
+ self.assertEqual(c1.user, None)
+ self.assertEqual(c3.user, c4.user)
+
+class CommentManagerTests(CommentTestCase):
+
+ def testInModeration(self):
+ """Comments that aren't public are considered in moderation"""
+ c1, c2, c3, c4 = self.createSomeComments()
+ c1.is_public = False
+ c2.is_public = False
+ c1.save()
+ c2.save()
+ moderated_comments = list(Comment.objects.in_moderation().order_by("id"))
+ self.assertEqual(moderated_comments, [c1, c2])
+
+ def testRemovedCommentsNotInModeration(self):
+ """Removed comments are not considered in moderation"""
+ c1, c2, c3, c4 = self.createSomeComments()
+ c1.is_public = False
+ c2.is_public = False
+ c2.is_removed = True
+ c1.save()
+ c2.save()
+ moderated_comments = list(Comment.objects.in_moderation())
+ self.assertEqual(moderated_comments, [c1])
+
+ def testForModel(self):
+ c1, c2, c3, c4 = self.createSomeComments()
+ article_comments = list(Comment.objects.for_model(Article).order_by("id"))
+ author_comments = list(Comment.objects.for_model(Author.objects.get(pk=1)))
+ self.assertEqual(article_comments, [c1, c3])
+ self.assertEqual(author_comments, [c2])
diff --git a/tests/regressiontests/comment_tests/tests/moderation_view_tests.py b/tests/regressiontests/comment_tests/tests/moderation_view_tests.py
new file mode 100644
index 0000000000..e5ebbb935f
--- /dev/null
+++ b/tests/regressiontests/comment_tests/tests/moderation_view_tests.py
@@ -0,0 +1,181 @@
+from django.contrib.comments.models import Comment, CommentFlag
+from django.contrib.auth.models import User, Permission
+from django.contrib.contenttypes.models import ContentType
+from regressiontests.comment_tests.tests import CommentTestCase
+from django.contrib.comments import signals
+
+class FlagViewTests(CommentTestCase):
+
+ def testFlagGet(self):
+ """GET the flag view: render a confirmation page."""
+ self.createSomeComments()
+ self.client.login(username="normaluser", password="normaluser")
+ response = self.client.get("/flag/1/")
+ self.assertTemplateUsed(response, "comments/flag.html")
+
+ def testFlagPost(self):
+ """POST the flag view: actually flag the view (nice for XHR)"""
+ self.createSomeComments()
+ self.client.login(username="normaluser", password="normaluser")
+ response = self.client.post("/flag/1/")
+ self.assertEqual(response["Location"], "http://testserver/flagged/?c=1")
+ c = Comment.objects.get(pk=1)
+ self.assertEqual(c.flags.filter(flag=CommentFlag.SUGGEST_REMOVAL).count(), 1)
+ return c
+
+ def testFlagPostTwice(self):
+ """Users don't get to flag comments more than once."""
+ c = self.testFlagPost()
+ self.client.post("/flag/1/")
+ self.client.post("/flag/1/")
+ self.assertEqual(c.flags.filter(flag=CommentFlag.SUGGEST_REMOVAL).count(), 1)
+
+ def testFlagAnon(self):
+ """GET/POST the flag view while not logged in: redirect to log in."""
+ self.createSomeComments()
+ response = self.client.get("/flag/1/")
+ self.assertEqual(response["Location"], "http://testserver/accounts/login/?next=/flag/1/")
+ response = self.client.post("/flag/1/")
+ self.assertEqual(response["Location"], "http://testserver/accounts/login/?next=/flag/1/")
+
+ def testFlaggedView(self):
+ self.createSomeComments()
+ response = self.client.get("/flagged/", data={"c":1})
+ self.assertTemplateUsed(response, "comments/flagged.html")
+
+ def testFlagSignals(self):
+ """Test signals emitted by the comment flag view"""
+
+ # callback
+ def receive(sender, **kwargs):
+ flag = sender.flags.get(id=1)
+ self.assertEqual(flag.flag, CommentFlag.SUGGEST_REMOVAL)
+ self.assertEqual(flag.user.username, "normaluser")
+ received_signals.append(kwargs.get('signal'))
+
+ # Connect signals and keep track of handled ones
+ received_signals = []
+ signals.comment_was_flagged.connect(receive)
+
+ # Post a comment and check the signals
+ self.testFlagPost()
+ self.assertEqual(received_signals, [signals.comment_was_flagged])
+
+def makeModerator(username):
+ u = User.objects.get(username=username)
+ ct = ContentType.objects.get_for_model(Comment)
+ p = Permission.objects.get(content_type=ct, codename="can_moderate")
+ u.user_permissions.add(p)
+
+class DeleteViewTests(CommentTestCase):
+
+ def testDeletePermissions(self):
+ """The delete view should only be accessible to 'moderators'"""
+ self.createSomeComments()
+ self.client.login(username="normaluser", password="normaluser")
+ response = self.client.get("/delete/1/")
+ self.assertEqual(response["Location"], "http://testserver/accounts/login/?next=/delete/1/")
+
+ makeModerator("normaluser")
+ response = self.client.get("/delete/1/")
+ self.assertEqual(response.status_code, 200)
+
+ def testDeletePost(self):
+ """POSTing the delete view should mark the comment as removed"""
+ self.createSomeComments()
+ makeModerator("normaluser")
+ self.client.login(username="normaluser", password="normaluser")
+ response = self.client.post("/delete/1/")
+ self.assertEqual(response["Location"], "http://testserver/deleted/?c=1")
+ c = Comment.objects.get(pk=1)
+ self.failUnless(c.is_removed)
+ self.assertEqual(c.flags.filter(flag=CommentFlag.MODERATOR_DELETION, user__username="normaluser").count(), 1)
+
+ def testDeleteSignals(self):
+ def receive(sender, **kwargs):
+ received_signals.append(kwargs.get('signal'))
+
+ # Connect signals and keep track of handled ones
+ received_signals = []
+ signals.comment_was_flagged.connect(receive)
+
+ # Post a comment and check the signals
+ self.testDeletePost()
+ self.assertEqual(received_signals, [signals.comment_was_flagged])
+
+ def testDeletedView(self):
+ self.createSomeComments()
+ response = self.client.get("/deleted/", data={"c":1})
+ self.assertTemplateUsed(response, "comments/deleted.html")
+
+class ApproveViewTests(CommentTestCase):
+
+ def testApprovePermissions(self):
+ """The delete view should only be accessible to 'moderators'"""
+ self.createSomeComments()
+ self.client.login(username="normaluser", password="normaluser")
+ response = self.client.get("/approve/1/")
+ self.assertEqual(response["Location"], "http://testserver/accounts/login/?next=/approve/1/")
+
+ makeModerator("normaluser")
+ response = self.client.get("/approve/1/")
+ self.assertEqual(response.status_code, 200)
+
+ def testApprovePost(self):
+ """POSTing the delete view should mark the comment as removed"""
+ c1, c2, c3, c4 = self.createSomeComments()
+ c1.is_public = False; c1.save()
+
+ makeModerator("normaluser")
+ self.client.login(username="normaluser", password="normaluser")
+ response = self.client.post("/approve/1/")
+ self.assertEqual(response["Location"], "http://testserver/approved/?c=1")
+ c = Comment.objects.get(pk=1)
+ self.failUnless(c.is_public)
+ self.assertEqual(c.flags.filter(flag=CommentFlag.MODERATOR_APPROVAL, user__username="normaluser").count(), 1)
+
+ def testApproveSignals(self):
+ def receive(sender, **kwargs):
+ received_signals.append(kwargs.get('signal'))
+
+ # Connect signals and keep track of handled ones
+ received_signals = []
+ signals.comment_was_flagged.connect(receive)
+
+ # Post a comment and check the signals
+ self.testApprovePost()
+ self.assertEqual(received_signals, [signals.comment_was_flagged])
+
+ def testApprovedView(self):
+ self.createSomeComments()
+ response = self.client.get("/approved/", data={"c":1})
+ self.assertTemplateUsed(response, "comments/approved.html")
+
+
+class ModerationQueueTests(CommentTestCase):
+
+ def testModerationQueuePermissions(self):
+ """Only moderators can view the moderation queue"""
+ self.client.login(username="normaluser", password="normaluser")
+ response = self.client.get("/moderate/")
+ self.assertEqual(response["Location"], "http://testserver/accounts/login/?next=/moderate/")
+
+ makeModerator("normaluser")
+ response = self.client.get("/moderate/")
+ self.assertEqual(response.status_code, 200)
+
+ def testModerationQueueContents(self):
+ """Moderation queue should display non-public, non-removed comments."""
+ c1, c2, c3, c4 = self.createSomeComments()
+ makeModerator("normaluser")
+ self.client.login(username="normaluser", password="normaluser")
+
+ c1.is_public = c2.is_public = False
+ c1.save(); c2.save()
+ response = self.client.get("/moderate/")
+ self.assertEqual(list(response.context[0]["comments"]), [c1, c2])
+
+ c2.is_removed = True
+ c2.save()
+ response = self.client.get("/moderate/")
+ self.assertEqual(list(response.context[0]["comments"]), [c1])
diff --git a/tests/regressiontests/comment_tests/tests/templatetag_tests.py b/tests/regressiontests/comment_tests/tests/templatetag_tests.py
new file mode 100644
index 0000000000..a1187ca732
--- /dev/null
+++ b/tests/regressiontests/comment_tests/tests/templatetag_tests.py
@@ -0,0 +1,65 @@
+from django.contrib.comments.forms import CommentForm
+from django.contrib.comments.models import Comment
+from django.template import Template, Context
+from regressiontests.comment_tests.models import Article, Author
+from regressiontests.comment_tests.tests import CommentTestCase
+
+class CommentTemplateTagTests(CommentTestCase):
+
+ def render(self, t, **c):
+ ctx = Context(c)
+ out = Template(t).render(ctx)
+ return ctx, out
+
+ def testCommentFormTarget(self):
+ ctx, out = self.render("{% load comments %}{% comment_form_target %}")
+ self.assertEqual(out, "/post/")
+
+ def testGetCommentForm(self, tag=None):
+ t = "{% load comments %}" + (tag or "{% get_comment_form for comment_tests.article a.id as form %}")
+ ctx, out = self.render(t, a=Article.objects.get(pk=1))
+ self.assertEqual(out, "")
+ self.assert_(isinstance(ctx["form"], CommentForm))
+
+ def testGetCommentFormFromLiteral(self):
+ self.testGetCommentForm("{% get_comment_form for comment_tests.article 1 as form %}")
+
+ def testGetCommentFormFromObject(self):
+ self.testGetCommentForm("{% get_comment_form for a as form %}")
+
+ def testRenderCommentForm(self, tag=None):
+ t = "{% load comments %}" + (tag or "{% render_comment_form for comment_tests.article a.id %}")
+ ctx, out = self.render(t, a=Article.objects.get(pk=1))
+ self.assert_(out.strip().startswith("<form action="))
+ self.assert_(out.strip().endswith("</form>"))
+
+ def testRenderCommentFormFromLiteral(self):
+ self.testRenderCommentForm("{% render_comment_form for comment_tests.article 1 %}")
+
+ def testRenderCommentFormFromObject(self):
+ self.testRenderCommentForm("{% render_comment_form for a %}")
+
+ def testGetCommentCount(self, tag=None):
+ self.createSomeComments()
+ t = "{% load comments %}" + (tag or "{% get_comment_count for comment_tests.article a.id as cc %}") + "{{ cc }}"
+ ctx, out = self.render(t, a=Article.objects.get(pk=1))
+ self.assertEqual(out, "2")
+
+ def testGetCommentCountFromLiteral(self):
+ self.testGetCommentCount("{% get_comment_count for comment_tests.article 1 as cc %}")
+
+ def testGetCommentCountFromObject(self):
+ self.testGetCommentCount("{% get_comment_count for a as cc %}")
+
+ def testGetCommentList(self, tag=None):
+ c1, c2, c3, c4 = self.createSomeComments()
+ t = "{% load comments %}" + (tag or "{% get_comment_list for comment_tests.author a.id as cl %}")
+ ctx, out = self.render(t, a=Author.objects.get(pk=1))
+ self.assertEqual(out, "")
+ self.assertEqual(list(ctx["cl"]), [c2])
+
+ def testGetCommentListFromLiteral(self):
+ self.testGetCommentList("{% get_comment_list for comment_tests.author 1 as cl %}")
+
+ def testGetCommentListFromObject(self):
+ self.testGetCommentList("{% get_comment_list for a as cl %}")