Class used to filter incoming comments. This will get replaced with a plugin once the plugin architecture is complete, but the logic will probably get ported.
Inheritance: ICommentFilter
コード例 #1
0
        public void FilterAfterPersistWithCommentModerationDisabledCausesNewCommentsToBeActive()
        {
            //arrange
            var subtextContext = new Mock<ISubtextContext>();
            var cache = new TestCache();
            subtextContext.Setup(c => c.Cache).Returns(cache);
            subtextContext.Setup(c => c.User.IsInRole("Admins")).Returns(false);
            subtextContext.Setup(c => c.Blog).Returns(new Blog { ModerationEnabled = false });
            FeedbackItem savedFeedback = null;
            subtextContext.Setup(c => c.Repository.UpdateInternal(It.IsAny<FeedbackItem>())).Callback<FeedbackItem>(
                f => savedFeedback = f);

            var commentSpamFilter = new Mock<ICommentSpamService>();
            var commentFilter = new CommentFilter(subtextContext.Object, commentSpamFilter.Object);
            var feedback = new FeedbackItem(FeedbackType.Comment) { };
            Assert.IsFalse(feedback.Approved);

            //act
            commentFilter.FilterAfterPersist(feedback);

            //assert
            Assert.IsTrue(savedFeedback.Approved);
        }
コード例 #2
0
        public void FilterBeforePersistDoesNotAllowDuplicateComments()
        {
            //arrange
            var recentCommentChecksums = new Queue<string>();
            recentCommentChecksums.Enqueue("TestChecksum");
            var subtextContext = new Mock<ISubtextContext>();
            var cache = new TestCache();
            cache["COMMENT FILTER:.RECENT_COMMENTS"] = recentCommentChecksums;
            subtextContext.Setup(c => c.Cache).Returns(cache);
            subtextContext.Setup(c => c.User.IsInRole("Admins")).Returns(false);
            subtextContext.Setup(c => c.Blog).Returns(new Blog { CommentDelayInMinutes = 0, DuplicateCommentsEnabled = false });
            var commentSpamFilter = new Mock<ICommentSpamService>();
            var commentFilter = new CommentFilter(subtextContext.Object, commentSpamFilter.Object);

            //act, assert
            UnitTestHelper.AssertThrows<CommentDuplicateException>(() =>
                                                                   commentFilter.FilterBeforePersist(
                                                                       new FeedbackItem(FeedbackType.Comment) { ChecksumHash = "TestChecksum" })
                );
        }
コード例 #3
0
        public void FilterBeforePersistIgnoresAdminRole()
        {
            //arrange
            var subtextContext = new Mock<ISubtextContext>();
            var cache = new TestCache();
            cache["COMMENT FILTER:127.0.0.1"] = new FeedbackItem(FeedbackType.Comment);
            subtextContext.Setup(c => c.Cache).Returns(cache);
            subtextContext.Setup(c => c.User.IsInRole("Admins")).Returns(true);
            subtextContext.Setup(c => c.Blog).Returns(new Blog { CommentDelayInMinutes = 1 });
            var commentSpamFilter = new Mock<ICommentSpamService>();
            var commentFilter = new CommentFilter(subtextContext.Object, commentSpamFilter.Object);

            //act, assert (no throw)
            commentFilter.FilterBeforePersist(new FeedbackItem(FeedbackType.PingTrack) { IpAddress = IPAddress.Parse("127.0.0.1") });
        }
コード例 #4
0
        public void FilterBeforePersistDoesNotAllowTooManyCommentsWithinCommentDelay()
        {
            //arrange
            var subtextContext = new Mock<ISubtextContext>();
            var cache = new TestCache();
            cache["COMMENT FILTER:127.0.0.1"] = new FeedbackItem(FeedbackType.Comment);
            subtextContext.Setup(c => c.Cache).Returns(cache);
            subtextContext.Setup(c => c.User.IsInRole("Admins")).Returns(false); // change to true.
            subtextContext.Setup(c => c.Blog).Returns(new Blog { CommentDelayInMinutes = 100 });
            var commentSpamFilter = new Mock<ICommentSpamService>();
            var commentFilter = new CommentFilter(subtextContext.Object, commentSpamFilter.Object);

            //act, assert (no throw)
            UnitTestHelper.AssertThrows<CommentFrequencyException>(() =>
                                                                   commentFilter.FilterBeforePersist(
                                                                       new FeedbackItem(FeedbackType.Comment) { IpAddress = IPAddress.Parse("127.0.0.1") })
                );
        }
コード例 #5
0
        protected void lkbPost_Click(object sender, EventArgs e)
        {
            try
            {
                var entry = new Trackback(EntryId, txbTitle.Text, txbUrl.Text.EnsureUrl(), string.Empty,
                                          txbBody.Text.Trim().Length > 0 ? txbBody.Text.Trim() : txbTitle.Text,
                                          Config.CurrentBlog.TimeZone.Now);
                var commentService = new CommentService(SubtextContext, null);

                if(commentService.Create(entry, true/*runFilters*/) > 0)
                {
                    ICommentSpamService feedbackService = null;
                    if(Config.CurrentBlog.FeedbackSpamServiceEnabled)
                    {
                        feedbackService = new AkismetSpamService(Config.CurrentBlog.FeedbackSpamServiceKey,
                                                                 Config.CurrentBlog, null, Url);
                    }
                    var filter = new CommentFilter(SubtextContext, feedbackService);
                    filter.FilterAfterPersist(entry);
                    Messages.ShowMessage(Constants.RES_SUCCESSNEW);
                    Edit.Visible = false;
                    Results.Visible = true;
                }
                else
                {
                    Messages.ShowError(Constants.RES_FAILUREEDIT
                                       + " There was a baseline problem posting your Trackback.");
                }
            }
            catch(Exception ex)
            {
                Log.Error(ex.Message, ex);
                Messages.ShowError(String.Format(Constants.RES_EXCEPTION,
                                                 Constants.RES_FAILUREEDIT, ex.Message));
            }
        }
コード例 #6
0
ファイル: Referrers.aspx.cs プロジェクト: ayende/Subtext
        protected void lkbPost_Click(object sender, EventArgs e)
        {
            try
            {
                Trackback entry = new Trackback(EntryID, txbTitle.Text, HtmlHelper.CheckForUrl(txbUrl.Text), string.Empty, txbBody.Text.Trim().Length > 0 ? txbBody.Text.Trim() : txbTitle.Text, Config.CurrentBlog.TimeZone.Now);

                if(FeedbackItem.Create(entry, null) > 0)
                {
                    CommentFilter filter = new CommentFilter(HttpContext.Current.Cache);
                    filter.FilterAfterPersist(entry);
                    this.Messages.ShowMessage(Constants.RES_SUCCESSNEW);
                    this.Edit.Visible = false;
                    this.Results.Visible = true;
                }
                else
                {
                    this.Messages.ShowError(Constants.RES_FAILUREEDIT
                        + " There was a baseline problem posting your Trackback.");
                }
            }
            catch(Exception ex)
            {
                log.Error(ex.Message, ex);
                this.Messages.ShowError(String.Format(Constants.RES_EXCEPTION,
                    Constants.RES_FAILUREEDIT, ex.Message));
            }
        }