public async Task Confirmed_Run_SendsEmail() { using (IdentityWsDbContext ef = CreateEf()) { Email email; ef.Emails.Add(email = new Email { To = new Alias { DateConfirmed = now.UtcNow } }); await ef.SaveChangesAsync(); EmailQueueProcessor patient = new EmailQueueProcessor(dummyLog, now); bool email_sent = false; Mock <IEmailSender> mock_sender = new Mock <IEmailSender>(); mock_sender.Setup(m => m.Send(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())) .Callback <string, string, string, string, string, string>((a, b, c, d, e, f) => email_sent = true); IServiceProvider services = MakeServices(mock_sender.Object, ef); patient.Run(services, null); email.Should().Match <Email>(e => e.ProcessingCount == 1 && e.DateLastProcessed == now.UtcNow && string.IsNullOrEmpty(e.LastProcessingError), "the alias is confirmed"); email_sent.Should().BeTrue("the email can be sent"); } }
public async Task ProhibitedUnconfirmed_Run_FailsPermanently() { using (IdentityWsDbContext ef = CreateEf()) { Email email; ef.Emails.Add(email = new Email { To = new Alias() }); await ef.SaveChangesAsync(); EmailQueueProcessor patient = new EmailQueueProcessor(dummyLog, now); bool email_sent = false; Mock <IEmailSender> mock_sender = new Mock <IEmailSender>(); mock_sender.Setup(m => m.Send(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())) .Callback <string, string, string, string, string, string>((a, b, c, d, e, f) => email_sent = true); IServiceProvider services = MakeServices(mock_sender.Object, ef); patient.Run(services, null); email.Should().Match <Email>(e => e.ProcessingCount == 10 && e.DateLastProcessed == now.UtcNow && e.LastProcessingError == "Unconfirmed", "the alias is unconfirmed and Email.SendIfUnconfirmed is false"); email_sent.Should().BeFalse("an error ocurred"); } }
public async Task OutsideBackoffPeriod_Run_SendsEmail() { using (IdentityWsDbContext ef = CreateEf()) { DateTime last_processed = now.UtcNow.AddMinutes(-16); Email email; ef.Emails.Add(email = new Email { To = new Alias { DateConfirmed = now.UtcNow }, ProcessingCount = 4, // Processing should be allowed 16 mins after error (2^^4). LastProcessingError = "some weird error", DateLastProcessed = last_processed }); await ef.SaveChangesAsync(); EmailQueueProcessor patient = new EmailQueueProcessor(dummyLog, now); bool email_sent = false; Mock <IEmailSender> mock_sender = new Mock <IEmailSender>(); mock_sender.Setup(m => m.Send(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())) .Callback <string, string, string, string, string, string>((a, b, c, d, e, f) => email_sent = true); IServiceProvider services = MakeServices(mock_sender.Object, ef); patient.Run(services, null); email.Should().Match <Email>(e => e.ProcessingCount == 5 && e.DateLastProcessed == now.UtcNow && string.IsNullOrEmpty(e.LastProcessingError), "the last processing date is (just) outside the back-off period"); email_sent.Should().BeTrue("the email can be sent"); } }
public async Task ExceptionDuringSend_Run_SavesMessageAndContinues() { using (IdentityWsDbContext ef = CreateEf()) { Email email1, email2; ef.Emails.AddRange(new[] { email1 = new Email { To = new Alias { DateConfirmed = now.UtcNow }, BodyText = "Hello" }, email2 = new Email { To = new Alias { DateConfirmed = now.UtcNow }, BodyText = "World" } }); await ef.SaveChangesAsync(); EmailQueueProcessor patient = new EmailQueueProcessor(dummyLog, now); string emailed_text = null; bool called_back = false; Mock <IEmailSender> mock_sender = new Mock <IEmailSender>(); mock_sender.Setup(m => m.Send(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())) .Callback <string, string, string, string, string, string>((a, b, c, d, e, f) => { if (!called_back) { called_back = true; throw new Exception("Exceptional!"); } emailed_text = e; }); IServiceProvider services = MakeServices(mock_sender.Object, ef); patient.Run(services, null); email1.Should().Match <Email>(e => e.ProcessingCount == 1 && e.DateLastProcessed == now.UtcNow && e.LastProcessingError == "Exceptional!", "an exception was thrown during sending"); email2.Should().Match <Email>(e => e.ProcessingCount == 1 && e.DateLastProcessed == now.UtcNow && string.IsNullOrEmpty(e.LastProcessingError), "sending should succeed"); emailed_text.Should().Be("World", "that's the subject of email2"); } }