Beispiel #1
0
        public void SendsAllNotificationsTheFirstTime() {
            DeleteDataFile();

            var expectedResults = new List<MailMessage>();
            foreach (var m in messagesToSend) {
                expectedResults.Add(StandardMailMessage(m, m.Substring(0, m.IndexOf(" "))));
            }

            using (ShimsContext.Create()) {
                var notificationMails = new List<MailMessage>();

                ShimSmtpClient.Constructor = @this => {
                    var shim = new ShimSmtpClient(@this);
                    shim.SendMailMessage = e => {
                        notificationMails.Add(e);
                    };
                };

                using (var n = new Notifier("mail.test.com")) {
                    n.Sender = emailSender;
                    n.NotificationHistoryFile = dataFilename;
                    n.DaysToWait = 7;

                    n.AddNotification(notificationName, notificationSubjectPrefix, emailRecipient);

                    foreach (var m in messagesToSend) {
                        n.SendNotification(notificationName, m, m.Substring(0, m.IndexOf(" ")));
                    }
                }

                CollectionAssert.AreEqual(expectedResults, notificationMails, new Comparers.MailMessageComparer(), "Notification emails sent do not match the expected result.");
            }
        }
        public void SendsEmailMessagesForAllLogEntries() {
            var logMessages = new List<string>();

            using (ShimsContext.Create()) {
                // make DateTime.Now deterministic based on the number of log messages that have been written.
                ShimDateTime.NowGet = () => {
                    return DeterministicDateTime(logMessages.Count);
                };

                ShimSmtpClient.Constructor = @this => {
                    var shim = new ShimSmtpClient(@this);
                    shim.SendMailMessage = e => {
                        logMessages.Add(e.Body);
                    };
                };

                using (var logWriter = new EmailLogger("test.server", LogLevel.All) {
                    Sender = new MailAddress("*****@*****.**"),
                    Asynchronous = false
                }) {
                    logWriter.AddRecipient("*****@*****.**");

                    logWriter.Write("the quick brown fox jumped over the lazy dog.", LogLevel.All);
                    logWriter.Write("Who are you people? Torchwood.", LogLevel.Warn);

                    var expectedResult = new List<string>() {
                        // log line for logLevel.All output
                        $"{ DeterministicDateTime(0).ToString(logWriter.TimestampFormat) }  the quick brown fox jumped over the lazy dog.",
                        $"{ DeterministicDateTime(1).ToString(logWriter.TimestampFormat) } [WARN] Who are you people? Torchwood.",
                    };

                    CollectionAssert.AreEqual(logMessages, expectedResult, "Log messages do not match.");
                }
            }
        }
        public void MessagesAreNotSentAtTheWrongLoggingLevel() {
            var logMessages = new List<string>();

            using (ShimsContext.Create()) {
                // make DateTime.Now deterministic based on the number of log messages that have been written.
                ShimDateTime.NowGet = () => {
                    return DeterministicDateTime(logMessages.Count);
                };

                ShimSmtpClient.Constructor = @this => {
                    var shim = new ShimSmtpClient(@this);
                    shim.SendMailMessage = e => {
                        logMessages.Add(e.Body);
                    };
                };

                var messagesToSend = new List<KeyValuePair<LogLevel, string>>() {
                    new KeyValuePair<LogLevel, string>(LogLevel.Debug, "this is a debug message"),
                    new KeyValuePair<LogLevel, string>(LogLevel.Fatal, "this is a fatal message"),
                    new KeyValuePair<LogLevel, string>(LogLevel.Info, "this is an info message"),
                    new KeyValuePair<LogLevel, string>(LogLevel.None, "this is a none message"),
                    new KeyValuePair<LogLevel, string>(LogLevel.Warn, "this is a warn message"),
                    new KeyValuePair<LogLevel, string>(LogLevel.All, "this is an all message")
                };

                foreach (LogLevel lvl in Enum.GetValues(typeof(LogLevel))) {
                    // reset log messages
                    logMessages = new List<string>();

                    using (var logWriter = new EmailLogger("test.server", lvl) {
                        Sender = new MailAddress("*****@*****.**"),
                        Asynchronous = false
                    }) {
                        // write log messages
                        foreach(var m in messagesToSend.OrderBy(x => x.Key)) {
                            logWriter.Write(m.Value, m.Key);
                        }

                        // check log messages
                        var expectedResults = new List<string>();
                        int i = 0;
                        foreach(var m in messagesToSend.OrderBy(x => x.Key)) {
                            if (lvl != LogLevel.None && m.Key != LogLevel.None) {
                                if (lvl == LogLevel.All || m.Key <= lvl) {
                                    expectedResults.Add($"{ DeterministicDateTime(i).ToString(logWriter.TimestampFormat) } { ((m.Key == LogLevel.All) ? string.Empty : $"[{ m.Key.ToString().ToUpper() }]") } { m.Value }");
                                    i++;
                                }
                            }
                        }

                        CollectionAssert.AreEqual(logMessages, expectedResults, $"Failed for log level { lvl.ToString().ToUpper() }.");
                    }
                }
            }
        }
        public void WhenSmtpClientFailsToSendThenErrorStatusReturned()
        {
            using (ShimsContext.Create())
            {
                var emailInfo = new EmailInformation {
                };

                ShimMailAddress.ConstructorStringString =
                    (@this, addr, name) =>
                {
                    new ShimMailAddress(@this);
                };

                int emailSendCalled = 0;
                ShimSmtpClient.Constructor =
                    @this =>
                {
                    var shim = new ShimSmtpClient(@this);
                    shim.SendMailMessage =
                        e =>
                    {
                        ++emailSendCalled;
                        throw new SmtpException("Error sending email.");
                    };
                };

                ShimMailMessage.Constructor =
                    @this =>
                {
                    var msg = new ShimMailMessage(@this);
                    msg.FromSetMailAddress = mailAddr => { };
                    msg.ToGet                = () => new MailAddressCollection();
                    msg.SubjectSetString     = subject => { };
                    msg.BodySetString        = bodyText => { };
                    msg.IsBodyHtmlSetBoolean = isHtml => { };
                };

                var sendEmail = new SendEmail();
                var status    = sendEmail.Send(emailInfo);

                Assert.AreEqual(1, emailSendCalled);

                Assert.IsNotNull(status);
                Assert.AreEqual(false, status.WasSent);
                Assert.AreEqual("Error sending email.", status.ErrorMessage);
            }
        }
        public void SendAsync_WithFiveParameters_ShouldCallSendAsyncWithFiveParametersOfTheWrappedSmtpClient()
        {
            using(ShimsContext.Create())
            {
                bool sendAsyncIsCalled = false;

                ShimSmtpClient shimSmtpClient = new ShimSmtpClient()
                {
                    SendAsyncStringStringStringStringObject = delegate { sendAsyncIsCalled = true; }
                };

                Assert.IsFalse(sendAsyncIsCalled);

                new SmtpClientWrapper(shimSmtpClient.Instance).SendAsync(null, null, null, null, null);
                Assert.IsTrue(sendAsyncIsCalled);
            }
        }
        public void WhenSmtpClientFailsToSendThenErrorStatusReturned()
        {
            using (ShimsContext.Create())
            {
                var emailInfo = new EmailInformation { };

                ShimMailAddress.ConstructorStringString =
                    (@this, addr, name) =>
                    {
                        new ShimMailAddress(@this);
                    };

                int emailSendCalled = 0;
                ShimSmtpClient.Constructor =
                    @this =>
                    {
                        var shim = new ShimSmtpClient(@this);
                        shim.SendMailMessage =
                            e =>
                            {
                                ++emailSendCalled;
                                throw new SmtpException("Error sending email.");
                            };
                    };

                ShimMailMessage.Constructor =
                    @this =>
                    {
                        var msg = new ShimMailMessage(@this);
                        msg.FromSetMailAddress = mailAddr => { };
                        msg.ToGet = () => new MailAddressCollection();
                        msg.SubjectSetString = subject => { };
                        msg.BodySetString = bodyText => { };
                        msg.IsBodyHtmlSetBoolean = isHtml => { };
                    };

                var sendEmail = new SendEmail();
                var status = sendEmail.Send(emailInfo);

                Assert.AreEqual(1, emailSendCalled);

                Assert.IsNotNull(status);
                Assert.AreEqual(false, status.WasSent);
                Assert.AreEqual("Error sending email.", status.ErrorMessage);
            }
        }
        public void SendAsyncCancel_ShouldCallSendAsyncCancelOfTheWrappedSmtpClient()
        {
            using(ShimsContext.Create())
            {
                bool sendAsyncCancelIsCalled = false;

                ShimSmtpClient shimSmtpClient = new ShimSmtpClient()
                {
                    SendAsyncCancel = delegate { sendAsyncCancelIsCalled = true; }
                };

                Assert.IsFalse(sendAsyncCancelIsCalled);

                new SmtpClientWrapper(shimSmtpClient.Instance).SendAsyncCancel();
                Assert.IsTrue(sendAsyncCancelIsCalled);
            }
        }
        public void ClientCertificates_Get_ShouldCallClientCertificatesGetOfTheWrappedSmtpClient()
        {
            using(ShimsContext.Create())
            {
                bool clientCertificatesGetIsCalled = false;
                X509CertificateCollection clientCertificates = new X509CertificateCollection();

                ShimSmtpClient shimSmtpClient = new ShimSmtpClient()
                {
                    ClientCertificatesGet = delegate
                    {
                        clientCertificatesGetIsCalled = true;
                        return clientCertificates;
                    }
                };

                Assert.IsFalse(clientCertificatesGetIsCalled);

                Assert.AreEqual(clientCertificates, new SmtpClientWrapper(shimSmtpClient.Instance).ClientCertificates);
                Assert.IsTrue(clientCertificatesGetIsCalled);
            }
        }
