Exemple #1
0
    def test_comment_post(self):
        """
        Make an ajax post.
        """
        content_type = "article.article"
        timestamp = str(int(time.time()))
        article = factories.create_article()

        form = CommentForm(article)
        security_hash = form.generate_security_hash(content_type, str(article.pk), timestamp)
        post_data = {
            "content_type": content_type,
            "object_pk": article.pk,
            "name": "Testing name",
            "email": "*****@*****.**",
            "comment": "Testing comment",
            "timestamp": timestamp,
            "security_hash": security_hash,
        }
        url = reverse("comments-post-comment-ajax")
        response = self.client.post(url, post_data, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
        self.assertContains(response, "Testing comment", status_code=200)
        self.assertEqual(response.status_code, 200)

        json_response = json.loads(response.content.decode("utf-8"))
        self.assertTrue(json_response['success'])
        self.assertEqual(json_response['errors'], {})
        self.assertIn('Testing name', json_response['html'])
    def test_comment_post(self):
        """
        Make an ajax post.
        """
        content_type = "article.article"
        timestamp = str(int(time.time()))
        article = factories.create_article()

        form = CommentForm(article)
        security_hash = form.generate_security_hash(content_type, str(article.pk), timestamp)
        post_data = {
            "content_type": content_type,
            "object_pk": article.pk,
            "name": "Testing name",
            "email": "*****@*****.**",
            "comment": "Testing comment",
            "timestamp": timestamp,
            "security_hash": security_hash,
        }
        url = reverse("comments-post-comment-ajax")
        response = self.client.post(url, post_data, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
        self.assertContains(response, "Testing comment", status_code=200)
        self.assertEqual(response.status_code, 200)

        json_response = json.loads(response.content.decode("utf-8"))
        self.assertTrue(json_response['success'])
        self.assertEqual(json_response['errors'], {})
        self.assertIn('Testing name', json_response['html'])
    def test_comment_post_missing(self):
        """
        Make an ajax post.
        """
        content_type = "article.article"
        timestamp = str(int(time.time()))
        article = factories.create_article()

        form = CommentForm(article)
        security_hash = form.generate_security_hash(content_type,
                                                    str(article.pk), timestamp)
        post_data = {
            "content_type": content_type,
            "object_pk": article.pk,
            "timestamp": timestamp,
            "security_hash": security_hash,
        }
        url = reverse("comments-post-comment-ajax")
        response = self.client.post(url,
                                    post_data,
                                    HTTP_X_REQUESTED_WITH='XMLHttpRequest')
        self.assertEqual(response.status_code, 200)

        json_response = json.loads(response.content.decode("utf-8"))
        self.assertFalse(json_response['success'])
        self.assertEqual(set(json_response['errors'].keys()),
                         set(['name', 'email', 'comment']))
Exemple #4
0
    def test_form_class(self):
        """
        Test how overriding the form class works.
        """
        form_class = fluent_comments.get_form()
        self.assertIs(form_class, CompactCommentForm)

        article = factories.create_article()
        form = form_class(article)

        if appsettings.USE_THREADEDCOMMENTS:
            self.assertEqual(
                [f.name for f in form.visible_fields()],
                ['name', 'email', 'url', 'title', 'comment', 'honeypot'])
            self.assertEqual(form.helper.layout.fields[3], 'security_hash')
            self.assertIsInstance(form.helper.layout.fields[4], Row)
            self.assertEqual(form.helper.layout.fields[6], 'comment')
            self.assertEqual(form.helper.layout.fields[7], 'honeypot')
        else:
            self.assertEqual([f.name for f in form.visible_fields()],
                             ['name', 'email', 'url', 'comment', 'honeypot'])
            self.assertEqual(form.helper.layout.fields[3], 'security_hash')
            self.assertIsInstance(form.helper.layout.fields[4], Row)
            self.assertEqual(form.helper.layout.fields[5], 'comment')
            self.assertEqual(form.helper.layout.fields[6], 'honeypot')
Exemple #5
0
    def test_compact_ordering1(self):
        """
        Test how field ordering works.
        """
        article = factories.create_article()
        form = CompactCommentForm(article)
        self.assertEqual([f.name for f in form.visible_fields()],
                         ['comment', 'name', 'email', 'url', 'honeypot'])
        if appsettings.USE_THREADEDCOMMENTS:
            self.assertEqual(list(form.fields.keys()), [
                'content_type', 'object_pk', 'timestamp', 'security_hash',
                'parent', 'comment', 'name', 'email', 'url', 'honeypot'
            ])

            self.assertEqual(form.helper.layout.fields[3], 'security_hash')
            self.assertEqual(form.helper.layout.fields[5], 'comment')
            self.assertIsInstance(form.helper.layout.fields[6], Row)
            self.assertEqual(form.helper.layout.fields[7], 'honeypot')
        else:
            self.assertEqual(list(form.fields.keys()), [
                'content_type', 'object_pk', 'timestamp', 'security_hash',
                'comment', 'name', 'email', 'url', 'honeypot'
            ])

            self.assertEqual(form.helper.layout.fields[3], 'security_hash')
            self.assertEqual(form.helper.layout.fields[4], 'comment')
            self.assertIsInstance(form.helper.layout.fields[5], Row)
            self.assertEqual(form.helper.layout.fields[6], 'honeypot')
    def test_get_article_with_comment(self):
        """
        See if the comment renders
        """
        article = factories.create_article()
        comment = factories.create_comment(article=article, comment="Test-Comment")

        response = self.client.get(reverse('article-details', kwargs={"slug": article.slug}))
        self.assertContains(response, "Test-Comment", status_code=200)
Exemple #7
0
    def test_get_article_with_comment(self):
        """
        See if the comment renders
        """
        article = factories.create_article()
        comment = factories.create_comment(article=article, comment="Test-Comment")

        response = self.client.get(reverse('article-details', kwargs={"slug": article.slug}))
        self.assertContains(response, "Test-Comment", status_code=200)
    def test_comment_post_moderated(self):
        """
        See that soft delete works properly.
        """
        # Double check preconditions for moderation
        self.assertIsNotNone(get_model_moderator(Article))
        self.assertTrue(len(signals.comment_will_be_posted.receivers))
        self.assertEqual(id(get_comment_model()),
                         signals.comment_will_be_posted.receivers[0][0][1])

        content_type = "article.article"
        timestamp = str(int(time.time()))
        article = factories.create_article()

        form = CommentForm(article)
        security_hash = form.generate_security_hash(content_type,
                                                    str(article.pk), timestamp)
        post_data = {
            "content_type": content_type,
            "object_pk": article.pk,
            "name": "Testing name",
            "email": "*****@*****.**",
            "comment": "Testing comment",
            "timestamp": timestamp,
            "security_hash": security_hash,
        }

        for url, is_ajax in [
            (reverse("comments-post-comment-ajax"), True),
            (reverse("comments-post-comment"), False),
        ]:
            with patch.object(Akismet,
                              "_request",
                              return_value=MockedResponse(True)) as m:
                response = self.client.post(
                    url, post_data, HTTP_X_REQUESTED_WITH="XMLHttpRequest")
            self.assertEqual(m.call_count, 1, "Moderator not called by " + url)

            if is_ajax:
                self.assertContains(response,
                                    "Testing comment",
                                    status_code=200)
                self.assertEqual(response.status_code, 200)

                json_response = json.loads(response.content.decode("utf-8"))
                self.assertTrue(json_response["success"])
                self.assertEqual(json_response["errors"], {})
            else:
                self.assertRedirects(response,
                                     reverse("comments-comment-done") + "?c=1")

            comment = get_comment_model().objects.filter(
                user_email="*****@*****.**")[0]
            self.assertFalse(comment.is_public, "Not moderated by " + url)
            self.assertTrue(comment.is_removed)
    def test_moderator_no_akismet(self, *mocks):
        """
        Testing moderation without akismet
        """
        request = RequestFactory().post(reverse('comments-post-comment-ajax'))
        article = factories.create_article()
        comment = factories.create_comment(article=article)
        moderator = get_model_moderator(Article)  # type: FluentCommentsModerator

        self.assertTrue(article.enable_comments)
        self.assertFalse(moderator.akismet_check)
        self.assertTrue(moderator.allow(comment, article, request), "no akismet, comment should be allowed")
    def test_moderator_no_akismet(self, *mocks):
        """
        Testing moderation without akismet
        """
        request = RequestFactory().post(reverse('comments-post-comment-ajax'))
        article = factories.create_article()
        comment = factories.create_comment(article=article)
        moderator = get_model_moderator(Article)  # type: FluentCommentsModerator

        self.assertTrue(article.enable_comments)
        self.assertFalse(moderator.akismet_check)
        self.assertTrue(moderator.allow(comment, article, request), "no akismet, comment should be allowed")
 def test_compact_ordering1(self):
     """
     Test how field ordering works.
     """
     article = factories.create_article()
     form = CompactCommentForm(article)
     self.assertEqual([f.name for f in form.visible_fields()], ['comment', 'name', 'email', 'url', 'honeypot'])
     self.assertEqual(list(form.fields.keys()), ['content_type', 'object_pk', 'timestamp', 'security_hash', 'comment', 'name', 'email', 'url', 'honeypot'])
     self.assertEqual(form.helper.layout.fields[3], 'security_hash')
     self.assertEqual(form.helper.layout.fields[4], 'comment')
     self.assertIsInstance(form.helper.layout.fields[5], Row)
     self.assertEqual(form.helper.layout.fields[6], 'honeypot')
    def test_compact_ordering1(self):
        """
        Test how field ordering works.
        """
        article = factories.create_article()
        form = CompactCommentForm(article)
        self.assertEqual(
            [f.name for f in form.visible_fields()],
            ["comment", "name", "email", "url", "honeypot"],
        )
        if appsettings.USE_THREADEDCOMMENTS:
            self.assertEqual(
                list(form.fields.keys()),
                [
                    "content_type",
                    "object_pk",
                    "timestamp",
                    "security_hash",
                    "parent",
                    "comment",
                    "name",
                    "email",
                    "url",
                    "honeypot",
                ],
            )

            self.assertEqual(form.helper.layout.fields[3], "security_hash")
            self.assertEqual(form.helper.layout.fields[5], "comment")
            self.assertIsInstance(form.helper.layout.fields[6], Row)
            self.assertEqual(form.helper.layout.fields[7], "honeypot")
        else:
            self.assertEqual(
                list(form.fields.keys()),
                [
                    "content_type",
                    "object_pk",
                    "timestamp",
                    "security_hash",
                    "comment",
                    "name",
                    "email",
                    "url",
                    "honeypot",
                ],
            )

            self.assertEqual(form.helper.layout.fields[3], "security_hash")
            self.assertEqual(form.helper.layout.fields[4], "comment")
            self.assertIsInstance(form.helper.layout.fields[5], Row)
            self.assertEqual(form.helper.layout.fields[6], "honeypot")
    def test_bad_words(self, *mocks):
        """
        Test moderation on bad words.
        """
        request = RequestFactory().post(reverse('comments-post-comment-ajax'))
        article = factories.create_article()
        comment = factories.create_comment(article=article, comment='Testing:viagra!!')
        moderator = get_model_moderator(Article)  # type: FluentCommentsModerator

        self.assertTrue(moderator.moderate_bad_words)  # see that settings are correctly patched
        self.assertTrue(moderator.moderate(comment, article, request), "bad_words should reject")

        comment.comment = "Just normal words"
        self.assertFalse(moderator.moderate(comment, article, request), "bad_words should not trigger")
    def test_akismet(self, *mocks):
        """
        Test an akismet call
        """
        request = RequestFactory().post(reverse('comments-post-comment-ajax'))
        article = factories.create_article()
        comment = factories.create_comment(article=article, user_name='viagra-test-123')
        moderator = get_model_moderator(Article)  # type: FluentCommentsModerator

        self.assertTrue(article.enable_comments)
        self.assertTrue(moderator.akismet_check)  # see that settings are correctly patched

        with patch.object(Akismet, '_request', return_value=MockedResponse(True)):
            self.assertTrue(moderator.moderate(comment, article, request), "akismet should reject")
    def test_form_class(self):
        """
        Test how overriding the form class works.
        """
        form_class = fluent_comments.get_form()
        self.assertIs(form_class, CompactCommentForm)

        article = factories.create_article()
        form = form_class(article)
        self.assertEqual([f.name for f in form.visible_fields()], ['name', 'email', 'url', 'comment', 'honeypot'])
        self.assertEqual(form.helper.layout.fields[3], 'security_hash')
        self.assertIsInstance(form.helper.layout.fields[4], Row)
        self.assertEqual(form.helper.layout.fields[5], 'comment')
        self.assertEqual(form.helper.layout.fields[6], 'honeypot')
    def test_bad_words(self, *mocks):
        """
        Test moderation on bad words.
        """
        request = RequestFactory().post(reverse('comments-post-comment-ajax'))
        article = factories.create_article()
        comment = factories.create_comment(article=article, comment='Testing:viagra!!')
        moderator = get_model_moderator(Article)  # type: FluentCommentsModerator

        self.assertTrue(moderator.moderate_bad_words)  # see that settings are correctly patched
        self.assertTrue(moderator.moderate(comment, article, request), "bad_words should reject")

        comment.comment = "Just normal words"
        self.assertFalse(moderator.moderate(comment, article, request), "bad_words should not trigger")
    def test_akismet(self, *mocks):
        """
        Test an akismet call
        """
        request = RequestFactory().post(reverse('comments-post-comment-ajax'))
        article = factories.create_article()
        comment = factories.create_comment(article=article, user_name='viagra-test-123')
        moderator = get_model_moderator(Article)  # type: FluentCommentsModerator

        self.assertTrue(article.enable_comments)
        self.assertTrue(moderator.akismet_check)  # see that settings are correctly patched

        with patch.object(Akismet, '_request', return_value=MockedResponse(True)):
            self.assertTrue(moderator.moderate(comment, article, request), "akismet should reject")
    def test_comment_post_moderated(self):
        """
        See that soft delete works properly.
        """
        # Double check preconditions for moderation
        self.assertIsNotNone(get_model_moderator(Article))
        self.assertTrue(len(signals.comment_will_be_posted.receivers))
        self.assertEqual(id(get_comment_model()), signals.comment_will_be_posted.receivers[0][0][1])

        content_type = "article.article"
        timestamp = str(int(time.time()))
        article = factories.create_article()

        form = CommentForm(article)
        security_hash = form.generate_security_hash(content_type, str(article.pk), timestamp)
        post_data = {
            "content_type": content_type,
            "object_pk": article.pk,
            "name": "Testing name",
            "email": "*****@*****.**",
            "comment": "Testing comment",
            "timestamp": timestamp,
            "security_hash": security_hash,
        }

        for url, is_ajax in [
            (reverse("comments-post-comment-ajax"), True),
            (reverse("comments-post-comment"), False),
        ]:
            with patch.object(Akismet, '_request', return_value=MockedResponse(True)) as m:
                response = self.client.post(url, post_data, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
            self.assertEqual(m.call_count, 1, "Moderator not called by " + url)

            if is_ajax:
                self.assertContains(response, "Testing comment", status_code=200)
                self.assertEqual(response.status_code, 200)

                json_response = json.loads(response.content.decode("utf-8"))
                self.assertTrue(json_response['success'])
                self.assertEqual(json_response['errors'], {})
            else:
                self.assertRedirects(response, reverse('comments-comment-done') + "?c=1")

            comment = get_comment_model().objects.filter(user_email="*****@*****.**")[0]
            self.assertFalse(comment.is_public, "Not moderated by " + url)
            self.assertTrue(comment.is_removed)
    def test_comment_post_bad_requests(self):
        """
        See how error handling works on bad requests
        """
        content_type = "article.article"
        timestamp = str(int(time.time()))
        article = factories.create_article()

        form = CommentForm(article)
        security_hash = form.generate_security_hash(content_type,
                                                    str(article.pk), timestamp)
        correct_data = {
            "content_type": content_type,
            "object_pk": article.pk,
            "timestamp": timestamp,
            "security_hash": security_hash,
        }
        url = reverse("comments-post-comment-ajax")
        headers = dict(HTTP_X_REQUESTED_WITH='XMLHttpRequest')

        # No data
        self.assertEqual(self.client.post(url, {}, **headers).status_code, 400)

        # invalid pk
        post_data = correct_data.copy()
        post_data['object_pk'] = 999
        self.assertEqual(
            self.client.post(url, post_data, **headers).status_code, 400)

        # invalid content type
        post_data = correct_data.copy()
        post_data['content_type'] = 'article.foo'
        self.assertEqual(
            self.client.post(url, post_data, **headers).status_code, 400)

        # invalid security hash
        post_data = correct_data.copy()
        post_data['timestamp'] = 0
        self.assertEqual(
            self.client.post(url, post_data, **headers).status_code, 400)
    def test_comment_post_missing(self):
        """
        Make an ajax post.
        """
        content_type = "article.article"
        timestamp = str(int(time.time()))
        article = factories.create_article()

        form = CommentForm(article)
        security_hash = form.generate_security_hash(content_type, str(article.pk), timestamp)
        post_data = {
            "content_type": content_type,
            "object_pk": article.pk,
            "timestamp": timestamp,
            "security_hash": security_hash,
        }
        url = reverse("comments-post-comment-ajax")
        response = self.client.post(url, post_data, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
        self.assertEqual(response.status_code, 200)

        json_response = json.loads(response.content.decode("utf-8"))
        self.assertFalse(json_response['success'])
        self.assertEqual(set(json_response['errors'].keys()), set(['name', 'email', 'comment']))
    def test_comment_post_bad_requests(self):
        """
        See how error handling works on bad requests
        """
        content_type = "article.article"
        timestamp = str(int(time.time()))
        article = factories.create_article()

        form = CommentForm(article)
        security_hash = form.generate_security_hash(content_type, str(article.pk), timestamp)
        correct_data = {
            "content_type": content_type,
            "object_pk": article.pk,
            "timestamp": timestamp,
            "security_hash": security_hash,
        }
        url = reverse("comments-post-comment-ajax")
        headers = dict(HTTP_X_REQUESTED_WITH='XMLHttpRequest')

        # No data
        self.assertEqual(self.client.post(url, {}, **headers).status_code, 400)

        # invalid pk
        post_data = correct_data.copy()
        post_data['object_pk'] = 999
        self.assertEqual(self.client.post(url, post_data, **headers).status_code, 400)

        # invalid content type
        post_data = correct_data.copy()
        post_data['content_type'] = 'article.foo'
        self.assertEqual(self.client.post(url, post_data, **headers).status_code, 400)

        # invalid security hash
        post_data = correct_data.copy()
        post_data['timestamp'] = 0
        self.assertEqual(self.client.post(url, post_data, **headers).status_code, 400)