Ejemplo n.º 1
0
        public void CreateTrackbackSetsFeedbackTypeCorrectly()
        {
            string hostname = UnitTestHelper.GenerateUniqueString();
            var repository = new DatabaseObjectProvider();
            repository.CreateBlog("", "username", "password", hostname, string.Empty);
            UnitTestHelper.SetHttpContextWithBlogRequest(hostname, string.Empty, string.Empty);
            Blog blog = repository.GetBlog(hostname, string.Empty);
            BlogRequest.Current.Blog = blog;

            Entry entry = UnitTestHelper.CreateEntryInstanceForSyndication("phil", "title", "body");
            int parentId = UnitTestHelper.Create(entry);

            var trackback = new Trackback(parentId, "title", new Uri("http://url"), "phil", "body");
            var subtextContext = new Mock<ISubtextContext>();
            subtextContext.Setup(c => c.Blog).Returns(Config.CurrentBlog);
            //TODO: FIX!!!
            subtextContext.Setup(c => c.Repository).Returns(repository);
            subtextContext.Setup(c => c.Cache).Returns(new TestCache());
            subtextContext.Setup(c => c.HttpContext.Items).Returns(new Hashtable());
            var commentService = new CommentService(subtextContext.Object, null);
            int id = commentService.Create(trackback, true/*runFilters*/);

            FeedbackItem loadedTrackback = repository.Get(id);
            Assert.IsNotNull(loadedTrackback, "Was not able to load trackback from storage.");
            Assert.AreEqual(FeedbackType.PingTrack, loadedTrackback.FeedbackType, "Feedback should be a PingTrack");
        }
Ejemplo n.º 2
0
        public void CreateSetsDateCreated()
        {
            //arrange
            var blog = new Mock<Blog>();
            DateTime dateCreatedUtc = DateTime.UtcNow;
            blog.Object.Id = 1;
            var entry = new Entry(PostType.BlogPost, blog.Object) { Id = 123, BlogId = 1, CommentingClosed = false };
            var repository = new Mock<ObjectRepository>();
            repository.Setup(r => r.GetEntry(It.IsAny<int>(), true, true)).Returns(entry);
            var context = new Mock<ISubtextContext>();
            context.SetupGet(c => c.Repository).Returns(repository.Object);
            context.SetupGet(c => c.Blog).Returns(blog.Object);
            context.SetupGet(c => c.HttpContext.Items).Returns(new Hashtable());
            context.SetupGet(c => c.Cache).Returns(new TestCache());

            var service = new CommentService(context.Object, null);
            var comment = new FeedbackItem(FeedbackType.Comment) { EntryId = 123, BlogId = 1, Body = "test", Title = "title" };

            //act
            service.Create(comment, true/*runFilters*/);

            //assert
            Assert.GreaterEqualThan(comment.DateCreatedUtc, dateCreatedUtc);
            Assert.GreaterEqualThan(DateTime.UtcNow, comment.DateCreatedUtc);
        }
Ejemplo n.º 3
0
        public void CreateDoesNotChangeDateCreatedAndDateModifiedIfAlreadySpecified()
        {
            //arrange
            var blog = new Mock<Blog>();
            DateTime dateCreated = DateTime.Now;
            blog.Object.Id = 1;
            blog.Setup(b => b.TimeZone.Now).Returns(dateCreated);
            var entry = new Entry(PostType.BlogPost, blog.Object) {Id = 123, BlogId = 1, CommentingClosed = false};
            var repository = new Mock<ObjectProvider>();
            repository.Setup(r => r.GetEntry(It.IsAny<int>(), true, true)).Returns(entry);
            var context = new Mock<ISubtextContext>();
            context.SetupGet(c => c.Repository).Returns(repository.Object);
            context.SetupGet(c => c.Blog).Returns(blog.Object);
            context.SetupGet(c => c.HttpContext.Items).Returns(new Hashtable());
            context.SetupGet(c => c.Cache).Returns(new TestCache());

            var service = new CommentService(context.Object, null);
            var comment = new FeedbackItem(FeedbackType.Comment)
            {
                EntryId = 123,
                BlogId = 1,
                Body = "test",
                Title = "title",
                DateCreated = dateCreated.AddDays(-2),
                DateModified = dateCreated.AddDays(-1)
            };

            //act
            service.Create(comment, true/*runFilters*/);

            //assert
            Assert.AreEqual(dateCreated.AddDays(-2), comment.DateCreated);
            Assert.AreEqual(dateCreated.AddDays(-1), comment.DateModified);
        }