Beispiel #9
0
        public void CanSendDuplicateNotificationsUsingMoreThanOneNotificationType() {
            DeleteDataFile();

            using (ShimsContext.Create()) {
                int currentMinute = 0;
                List<MailMessage> notificationMails = new List<MailMessage>();
                List<MailMessage> expectedResult;

                ShimSmtpClient.Constructor = @this => {
                    var shim = new ShimSmtpClient(@this);
                    shim.SendMailMessage = e => {
                        notificationMails.Add(e);
                    };
                };

                ShimDateTime.NowGet = () => {
                    return new DateTime(2000, 1, 1, 12, currentMinute++, 13);
                };

                expectedResult = new List<MailMessage>() {
                    StandardMailMessage(messagesToSend[0], "dupe"),
                    StandardMailMessage(messagesToSend[0], "dupe")
                };

                using (var n = new Notifier("mail.test.com")) {
                    n.Sender = emailSender;
                    n.NotificationHistoryFile = dataFilename;
                    n.DaysToWait = 7;

                    n.AddNotification("notification1", notificationSubjectPrefix, emailRecipient);
                    n.AddNotification("notification2", notificationSubjectPrefix, emailRecipient);

                    n.SendNotification("notification1", messagesToSend[0], "dupe");
                    n.SendNotification("notification2", messagesToSend[0], "dupe");
                }

                CollectionAssert.AreEqual(expectedResult, notificationMails, new Comparers.MailMessageComparer(), "Notifications sent do not match the expected result.");
            }
        }
