Example #1
0
 public static void AssertBodyChecks(this MockEmail email, string rootPath, bool checkLineLengths, string[] longLines)
 {
     Assert.IsTrue(email.IsBodyHtml);
     AssertBodyIsAsciiEncoding(email);
     AssertIsXml(email.Body);
     AssertTinyUrls(email.Body, rootPath);
     AssertLineLengths(email, checkLineLengths, longLines);
 }
Example #2
0
        private static void AssertAddresses(MockEmail email, EmailRecipient from, EmailRecipient ret, EmailRecipient[] to, bool assertCc, EmailRecipient[] cc, bool assertBcc, EmailRecipient bcc)
        {
            // From. Address should actually be the return address and the ReplyTo field is set to what it is the from address.

            Assert.AreEqual(ret.Address, email.From.Address, "'From' email address differs");
            Assert.AreEqual(from.DisplayName, email.From.DisplayName, "'From' display name differs");

            Assert.AreEqual(from.Address, email.ReplyTo.Address, "'ReplyTo' email address differs");
            Assert.AreEqual(from.DisplayName, email.ReplyTo.DisplayName, "'ReplyTo' display name differs");

            // To.

            Assert.AreEqual(to.Length, email.To.Count);
            for (int index = 0; index < to.Length; ++index)
            {
                Assert.AreEqual(to[index].Address, email.To[index].Address, "'To' email address differs");
                Assert.AreEqual(to[index].DisplayName, email.To[index].DisplayName, "'To' display name differs");
            }

            // Cc.

            if (assertCc)
            {
                if (cc == null)
                {
                    Assert.AreEqual(0, email.Cc.Count);
                }
                else
                {
                    Assert.AreEqual(cc.Length, email.Cc.Count);
                    for (var index = 0; index < cc.Length; ++index)
                    {
                        Assert.AreEqual(cc[index].Address, email.Cc[index].Address, "'Cc' email address differs");
                        Assert.AreEqual(cc[index].DisplayName, email.Cc[index].DisplayName, "'Cc' display name differs");
                    }
                }
            }

            // Bcc.

            if (assertBcc)
            {
                if (bcc == null)
                {
                    Assert.AreEqual(0, email.Bcc.Count);
                }
                else
                {
                    Assert.AreEqual(1, email.Bcc.Count);
                    Assert.AreEqual(bcc.Address, email.Bcc[0].Address, "'Bcc' email address differs");
                    Assert.AreEqual(bcc.DisplayName, email.Bcc[0].DisplayName, "'Bcc' display name differs");
                }
            }

            // Sender.

            Assert.IsNull(email.Sender);
        }
Example #3
0
        private static void AssertLineLengths(MockEmail email, bool checkLineLengths, IEnumerable <string> longLines)
        {
            string line;
            var    reader = new StringReader(email.Body);

            while ((line = reader.ReadLine()) != null)
            {
                line = line.Trim().Replace("&amp;", "&");

                // Ignore link, div and img.

                if (!line.StartsWith("<link href=\"") && !line.StartsWith("<div ") && !line.StartsWith("<img src=\""))
                {
                    if (line.StartsWith("<a "))
                    {
                        // Always check that the href does not exceed the maximum length.

                        int pos = line.IndexOf("href=\"");
                        if (pos != -1)
                        {
                            line = line.Substring(pos + "href=\"".Length);
                            pos  = line.IndexOf("\"");
                            if (pos != -1)
                            {
                                line = line.Substring(0, pos);
                                Assert.IsTrue(line.Length < MaxLineLength + 1, "The '" + line + "' href exceeds the maximum length " + MaxLineLength + ".");
                            }
                        }
                    }
                    else
                    {
                        if (checkLineLengths)
                        {
                            // Check that the line is known to be long.

                            bool ok = false;
                            if (longLines != null)
                            {
                                foreach (string longLine in longLines)
                                {
                                    if (line.StartsWith(longLine))
                                    {
                                        ok = true;
                                        break;
                                    }
                                }
                            }

                            if (!ok)
                            {
                                Assert.IsTrue(line.Length < MaxLineLength + 1, "The '" + line + "' line exceeds the maximum length " + MaxLineLength + ".");
                            }
                        }
                    }
                }
            }
        }
