Skip to content

Commit

Permalink
chore: update test files
Browse files Browse the repository at this point in the history
  • Loading branch information
hermanho committed Mar 21, 2024
1 parent b99849f commit 0dc4219
Show file tree
Hide file tree
Showing 11 changed files with 242 additions and 69 deletions.
58 changes: 47 additions & 11 deletions src/Postal.Tests/EmailParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public async Task Parse_creates_MailMessage_with_headers_and_body()
Hello, World!";
var renderer = new Mock<IEmailViewRender>();
var parser = new EmailParser(renderer.Object);
using (var message = await parser.ParseAsync(input, new Email("Test")))
var imageEmbedder = new Mock<ImageEmbedder>();
using (var message = await parser.ParseAsync(input, new Email("Test"), imageEmbedder.Object))
{
message.To[0].Address.ShouldBe("test1@test.com");
message.From.Address.ShouldBe("test2@test.com");
Expand Down Expand Up @@ -56,7 +57,8 @@ public async Task Parse_creates_email_addresses_with_display_name()
Hello, World!";
var renderer = new Mock<IEmailViewRender>();
var parser = new EmailParser(renderer.Object);
using (var message = await parser.ParseAsync(input, new Email("Test")))
var imageEmbedder = new Mock<ImageEmbedder>();
using (var message = await parser.ParseAsync(input, new Email("Test"), imageEmbedder.Object))
{
message.To[0].Address.ShouldBe("test1@test.com");
message.To[0].DisplayName.ShouldBe("John H Smith");
Expand All @@ -78,7 +80,8 @@ public async Task Repeating_CC_adds_each_email_address_to_list()
Hello, World!";
var renderer = new Mock<IEmailViewRender>();
var parser = new EmailParser(renderer.Object);
using (var message = await parser.ParseAsync(input, new Email("Test")))
var imageEmbedder = new Mock<ImageEmbedder>();
using (var message = await parser.ParseAsync(input, new Email("Test"), imageEmbedder.Object))
{
message.CC[0].Address.ShouldBe("test3@test.com");
message.CC[1].Address.ShouldBe("test4@test.com");
Expand All @@ -98,7 +101,8 @@ public async Task Can_parse_multiple_email_addresses_in_header()
Hello, World!";
var renderer = new Mock<IEmailViewRender>();
var parser = new EmailParser(renderer.Object);
using (var message = await parser.ParseAsync(input, new Email("Test")))
var imageEmbedder = new Mock<ImageEmbedder>();
using (var message = await parser.ParseAsync(input, new Email("Test"), imageEmbedder.Object))
{
message.CC[0].Address.ShouldBe("test3@test.com");
message.CC[1].Address.ShouldBe("test4@test.com");
Expand All @@ -118,7 +122,8 @@ public async Task Can_detect_HTML_body()
<p>Hello, World!</p>";
var renderer = new Mock<IEmailViewRender>();
var parser = new EmailParser(renderer.Object);
using (var message = await parser.ParseAsync(input, new Email("Test")))
var imageEmbedder = new Mock<ImageEmbedder>();
using (var message = await parser.ParseAsync(input, new Email("Test"), imageEmbedder.Object))
{
message.Body.ShouldBe("<p>Hello, World!</p>");
message.IsBodyHtml.ShouldBeTrue();
Expand All @@ -145,6 +150,36 @@ public async Task Can_detect_HTML_body_with_leading_whitespace()
renderer.Verify();
}

[Fact]
public async Task Same_image_embedder_in_life_cycle()
{
var input = @"
To: test1@test.com
From: test2@test.com
Subject: Test Subject
Views: Html
<p>Hello, World!</p>";
var html = @"Content-Type: text/html
<p>Hello, World!</p>";

var email = new Email("Test");
var renderer = new Mock<IEmailViewRender>();
var imageEmbedder = new Mock<ImageEmbedder>();

renderer.Setup(r => r.RenderAsync(email, "Test.Html", imageEmbedder.Object))
.Returns(Task.FromResult(html));

var parser = new EmailParser(renderer.Object);
using (var message = await parser.ParseAsync(input, email, imageEmbedder.Object))
{
}
renderer.Verify(m => m.RenderAsync(email, It.IsAny<string>(), It.IsAny<ImageEmbedder>()), Times.Once);
renderer.Verify();
}


[Fact]
public async Task Alternative_views_are_added_to_MailMessage()
{
Expand All @@ -162,8 +197,8 @@ public async Task Alternative_views_are_added_to_MailMessage()

var email = new Email("Test");
var renderer = new Mock<IEmailViewRender>();
renderer.Setup(r => r.RenderAsync(email, "Test.Text")).Returns(Task.FromResult(text));
renderer.Setup(r => r.RenderAsync(email, "Test.Html")).Returns(Task.FromResult(html));
renderer.Setup(r => r.RenderAsync(email, "Test.Text", null)).Returns(Task.FromResult(text));
renderer.Setup(r => r.RenderAsync(email, "Test.Html", null)).Returns(Task.FromResult(html));

var parser = new EmailParser(renderer.Object);
using (var message = await parser.ParseAsync(input, email))
Expand Down Expand Up @@ -196,8 +231,8 @@ public async Task Given_base_view_is_full_path_Then_alternative_views_are_correc

var email = new Email("~/Views/Emails/Test.cshtml");
var renderer = new Mock<IEmailViewRender>();
renderer.Setup(r => r.RenderAsync(email, "~/Views/Emails/Test.Text.cshtml")).Returns(Task.FromResult(text));
renderer.Setup(r => r.RenderAsync(email, "~/Views/Emails/Test.Html.cshtml")).Returns(Task.FromResult(html));
renderer.Setup(r => r.RenderAsync(email, "~/Views/Emails/Test.Text.cshtml", null)).Returns(Task.FromResult(text));
renderer.Setup(r => r.RenderAsync(email, "~/Views/Emails/Test.Html.cshtml", null)).Returns(Task.FromResult(html));

var parser = new EmailParser(renderer.Object);
using (var message = await parser.ParseAsync(input, email))
Expand Down Expand Up @@ -247,8 +282,8 @@ public async Task ContentType_determined_by_view_name_when_alternative_view_is_m

var email = new Email("Test");
var renderer = new Mock<IEmailViewRender>();
renderer.Setup(r => r.RenderAsync(email, "Test.Text")).Returns(Task.FromResult(text));
renderer.Setup(r => r.RenderAsync(email, "Test.Html")).Returns(Task.FromResult(html));
renderer.Setup(r => r.RenderAsync(email, "Test.Text", null)).Returns(Task.FromResult(text));
renderer.Setup(r => r.RenderAsync(email, "Test.Html", null)).Returns(Task.FromResult(html));

var parser = new EmailParser(renderer.Object);
using (var message = await parser.ParseAsync(input, email))
Expand All @@ -268,6 +303,7 @@ public async Task To_header_can_be_set_automatically()
var parser = new EmailParser(Mock.Of<IEmailViewRender>());
using (var message = await parser.ParseAsync("body", (Email)email))
{
message.To.ShouldHaveSingleItem();
message.To[0].Address.ShouldBe("test@test.com");
}
}
Expand Down
37 changes: 25 additions & 12 deletions src/Postal.Tests/EmailServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.WebEncoders.Testing;
using System.Text.Encodings.Web;
using Microsoft.AspNetCore.Mvc.Routing;

namespace Postal
{
Expand All @@ -34,7 +35,7 @@ public async Task CreateMessage_returns_MailMessage_created_by_parser()
<p>Hello, World!</p>";
var email = new Email("Test");
var renderer = new Mock<IEmailViewRender>();
renderer.Setup(r => r.RenderAsync(email)).Returns(Task.FromResult(html));
renderer.Setup(r => r.RenderAsync(email, null, It.IsAny<ImageEmbedder>())).Returns(Task.FromResult(html));
var parser = new Mock<IEmailParser>();
var emailOptions = new EmailServiceOptions();
emailOptions.CreateSmtpClient = () => null;
Expand All @@ -43,7 +44,7 @@ public async Task CreateMessage_returns_MailMessage_created_by_parser()
var logger = new Mock<ILogger<EmailService>>();
var service = new EmailService(renderer.Object, parser.Object, options.Object, logger.Object);
var expectedMailMessage = new MailMessage();
parser.Setup(p => p.ParseAsync(It.IsAny<string>(), email)).Returns(Task.FromResult(expectedMailMessage));
parser.Setup(p => p.ParseAsync(It.IsAny<string>(), email, null)).Returns(Task.FromResult(expectedMailMessage));

var actualMailMessage = await service.CreateMailMessageAsync(email);

Expand All @@ -55,7 +56,7 @@ public async Task CreateMessage_returns_MailMessage_created_by_parser()
}

[Fact]
public void SendAync_returns_a_Task_and_sends_email()
public async Task SendAync_returns_a_Task_and_sends_email()
{
var html = @"Content-Type: text/html
To: test1@test.com
Expand All @@ -75,19 +76,20 @@ public void SendAync_returns_a_Task_and_sends_email()

var email = new Email("Test");
var renderer = new Mock<IEmailViewRender>();
renderer.Setup(r => r.RenderAsync(email)).Returns(Task.FromResult(html));
var imageEmbedder = new Mock<ImageEmbedder>();
renderer.Setup(r => r.RenderAsync(email, null, It.IsAny<ImageEmbedder>())).Returns(Task.FromResult(html));
var emailOptions = new EmailServiceOptions();
emailOptions.CreateSmtpClient = () => smtp;
var options = new Mock<IOptions<EmailServiceOptions>>();
options.SetupGet(o => o.Value).Returns(emailOptions);
var parser = new Mock<IEmailParser>();
var logger = new Mock<ILogger<EmailService>>();
var service = new EmailService(renderer.Object, parser.Object, options.Object, logger.Object);
parser.Setup(p => p.ParseAsync(It.IsAny<string>(), It.IsAny<Email>()))
parser.Setup(p => p.ParseAsync(It.IsAny<string>(), It.IsAny<Email>(), null))
.Returns(Task.FromResult(new MailMessage("test@test.com", "test@test.com")));

var sending = service.SendAsync(email);
sending.Wait();
await sending;

Directory.GetFiles(dir).Length.ShouldBe(1);
parser.Verify();
Expand All @@ -111,6 +113,7 @@ public void Dependency_injection_default()
var razorPageActivator = new Mock<IRazorPageActivator>();
var logger = new Mock<ILogger<EmailService>>();
var logger2 = new Mock<ILogger<TemplateService>>();
var urlHelperFactory = new Mock<IUrlHelperFactory>();
serviceCollection.AddSingleton(logger.Object);
serviceCollection.AddSingleton(logger2.Object);
serviceCollection.AddSingleton(viewEngine.Object);
Expand All @@ -120,6 +123,7 @@ public void Dependency_injection_default()
serviceCollection.AddSingleton(razorPageActivator.Object);
serviceCollection.AddSingleton<HtmlEncoder>(new HtmlTestEncoder());
serviceCollection.AddSingleton(new System.Diagnostics.DiagnosticListener("Postal.Tests"));
serviceCollection.AddSingleton(urlHelperFactory.Object);

serviceCollection.AddPostal();

Expand Down Expand Up @@ -154,6 +158,7 @@ public void Dependency_injection_smtpOtions1()
var razorPageActivator = new Mock<IRazorPageActivator>();
var logger = new Mock<ILogger<EmailService>>();
var logger2 = new Mock<ILogger<TemplateService>>();
var urlHelperFactory = new Mock<IUrlHelperFactory>();
serviceCollection.AddSingleton(logger.Object);
serviceCollection.AddSingleton(logger2.Object); serviceCollection.AddSingleton(viewEngine.Object);
serviceCollection.AddSingleton(viewEngine.Object);
Expand All @@ -163,6 +168,7 @@ public void Dependency_injection_smtpOtions1()
serviceCollection.AddSingleton(razorPageActivator.Object);
serviceCollection.AddSingleton<HtmlEncoder>(new HtmlTestEncoder());
serviceCollection.AddSingleton(new System.Diagnostics.DiagnosticListener("Postal.Tests"));
serviceCollection.AddSingleton(urlHelperFactory.Object);

serviceCollection.Configure<EmailServiceOptions>(_configuration);
serviceCollection.AddPostal();
Expand Down Expand Up @@ -197,6 +203,8 @@ public void Dependency_injection_smtpOtions2()
var razorPageActivator = new Mock<IRazorPageActivator>();
var logger = new Mock<ILogger<EmailService>>();
var logger2 = new Mock<ILogger<TemplateService>>();
var urlHelperFactory = new Mock<IUrlHelperFactory>();
serviceCollection.AddSingleton<HtmlEncoder>(new HtmlTestEncoder());
serviceCollection.AddSingleton(logger.Object);
serviceCollection.AddSingleton(logger2.Object);
serviceCollection.AddSingleton(viewEngine.Object);
Expand All @@ -206,6 +214,7 @@ public void Dependency_injection_smtpOtions2()
serviceCollection.AddSingleton(razorPageActivator.Object);
serviceCollection.AddSingleton<HtmlEncoder>(new HtmlTestEncoder());
serviceCollection.AddSingleton(new System.Diagnostics.DiagnosticListener("Postal.Tests"));
serviceCollection.AddSingleton(urlHelperFactory.Object);

serviceCollection.Configure<EmailServiceOptions>(o =>
{
Expand All @@ -228,12 +237,12 @@ public void Dependency_injection_smtpOtions2()
emailOptionField.FromAddress.ShouldBe("qwerty");
emailOptionField.UserName.ShouldBe("zxcvbn");
emailOptionField.Password.ShouldBe("asdfgh");

emailOptionField.CreateSmtpClient().ShouldBeOfType<FactExcetpionForSmtpClient>();
}

[Fact]
public void Dependency_injection_smtpOtions3()
public async Task Dependency_injection_smtpOtions3()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddOptions();
Expand All @@ -243,6 +252,7 @@ public void Dependency_injection_smtpOtions3()
var razorPageActivator = new Mock<IRazorPageActivator>();
var logger = new Mock<ILogger<EmailService>>();
var logger2 = new Mock<ILogger<TemplateService>>();
var urlHelperFactory = new Mock<IUrlHelperFactory>();
serviceCollection.AddSingleton(logger.Object);
serviceCollection.AddSingleton(logger2.Object);
serviceCollection.AddSingleton(viewEngine.Object);
Expand All @@ -252,6 +262,7 @@ public void Dependency_injection_smtpOtions3()
serviceCollection.AddSingleton(razorPageActivator.Object);
serviceCollection.AddSingleton<HtmlEncoder>(new HtmlTestEncoder());
serviceCollection.AddSingleton(new System.Diagnostics.DiagnosticListener("Postal.Tests"));
serviceCollection.AddSingleton(urlHelperFactory.Object);

serviceCollection.Configure<EmailServiceOptions>(o =>
{
Expand All @@ -263,13 +274,13 @@ public void Dependency_injection_smtpOtions3()
var services = serviceCollection.BuildServiceProvider();
var emailService = services.GetRequiredService<IEmailService>();

Assert.ThrowsAsync<FactExcetpionForSmtpCreation>(() => emailService.SendAsync(new Email("testView")));
await Assert.ThrowsAsync<FactExcetpionForSmtpCreation>(() => emailService.SendAsync(new MailMessage()));
EmailServiceOptions emailOptionField = GetInstanceField(typeof(EmailService), emailService, "options") as EmailServiceOptions;
Assert.Throws<FactExcetpionForSmtpCreation>(() => emailOptionField.CreateSmtpClient());
}

[Fact]
public void Dependency_injection_generic_host()
public async Task Dependency_injection_generic_host()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddOptions();
Expand All @@ -279,14 +290,16 @@ public void Dependency_injection_generic_host()
var razorPageActivator = new Mock<IRazorPageActivator>();
var logger = new Mock<ILogger<EmailService>>();
var logger2 = new Mock<ILogger<TemplateService>>();
var urlHelperFactory = new Mock<IUrlHelperFactory>();
serviceCollection.AddSingleton(logger.Object);
serviceCollection.AddSingleton(logger2.Object);
serviceCollection.AddSingleton(logger2.Object);
serviceCollection.AddSingleton(viewEngine.Object);
serviceCollection.AddSingleton(tempDataProvider.Object);
serviceCollection.AddSingleton(hostingEnvironment.Object);
serviceCollection.AddSingleton(razorPageActivator.Object);
serviceCollection.AddSingleton<HtmlEncoder>(new HtmlTestEncoder());
serviceCollection.AddSingleton(new System.Diagnostics.DiagnosticListener("Postal.Tests"));
serviceCollection.AddSingleton(urlHelperFactory.Object);

serviceCollection.Configure<EmailServiceOptions>(o =>
{
Expand All @@ -298,7 +311,7 @@ public void Dependency_injection_generic_host()
var services = serviceCollection.BuildServiceProvider();
var emailService = services.GetRequiredService<IEmailService>();

Assert.ThrowsAsync<FactExcetpionForSmtpCreation>(() => emailService.SendAsync(new Email("testView")));
await Assert.ThrowsAsync<FactExcetpionForSmtpCreation>(() => emailService.SendAsync(new MailMessage()));
EmailServiceOptions emailOptionField = GetInstanceField(typeof(EmailService), emailService, "options") as EmailServiceOptions;
Assert.Throws<FactExcetpionForSmtpCreation>(() => emailOptionField.CreateSmtpClient());
}
Expand Down
7 changes: 0 additions & 7 deletions src/Postal.Tests/EmailTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,6 @@ public void Send_creates_EmailService_and_calls_Send()
emailService.Verify(s => s.SendAsync(email));
}

[Fact]
public void Derived_Email_sets_ViewData_Model()
{
var email = new TestEmail();
email.ViewData.Model.ShouldBeSameAs(email);
}

[Fact]
public void Derived_Email_sets_ViewName_from_class_name()
{
Expand Down
Loading

0 comments on commit 0dc4219

Please sign in to comment.