Beispiel #10
0
        public void CorrectlyHandlesMultipleNotificationTypes() {
            DeleteDataFile();

            using (ShimsContext.Create()) {
                int currentMinute = 0;
                List<MailMessage> notificationMails = new List<MailMessage>();
                List<MailMessage> expectedResult;

                ShimSmtpClient.Constructor = @this => {
                    var shim = new ShimSmtpClient(@this);
                    shim.SendMailMessage = e => {
                        notificationMails.Add(e);
                    };
                };

                ShimDateTime.NowGet = () => {
                    return new DateTime(2000, 1, 1, 12, currentMinute++, 13);
                };

                expectedResult = new List<MailMessage>() {
                    StandardMailMessage(messagesToSend[0], "n1m1", "*****@*****.**", "notification type 1"),
                    StandardMailMessage(messagesToSend[1], "n2m1", "*****@*****.**", "notification type 2"),
                    StandardMailMessage(messagesToSend[2], "n3m1", "*****@*****.**", "notification type 3"),
                    StandardMailMessage(messagesToSend[3], "n1m2", "*****@*****.**", "notification type 1"),
                    StandardMailMessage(messagesToSend[4], "n2m2", "*****@*****.**", "notification type 2")
                };

                using (var n = new Notifier("mail.test.com")) {
                    n.Sender = emailSender;
                    n.NotificationHistoryFile = dataFilename;
                    n.DaysToWait = 7;

                    n.AddNotification("notification1", "notification type 1", "*****@*****.**");
                    n.AddNotification("notification2", "notification type 2", "*****@*****.**");
                    n.AddNotification("notification3", "notification type 3", "*****@*****.**");

                    n.SendNotification("notification1", messagesToSend[0], "n1m1");
                    n.SendNotification("notification2", messagesToSend[1], "n2m1");
                    n.SendNotification("notification3", messagesToSend[2], "n3m1");
                    n.SendNotification("notification1", messagesToSend[3], "n1m2");
                    n.SendNotification("notification2", messagesToSend[4], "n2m2");
                }

                CollectionAssert.AreEqual(expectedResult, notificationMails, new Comparers.MailMessageComparer(), "Notifications sent do not match the expected result.");
            }
        }
        public void WhenSmtpClientSendRequestedThenMailMessageValuesArePopulatedCorrectly()
        {
            using (ShimsContext.Create())
            {
                var emailInfo =
                    new EmailInformation
                {
                    FromAddress   = "*****@*****.**",
                    FromName      = "From Name",
                    ToAddress     = "*****@*****.**",
                    ToName        = "To Name",
                    Subject       = "Email Subject",
                    MessageText   = "Email Body",
                    IsHtmlMessage = false,
                };

                int mailAddrCalled = 0;
                ShimMailAddress.ConstructorStringString =
                    (@this, addr, name) =>
                {
                    new ShimMailAddress(@this);
                    switch (mailAddrCalled++)
                    {
                    case 0:
                        Assert.AreEqual("*****@*****.**", addr);
                        Assert.AreEqual("From Name", name);
                        break;

                    case 1:
                        Assert.AreEqual("*****@*****.**", addr);
                        Assert.AreEqual("To Name", name);
                        break;
                    }
                };

                ShimSmtpClient.Constructor =
                    @this =>
                {
                    var shim = new ShimSmtpClient(@this);
                    shim.SendMailMessage = e => { };
                };

                int fromCalled    = 0;
                int toCalled      = 0;
                int subjectCalled = 0;
                int bodyCalled    = 0;
                int isHtmlCalled  = 0;
                ShimMailMessage.Constructor =
                    @this =>
                {
                    var msg = new ShimMailMessage(@this);
                    msg.FromSetMailAddress =
                        mailAddr =>
                    {
                        ++fromCalled;
                    };
                    msg.ToGet =
                        () =>
                    {
                        ++toCalled;
                        return(new MailAddressCollection());
                    };
                    msg.SubjectSetString =
                        subject =>
                    {
                        Assert.AreEqual("Email Subject", subject);
                        ++subjectCalled;
                    };
                    msg.BodySetString =
                        bodyText =>
                    {
                        Assert.AreEqual("Email Body", bodyText);
                        ++bodyCalled;
                    };
                    msg.IsBodyHtmlSetBoolean =
                        isHtml =>
                    {
                        Assert.AreEqual(false, isHtml);
                        ++isHtmlCalled;
                    };
                };

                var sendEmail = new SendEmail();
                sendEmail.Send(emailInfo);

                Assert.AreEqual(2, mailAddrCalled);
                Assert.AreEqual(1, fromCalled);
                Assert.AreEqual(1, toCalled);
                Assert.AreEqual(1, subjectCalled);
                Assert.AreEqual(1, bodyCalled);
                Assert.AreEqual(1, isHtmlCalled);
            }
        }