Example #4
0
 public static void AssertAttachments(this MockEmail email, params MockEmailAttachment[] expectedAttachments)
 {
     Assert.AreEqual(expectedAttachments.Length, email.Attachments.Count);
     for (var index = 0; index < expectedAttachments.Length; ++index)
     {
         var expectedAttachment = expectedAttachments[index];
         var attachment         = (from a in email.Attachments where a.Name == expectedAttachment.Name select a).Single();
         Assert.AreEqual(expectedAttachment.MediaType, attachment.MediaType);
     }
 }
Example #5
0
        public static MockEmailView GetView(this MockEmail email, string mimeType)
        {
            var view = email.AlternateViews.SingleOrDefault(v => v.MediaType == mimeType);

            if (view == null)
            {
                Assert.Fail("'{0}' view is not found in the email.", mimeType);
            }
            return(view);
        }
Example #6
0
        public static MockEmail[] AssertEmailsSent(this IMockEmailServer emailServer, int count)
        {
            var messages = emailServer.GetEmails();

            Assert.AreEqual(count, messages.Count);
            var emails = new MockEmail[count];

            messages.CopyTo(emails, 0);
            emailServer.ClearEmails();
            return((from e in emails select e).ToArray());
        }
Example #7
0
        public static void AssertViewChecks(this MockEmail email, string mimeType, string rootPath, bool checkLineLengths, string[] longLines)
        {
            var view = GetView(email, mimeType);

            Assert.IsNotNull(view);

            if (mimeType == MediaTypeNames.Text.Html)
            {
                AssertIsXml(view.Body);
            }

            AssertTinyUrls(view.Body, rootPath);
            AssertLineLengths(email, checkLineLengths, longLines);
        }
Example #8
0
        void IMockEmailServer.Send(MockEmail email)
        {
            // Look for bad email addresses.

            foreach (var to in email.To)
            {
                foreach (var badEmailAddress in BadEmailAddresses)
                {
                    if (to.Address == badEmailAddress)
                    {
                        throw new ApplicationException("Cannot send email to '" + badEmailAddress + "'.");
                    }
                }
            }

            Emails.Add(email);
        }
Example #9
0
        private static void AssertBodyIsAsciiEncoding(MockEmail email)
        {
            if (email.BodyEncoding != null && !(email.BodyEncoding is UTF8Encoding))
            {
                // Try to find the special character causing this problem.

                var message = "The email body encoding is not ASCII - did you paste some special character into the email template?";

                var charIndex = GetFirstNonAsciiCharIndex(email.Body);
                if (charIndex != -1)
                {
                    message += string.Format("\r\nThe suspected character is '{0}' (0x{1:x}) at index {2}: \"{3}\"",
                                             email.Body[charIndex], (int)email.Body[charIndex], charIndex,
                                             TextUtil.TruncateForDisplay(email.Body.Substring(charIndex, 101), 100));
                }

                Assert.Fail(message);
            }
        }
Example #10
0
 public static void AssertAddresses(this MockEmail email, ICommunicationUser from, EmailRecipient ret, string toAddress)
 {
     email.AssertAddresses(GetEmailRecipient(from), ret, new EmailRecipient(toAddress));
 }
Example #11
0
 public static void AssertAddresses(this MockEmail email, ICommunicationUser from, EmailRecipient ret, EmailRecipient to)
 {
     email.AssertAddresses(GetEmailRecipient(from), ret, to, null, null);
 }
Example #12
0
 public static void AssertAddresses(this MockEmail email, EmailRecipient from, EmailRecipient ret, ICommunicationUser to)
 {
     email.AssertAddresses(from, ret, GetEmailRecipient(to));
 }
