public void Spool_WhenOneEmailNotSent_WhenEmailSendsSuccessfully_MarksEmailAsSent() { // test setup var email = CreateSubstituteEmail(); var func = EmailGeneratorWith(email); var dto = EmailBuilder.Create().WithRandomProps().WithRandomRecipient().WithRandomAttachment(2).Build(); dto.EmailRecipients.First().IsBCC = true; dto.EmailRecipients.First().IsCC = true; var ctx = SubstituteEmailContextBuilder.Create().WithEmails(dto).Build(); var deps = FakeEmailSpoolerDependenciesBuilder.Create() .WithDbContext(ctx) .WithEmailGenerator(func) .Build(); // pre-conditions Assert.AreEqual(2, dto.EmailAttachments.Count(a => a.Data.Length > 0 && a.Name.Length > 0)); // execute test ctx.DidNotReceive().SaveChanges(); using (var spooler = new EmailSpooler(deps)) { spooler.Spool(); } // test result Assert.IsTrue(dto.Sent); ctx.Received().SaveChanges(); }
public void Spool_PurgesUnsendableEmailOlderThanConfiguredKeepSentForValue() { // test setup var email = CreateSubstituteEmail(); var func = EmailGeneratorWith(email); var dto = EmailBuilder.BuildRandomWithRecipient(); var ctx = SubstituteEmailContextBuilder.Create().WithEmails(dto).Build(); var deps = FakeEmailSpoolerDependenciesBuilder.Create() .WithDbContext(ctx) .WithEmailGenerator(func) .Build(); var dayVal = RandomValueGen.GetRandomInt(30, 40); deps.EmailSpoolerConfig.PurgeMessageWithAgeInDays.ReturnsForAnyArgs(ci => dayVal); dto.Sent = false; dto.SendAttempts = deps.EmailSpoolerConfig.MaxSendAttempts; dto.LastModified = DateTime.Now.AddDays(-dayVal).AddSeconds(-10); // pre-conditions Assert.AreEqual(1, ctx.Emails.Count()); // execute test using (var spooler = new EmailSpooler(deps)) { spooler.Spool(); } // test result email.DidNotReceive().Send(); Assert.AreEqual(0, ctx.Emails.Count()); ctx.Received().SaveChanges(); }
public void Spool_DoesNotSendDisabledMails() { // test setup var email = CreateSubstituteEmail(); var func = EmailGeneratorWith(email); var dto = EmailBuilder.Create().WithRandomProps().WithRandomRecipient().WithRandomAttachment(2).Build(); dto.EmailRecipients.First().IsBCC = true; dto.EmailRecipients.First().IsCC = true; dto.SendAt = DateTime.Now.AddYears(-1); dto.Enabled = false; var ctx = SubstituteEmailContextBuilder.Create().WithEmails(dto).Build(); var deps = FakeEmailSpoolerDependenciesBuilder.Create() .WithDbContext(ctx) .WithEmailGenerator(func) .Build(); // pre-conditions Assert.AreEqual(2, dto.EmailAttachments.Count(a => a.Data.Length > 0 && a.Name.Length > 0)); // execute test using (var spooler = new EmailSpooler(deps)) { spooler.Spool(); } // test result email.DidNotReceive().Send(); }
public void Spool_WhenOneEmailNotSentButSendAttemptsExceeded_DoesNotSendMail() { // test setup var email = CreateSubstituteEmail(); var func = EmailGeneratorWith(email); var dto = EmailBuilder.Create().WithRandomProps().WithRandomRecipient().WithRandomAttachment(2).Build(); var maxAttempts = RandomValueGen.GetRandomInt(3, 6); dto.EmailRecipients.First().IsBCC = true; dto.EmailRecipients.First().IsCC = true; dto.SendAt = DateTime.Now.AddYears(-1); dto.SendAttempts = maxAttempts + 1; var ctx = SubstituteEmailContextBuilder.Create().WithEmails(dto).Build(); var deps = FakeEmailSpoolerDependenciesBuilder.Create() .WithDbContext(ctx) .WithEmailGenerator(func) .Build(); deps.EmailSpoolerConfig.MaxSendAttempts.ReturnsForAnyArgs(ci => maxAttempts); // pre-conditions Assert.AreEqual(2, dto.EmailAttachments.Count(a => a.Data.Length > 0 && a.Name.Length > 0)); // execute test using (var spooler = new EmailSpooler(deps)) { spooler.Spool(); } // test result email.DidNotReceive().Send(); }
public void Spool_WhenOneEmailNotSent_AddsAllAttachmentsWithContentIDsWithContentIds() { // test setup var email = CreateSubstituteEmail(); var func = EmailGeneratorWith(email); var dto = EmailBuilder.Create().WithRandomProps().WithRandomRecipient().WithRandomAttachment(2).Build(); dto.EmailRecipients.First().IsBCC = true; dto.EmailRecipients.First().IsCC = true; var ctx = SubstituteEmailContextBuilder.Create().WithEmails(dto).Build(); var deps = FakeEmailSpoolerDependenciesBuilder.Create() .WithDbContext(ctx) .WithEmailGenerator(func) .Build(); // pre-conditions Assert.AreEqual(2, dto.EmailAttachments.Count(a => a.Data.Length > 0 && a.Name.Length > 0)); // execute test using (var spooler = new EmailSpooler(deps)) { spooler.Spool(); } // test result foreach (var attachment in dto.EmailAttachments) { email.Received().AddAttachment(attachment.Name, attachment.Data, attachment.MIMEType, attachment.ContentID); } }
public void Spool_WhenOneEmailNotSent_AddsRecipientsToAllConfiguredFields() { // test setup var email = CreateSubstituteEmail(); var func = EmailGeneratorWith(email); var dto = EmailBuilder.Create().WithRandomProps().WithRandomRecipient().Build(); dto.EmailRecipients.First().IsBCC = true; dto.EmailRecipients.First().IsCC = true; var ctx = SubstituteEmailContextBuilder.Create().WithEmails(dto).Build(); var deps = FakeEmailSpoolerDependenciesBuilder.Create() .WithDbContext(ctx) .WithEmailGenerator(func) .Build(); // pre-conditions Assert.IsTrue(dto.EmailRecipients.Count(e => e.IsBCC && e.IsPrimaryRecipient && e.IsCC) == 1); // execute test using (var spooler = new EmailSpooler(deps)) { spooler.Spool(); } // test result foreach (var recipient in dto.EmailRecipients) { email.Received().AddRecipient(recipient.Recipient); email.Received().AddBCC(recipient.Recipient); email.Received().AddCC(recipient.Recipient); } }
public void Spool_WhenOneEmailNotSent_WhenEmailFailsToSend_BacksOffIncrementallyWithMultiplier() { // test setup var email = CreateSubstituteEmail(); var func = EmailGeneratorWith(email); var errorMessage = RandomValueGen.GetRandomString(); email.When(e => e.Send()).Do(ci => { throw new Exception(errorMessage); }); var dto = EmailBuilder.Create().WithRandomProps().WithRandomRecipient().WithRandomAttachment(2).Build(); dto.EmailRecipients.First().IsBCC = true; dto.EmailRecipients.First().IsCC = true; dto.SendAttempts = 1; var ctx = SubstituteEmailContextBuilder.Create().WithEmails(dto).Build(); var deps = FakeEmailSpoolerDependenciesBuilder.Create() .WithDbContext(ctx) .WithEmailGenerator(func) .Build(); var backoff = RandomValueGen.GetRandomInt(2, 10); deps.EmailSpoolerConfig.BackoffIntervalInMinutes.ReturnsForAnyArgs(ci => backoff); // pre-conditions Assert.AreEqual(2, dto.EmailAttachments.Count(a => a.Data.Length > 0 && a.Name.Length > 0)); // execute test ctx.DidNotReceive().SaveChanges(); var beforeRun = DateTime.Now; Assert.DoesNotThrow(() => { using (var spooler = new EmailSpooler(deps)) { spooler.Spool(); } }); // test result Assert.IsFalse(dto.Sent); Assert.AreEqual(errorMessage, dto.LastError); Assert.AreEqual(2, dto.SendAttempts); Assert.That(beforeRun.AddMinutes(backoff * 4), Is.LessThanOrEqualTo(dto.SendAt)); Assert.That(beforeRun.AddMinutes(backoff * 4).AddSeconds(5), Is.GreaterThanOrEqualTo(dto.SendAt)); ctx.Received().SaveChanges(); }
public void Spool_ShouldDisposeOfEmailsProvidedByGenerator() { var email = CreateSubstituteEmail(); var func = EmailGeneratorWith(email); var dto = EmailBuilder.BuildRandomWithRecipient(); var ctx = SubstituteEmailContextBuilder.Create().WithEmails(dto).Build(); var deps = FakeEmailSpoolerDependenciesBuilder.Create() .WithDbContext(ctx) .WithEmailGenerator(func) .Build(); // pre-conditions Assert.IsTrue(dto.EmailRecipients.Any()); // execute test using (var spooler = new EmailSpooler(deps)) { spooler.Spool(); } // test result email.Received().Dispose(); }
public void Spool_WhenOneEmailNotSent_CreatesEmailWithPropertiesFromDTO(string dtoProp, string emailProp) { // test setup var email = CreateSubstituteEmail(); var func = EmailGeneratorWith(email); var dto = EmailBuilder.BuildRandomWithRecipient(); var ctx = SubstituteEmailContextBuilder.Create().WithEmails(dto).Build(); var deps = FakeEmailSpoolerDependenciesBuilder.Create() .WithDbContext(ctx) .WithEmailGenerator(func) .Build(); // pre-conditions Assert.IsTrue(dto.EmailRecipients.Any()); // execute test using (var spooler = new EmailSpooler(deps)) { spooler.Spool(); } // test result PropertyAssert.AreEqual(dto, email, dtoProp, emailProp); }