Beispiel #12
0
        public void MessagesAreNeverSuppressedWhenDaysToWaitIsZero() {
            int notificationTimeout = 0;
            DeleteDataFile();

            using (ShimsContext.Create()) {
                int CurrentDay = 0;
                List<MailMessage> notificationMails = new List<MailMessage>();
                List<MailMessage> expectedResult;

                ShimSmtpClient.Constructor = @this => {
                    var shim = new ShimSmtpClient(@this);
                    shim.SendMailMessage = e => {
                        notificationMails.Add(e);
                    };
                };

                ShimDateTime.NowGet = () => {
                    return new DateTime(2000, 1, CurrentDay, 11, 12, 13);
                };

                /* DAY ONE */
                CurrentDay = 1;
                notificationMails = new List<MailMessage>();
                expectedResult = new List<MailMessage>() {
                    StandardMailMessage(messagesToSend[0], "fridge lazer"),
                    StandardMailMessage(messagesToSend[1], "pew pew pew"),
                    StandardMailMessage(messagesToSend[2], "rome antium cumae"),
                    StandardMailMessage(messagesToSend[3], "black mirror")
                };

                using (var n = new Notifier("mail.test.com")) {
                    n.Sender = emailSender;
                    n.NotificationHistoryFile = dataFilename;
                    n.DaysToWait = notificationTimeout;

                    n.AddNotification(notificationName, notificationSubjectPrefix, emailRecipient);

                    n.SendNotification(notificationName, messagesToSend[0], "fridge lazer");
                    n.SendNotification(notificationName, messagesToSend[1], "pew pew pew");
                    n.SendNotification(notificationName, messagesToSend[2], "rome antium cumae");
                    n.SendNotification(notificationName, messagesToSend[3], "black mirror");
                }

                CollectionAssert.AreEqual(expectedResult, notificationMails, new Comparers.MailMessageComparer(), "Notifications sent do not match the expected result for Day 1.");

                /* DAY TWO */
                CurrentDay = 2;
                notificationMails = new List<MailMessage>();
                expectedResult = new List<MailMessage>() {
                    StandardMailMessage(messagesToSend[0], "fridge lazer"),
                    StandardMailMessage(messagesToSend[1], "pew pew pew"),
                    StandardMailMessage(messagesToSend[2], "rome antium cumae"),
                    StandardMailMessage(messagesToSend[3], "black mirror"),
                    StandardMailMessage(messagesToSend[4], "cool ridge"),
                    StandardMailMessage(messagesToSend[5], "cables to go"),
                    StandardMailMessage(messagesToSend[6], "elsa anna olaf"),
                    StandardMailMessage(messagesToSend[7], "product guide")
                };

                using (var n = new Notifier("mail.test.com")) {
                    n.Sender = emailSender;
                    n.NotificationHistoryFile = dataFilename;
                    n.DaysToWait = notificationTimeout;

                    n.AddNotification(notificationName, notificationSubjectPrefix, emailRecipient);

                    n.SendNotification(notificationName, messagesToSend[0], "fridge lazer");
                    n.SendNotification(notificationName, messagesToSend[1], "pew pew pew");
                    n.SendNotification(notificationName, messagesToSend[2], "rome antium cumae");
                    n.SendNotification(notificationName, messagesToSend[3], "black mirror");
                    n.SendNotification(notificationName, messagesToSend[4], "cool ridge");
                    n.SendNotification(notificationName, messagesToSend[5], "cables to go");
                    n.SendNotification(notificationName, messagesToSend[6], "elsa anna olaf");
                    n.SendNotification(notificationName, messagesToSend[7], "product guide");
                }

                CollectionAssert.AreEqual(expectedResult, notificationMails, new Comparers.MailMessageComparer(), "Notifications sent do not match the expected result for Day 2.");

                /* DAY THREE */
                CurrentDay = 3;
                notificationMails = new List<MailMessage>();
                expectedResult = new List<MailMessage>() {
                    StandardMailMessage(messagesToSend[0], "fridge lazer"),
                    StandardMailMessage(messagesToSend[4], "cool ridge"),
                    StandardMailMessage(messagesToSend[5], "cables to go"),
                    StandardMailMessage(messagesToSend[6], "elsa anna olaf"),
                    StandardMailMessage(messagesToSend[7], "product guide")
                };

                using (var n = new Notifier("mail.test.com")) {
                    n.Sender = emailSender;
                    n.NotificationHistoryFile = dataFilename;
                    n.DaysToWait = notificationTimeout;

                    n.AddNotification(notificationName, notificationSubjectPrefix, emailRecipient);

                    n.SendNotification(notificationName, messagesToSend[0], "fridge lazer");
                    n.SendNotification(notificationName, messagesToSend[4], "cool ridge");
                    n.SendNotification(notificationName, messagesToSend[5], "cables to go");
                    n.SendNotification(notificationName, messagesToSend[6], "elsa anna olaf");
                    n.SendNotification(notificationName, messagesToSend[7], "product guide");
                }

                CollectionAssert.AreEqual(expectedResult, notificationMails, new Comparers.MailMessageComparer(), "Notifications sent do not match the expected result for Day 3.");
            }
        }
