EmailServiceTests.cs :  » Bloggers » SubText » UnitTests » Subtext » Framework » Email » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Bloggers » SubText 
SubText » UnitTests » Subtext » Framework » Email » EmailServiceTests.cs
using System;
using System.Net;
using MbUnit.Framework;
using Moq;
using Subtext.Extensibility;
using Subtext.Extensibility.Providers;
using Subtext.Framework;
using Subtext.Framework.Components;
using Subtext.Framework.Email;
using Subtext.Framework.Routing;

namespace UnitTests.Subtext.Framework.Email{
    [TestFixture]
    public class EmailServiceTests
    {
        [Test]
        public void EmailCommentToBlogAuthor_WithCurrentUserIsAnAdmin_DoesNotSendEmail()
        {
            //arrange
            var comment = new FeedbackItem(FeedbackType.Comment) {};
            var blog = new Blog {Email = "cody@example.com", UserName = "cody"};
            var emailProvider = new Mock<EmailProvider>();
            var context = new Mock<ISubtextContext>();
            context.Setup(c => c.UrlHelper).Returns(new Mock<UrlHelper>().Object);
            context.Setup(c => c.Blog).Returns(blog);
            context.Setup(c => c.User.Identity.Name).Returns("cody");
            context.Setup(c => c.User.IsInRole("Admins")).Returns(true);
            var emailService = new EmailService(emailProvider.Object, new Mock<ITemplateEngine>().Object, context.Object);
            emailProvider.Setup(
                e => e.Send(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Throws(
                new Exception());

            //act
            emailService.EmailCommentToBlogAuthor(comment);
        }

        [Test]
        public void EmailCommentToBlogAuthor_WithBlogHavingNullEmail_DoesNotSendEmail()
        {
            //arrange
            var comment = new FeedbackItem(FeedbackType.Comment) {};
            var blog = new Blog {Email = string.Empty};
            var emailProvider = new Mock<EmailProvider>();
            var context = new Mock<ISubtextContext>();
            context.Setup(c => c.UrlHelper).Returns(new Mock<UrlHelper>().Object);
            context.Setup(c => c.Blog).Returns(blog);
            var emailService = new EmailService(emailProvider.Object, new Mock<ITemplateEngine>().Object, context.Object);
            emailProvider.Setup(
                e => e.Send(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Throws(
                new Exception());

            //act
            emailService.EmailCommentToBlogAuthor(comment);
        }

        [Test]
        public void EmailCommentToBlogAuthor_WithCommentThatIsTrackback_DoesNotSendEmail()
        {
            //arrange
            var comment = new FeedbackItem(FeedbackType.PingTrack) {};
            var blog = new Blog {Email = "foo@example.com"};
            var context = new Mock<ISubtextContext>();
            context.Setup(c => c.UrlHelper).Returns(new Mock<UrlHelper>().Object);
            context.Setup(c => c.Blog).Returns(blog);
            var emailProvider = new Mock<EmailProvider>();
            var emailService = new EmailService(emailProvider.Object, new Mock<ITemplateEngine>().Object, context.Object);
            emailProvider.Setup(
                e => e.Send(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Throws(
                new Exception());


            //act
            emailService.EmailCommentToBlogAuthor(comment);
        }

        [Test]
        public void EmailCommentToBlogAuthor_WithComment_UsesTitleForSubject()
        {
            //arrange
            var comment = new FeedbackItem(FeedbackType.Comment)
            {Id = 121, Author = "me", Title = "the subject", FlaggedAsSpam = false};
            var emailProvider = new Mock<EmailProvider>();
            EmailService emailService = SetupEmailService(comment, emailProvider);
            string subject = null;
            emailProvider.Setup(
                e => e.Send(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Callback
                <string, string, string, string>((to, from, title, message) => subject = title);
            //act
            emailService.EmailCommentToBlogAuthor(comment);

            //assert
            Assert.AreEqual("Comment: the subject (via the blog)", subject);
        }

        [Test]
        public void EmailCommentToBlogAuthor_WithCommentHavingNullSource_SendsEmail()
        {
            //arrange
            var comment = new FeedbackItem(FeedbackType.Comment) { Id = 121, Author = "me", Title = "the subject", FlaggedAsSpam = false, Entry = null };
            var emailProvider = new Mock<EmailProvider>();
            var context = new Mock<ISubtextContext>();
            context.Setup(c => c.Blog).Returns(new Blog {Title = "the blog", Email = "haacked@example.com"});
            context.Setup(c => c.User.IsInRole("Admins")).Returns(false);
            context.Setup(c => c.UrlHelper.FeedbackUrl(It.IsAny<FeedbackItem>())).Returns((VirtualPath)null);
            var templateEngine = new Mock<ITemplateEngine>();
            var template = new Mock<ITextTemplate>();
            template.Setup(t => t.Format(It.IsAny<Object>())).Returns("whatever");
            templateEngine.Setup(t => t.GetTemplate("CommentReceived")).Returns(template.Object);
            var emailService = new EmailService(emailProvider.Object, templateEngine.Object, context.Object);
            string subject = null;
            emailProvider.Setup(
                e => e.Send(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Callback
                <string, string, string, string>((to, from, title, message) => subject = title);
            
            //act
            emailService.EmailCommentToBlogAuthor(comment);

            //assert
            Assert.AreEqual("Comment: the subject (via the blog)", subject);
        }

        [Test]
        public void EmailCommentToBlogAuthor_WithCommentFlaggedAsSpam_PrefacesSubjectWithSpamHeader()
        {
            //arrange
            var comment = new FeedbackItem(FeedbackType.Comment)
            {Id = 121, FlaggedAsSpam = true, Author = "me", Title = "the subject"};
            var emailProvider = new Mock<EmailProvider>();
            EmailService emailService = SetupEmailService(comment, emailProvider);
            string subject = null;
            emailProvider.Setup(
                e => e.Send(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Callback
                <string, string, string, string>((to, from, title, message) => subject = title);
            //act
            emailService.EmailCommentToBlogAuthor(comment);

            //assert
            Assert.AreEqual("[SPAM Flagged] Comment: the subject (via the blog)", subject);
        }

        [Test]
        public void EmailCommentToBlogAuthor_WithCommentHavingEmail_UsesEmailAsFromEmail()
        {
            //arrange
            var comment = new FeedbackItem(FeedbackType.Comment)
            {Id = 121, Email = "from@example.com", Author = "me", Title = "the subject", FlaggedAsSpam = true};
            var emailProvider = new Mock<EmailProvider>();
            emailProvider.Object.UseCommentersEmailAsFromAddress = true;
            emailProvider.Object.AdminEmail = "admin@example.com";
            EmailService emailService = SetupEmailService(comment, emailProvider);
            string fromEmail = null;
            emailProvider.Setup(
                e => e.Send(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Callback
                <string, string, string, string>((to, from, title, message) => fromEmail = from);
            //act
            emailService.EmailCommentToBlogAuthor(comment);

            //assert
            Assert.AreEqual("from@example.com", fromEmail);
        }

        [Test]
        public void
            EmailCommentToBlogAuthor_WithCommentHavingEmailButUseCommentersEmailAsFromAddressSetToFalse_UsesAdminEmailAsFromEmail
            ()
        {
            //arrange
            var comment = new FeedbackItem(FeedbackType.Comment)
            {Id = 121, Email = "from@example.com", Author = "me", Title = "the subject", FlaggedAsSpam = true};
            var emailProvider = new Mock<EmailProvider>();
            emailProvider.Object.UseCommentersEmailAsFromAddress = false;
            emailProvider.Object.AdminEmail = "admin@example.com";
            EmailService emailService = SetupEmailService(comment, emailProvider);
            string fromEmail = null;
            emailProvider.Setup(
                e => e.Send(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Callback
                <string, string, string, string>((to, from, title, message) => fromEmail = from);
            //act
            emailService.EmailCommentToBlogAuthor(comment);

            //assert
            Assert.AreEqual("admin@example.com", fromEmail);
        }

        [Test]
        public void EmailCommentToBlogAuthor_WithCommentHavingNullEmail_UsesProviderEmail()
        {
            //arrange
            var comment = new FeedbackItem(FeedbackType.Comment)
            {Id = 121, Email = null, Author = "me", Title = "the subject", FlaggedAsSpam = true};
            var emailProvider = new Mock<EmailProvider>();
            emailProvider.Object.AdminEmail = "admin@example.com";
            EmailService emailService = SetupEmailService(comment, emailProvider);
            string fromEmail = null;
            emailProvider.Setup(
                e => e.Send(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Callback
                <string, string, string, string>((to, from, title, message) => fromEmail = from);
            //act
            emailService.EmailCommentToBlogAuthor(comment);

            //assert
            Assert.AreEqual("admin@example.com", fromEmail);
        }

        private static EmailService SetupEmailService(FeedbackItem comment, Mock<EmailProvider> emailProvider)
        {
            var templateEngine = new Mock<ITemplateEngine>();
            var template = new Mock<ITextTemplate>();
            templateEngine.Setup(t => t.GetTemplate(It.IsAny<string>())).Returns(template.Object);
            template.Setup(t => t.Format(It.IsAny<object>())).Returns("message");
            var urlHelper = new Mock<UrlHelper>();
            urlHelper.Setup(u => u.FeedbackUrl(comment)).Returns("/");
            var context = new Mock<ISubtextContext>();
            context.Setup(c => c.UrlHelper).Returns(urlHelper.Object);
            context.Setup(c => c.Blog).Returns(new Blog
            {Email = "test@test.com", Author = "to", Host = "localhost", Title = "the blog"});

            var emailService = new EmailService(emailProvider.Object, templateEngine.Object, context.Object);
            return emailService;
        }

        [Test]
        public void EmailCommentToBlogAuthor_WithBlog_UsesBlogEmailForToEmail()
        {
            //arrange
            var comment = new FeedbackItem(FeedbackType.Comment)
            {Id = 121, Author = "me", Title = "the subject", FlaggedAsSpam = false};
            var emailProvider = new Mock<EmailProvider>();
            var templateEngine = new Mock<ITemplateEngine>();
            var template = new Mock<ITextTemplate>();
            templateEngine.Setup(t => t.GetTemplate(It.IsAny<string>())).Returns(template.Object);
            template.Setup(t => t.Format(It.IsAny<object>())).Returns("message");
            var urlHelper = new Mock<UrlHelper>();
            urlHelper.Setup(u => u.FeedbackUrl(comment)).Returns("/");
            var context = new Mock<ISubtextContext>();
            context.Setup(c => c.UrlHelper).Returns(urlHelper.Object);
            context.Setup(c => c.Blog).Returns(new Blog
            {Email = "test@test.com", Author = "to", Host = "localhost", Title = "the blog"});

            string toEmail = null;
            emailProvider.Setup(
                e => e.Send(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Callback
                <string, string, string, string>((to, from, title, message) => toEmail = to);
            var emailService = new EmailService(emailProvider.Object, templateEngine.Object, context.Object);

            //act
            emailService.EmailCommentToBlogAuthor(comment);

            //assert
            Assert.AreEqual("test@test.com", toEmail);
        }

        [Test]
        public void EmailCommentToBlogAuthor_WithCommentFlaggedAsSpam_SetsSpamField()
        {
            //arrange
            var comment = new FeedbackItem(FeedbackType.Comment)
            {Id = 121, Author = "me", Title = "subject", FlaggedAsSpam = true};
            string sentMessage = null;
            EmailService emailService = SetupEmailService(comment, "{spamflag}", sent => sentMessage = sent);

            //act
            emailService.EmailCommentToBlogAuthor(comment);

            //assert
            Assert.AreEqual("Spam Flagged ", sentMessage);
        }

        [Test]
        public void EmailCommentToBlogAuthor_WithCommentHavingId_SetsSourceFieldWithUrlContainingId()
        {
            //arrange
            var comment = new FeedbackItem(FeedbackType.Comment)
            {Id = 121, Author = "me", Title = "subject", FlaggedAsSpam = true};
            string sentMessage = null;
            EmailService emailService = SetupEmailService(comment, "{comment.source}", sent => sentMessage = sent);

            //act
            emailService.EmailCommentToBlogAuthor(comment);

            //assert
            Assert.AreEqual("http://localhost/comment#121", sentMessage);
        }

        [Test]
        public void EmailCommentToBlogAuthor_WithEmail_SetsFromEmailAccordingly()
        {
            //arrange
            var comment = new FeedbackItem(FeedbackType.Comment)
            {Id = 121, Author = "me", Title = "subject", Email = "test@example.com"};
            string sentMessage = null;
            EmailService emailService = SetupEmailService(comment, "{comment.email}", sent => sentMessage = sent);

            //act
            emailService.EmailCommentToBlogAuthor(comment);

            //assert
            Assert.AreEqual("test@example.com", sentMessage);
        }

        [Test]
        public void EmailCommentToBlogAuthor_WithoutEmail_SetsFromEmailToNoneProvided()
        {
            //arrange
            var comment = new FeedbackItem(FeedbackType.Comment)
            {Id = 121, Author = "me", Title = "subject", Email = null};
            string sentMessage = null;
            EmailService emailService = SetupEmailService(comment, "{comment.email}", sent => sentMessage = sent);

            //act
            emailService.EmailCommentToBlogAuthor(comment);

            //assert
            Assert.AreEqual("none given", sentMessage);
        }

        [Test]
        public void EmailCommentToBlogAuthor_WithAuthor_SetsAuthorName()
        {
            //arrange
            var comment = new FeedbackItem(FeedbackType.Comment)
            {Id = 121, Author = "me", Title = "subject", Email = null};
            string sentMessage = null;
            EmailService emailService = SetupEmailService(comment, "{comment.author}", sent => sentMessage = sent);

            //act
            emailService.EmailCommentToBlogAuthor(comment);

            //assert
            Assert.AreEqual("me", sentMessage);
        }

        [Test]
        public void EmailCommentToBlogAuthor_WithSourceUrlSpecified_SetsUrl()
        {
            //arrange
            var comment = new FeedbackItem(FeedbackType.Comment)
            {Id = 121, Author = "me", Title = "subject", SourceUrl = new Uri("http://example.com/")};
            string sentMessage = null;
            EmailService emailService = SetupEmailService(comment, "{comment.authorUrl}", sent => sentMessage = sent);

            //act
            emailService.EmailCommentToBlogAuthor(comment);

            //assert
            Assert.AreEqual("http://example.com/", sentMessage);
        }

        [Test]
        public void EmailCommentToBlogAuthor_WithSourceIp_SetsIp()
        {
            //arrange
            var comment = new FeedbackItem(FeedbackType.Comment)
            {Id = 121, Author = "me", Title = "subject", IpAddress = IPAddress.Parse("127.0.0.1")};
            string sentMessage = null;
            EmailService emailService = SetupEmailService(comment, "{comment.ip}", sent => sentMessage = sent);

            //act
            emailService.EmailCommentToBlogAuthor(comment);

            //assert
            Assert.AreEqual("127.0.0.1", sentMessage);
        }

        [Test]
        public void EmailCommentToBlogAuthor_WithBodyContainingHtml_CleansHtml()
        {
            //arrange
            var comment = new FeedbackItem(FeedbackType.Comment)
            {Id = 121, Author = "me", Title = "subject", Body = "This<br />is not&lt;br /&gt;right"};
            string sentMessage = null;
            EmailService emailService = SetupEmailService(comment, "{comment.body}", sent => sentMessage = sent);

            //act
            emailService.EmailCommentToBlogAuthor(comment);

            //assert
            Assert.AreEqual("This" + Environment.NewLine + "is not" + Environment.NewLine + "right", sentMessage);
        }

        private EmailService SetupEmailService(FeedbackItem comment, string templateText, Action<string> messageCallback)
        {
            var emailProvider = new Mock<EmailProvider>();
            var templateEngine = new Mock<ITemplateEngine>();
            var template = new NamedFormatTextTemplate(templateText);
            var urlHelper = new Mock<UrlHelper>();
            var context = new Mock<ISubtextContext>();
            context.Setup(c => c.UrlHelper).Returns(urlHelper.Object);
            context.Setup(c => c.Blog).Returns(new Blog {Email = "foo@example.com", Author = "to", Host = "localhost"});

            urlHelper.Setup(u => u.FeedbackUrl(comment)).Returns<FeedbackItem>(f => "/comment#" + f.Id);
            templateEngine.Setup(t => t.GetTemplate("CommentReceived")).Returns(template);
            emailProvider.Setup(
                e => e.Send(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Callback
                <string, string, string, string>((to, from, title, message) => messageCallback(message));

            return new EmailService(emailProvider.Object, templateEngine.Object, context.Object);
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.