Ejemplo n.º 4
0
        public void TrackbackShowsUpInFeedbackList()
        {
            string hostname = UnitTestHelper.GenerateUniqueString();
            var repository = new DatabaseObjectProvider();
            repository.CreateBlog("", "username", "password", hostname, "blog");
            UnitTestHelper.SetHttpContextWithBlogRequest(hostname, "blog", string.Empty);
            Blog blog = repository.GetBlog(hostname, "blog");
            BlogRequest.Current.Blog = blog;

            Entry parentEntry = UnitTestHelper.CreateEntryInstanceForSyndication("philsath aeuoa asoeuhtoensth",
                                                                                 "sntoehu title aoeuao eu",
                                                                                 "snaot hu aensaoehtu body");
            int parentId = UnitTestHelper.Create(parentEntry);

            ICollection<FeedbackItem> entries = repository.GetFeedbackForEntry(parentEntry);
            Assert.AreEqual(0, entries.Count, "Did not expect any feedback yet.");

            var trackback = new Trackback(parentId, "title", new Uri("http://url"), "phil", "body");
            Config.CurrentBlog.DuplicateCommentsEnabled = true;
            var subtextContext = new Mock<ISubtextContext>();
            subtextContext.Setup(c => c.Cache).Returns(new TestCache());
            subtextContext.SetupBlog(Config.CurrentBlog);
            subtextContext.SetupRepository(repository);
            subtextContext.Setup(c => c.HttpContext.Items).Returns(new Hashtable());
            var commentService = new CommentService(subtextContext.Object, null);
            int trackbackId = commentService.Create(trackback, true/*runFilters*/);

            new DatabaseObjectProvider().Approve(trackback, null);

            entries = repository.GetFeedbackForEntry(parentEntry);
            Assert.AreEqual(1, entries.Count, "Expected a trackback.");
            Assert.AreEqual(trackbackId, entries.First().Id,
                            "The feedback was not the same one we expected. The IDs do not match.");
        }
Ejemplo n.º 5
0
        static FeedbackItem CreateAndUpdateFeedbackWithExactStatus(Entry entry, FeedbackType type,
                                                                   FeedbackStatusFlag status)
        {
            var repository = new DatabaseObjectProvider();
            var feedback = new FeedbackItem(type);
            feedback.Title = UnitTestHelper.GenerateUniqueString();
            feedback.Body = UnitTestHelper.GenerateUniqueString();
            feedback.EntryId = entry.Id;
            feedback.Author = "TestAuthor";

            var subtextContext = new Mock<ISubtextContext>();
            subtextContext.Setup(c => c.Cache).Returns(new TestCache());
            subtextContext.SetupBlog(Config.CurrentBlog);
            subtextContext.SetupRepository(repository);
            subtextContext.Setup(c => c.HttpContext.Items).Returns(new Hashtable());
            subtextContext.Setup(c => c.HttpContext).Returns(new HttpContextWrapper(HttpContext.Current));

            var service = new CommentService(subtextContext.Object, null);
            int id = service.Create(feedback, true/*runFilters*/);

            feedback = repository.Get(id);
            feedback.Status = status;
            repository.Update(feedback);

            return repository.Get(id);
        }
Ejemplo n.º 6
0
        private void OnSubmitButtonClick(object sender, EventArgs e)
        {
            Thread.Sleep(5000);
            if(!Page.IsValid)
            {
                return;
            }

            LastDitchValidation();
            try
            {
                Entry currentEntry = RealEntry;
                if(IsCommentAllowed)
                {
                    FeedbackItem feedbackItem = CreateFeedbackInstanceFromFormInput(currentEntry);
                    ICommentSpamService feedbackService = null;
                    if(Blog.FeedbackSpamServiceEnabled)
                    {
                        feedbackService = new AkismetSpamService(Blog.FeedbackSpamServiceKey, Blog, null, Url);
                    }
                    var commentService = new CommentService(SubtextContext,
                                                            new CommentFilter(SubtextContext, feedbackService));
                    commentService.Create(feedbackItem, true/*runFilters*/);
                    var emailService = new EmailService(EmailProvider.Instance(), new EmbeddedTemplateEngine(),
                                                        SubtextContext);
                    emailService.EmailCommentToBlogAuthor(feedbackItem);

                    if(chkRemember == null || chkRemember.Checked)
                    {
                        SetRememberedUserCookie();
                    }

                    DisplayResultMessage(feedbackItem);
                }
            }
            catch(BaseCommentException exception)
            {
                Message.Text = exception.Message;
            }
        }
Ejemplo n.º 7
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));
            }
        }