Beispiel #13
0
        public void SendsNotificationAfterMessageIsNotRepeated() {
            int notificationTimeout = 7;
            DeleteDataFile();

            using (ShimsContext.Create()) {
                int CurrentDay = 0;
                List<MailMessage> notificationMails = new List<MailMessage>();
                List<MailMessage> expectedResult;

                ShimSmtpClient.Constructor = @this => {
                    var shim = new ShimSmtpClient(@this);
                    shim.SendMailMessage = e => {
                        notificationMails.Add(e);
                    };
                };

                ShimDateTime.NowGet = () => {
                    return new DateTime(2000, 1, CurrentDay, 11, 12, 13);
                };

                /* DAY ONE */
                CurrentDay = 1;
                notificationMails = new List<MailMessage>();
                expectedResult = new List<MailMessage>() {
                    StandardMailMessage(messagesToSend[0], "velvet revolver")
                };

                using (var n = new Notifier("mail.test.com")) {
                    n.Sender = emailSender;
                    n.NotificationHistoryFile = dataFilename;
                    n.DaysToWait = notificationTimeout;

                    n.AddNotification(notificationName, notificationSubjectPrefix, emailRecipient);

                    n.SendNotification(notificationName, messagesToSend[0], "velvet revolver");
                }

                CollectionAssert.AreEqual(expectedResult, notificationMails, new Comparers.MailMessageComparer(), "Notifications sent do not match the expected result for Day 1.");

                /* DAY TWO */
                CurrentDay = 2;
                notificationMails = new List<MailMessage>();
                expectedResult = new List<MailMessage>();

                using (var n = new Notifier("mail.test.com")) {
                    n.Sender = emailSender;
                    n.NotificationHistoryFile = dataFilename;
                    n.DaysToWait = notificationTimeout;

                    n.AddNotification(notificationName, notificationSubjectPrefix, emailRecipient);
                }

                CollectionAssert.AreEqual(expectedResult, notificationMails, new Comparers.MailMessageComparer(), "Notifications sent do not match the expected result for Day 2.");

                /* DAY THREE */
                CurrentDay = 3;
                notificationMails = new List<MailMessage>();
                expectedResult = new List<MailMessage>() {
                    StandardMailMessage(messagesToSend[0], "velvet revolver")
                };

                using (var n = new Notifier("mail.test.com")) {
                    n.Sender = emailSender;
                    n.NotificationHistoryFile = dataFilename;
                    n.DaysToWait = notificationTimeout;

                    n.AddNotification(notificationName, notificationSubjectPrefix, emailRecipient);

                    n.SendNotification(notificationName, messagesToSend[0], "velvet revolver");
                }

                CollectionAssert.AreEqual(expectedResult, notificationMails, new Comparers.MailMessageComparer(), "Notifications sent do not match the expected result for Day 3.");
            }
        }
        public void WhenSmtpClientSendRequestedThenMailMessageValuesArePopulatedCorrectly()
        {
            using (ShimsContext.Create())
            {
                var emailInfo =
                    new EmailInformation
                    {
                        FromAddress = "*****@*****.**",
                        FromName = "From Name",
                        ToAddress = "*****@*****.**",
                        ToName = "To Name",
                        Subject = "Email Subject",
                        MessageText = "Email Body",
                        IsHtmlMessage = false,
                    };

                int mailAddrCalled = 0;
                ShimMailAddress.ConstructorStringString =
                    (@this, addr, name) =>
                    {
                        new ShimMailAddress(@this);
                        switch (mailAddrCalled++)
                        {
                            case 0:
                                Assert.AreEqual("*****@*****.**", addr);
                                Assert.AreEqual("From Name", name);
                                break;
                            case 1:
                                Assert.AreEqual("*****@*****.**", addr);
                                Assert.AreEqual("To Name", name);
                                break;
                        }
                    };

                ShimSmtpClient.Constructor =
                    @this =>
                    {
                        var shim = new ShimSmtpClient(@this);
                        shim.SendMailMessage = e => { };
                    };

                int fromCalled = 0;
                int toCalled = 0;
                int subjectCalled = 0;
                int bodyCalled = 0;
                int isHtmlCalled = 0;
                ShimMailMessage.Constructor =
                    @this =>
                    {
                        var msg = new ShimMailMessage(@this);
                        msg.FromSetMailAddress =
                            mailAddr =>
                            {
                                ++fromCalled;
                            };
                        msg.ToGet =
                            () =>
                            {
                                ++toCalled;
                                return new MailAddressCollection();
                            };
                        msg.SubjectSetString =
                            subject =>
                            {
                                Assert.AreEqual("Email Subject", subject);
                                ++subjectCalled;
                            };
                        msg.BodySetString =
                            bodyText =>
                            {
                                Assert.AreEqual("Email Body", bodyText);
                                ++bodyCalled;
                            };
                        msg.IsBodyHtmlSetBoolean =
                            isHtml =>
                            {
                                Assert.AreEqual(false, isHtml);
                                ++isHtmlCalled;
                            };
                    };

                var sendEmail = new SendEmail();
                sendEmail.Send(emailInfo);

                Assert.AreEqual(2, mailAddrCalled);
                Assert.AreEqual(1, fromCalled);
                Assert.AreEqual(1, toCalled);
                Assert.AreEqual(1, subjectCalled);
                Assert.AreEqual(1, bodyCalled);
                Assert.AreEqual(1, isHtmlCalled);
            }
        }
        public void Send_WithOneParameter_ShouldCallSendWithOneParameterOfTheWrappedSmtpClient()
        {
            using(ShimsContext.Create())
            {
                bool sendIsCalled = false;

                ShimSmtpClient shimSmtpClient = new ShimSmtpClient()
                {
                    SendMailMessage = delegate { sendIsCalled = true; }
                };

                Assert.IsFalse(sendIsCalled);

                new SmtpClientWrapper(shimSmtpClient.Instance).Send(null);
                Assert.IsTrue(sendIsCalled);
            }
        }
