From 5e84abec14fe43cc08198411cbd2b67804ab7e47 Mon Sep 17 00:00:00 2001 From: Preston Timmons Date: Sat, 6 Apr 2013 13:29:35 -0500 Subject: Modified comment_tests for unittest2 discovery. --- tests/comment_tests/tests/__init__.py | 16 +- tests/comment_tests/tests/app_api_tests.py | 80 ----- tests/comment_tests/tests/comment_form_tests.py | 85 ------ .../tests/comment_utils_moderators_tests.py | 99 ------- tests/comment_tests/tests/comment_view_tests.py | 324 --------------------- tests/comment_tests/tests/feed_tests.py | 53 ---- tests/comment_tests/tests/model_tests.py | 58 ---- tests/comment_tests/tests/moderation_view_tests.py | 315 -------------------- tests/comment_tests/tests/templatetag_tests.py | 205 ------------- tests/comment_tests/tests/test_app_api.py | 80 +++++ tests/comment_tests/tests/test_comment_form.py | 85 ++++++ .../tests/test_comment_utils_moderators.py | 99 +++++++ tests/comment_tests/tests/test_comment_view.py | 324 +++++++++++++++++++++ tests/comment_tests/tests/test_feeds.py | 53 ++++ tests/comment_tests/tests/test_models.py | 58 ++++ tests/comment_tests/tests/test_moderation_views.py | 315 ++++++++++++++++++++ tests/comment_tests/tests/test_templatetags.py | 205 +++++++++++++ 17 files changed, 1227 insertions(+), 1227 deletions(-) delete mode 100644 tests/comment_tests/tests/app_api_tests.py delete mode 100644 tests/comment_tests/tests/comment_form_tests.py delete mode 100644 tests/comment_tests/tests/comment_utils_moderators_tests.py delete mode 100644 tests/comment_tests/tests/comment_view_tests.py delete mode 100644 tests/comment_tests/tests/feed_tests.py delete mode 100644 tests/comment_tests/tests/model_tests.py delete mode 100644 tests/comment_tests/tests/moderation_view_tests.py delete mode 100644 tests/comment_tests/tests/templatetag_tests.py create mode 100644 tests/comment_tests/tests/test_app_api.py create mode 100644 tests/comment_tests/tests/test_comment_form.py create mode 100644 tests/comment_tests/tests/test_comment_utils_moderators.py create mode 100644 tests/comment_tests/tests/test_comment_view.py create mode 100644 tests/comment_tests/tests/test_feeds.py create mode 100644 tests/comment_tests/tests/test_models.py create mode 100644 tests/comment_tests/tests/test_moderation_views.py create mode 100644 tests/comment_tests/tests/test_templatetags.py diff --git a/tests/comment_tests/tests/__init__.py b/tests/comment_tests/tests/__init__.py index d959dab243..ae4585e187 100644 --- a/tests/comment_tests/tests/__init__.py +++ b/tests/comment_tests/tests/__init__.py @@ -85,11 +85,11 @@ class CommentTestCase(TestCase): d.update(f.initial) return d -from comment_tests.tests.app_api_tests import * -from comment_tests.tests.feed_tests import * -from comment_tests.tests.model_tests import * -from comment_tests.tests.comment_form_tests import * -from comment_tests.tests.templatetag_tests import * -from comment_tests.tests.comment_view_tests import * -from comment_tests.tests.moderation_view_tests import * -from comment_tests.tests.comment_utils_moderators_tests import * +from comment_tests.tests.test_app_api import * +from comment_tests.tests.test_feeds import * +from comment_tests.tests.test_models import * +from comment_tests.tests.test_comment_form import * +from comment_tests.tests.test_templatetags import * +from comment_tests.tests.test_comment_view import * +from comment_tests.tests.test_comment_utils_moderators import * +from comment_tests.tests.test_moderation_views import * diff --git a/tests/comment_tests/tests/app_api_tests.py b/tests/comment_tests/tests/app_api_tests.py deleted file mode 100644 index ed23ba39cc..0000000000 --- a/tests/comment_tests/tests/app_api_tests.py +++ /dev/null @@ -1,80 +0,0 @@ -from __future__ import absolute_import - -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 django.core.exceptions import ImproperlyConfigured -from django.test.utils import override_settings -from django.utils import six - -from . import CommentTestCase - - -class CommentAppAPITests(CommentTestCase): - """Tests for the "comment app" API""" - - def testGetCommentApp(self): - self.assertEqual(comments.get_comment_app(), comments) - - @override_settings( - COMMENTS_APP='missing_app', - INSTALLED_APPS=list(settings.INSTALLED_APPS) + ['missing_app'], - ) - def testGetMissingCommentApp(self): - with six.assertRaisesRegex(self, ImproperlyConfigured, 'missing_app'): - _ = comments.get_comment_app() - - 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/") - - -@override_settings( - COMMENTS_APP='comment_tests.custom_comments', - INSTALLED_APPS=list(settings.INSTALLED_APPS) + [ - 'comment_tests.custom_comments'], -) -class CustomCommentTest(CommentTestCase): - urls = 'comment_tests.urls' - - def testGetCommentApp(self): - from comment_tests import custom_comments - self.assertEqual(comments.get_comment_app(), custom_comments) - - def testGetModel(self): - from comment_tests.custom_comments.models import CustomComment - self.assertEqual(comments.get_model(), CustomComment) - - def testGetForm(self): - from comment_tests.custom_comments.forms import CustomCommentForm - self.assertEqual(comments.get_form(), CustomCommentForm) - - 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/comment_tests/tests/comment_form_tests.py b/tests/comment_tests/tests/comment_form_tests.py deleted file mode 100644 index 39ba57928d..0000000000 --- a/tests/comment_tests/tests/comment_form_tests.py +++ /dev/null @@ -1,85 +0,0 @@ -from __future__ import absolute_import - -import time - -from django.conf import settings -from django.contrib.comments.forms import CommentForm -from django.contrib.comments.models import Comment - -from . import CommentTestCase -from ..models import Article - - -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.assertNotEqual(f.initial['security_hash'], None) - self.assertNotEqual(f.initial['timestamp'], None) - - def testValidPost(self): - a = Article.objects.get(pk=1) - f = CommentForm(a, data=self.getValidData(a)) - self.assertTrue(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.assertFalse(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.assertTrue("honeypot" in f.security_errors()) - - def testGetCommentObject(self): - f = self.testValidPost() - c = f.get_comment_object() - self.assertTrue(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.assertFalse(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.assertTrue(f.is_valid()) - - # Restore settings - settings.PROFANITIES_LIST, settings.COMMENTS_ALLOW_PROFANITIES = saved diff --git a/tests/comment_tests/tests/comment_utils_moderators_tests.py b/tests/comment_tests/tests/comment_utils_moderators_tests.py deleted file mode 100644 index 6c7a882e25..0000000000 --- a/tests/comment_tests/tests/comment_utils_moderators_tests.py +++ /dev/null @@ -1,99 +0,0 @@ -from __future__ import absolute_import - -from django.contrib.comments.models import Comment -from django.contrib.comments.moderation import (moderator, CommentModerator, - AlreadyModerated) -from django.core import mail -from django.test.utils import override_settings - -from . import CommentTestCase -from ..models import Entry - - -class EntryModerator1(CommentModerator): - email_notification = True - -class EntryModerator2(CommentModerator): - enable_field = 'enable_comments' - -class EntryModerator3(CommentModerator): - auto_close_field = 'pub_date' - close_after = 7 - -class EntryModerator4(CommentModerator): - auto_moderate_field = 'pub_date' - moderate_after = 7 - -class EntryModerator5(CommentModerator): - auto_moderate_field = 'pub_date' - moderate_after = 0 - -class EntryModerator6(CommentModerator): - auto_close_field = 'pub_date' - close_after = 0 - -class CommentUtilsModeratorTests(CommentTestCase): - fixtures = ["comment_utils.xml"] - - def createSomeComments(self): - # Tests for the moderation signals must actually post data - # through the comment views, because only the comment views - # emit the custom signals moderation listens for. - e = Entry.objects.get(pk=1) - data = self.getValidData(e) - - self.client.post("/post/", data, REMOTE_ADDR="1.2.3.4") - - # We explicitly do a try/except to get the comment we've just - # posted because moderation may have disallowed it, in which - # case we can just return it as None. - try: - c1 = Comment.objects.all()[0] - except IndexError: - c1 = None - - self.client.post("/post/", data, REMOTE_ADDR="1.2.3.4") - - try: - c2 = Comment.objects.all()[0] - except IndexError: - c2 = None - return c1, c2 - - def tearDown(self): - moderator.unregister(Entry) - - def testRegisterExistingModel(self): - moderator.register(Entry, EntryModerator1) - self.assertRaises(AlreadyModerated, moderator.register, Entry, EntryModerator1) - - def testEmailNotification(self): - with override_settings(MANAGERS=("test@example.com",)): - moderator.register(Entry, EntryModerator1) - self.createSomeComments() - self.assertEqual(len(mail.outbox), 2) - - def testCommentsEnabled(self): - moderator.register(Entry, EntryModerator2) - self.createSomeComments() - self.assertEqual(Comment.objects.all().count(), 1) - - def testAutoCloseField(self): - moderator.register(Entry, EntryModerator3) - self.createSomeComments() - self.assertEqual(Comment.objects.all().count(), 0) - - def testAutoModerateField(self): - moderator.register(Entry, EntryModerator4) - c1, c2 = self.createSomeComments() - self.assertEqual(c2.is_public, False) - - def testAutoModerateFieldImmediate(self): - moderator.register(Entry, EntryModerator5) - c1, c2 = self.createSomeComments() - self.assertEqual(c2.is_public, False) - - def testAutoCloseFieldImmediate(self): - moderator.register(Entry, EntryModerator6) - c1, c2 = self.createSomeComments() - self.assertEqual(Comment.objects.all().count(), 0) diff --git a/tests/comment_tests/tests/comment_view_tests.py b/tests/comment_tests/tests/comment_view_tests.py deleted file mode 100644 index 0d994d3af8..0000000000 --- a/tests/comment_tests/tests/comment_view_tests.py +++ /dev/null @@ -1,324 +0,0 @@ -from __future__ import absolute_import, unicode_literals - -import re - -from django.conf import settings -from django.contrib.auth.models import User -from django.contrib.comments import signals -from django.contrib.comments.models import Comment - -from . import CommentTestCase -from ..models import Article, Book - - -post_redirect_re = re.compile(r'^http://testserver/posted/\?c=(?P\d+$)') - -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 testPostInvalidIntegerPK(self): - a = Article.objects.get(pk=1) - data = self.getValidData(a) - data["comment"] = "This is another comment" - data["object_pk"] = '\ufffd' - response = self.client.post("/post/", data) - self.assertEqual(response.status_code, 400) - - def testPostInvalidDecimalPK(self): - b = Book.objects.get(pk='12.34') - data = self.getValidData(b) - data["comment"] = "This is another comment" - data["object_pk"] = 'cookies' - 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["preview"] = "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): - address = "1.2.3.4" - a = Article.objects.get(pk=1) - data = self.getValidData(a) - self.response = self.client.post("/post/", data, REMOTE_ADDR=address) - self.assertEqual(self.response.status_code, 302) - self.assertEqual(Comment.objects.count(), 1) - c = Comment.objects.all()[0] - self.assertEqual(c.ip_address, address) - self.assertEqual(c.comment, "This is my comment") - - def testCreateValidCommentIPv6(self): - """ - Test creating a valid comment with a long IPv6 address. - Note that this test should fail when Comment.ip_address is an IPAddress instead of a GenericIPAddress, - but does not do so on SQLite or PostgreSQL, because they use the TEXT and INET types, which already - allow storing an IPv6 address internally. - """ - address = "2a02::223:6cff:fe8a:2e8a" - a = Article.objects.get(pk=1) - data = self.getValidData(a) - self.response = self.client.post("/post/", data, REMOTE_ADDR=address) - self.assertEqual(self.response.status_code, 302) - self.assertEqual(Comment.objects.count(), 1) - c = Comment.objects.all()[0] - self.assertEqual(c.ip_address, address) - self.assertEqual(c.comment, "This is my comment") - - def testCreateValidCommentIPv6Unpack(self): - address = "::ffff:18.52.18.52" - a = Article.objects.get(pk=1) - data = self.getValidData(a) - self.response = self.client.post("/post/", data, REMOTE_ADDR=address) - self.assertEqual(self.response.status_code, 302) - self.assertEqual(Comment.objects.count(), 1) - c = Comment.objects.all()[0] - # We trim the '::ffff:' bit off because it is an IPv4 addr - self.assertEqual(c.ip_address, address[7:]) - self.assertEqual(c.comment, "This is my comment") - - def testPostAsAuthenticatedUser(self): - a = Article.objects.get(pk=1) - data = self.getValidData(a) - data['name'] = data['email'] = '' - self.client.login(username="normaluser", password="normaluser") - 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") - u = User.objects.get(username='normaluser') - self.assertEqual(c.user, u) - self.assertEqual(c.user_name, u.get_full_name()) - self.assertEqual(c.user_email, u.email) - - def testPostAsAuthenticatedUserWithoutFullname(self): - """ - Check that the user's name in the comment is populated for - authenticated users without first_name and last_name. - """ - user = User.objects.create_user(username='jane_other', - email='jane@example.com', password='jane_other') - a = Article.objects.get(pk=1) - data = self.getValidData(a) - data['name'] = data['email'] = '' - self.client.login(username="jane_other", password="jane_other") - self.response = self.client.post("/post/", data, REMOTE_ADDR="1.2.3.4") - c = Comment.objects.get(user=user) - self.assertEqual(c.ip_address, "1.2.3.4") - self.assertEqual(c.user_name, 'jane_other') - user.delete() - - 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(kwargs['comment'].comment, "This is my comment") - self.assertTrue('request' in kwargs) - received_signals.append(kwargs.get('signal')) - - # Connect signals and keep track of handled ones - received_signals = [] - expected_signals = [ - signals.comment_will_be_posted, signals.comment_was_posted - ] - for signal in expected_signals: - signal.connect(receive) - - # Post a comment and check the signals - self.testCreateValidComment() - self.assertEqual(received_signals, expected_signals) - - for signal in expected_signals: - signal.disconnect(receive) - - 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, dispatch_uid="comment-test") - 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) - signals.comment_will_be_posted.disconnect(dispatch_uid="comment-test") - - def testWillBePostedSignalModifyComment(self): - """ - Test that the comment_will_be_posted signal can modify a comment before - it gets posted - """ - def receive(sender, **kwargs): - # a bad but effective spam filter :)... - kwargs['comment'].is_public = False - - signals.comment_will_be_posted.connect(receive) - self.testCreateValidComment() - c = Comment.objects.all()[0] - self.assertFalse(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) - location = response["Location"] - match = post_redirect_re.match(location) - self.assertTrue(match != None, "Unexpected redirect location: %s" % location) - - data["next"] = "/somewhere/else/" - data["comment"] = "This is another comment" - response = self.client.post("/post/", data) - location = response["Location"] - match = re.search(r"^http://testserver/somewhere/else/\?c=\d+$", location) - self.assertTrue(match != None, "Unexpected redirect location: %s" % location) - - data["next"] = "http://badserver/somewhere/else/" - data["comment"] = "This is another comment with an unsafe next url" - response = self.client.post("/post/", data) - location = response["Location"] - match = post_redirect_re.match(location) - self.assertTrue(match != None, "Unsafe redirection to: %s" % location) - - def testCommentDoneView(self): - a = Article.objects.get(pk=1) - data = self.getValidData(a) - response = self.client.post("/post/", data) - location = response["Location"] - match = post_redirect_re.match(location) - self.assertTrue(match != None, "Unexpected redirect location: %s" % location) - pk = int(match.group('pk')) - response = self.client.get(location) - self.assertTemplateUsed(response, "comments/posted.html") - self.assertEqual(response.context[0]["comment"], Comment.objects.get(pk=pk)) - - def testCommentNextWithQueryString(self): - """ - The `next` key needs to handle already having a query string (#10585) - """ - a = Article.objects.get(pk=1) - data = self.getValidData(a) - data["next"] = "/somewhere/else/?foo=bar" - data["comment"] = "This is another comment" - response = self.client.post("/post/", data) - location = response["Location"] - match = re.search(r"^http://testserver/somewhere/else/\?foo=bar&c=\d+$", location) - self.assertTrue(match != None, "Unexpected redirect location: %s" % location) - - def testCommentPostRedirectWithInvalidIntegerPK(self): - """ - Tests that attempting to retrieve the location specified in the - post redirect, after adding some invalid data to the expected - querystring it ends with, doesn't cause a server error. - """ - a = Article.objects.get(pk=1) - data = self.getValidData(a) - data["comment"] = "This is another comment" - response = self.client.post("/post/", data) - location = response["Location"] - broken_location = location + "\ufffd" - response = self.client.get(broken_location) - self.assertEqual(response.status_code, 200) - - def testCommentNextWithQueryStringAndAnchor(self): - """ - The `next` key needs to handle already having an anchor. Refs #13411. - """ - # With a query string also. - a = Article.objects.get(pk=1) - data = self.getValidData(a) - data["next"] = "/somewhere/else/?foo=bar#baz" - data["comment"] = "This is another comment" - response = self.client.post("/post/", data) - location = response["Location"] - match = re.search(r"^http://testserver/somewhere/else/\?foo=bar&c=\d+#baz$", location) - self.assertTrue(match != None, "Unexpected redirect location: %s" % location) - - # Without a query string - a = Article.objects.get(pk=1) - data = self.getValidData(a) - data["next"] = "/somewhere/else/#baz" - data["comment"] = "This is another comment" - response = self.client.post("/post/", data) - location = response["Location"] - match = re.search(r"^http://testserver/somewhere/else/\?c=\d+#baz$", location) - self.assertTrue(match != None, "Unexpected redirect location: %s" % location) diff --git a/tests/comment_tests/tests/feed_tests.py b/tests/comment_tests/tests/feed_tests.py deleted file mode 100644 index 941ffb6bf2..0000000000 --- a/tests/comment_tests/tests/feed_tests.py +++ /dev/null @@ -1,53 +0,0 @@ -from __future__ import absolute_import - -from xml.etree import ElementTree as ET - -from django.conf import settings -from django.contrib.comments.models import Comment -from django.contrib.contenttypes.models import ContentType -from django.contrib.sites.models import Site - -from . import CommentTestCase -from ..models import Article - - -class CommentFeedTests(CommentTestCase): - urls = 'comment_tests.urls' - feed_url = '/rss/comments/' - - def setUp(self): - site_2 = Site.objects.create(id=settings.SITE_ID+1, - domain="example2.com", name="example2.com") - # A comment for another site - c5 = Comment.objects.create( - content_type = ContentType.objects.get_for_model(Article), - object_pk = "1", - user_name = "Joe Somebody", - user_email = "jsomebody@example.com", - user_url = "http://example.com/~joe/", - comment = "A comment for the second site.", - site = site_2, - ) - - def test_feed(self): - response = self.client.get(self.feed_url) - self.assertEqual(response.status_code, 200) - self.assertEqual(response['Content-Type'], 'application/rss+xml; charset=utf-8') - - rss_elem = ET.fromstring(response.content) - - self.assertEqual(rss_elem.tag, "rss") - self.assertEqual(rss_elem.attrib, {"version": "2.0"}) - - channel_elem = rss_elem.find("channel") - - title_elem = channel_elem.find("title") - self.assertEqual(title_elem.text, "example.com comments") - - link_elem = channel_elem.find("link") - self.assertEqual(link_elem.text, "http://example.com/") - - atomlink_elem = channel_elem.find("{http://www.w3.org/2005/Atom}link") - self.assertEqual(atomlink_elem.attrib, {"href": "http://example.com/rss/comments/", "rel": "self"}) - - self.assertNotContains(response, "A comment for the second site.") diff --git a/tests/comment_tests/tests/model_tests.py b/tests/comment_tests/tests/model_tests.py deleted file mode 100644 index 69c1a8118f..0000000000 --- a/tests/comment_tests/tests/model_tests.py +++ /dev/null @@ -1,58 +0,0 @@ -from __future__ import absolute_import - -from django.contrib.comments.models import Comment - -from . import CommentTestCase -from ..models import Author, Article - - -class CommentModelTests(CommentTestCase): - def testSave(self): - for c in self.createSomeComments(): - self.assertNotEqual(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]) - - def testPrefetchRelated(self): - c1, c2, c3, c4 = self.createSomeComments() - # one for comments, one for Articles, one for Author - with self.assertNumQueries(3): - qs = Comment.objects.prefetch_related('content_object') - [c.content_object for c in qs] diff --git a/tests/comment_tests/tests/moderation_view_tests.py b/tests/comment_tests/tests/moderation_view_tests.py deleted file mode 100644 index 02af35cfe4..0000000000 --- a/tests/comment_tests/tests/moderation_view_tests.py +++ /dev/null @@ -1,315 +0,0 @@ -from __future__ import absolute_import, unicode_literals - -from django.contrib.auth.models import User, Permission -from django.contrib.comments import signals -from django.contrib.comments.models import Comment, CommentFlag -from django.contrib.contenttypes.models import ContentType -from django.utils import translation - -from . import CommentTestCase - - -class FlagViewTests(CommentTestCase): - - def testFlagGet(self): - """GET the flag view: render a confirmation page.""" - comments = self.createSomeComments() - pk = comments[0].pk - self.client.login(username="normaluser", password="normaluser") - response = self.client.get("/flag/%d/" % pk) - self.assertTemplateUsed(response, "comments/flag.html") - - def testFlagPost(self): - """POST the flag view: actually flag the view (nice for XHR)""" - comments = self.createSomeComments() - pk = comments[0].pk - self.client.login(username="normaluser", password="normaluser") - response = self.client.post("/flag/%d/" % pk) - self.assertEqual(response["Location"], "http://testserver/flagged/?c=%d" % pk) - c = Comment.objects.get(pk=pk) - self.assertEqual(c.flags.filter(flag=CommentFlag.SUGGEST_REMOVAL).count(), 1) - return c - - def testFlagPostNext(self): - """ - POST the flag view, explicitly providing a next url. - """ - comments = self.createSomeComments() - pk = comments[0].pk - self.client.login(username="normaluser", password="normaluser") - response = self.client.post("/flag/%d/" % pk, {'next': "/go/here/"}) - self.assertEqual(response["Location"], - "http://testserver/go/here/?c=%d" % pk) - - def testFlagPostUnsafeNext(self): - """ - POSTing to the flag view with an unsafe next url will ignore the - provided url when redirecting. - """ - comments = self.createSomeComments() - pk = comments[0].pk - self.client.login(username="normaluser", password="normaluser") - response = self.client.post("/flag/%d/" % pk, - {'next': "http://elsewhere/bad"}) - self.assertEqual(response["Location"], - "http://testserver/flagged/?c=%d" % pk) - - def testFlagPostTwice(self): - """Users don't get to flag comments more than once.""" - c = self.testFlagPost() - self.client.post("/flag/%d/" % c.pk) - self.client.post("/flag/%d/" % c.pk) - 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.""" - comments = self.createSomeComments() - pk = comments[0].pk - response = self.client.get("/flag/%d/" % pk) - self.assertEqual(response["Location"], "http://testserver/accounts/login/?next=/flag/%d/" % pk) - response = self.client.post("/flag/%d/" % pk) - self.assertEqual(response["Location"], "http://testserver/accounts/login/?next=/flag/%d/" % pk) - - def testFlaggedView(self): - comments = self.createSomeComments() - pk = comments[0].pk - response = self.client.get("/flagged/", data={"c": pk}) - self.assertTemplateUsed(response, "comments/flagged.html") - - def testFlagSignals(self): - """Test signals emitted by the comment flag view""" - - # callback - def receive(sender, **kwargs): - self.assertEqual(kwargs['flag'].flag, CommentFlag.SUGGEST_REMOVAL) - self.assertEqual(kwargs['request'].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]) - - signals.comment_was_flagged.disconnect(receive) - -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'""" - comments = self.createSomeComments() - pk = comments[0].pk - self.client.login(username="normaluser", password="normaluser") - response = self.client.get("/delete/%d/" % pk) - self.assertEqual(response["Location"], "http://testserver/accounts/login/?next=/delete/%d/" % pk) - - makeModerator("normaluser") - response = self.client.get("/delete/%d/" % pk) - self.assertEqual(response.status_code, 200) - - def testDeletePost(self): - """POSTing the delete view should mark the comment as removed""" - comments = self.createSomeComments() - pk = comments[0].pk - makeModerator("normaluser") - self.client.login(username="normaluser", password="normaluser") - response = self.client.post("/delete/%d/" % pk) - self.assertEqual(response["Location"], "http://testserver/deleted/?c=%d" % pk) - c = Comment.objects.get(pk=pk) - self.assertTrue(c.is_removed) - self.assertEqual(c.flags.filter(flag=CommentFlag.MODERATOR_DELETION, user__username="normaluser").count(), 1) - - def testDeletePostNext(self): - """ - POSTing the delete view will redirect to an explicitly provided a next - url. - """ - comments = self.createSomeComments() - pk = comments[0].pk - makeModerator("normaluser") - self.client.login(username="normaluser", password="normaluser") - response = self.client.post("/delete/%d/" % pk, {'next': "/go/here/"}) - self.assertEqual(response["Location"], - "http://testserver/go/here/?c=%d" % pk) - - def testDeletePostUnsafeNext(self): - """ - POSTing to the delete view with an unsafe next url will ignore the - provided url when redirecting. - """ - comments = self.createSomeComments() - pk = comments[0].pk - makeModerator("normaluser") - self.client.login(username="normaluser", password="normaluser") - response = self.client.post("/delete/%d/" % pk, - {'next': "http://elsewhere/bad"}) - self.assertEqual(response["Location"], - "http://testserver/deleted/?c=%d" % pk) - - 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]) - - signals.comment_was_flagged.disconnect(receive) - - def testDeletedView(self): - comments = self.createSomeComments() - pk = comments[0].pk - response = self.client.get("/deleted/", data={"c": pk}) - self.assertTemplateUsed(response, "comments/deleted.html") - -class ApproveViewTests(CommentTestCase): - - def testApprovePermissions(self): - """The approve view should only be accessible to 'moderators'""" - comments = self.createSomeComments() - pk = comments[0].pk - self.client.login(username="normaluser", password="normaluser") - response = self.client.get("/approve/%d/" % pk) - self.assertEqual(response["Location"], "http://testserver/accounts/login/?next=/approve/%d/" % pk) - - makeModerator("normaluser") - response = self.client.get("/approve/%d/" % pk) - self.assertEqual(response.status_code, 200) - - def testApprovePost(self): - """POSTing the approve 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/%d/" % c1.pk) - self.assertEqual(response["Location"], "http://testserver/approved/?c=%d" % c1.pk) - c = Comment.objects.get(pk=c1.pk) - self.assertTrue(c.is_public) - self.assertEqual(c.flags.filter(flag=CommentFlag.MODERATOR_APPROVAL, user__username="normaluser").count(), 1) - - def testApprovePostNext(self): - """ - POSTing the approve view will redirect to an explicitly provided a next - url. - """ - 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/%d/" % c1.pk, - {'next': "/go/here/"}) - self.assertEqual(response["Location"], - "http://testserver/go/here/?c=%d" % c1.pk) - - def testApprovePostUnsafeNext(self): - """ - POSTing to the approve view with an unsafe next url will ignore the - provided url when redirecting. - """ - 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/%d/" % c1.pk, - {'next': "http://elsewhere/bad"}) - self.assertEqual(response["Location"], - "http://testserver/approved/?c=%d" % c1.pk) - - 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]) - - signals.comment_was_flagged.disconnect(receive) - - def testApprovedView(self): - comments = self.createSomeComments() - pk = comments[0].pk - response = self.client.get("/approved/", data={"c":pk}) - self.assertTemplateUsed(response, "comments/approved.html") - -class AdminActionsTests(CommentTestCase): - urls = "comment_tests.urls_admin" - - def setUp(self): - super(AdminActionsTests, self).setUp() - - # Make "normaluser" a moderator - u = User.objects.get(username="normaluser") - u.is_staff = True - perms = Permission.objects.filter( - content_type__app_label = 'comments', - codename__endswith = 'comment' - ) - for perm in perms: - u.user_permissions.add(perm) - u.save() - - def testActionsNonModerator(self): - comments = self.createSomeComments() - self.client.login(username="normaluser", password="normaluser") - response = self.client.get("/admin/comments/comment/") - self.assertNotContains(response, "approve_comments") - - def testActionsModerator(self): - comments = self.createSomeComments() - makeModerator("normaluser") - self.client.login(username="normaluser", password="normaluser") - response = self.client.get("/admin/comments/comment/") - self.assertContains(response, "approve_comments") - - def testActionsDisabledDelete(self): - "Tests a CommentAdmin where 'delete_selected' has been disabled." - comments = self.createSomeComments() - self.client.login(username="normaluser", password="normaluser") - response = self.client.get('/admin2/comments/comment/') - self.assertEqual(response.status_code, 200) - self.assertNotContains(response, '