Ejemplo n.º 8
0
        private void CreateTrackbackAndSendResponse(ISubtextContext subtextContext, IEntryIdentity entry, int entryId)
        {
            HttpContextBase context = subtextContext.RequestContext.HttpContext;
            string title = SafeParam(context, "title");
            string excerpt = SafeParam(context, "excerpt");
            string urlText = SafeParam(context, "url");
            string blogName = SafeParam(context, "blog_name");

            Uri url = urlText.ParseUri();
            if (url == null)
            {
                SendTrackbackResponse(context, 1, Resources.TrackbackResponse_NoUrl);
                return;
            }

            if (entry == null ||
               !IsSourceVerification(url,
                                     subtextContext.UrlHelper.EntryUrl(entry).ToFullyQualifiedUrl(subtextContext.Blog)))
            {
                SendTrackbackResponse(context, 2,
                                      String.Format(CultureInfo.InvariantCulture,
                                                    Resources.TrackbackResponse_NoRelevantLink, url));
                return;
            }

            var trackback = new Trackback(entryId, title, url, blogName, excerpt);
            ICommentSpamService feedbackService = null;
            Blog blog = subtextContext.Blog;
            if (blog.FeedbackSpamServiceEnabled)
            {
                feedbackService = new AkismetSpamService(blog.FeedbackSpamServiceKey, blog, null, Url);
            }
            var commentService = new CommentService(SubtextContext, new CommentFilter(SubtextContext, feedbackService));
            commentService.Create(trackback, true/*runFilters*/);
            //TODO: Create this using IoC container
            var emailService = new EmailService(EmailProvider.Instance(), new EmbeddedTemplateEngine(), subtextContext);
            emailService.EmailCommentToBlogAuthor(trackback);
        }
Ejemplo n.º 9
0
 private void CreateCommentWithContactMessage(FeedbackItem contactMessage)
 {
     try
     {
         ICommentSpamService feedbackService = null;
         if (Blog.FeedbackSpamServiceEnabled)
         {
             feedbackService = new AkismetSpamService(Blog.FeedbackSpamServiceKey, Blog, null, Url);
         }
         var commentService = new CommentService(SubtextContext,
                                                 new CommentFilter(SubtextContext, feedbackService));
         commentService.Create(contactMessage, true/*runFilters*/);
     }
     catch (BaseCommentException exc)
     {
         lblMessage.Text = exc.Message;
     }
 }
Ejemplo n.º 10
0
        public void Create_ForEntry_SetsEntryPropertyBeforeCallingFilters()
        {
            //arrange
            var blog = new Mock<Blog>();
            DateTime dateCreated = DateTime.UtcNow;
            blog.Object.Id = 1;
            var entry = new Entry(PostType.BlogPost, blog.Object) { Id = 123, BlogId = 1, CommentingClosed = false };
            var repository = new Mock<ObjectRepository>();
            repository.Setup(r => r.GetEntry(It.IsAny<int>(), true, true)).Returns(entry);
            var context = new Mock<ISubtextContext>();
            context.SetupGet(c => c.Repository).Returns(repository.Object);
            context.SetupGet(c => c.Blog).Returns(blog.Object);
            context.SetupGet(c => c.HttpContext.Items).Returns(new Hashtable());
            context.SetupGet(c => c.Cache).Returns(new TestCache());

            var commentFilter = new Mock<ICommentFilter>();
            FeedbackItem feedback = null;
            commentFilter.Setup(f => f.FilterBeforePersist(It.IsAny<FeedbackItem>())).Callback<FeedbackItem>(fb => feedback = fb);
            var service = new CommentService(context.Object, commentFilter.Object);
            var comment = new FeedbackItem(FeedbackType.Comment)
            {
                EntryId = 123,
                BlogId = 1,
                Body = "test",
                Title = "title",
                DateCreatedUtc = dateCreated.AddDays(-2),
                DateModifiedUtc = dateCreated.AddDays(-1)
            };

            //act
            service.Create(comment, true /*runFilters*/);

            //assert
            Assert.AreEqual(entry, feedback.Entry);
        }
Ejemplo n.º 11
0
        public void Create_WithRunFiltersFalse_DoesNotSetFlaggedSpamToTrue()
        {
            //arrange
            var blog = new Mock<Blog>();
            DateTime dateCreated = DateTime.UtcNow;
            blog.Object.Id = 1;
            var entry = new Entry(PostType.BlogPost, blog.Object) { Id = 123, BlogId = 1, CommentingClosed = false };
            var repository = new Mock<ObjectRepository>();
            repository.Setup(r => r.GetEntry(It.IsAny<int>(), true, true)).Returns(entry);
            var context = new Mock<ISubtextContext>();
            context.SetupGet(c => c.Repository).Returns(repository.Object);
            context.SetupGet(c => c.Blog).Returns(blog.Object);
            context.SetupGet(c => c.HttpContext.Items).Returns(new Hashtable());
            context.SetupGet(c => c.Cache).Returns(new TestCache());

            var service = new CommentService(context.Object, null);
            var comment = new FeedbackItem(FeedbackType.Comment)
            {
                EntryId = 123,
                BlogId = 1,
                Body = "test",
                Title = "title",
                DateCreatedUtc = dateCreated.AddDays(-2),
                DateModifiedUtc = dateCreated.AddDays(-1)
            };

            //act
            service.Create(comment, false /*runFilters*/);

            //assert
            Assert.IsFalse(comment.FlaggedAsSpam);
        }