Example #13
0
 public static void AssertViewCount(this MockEmail email, int count)
 {
     Assert.AreEqual(count, email.AlternateViews.Count);
 }
Example #14
0
 public static void AssertHtmlViewChecks(this MockEmail email)
 {
     email.AssertViewChecks(MediaTypeNames.Text.Html, null, true, null);
 }
Example #15
0
 void IMockEmailService.Send(MockEmail email)
 {
     EmailServer.Send(email);
 }
Example #16
0
 public static void AssertAddresses(this MockEmail email, EmailRecipient from, EmailRecipient ret, EmailRecipient to, EmailRecipient[] cc, EmailRecipient bcc)
 {
     AssertAddresses(email, from, ret, new[] { to }, true, cc, true, bcc);
 }
Example #17
0
 public static void AssertAttachments(this MockEmail email, int count)
 {
     Assert.AreEqual(count, email.Attachments.Count);
 }
Example #18
0
 public static void AssertViewDoesNotContain(this MockEmail email, string mimeType, string text)
 {
     AssertDoesNotContain(GetView(email, mimeType).Body, text);
 }
Example #19
0
 public static void AssertNoAttachments(this MockEmail email)
 {
     Assert.AreEqual(0, email.Attachments.Count);
 }
Example #20
0
 public static MockEmailView GetPlainTextView(this MockEmail email)
 {
     return(email.GetView(MediaTypeNames.Text.Plain));
 }
Example #21
0
 public static void AssertHtmlViewDoesNotContain(this MockEmail email, string text)
 {
     email.AssertViewDoesNotContain(MediaTypeNames.Text.Html, text);
 }
Example #22
0
 public static void AssertHtmlView(this MockEmail email, string body)
 {
     email.AssertView(MediaTypeNames.Text.Html, body);
 }
Example #23
0
 public static void AssertHtmlViewChecks(this MockEmail email, string rootPath, bool checkLineLengths, string[] longLines)
 {
     email.AssertViewChecks(MediaTypeNames.Text.Html, rootPath, checkLineLengths, longLines);
 }
Example #24
0
 public static void AssertAddresses(this MockEmail email, EmailRecipient from, EmailRecipient ret, EmailRecipient to)
 {
     AssertAddresses(email, from, ret, new[] { to }, false, null, false, null);
 }
Example #25
0
 public static void AssertAddresses(this MockEmail email, RegisteredUser from, EmailRecipient ret, EmailRecipient to, EmailRecipient[] cc, EmailRecipient bcc)
 {
     AssertAddresses(email, GetEmailRecipient(from), ret, new[] { to }, true, cc, true, bcc);
 }
Example #26
0
 public static void AssertView(this MockEmail email, string mimeType, string body)
 {
     Assert.AreEqual(body, GetView(email, mimeType).Body);
 }
Example #27
0
 public static void AssertAddresses(this MockEmail email, EmailRecipient from, EmailRecipient ret, ICommunicationUser to, ICommunicationUser[] cc, ICommunicationUser bcc)
 {
     AssertAddresses(email, from, ret, GetEmailRecipient(to), cc == null ? null : (from c in cc select GetEmailRecipient(c)).ToArray(), bcc == null ? null : GetEmailRecipient(bcc));
 }
Example #28
0
 public static void AssertViewChecks(this MockEmail email, string mimeType)
 {
     email.AssertViewChecks(mimeType, null, true, null);
 }
Example #29
0
 public static void AssertAttachment(this MockEmail email, string fileName, string mediaType)
 {
     email.AssertAttachments(new[] { new MockEmailAttachment {
                                         Name = fileName, MediaType = mediaType
                                     } });
 }
Example #30
0
 public static MockEmailView GetHtmlView(this MockEmail email)
 {
     return(email.GetView(MediaTypeNames.Text.Html));
 }