public void EmailWithNoViewNameShouldThrow()
        {
            var mockSender = A.Fake<IMailSender>();
            var attribute = new SmtpMailAttributes();
            var mailer = new TestMailerBase(attribute, mockSender);

            Assert.Throws<ArgumentNullException>(() => mailer.Email(null));
        }
        public void PassingAMailSenderShouldWork()
        {
            var mockSender = A.Fake<IMailSender>();
            var attribute = new SmtpMailAttributes();
            var mailer = new TestMailerBase(attribute, mockSender);
            var email = mailer.Email("TextViewNoModel");

            Assert.AreSame(mockSender, mailer.MailSender);
            Assert.AreSame(mockSender, email.MailSender);
        }
        public void MailContextConstructorSetsUpObjectProperly()
        {
            var mail = new SmtpMailAttributes {From = new MailAddress("*****@*****.**")};
            mail.To.Add(new MailAddress("*****@*****.**"));

            var context = new MailSendingContext(mail);

            Assert.AreEqual(mail, context.Mail);
            Assert.False(context.Cancel);
        }
        public void DeliverNotifiesWhenMailIsBeingSent()
        {
            var sender = A.Fake<IMailSender>();
            var interceptor = A.Fake<IMailInterceptor>();
            var helper = new DeliveryHelper(sender, interceptor);
            var mail = new SmtpMailAttributes();

            helper.Deliver(mail);

            A.CallTo(() => interceptor.OnMailSending(A<MailSendingContext>.Ignored)).MustHaveHappened();
        }
        public async void DeliverAsynchronouslyNotifiesWhenMailWasSent()
        {
            var sender = A.Fake<IMailSender>();
            var interceptor = A.Fake<IMailInterceptor>();
            var helper = new DeliveryHelper(sender, interceptor);
            var mail = new SmtpMailAttributes();

            await helper.DeliverAsync(mail);

            A.CallTo(() => interceptor.OnMailSent(A<IMailAttributes>.Ignored)).MustHaveHappened();
        }
        public void MultipartMessagesShouldRenderBothViews()
        {
            var mockSender = A.Fake<IMailSender>();
            var attribute = new SmtpMailAttributes();
            var mailer = new TestMailerBase(attribute, mockSender);

            var email = mailer.Email("MultipartNoModel");
            var textBody = new StreamReader(email.Mail.AlternateViews[0].ContentStream).ReadToEnd().Trim();
            var htmlBody = new StreamReader(email.Mail.AlternateViews[1].ContentStream).ReadToEnd().Trim();

            Assert.AreEqual("Testing multipart.", textBody);
            Assert.AreEqual("<p>Testing multipart.</p>", htmlBody);
        }
        public void PassingAModelShouldWork()
        {
            var mockSender = A.Fake<IMailSender>();
            var attribute = new SmtpMailAttributes();
            var mailer = new TestMailerBase(attribute, mockSender);
            var model = new TestModel
            {
                Name = "Foo"
            };

            var email = mailer.Email("TextViewWithModel", model);
            var body = new StreamReader(email.Mail.AlternateViews[0].ContentStream).ReadToEnd().Trim();

            Assert.AreEqual("Your name is:  Foo", body);
        }
        public void GeneratePropsectiveEmailMessageShouldSetCorrectMessageProperties()
        {
            var mailer = new SmtpMailAttributes();
            mailer.To.Add(new MailAddress("*****@*****.**"));
            mailer.From = new MailAddress("*****@*****.**");
            mailer.Subject = "test subject";
            mailer.Cc.Add(new MailAddress("*****@*****.**"));
            mailer.Bcc.Add(new MailAddress("*****@*****.**"));
            mailer.ReplyTo.Add(new MailAddress("*****@*****.**"));
            mailer.Headers.Add("X-No-Spam", "True");

            byte[] logoAttachmentBytes =
                File.ReadAllBytes(Path.Combine(Assembly.GetExecutingAssembly().FullName, "..", "..", "..", "SampleData",
                    "logo.png"));
            mailer.Attachments["logo.png"] = logoAttachmentBytes;

            mailer.Attachments.Inline["logo-inline.png"] = logoAttachmentBytes;

            MailMessage result = mailer.GenerateProspectiveMailMessage();
            Attachment attachment = result.Attachments[0];
            Attachment inlineAttachment = result.Attachments[1];
            var attachmentBytes = new byte[attachment.ContentStream.Length];
            var inlineAttachmentBytes = new byte[inlineAttachment.ContentStream.Length];
            attachment.ContentStream.Read(attachmentBytes, 0, Convert.ToInt32(attachment.ContentStream.Length));
            inlineAttachment.ContentStream.Read(inlineAttachmentBytes, 0,
                Convert.ToInt32(inlineAttachment.ContentStream.Length));

            Assert.AreEqual("*****@*****.**", result.To[0].Address);
            Assert.AreEqual("*****@*****.**", result.From.Address);
            Assert.AreEqual("test subject", result.Subject);
            Assert.AreEqual("*****@*****.**", result.CC[0].Address);
            Assert.AreEqual("*****@*****.**", result.Bcc[0].Address);
            Assert.AreEqual("*****@*****.**", result.ReplyToList[0].Address);
            Assert.AreEqual("True", result.Headers["X-No-Spam"]);
            Assert.AreEqual("logo.png", attachment.ContentId);
            Assert.AreEqual("logo-inline.png", inlineAttachment.ContentId);
            Assert.AreEqual("image/png", attachment.ContentType.MediaType);
            Assert.AreEqual("multipart/related", inlineAttachment.ContentType.MediaType);
            Assert.True(inlineAttachment.ContentDisposition.Inline);


            Assert.True(attachmentBytes.SequenceEqual(logoAttachmentBytes));
            Assert.True(inlineAttachmentBytes.SequenceEqual(logoAttachmentBytes));
        }
        public void RazorViewWithNoModelShouldRenderProperly()
        {
            var mockSender = A.Fake<IMailSender>();
            var attribute = new SmtpMailAttributes();
            var mailer = new TestMailerBase(attribute, mockSender);

            var email = mailer.Email("TextViewNoModel");
            var body = new StreamReader(email.Mail.AlternateViews[0].ContentStream).ReadToEnd().Trim();

            Assert.AreEqual("This is a test", body);
        }
        public void WhiteSpaceShouldBeTrimmedWhenRequired()
        {
            var mockSender = A.Fake<IMailSender>();
            var attribute = new SmtpMailAttributes();
            var mailer = new TestMailerBase(attribute, mockSender);

            var email = mailer.Email("WhitespaceTrimTest", trimBody: true);
            var body = new StreamReader(email.Mail.AlternateViews[0].ContentStream).ReadToEnd();

            Assert.AreEqual("This thing has leading and trailing whitespace.", body);
        }
        public void WhiteSpaceShouldBeIncludedWhenRequired()
        {
            var mockSender = A.Fake<IMailSender>();
            var attribute = new SmtpMailAttributes();
            var mailer = new TestMailerBase(attribute, mockSender);

            var email = mailer.Email("WhitespaceTrimTest", false);
            var body = new StreamReader(email.Mail.AlternateViews[0].ContentStream).ReadToEnd();

            Assert.True(body.StartsWith(Environment.NewLine));
            Assert.True(body.EndsWith(Environment.NewLine));
        }
        public void PassingAMailSenderShouldWork()
        {
            var mockSender = A.Fake<IMailSender>();
            var attributes = new SmtpMailAttributes();
            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new TextViewEngine());

            var mailer = new TestMailerBase(attributes, mockSender);
            mailer.HttpContextBase = MvcHelper.GetHttpContext("/app/", null, null);
            mailer.MailAttributes.From = new MailAddress("*****@*****.**");
            EmailResult result = mailer.Email("TestView");

            Assert.AreSame(mockSender, mailer.MailSender);
            Assert.AreSame(mockSender, result.MailSender);
        }
        public async void DeliveryAsynchronouslySendsMessage()
        {
            var sender = A.Fake<IMailSender>();
            var interceptor = A.Fake<IMailInterceptor>();
            var helper = new DeliveryHelper(sender, interceptor);
            var mail = new SmtpMailAttributes();

            await helper.DeliverAsync(mail);

            A.CallTo(() => sender.SendAsync(mail)).MustHaveHappened();
        }