public void SetupCachesTemplates() { // --- Arrange var sendertask = new EmailSenderTask(); // --- Act var templ1 = EmailSenderTask.TemplateDefinitions; sendertask.Setup(new EmailTaskExecutionContext()); var templ2 = EmailSenderTask.TemplateDefinitions; sendertask.Setup(new EmailTaskExecutionContext()); var templ3 = EmailSenderTask.TemplateDefinitions; // --- Assert templ1.ShouldBeNull(); templ2.ShouldNotBeNull(); templ3.ShouldNotBeNull(); templ2.ShouldBeSameAs(templ3); }
public void SetupReadsTemplates() { // --- Arrange var sendertask = new EmailSenderTask(); // --- Act sendertask.Setup(new EmailTaskExecutionContext()); // --- Assert EmailSenderTask.TemplateDefinitions.ShouldHaveCountOf(6); }
public void RetryWorksAsExpected() { // --- Arrange var service = ServiceManager.GetService <IEmailComposerService>(); var sendertask = new EmailSenderTask(); ServiceManager.RemoveService(typeof(IEmailSender)); ServiceManager.Register <IEmailSender, EmailSenderWithRetry>(); for (var i = 0; i < 5; i++) { var request = new EmailDefinitionDto { FromAddr = "*****@*****.**", FromName = "You", ToAddr = "*****@*****.**", TemplateType = "MeetingProposed", Parameters = new Dictionary <string, string> { { "FullName", "MyName" } }, SendAfterUtc = null }; service.SendEmail(request); } // --- Act FakeConfigurationReader.SetConfigurationValue("SmtpConfig", "EmailCount", "4"); FakeConfigurationReader.SetConfigurationValue("SmtpConfig", "MaxRetry", "3"); EmailSenderWithRetry.ForceFailure = true; sendertask.Setup(new EmailTaskExecutionContext()); sendertask.Run(); sendertask.Run(); EmailSenderWithRetry.ForceFailure = false; sendertask.Run(); // --- Assert var db = new SqlDatabase(DB_CONN); var emailsToSend = db.Fetch <EmailToSendRecord>(); emailsToSend.ShouldHaveCountOf(1); var emailsSent = db.Fetch <EmailSentRecord>(); emailsSent.ShouldHaveCountOf(4); var emailsFailed = db.Fetch <EmailFailedRecord>(); emailsFailed.ShouldHaveCountOf(0); foreach (var email in emailsSent) { email.Subject.ShouldEqual("Evaluation meeting proposed"); email.Message.ShouldContain("MyName"); email.Message.ShouldNotContain("FullName"); } }
public void EmailsFailWithUnknownTemplate() { // --- Arrange var service = ServiceManager.GetService <IEmailComposerService>(); var sendertask = new EmailSenderTask(); ServiceManager.RemoveService(typeof(IEmailSender)); ServiceManager.Register <IEmailSender, AlwaysWorkingEmailSender>(); for (var i = 0; i < 5; i++) { var request = new EmailDefinitionDto { FromAddr = "*****@*****.**", FromName = "You", ToAddr = "*****@*****.**", TemplateType = "Unknown", Parameters = new Dictionary <string, string> { { "FullName", "MyName" } }, SendAfterUtc = null }; service.SendEmail(request); } // --- Act FakeConfigurationReader.SetConfigurationValue("SmtpConfig", "EmailCount", "4"); FakeConfigurationReader.SetConfigurationValue("SmtpConfig", "MaxRetry", "0"); sendertask.Setup(new EmailTaskExecutionContext()); sendertask.Run(); // --- Assert var db = new SqlDatabase(DB_CONN); var emailsToSend = db.Fetch <EmailToSendRecord>(); emailsToSend.ShouldHaveCountOf(1); var emailsSent = db.Fetch <EmailSentRecord>(); emailsSent.ShouldHaveCountOf(0); var emailsFailed = db.Fetch <EmailFailedRecord>(); emailsFailed.ShouldHaveCountOf(4); foreach (var email in emailsFailed) { email.Subject.ShouldBeNull(); email.Message.ShouldBeNull(); email.LastError.ShouldContain("not found"); } }
public void EmailsFailWithProcessingException() { // --- Arrange var db = new SqlDatabase(DB_CONN); var sendertask = new EmailSenderTask(); ServiceManager.RemoveService(typeof(IEmailSender)); ServiceManager.Register <IEmailSender, AlwaysWorkingEmailSender>(); for (var i = 0; i < 5; i++) { db.Insert(new EmailToSendRecord { FromAddr = "*****@*****.**", FromName = "Me", ToAddr = "*****@*****.**", TemplateType = "MeetingProposed", Parameters = "<dummy><Parameter/></dummy>" }); } // --- Act FakeConfigurationReader.SetConfigurationValue("SmtpConfig", "EmailCount", "4"); FakeConfigurationReader.SetConfigurationValue("SmtpConfig", "MaxRetry", "0"); sendertask.Setup(new EmailTaskExecutionContext()); sendertask.Run(); // --- Assert var emailsToSend = db.Fetch <EmailToSendRecord>(); emailsToSend.ShouldHaveCountOf(1); //1 var emailsSent = db.Fetch <EmailSentRecord>(); emailsSent.ShouldHaveCountOf(0); var emailsFailed = db.Fetch <EmailFailedRecord>(); emailsFailed.ShouldHaveCountOf(4); foreach (var email in emailsFailed) { email.Subject.ShouldBeNull(); email.Message.ShouldBeNull(); email.LastError.ShouldContain("Email message"); } }