Beispiel #16
0
        public void EmailSubjectsAreCorrectlyConfiguredEvenWithNoPrefix() {
            DeleteDataFile();

            var notificationMails = new List<MailMessage>();

            using (ShimsContext.Create()) {
                ShimSmtpClient.Constructor = @this => {
                    var shim = new ShimSmtpClient(@this);
                    shim.SendMailMessage = e => {
                        notificationMails.Add(e);
                    };
                };

                var expectedResult = messagesToSend.OrderBy(m => m).ToList();

                using (var n = new Notifier("mail.test.com")) {
                    n.Sender = emailSender;
                    n.NotificationHistoryFile = dataFilename;

                    n.AddNotification(notificationName, string.Empty, emailRecipient);

                    foreach (var m in messagesToSend.OrderBy(m => m)) {
                        n.SendNotification(notificationName, m, m);
                    }

                    CollectionAssert.AreEqual(expectedResult, notificationMails.Select(m => m.Subject).ToList());
                }
            }
        }
        public void ServicePoint_Get_ShouldCallServicePointGetOfTheWrappedSmtpClient()
        {
            using(ShimsContext.Create())
            {
                bool servicePointGetIsCalled = false;
                ServicePoint servicePoint = new ShimServicePoint().Instance;

                ShimSmtpClient shimSmtpClient = new ShimSmtpClient()
                {
                    ServicePointGet = delegate
                    {
                        servicePointGetIsCalled = true;
                        return servicePoint;
                    }
                };

                Assert.IsFalse(servicePointGetIsCalled);

                Assert.AreEqual(servicePoint, new SmtpClientWrapper(shimSmtpClient.Instance).ServicePoint);
                Assert.IsTrue(servicePointGetIsCalled);
            }
        }