Ejemplo n.º 12
0
        public void Create_WithFilters_CallsIntoCommentFilters()
        {
            //arrange
            var blog = new Mock<Blog>();
            DateTime dateCreated = DateTime.UtcNow;
            blog.Object.Id = 1;
            var entry = new Entry(PostType.BlogPost, blog.Object) { Id = 123, BlogId = 1, CommentingClosed = false };
            var repository = new Mock<ObjectRepository>();
            repository.Setup(r => r.GetEntry(It.IsAny<int>(), true, true)).Returns(entry);
            var context = new Mock<ISubtextContext>();
            context.SetupGet(c => c.Repository).Returns(repository.Object);
            context.SetupGet(c => c.Blog).Returns(blog.Object);
            context.SetupGet(c => c.HttpContext.Items).Returns(new Hashtable());
            context.SetupGet(c => c.Cache).Returns(new TestCache());

            var commentFilter = new Mock<ICommentFilter>();
            bool wasBeforeCalled = false;
            bool wasAfterCalled = false;
            commentFilter.Setup(f => f.FilterBeforePersist(It.IsAny<FeedbackItem>())).Callback(
                () => wasBeforeCalled = true);
            commentFilter.Setup(f => f.FilterAfterPersist(It.IsAny<FeedbackItem>())).Callback(
                () => wasAfterCalled = true);
            var service = new CommentService(context.Object, commentFilter.Object);
            var comment = new FeedbackItem(FeedbackType.Comment)
            {
                EntryId = 123,
                BlogId = 1,
                Body = "test",
                Title = "title",
                DateCreatedUtc = dateCreated.AddDays(-2),
                DateModifiedUtc = dateCreated.AddDays(-1)
            };

            //act
            service.Create(comment, true /*runFilters*/);

            //assert
            Assert.IsTrue(wasBeforeCalled);
            Assert.IsTrue(wasAfterCalled);
            Assert.IsTrue(comment.FlaggedAsSpam);
        }
Ejemplo n.º 13
0
        public string pingBack(string sourceURI, string targetURI)
        {
            if (!Blog.TrackbacksEnabled)
            {
                return "Pingbacks are not enabled for this site.";
            }

            string pageTitle;

            // GetPostIDFromUrl returns the postID
            int? id = SubtextContext.RequestContext.GetIdFromRequest();
            if (id == null)
            {
                throw new XmlRpcFaultException(33, Resources.XmlRcpFault_DidNotLinkToPermalink);
            }

            Uri sourceUrl = sourceURI.ParseUri();
            Uri targetUrl = targetURI.ParseUri();

            // does the sourceURI actually contain the permalink ?
            if (sourceUrl == null || targetUrl == null ||
               !Verifier.SourceContainsTarget(sourceUrl, targetUrl, out pageTitle))
            {
                throw new XmlRpcFaultException(17, Resources.XmlRcpFault_InvalidLink);
            }

            //PTR = Pingback - TrackBack - Referral
            var trackback = new Trackback(id.Value, HtmlHelper.SafeFormat(pageTitle, SubtextContext.HttpContext.Server),
                                          new Uri(sourceURI), string.Empty,
                                          HtmlHelper.SafeFormat(pageTitle, SubtextContext.HttpContext.Server));
            ICommentSpamService feedbackService = null;
            if (Blog.FeedbackSpamServiceEnabled)
            {
                feedbackService = new AkismetSpamService(Blog.FeedbackSpamServiceKey, Blog, null, Url);
            }
            var commentService = new CommentService(SubtextContext, new CommentFilter(SubtextContext, feedbackService));
            commentService.Create(trackback, true/*runFilters*/);

            //TODO: Create this using IoC container
            var emailService = new EmailService(EmailProvider.Instance(), new EmbeddedTemplateEngine(), SubtextContext);
            emailService.EmailCommentToBlogAuthor(trackback);

            return String.Format(CultureInfo.InvariantCulture, Resources.XmlRpcMessage_ThanksForThePingback, sourceURI);
        }