Beispiel #18
0
        public void CorrectlySuppressesMessages() {
            int notificationTimeout = 7;
            DeleteDataFile();

            using (ShimsContext.Create()) {
                int CurrentDay = 0;
                List<MailMessage> notificationMails = new List<MailMessage>();
                List<MailMessage> expectedResult;

                ShimSmtpClient.Constructor = @this => {
                    var shim = new ShimSmtpClient(@this);
                    shim.SendMailMessage = e => {
                        notificationMails.Add(e);
                    };
                };

                ShimDateTime.NowGet = () => {
                    return new DateTime(2000, 1, CurrentDay, 11, 12, 13);
                };

                /* DAY ONE */
                CurrentDay = 1;
                notificationMails = new List<MailMessage>();
                expectedResult = new List<MailMessage>() {
                    StandardMailMessage(messagesToSend[0], "Day 1.")
                };

                using (var n = new Notifier("mail.test.com")) {
                    n.Sender = emailSender;
                    n.NotificationHistoryFile = dataFilename;
                    n.DaysToWait = notificationTimeout;

                    n.AddNotification(notificationName, notificationSubjectPrefix, emailRecipient);

                    n.SendNotification(notificationName, messagesToSend[0], "Day 1.");
                }

                CollectionAssert.AreEqual(expectedResult, notificationMails, new Comparers.MailMessageComparer(), "Notifications sent do not match the expected result for Day 1.");

                /* DAY TWO */
                CurrentDay = 2;
                notificationMails = new List<MailMessage>();
                expectedResult = new List<MailMessage>() {
                    StandardMailMessage(messagesToSend[1], "Day 2.")
                };

                using (var n = new Notifier("mail.test.com")) {
                    n.Sender = emailSender;
                    n.NotificationHistoryFile = dataFilename;
                    n.DaysToWait = notificationTimeout;

                    n.AddNotification(notificationName, notificationSubjectPrefix, emailRecipient);

                    // resend the previous messages. they should not be in the actual result.
                    n.SendNotification(notificationName, messagesToSend[0], "Day 1.");
                    // send a new notification that should be in the actual result.
                    n.SendNotification(notificationName, messagesToSend[1], "Day 2.");
                }

                CollectionAssert.AreEqual(expectedResult, notificationMails, new Comparers.MailMessageComparer(), "Notifications sent do not match the expected result for Day 2.");

                /* DAY THREE */
                CurrentDay = 3;
                notificationMails = new List<MailMessage>();

                expectedResult = new List<MailMessage>() {
                    StandardMailMessage(messagesToSend[2], "Day 3.")
                };

                using (var n = new Notifier("mail.test.com")) {
                    n.Sender = emailSender;
                    n.NotificationHistoryFile = dataFilename;
                    n.DaysToWait = notificationTimeout;

                    n.AddNotification(notificationName, notificationSubjectPrefix, emailRecipient);

                    // resend the previous messages. they should not be in the actual result.
                    n.SendNotification(notificationName, messagesToSend[0], "Day 1.");
                    n.SendNotification(notificationName, messagesToSend[1], "Day 2.");
                    // send a new notification that should be in the actual result.
                    n.SendNotification(notificationName, messagesToSend[2], "Day 3.");
                }

                CollectionAssert.AreEqual(expectedResult, notificationMails, new Comparers.MailMessageComparer(), "Notifications sent do not match the expected result for Day 3.");

                /* DAY FOUR - no new messages */
                notificationMails = new List<MailMessage>();
                CurrentDay = 4;
                expectedResult = new List<MailMessage>() { };

                using (var n = new Notifier("mail.test.com")) {
                    n.Sender = emailSender;
                    n.NotificationHistoryFile = dataFilename;
                    n.DaysToWait = notificationTimeout;

                    n.AddNotification(notificationName, notificationSubjectPrefix, emailRecipient);

                    // resend the previous messages. they should not be in the actual result.
                    n.SendNotification(notificationName, messagesToSend[0], "Day 1.");
                    n.SendNotification(notificationName, messagesToSend[1], "Day 2.");
                    n.SendNotification(notificationName, messagesToSend[2], "Day 3.");
                    // send a new notification that should be in the actual result.
                    // *** there are no new messages sent in this test ***
                }

                CollectionAssert.AreEqual(expectedResult, notificationMails, new Comparers.MailMessageComparer(), "Notifications sent do not match the expected result for Day 4.");
            }
        }
 public void SendCompleted_Add_ShouldCallSendCompletedAddOfTheWrappedSmtpClient()
 {
     using(ShimsContext.Create())
     {
         bool invoked = false;
         ShimSmtpClient shimSmtpClient = new ShimSmtpClient()
         {
             SendCompletedAddSendCompletedEventHandler = delegate { invoked = true; }
         };
         new SmtpClientWrapper(shimSmtpClient.Instance).SendCompleted += ((sender, args) => { });
         Assert.IsTrue(invoked);
     }
 }