Skip to content

Commit

Permalink
Merge pull request #46 from IowaComputerGurus/feature/fixes
Browse files Browse the repository at this point in the history
Feature/fixes
  • Loading branch information
mitchelsellers committed Aug 20, 2023
2 parents b8c6115 + 856884f commit 96ab2df
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 24 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ jobs:
env:
solution-path: './src/NetCore.Utilities.Email.Smtp.sln'
steps:
- name: Set up JDK 11
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: 11
java-version: 17
distribution: zulu

- uses: actions/checkout@v3
Expand Down Expand Up @@ -90,8 +90,8 @@ jobs:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
shell: powershell
run: |
.\.sonar\scanner\dotnet-sonarscanner begin /k:"IowaComputerGurus_netcore.utilities.email.smtp" /o:"iowacomputergurus-github" /d:sonar.login="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io"
.\.sonar\scanner\dotnet-sonarscanner begin /k:"IowaComputerGurus_netcore.utilities.email.smtp" /o:"iowacomputergurus-github" /d:sonar.token="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io"
dotnet restore "${{ env.solution-path }}"
dotnet build "${{ env.solution-path }}" --no-restore --configuration Release -p:version=${{ steps.gitversion.outputs.majorMinorPatch }}
dotnet test "${{ env.solution-path }}" --no-build --configuration Release --collect "XPlat Code Coverage" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=opencover --logger "trx;LogFileName=unittests.trx"
.\.sonar\scanner\dotnet-sonarscanner end /d:sonar.login="${{ secrets.SONAR_TOKEN }}"
.\.sonar\scanner\dotnet-sonarscanner end /d:sonar.token="${{ secrets.SONAR_TOKEN }}"
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class MimeMessageFactoryTests
private readonly Mock<ILogger<MimeMessageFactory>> _loggerMock;
private readonly IMimeMessageFactory _factory;
private readonly Mock<IEmailTemplateFactory> _emailTemplateFactoryMock;
private readonly Mock<IHostingEnvironment> _hostingEnvironment;
private readonly Mock<IHostEnvironment> _hostingEnvironment;
private readonly SmtpServiceOptions _options = new SmtpServiceOptions()
{
AdminEmail = "admin@test.com",
Expand All @@ -30,7 +30,7 @@ public class MimeMessageFactoryTests
public MimeMessageFactoryTests()
{
_loggerMock = new Mock<ILogger<MimeMessageFactory>>();
_hostingEnvironment = new Mock<IHostingEnvironment>();
_hostingEnvironment = new Mock<IHostEnvironment>();
_emailTemplateFactoryMock = new Mock<IEmailTemplateFactory>();
_factory = new MimeMessageFactory(new OptionsWrapper<SmtpServiceOptions>(_options), _loggerMock.Object, _hostingEnvironment.Object, _emailTemplateFactoryMock.Object);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PackageReference Include="Moq" Version="4.20.69" />
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
Expand Down
79 changes: 79 additions & 0 deletions src/NetCore.Utilities.Email.Smtp.Tests/SmtpServiceTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Extensions.Options;
using MimeKit;
Expand Down Expand Up @@ -117,6 +118,31 @@ public void SendMessage_WithoutCCRecipients_ShouldSend_DefaultingFromAddress()
_mimeKitServiceMock.Verify(k => k.SendEmail(mimeMessage));
}

[Fact]
public void SendMessageWithReplyTo_WithoutCCRecipients_ShouldSend_DefaultingFromAddress()
{
//Arrange
var replyTo = "me@me.com";
var replyToName = "Bob";
var to = "tester@test.com";
var subject = "test";
var message = "message";
var mimeMessage = new MimeMessage();
_mimeMessageFactoryMock
.Setup(f => f.CreateFromMessage(_options.AdminEmail, _options.AdminName, to, null, subject, message, ""))
.Returns(mimeMessage).Verifiable();

//Act
_service.SendWithReplyTo(replyTo, replyToName, to, subject, message);

//Verify
Assert.Equal(1, mimeMessage.ReplyTo.Count);

Check warning on line 139 in src/NetCore.Utilities.Email.Smtp.Tests/SmtpServiceTests.cs

View workflow job for this annotation

GitHub Actions / Validate Build

Do not use Assert.Equal() to check for collection size. Use Assert.Single instead.

Check warning on line 139 in src/NetCore.Utilities.Email.Smtp.Tests/SmtpServiceTests.cs

View workflow job for this annotation

GitHub Actions / Validate Build

Do not use Assert.Equal() to check for collection size. Use Assert.Single instead.

Check warning on line 139 in src/NetCore.Utilities.Email.Smtp.Tests/SmtpServiceTests.cs

View workflow job for this annotation

GitHub Actions / Build & Publish to NuGet

Do not use Assert.Equal() to check for collection size. Use Assert.Single instead. (https://xunit.net/xunit.analyzers/rules/xUnit2013)

Check warning on line 139 in src/NetCore.Utilities.Email.Smtp.Tests/SmtpServiceTests.cs

View workflow job for this annotation

GitHub Actions / Build & Publish to NuGet

Do not use Assert.Equal() to check for collection size. Use Assert.Single instead. (https://xunit.net/xunit.analyzers/rules/xUnit2013)
var replyToAsAdded = mimeMessage.ReplyTo.First();
Assert.Equal("\"Bob\" <me@me.com>", replyToAsAdded.ToString());
_mimeMessageFactoryMock.Verify();
_mimeKitServiceMock.Verify(k => k.SendEmail(mimeMessage));
}

[Fact]
public void SendMessage_WithCCRecipients_ShouldSend_DefaultingFromAddress()
{
Expand All @@ -138,6 +164,32 @@ public void SendMessage_WithCCRecipients_ShouldSend_DefaultingFromAddress()
_mimeKitServiceMock.Verify(k => k.SendEmail(mimeMessage));
}

[Fact]
public void SendMessageWithReplyTo_WithCCRecipients_ShouldSend_DefaultingFromAddress()
{
//Arrange
var replyTo = "me@me.com";
var replyToName = "Bob";
var to = "tester@test.com";
var cc = new List<string> { "Person1@test.com" };
var subject = "test";
var message = "message";
var mimeMessage = new MimeMessage();
_mimeMessageFactoryMock
.Setup(f => f.CreateFromMessage(_options.AdminEmail, _options.AdminName, to, cc, subject, message, ""))
.Returns(mimeMessage).Verifiable();

//Act
_service.SendWithReplyTo(replyTo, replyToName, to, cc, subject, message);

//Verify
Assert.Equal(1, mimeMessage.ReplyTo.Count);

Check warning on line 186 in src/NetCore.Utilities.Email.Smtp.Tests/SmtpServiceTests.cs

View workflow job for this annotation

GitHub Actions / Validate Build

Do not use Assert.Equal() to check for collection size. Use Assert.Single instead.

Check warning on line 186 in src/NetCore.Utilities.Email.Smtp.Tests/SmtpServiceTests.cs

View workflow job for this annotation

GitHub Actions / Validate Build

Do not use Assert.Equal() to check for collection size. Use Assert.Single instead.

Check warning on line 186 in src/NetCore.Utilities.Email.Smtp.Tests/SmtpServiceTests.cs

View workflow job for this annotation

GitHub Actions / Build & Publish to NuGet

Do not use Assert.Equal() to check for collection size. Use Assert.Single instead. (https://xunit.net/xunit.analyzers/rules/xUnit2013)

Check warning on line 186 in src/NetCore.Utilities.Email.Smtp.Tests/SmtpServiceTests.cs

View workflow job for this annotation

GitHub Actions / Build & Publish to NuGet

Do not use Assert.Equal() to check for collection size. Use Assert.Single instead. (https://xunit.net/xunit.analyzers/rules/xUnit2013)
var replyToAsAdded = mimeMessage.ReplyTo.First();
Assert.Equal("\"Bob\" <me@me.com>", replyToAsAdded.ToString());
_mimeMessageFactoryMock.Verify();
_mimeKitServiceMock.Verify(k => k.SendEmail(mimeMessage));
}

[Fact]
public void SendMessageWithAttachment_ShouldSend_DefaultingFromAddress()
{
Expand Down Expand Up @@ -183,6 +235,33 @@ public void SendMessage_ShouldPassOptionalTemplateName_ToMessageMethods()
_mimeKitServiceMock.Verify(k => k.SendEmail(mimeMessage));
}

[Fact]
public void SendMessageWithReplyTo_ShouldPassOptionalTemplateName_ToMessageMethods()
{
//Arrange
var replyTo = "me@me.com";
var replyToName = "Bob";
var to = "tester@test.com";
var cc = new List<string> { "Person1@test.com" };
var subject = "test";
var message = "message";
var requestedTemplate = "Test";
var mimeMessage = new MimeMessage();
_mimeMessageFactoryMock
.Setup(f => f.CreateFromMessage(_options.AdminEmail, _options.AdminName, to, cc, subject, message, requestedTemplate))
.Returns(mimeMessage).Verifiable();

//Act
_service.SendWithReplyTo(replyTo, replyToName, to, cc, subject, message, null, requestedTemplate);

//Assets
Assert.Equal(1, mimeMessage.ReplyTo.Count);

Check warning on line 258 in src/NetCore.Utilities.Email.Smtp.Tests/SmtpServiceTests.cs

View workflow job for this annotation

GitHub Actions / Validate Build

Do not use Assert.Equal() to check for collection size. Use Assert.Single instead.

Check warning on line 258 in src/NetCore.Utilities.Email.Smtp.Tests/SmtpServiceTests.cs

View workflow job for this annotation

GitHub Actions / Validate Build

Do not use Assert.Equal() to check for collection size. Use Assert.Single instead.

Check warning on line 258 in src/NetCore.Utilities.Email.Smtp.Tests/SmtpServiceTests.cs

View workflow job for this annotation

GitHub Actions / Build & Publish to NuGet

Do not use Assert.Equal() to check for collection size. Use Assert.Single instead. (https://xunit.net/xunit.analyzers/rules/xUnit2013)

Check warning on line 258 in src/NetCore.Utilities.Email.Smtp.Tests/SmtpServiceTests.cs

View workflow job for this annotation

GitHub Actions / Build & Publish to NuGet

Do not use Assert.Equal() to check for collection size. Use Assert.Single instead. (https://xunit.net/xunit.analyzers/rules/xUnit2013)
var replyToAsAdded = mimeMessage.ReplyTo.First();
Assert.Equal("\"Bob\" <me@me.com>", replyToAsAdded.ToString());
_mimeMessageFactoryMock.Verify();
_mimeKitServiceMock.Verify(k => k.SendEmail(mimeMessage));
}

[Fact]
public void SendMessageWithAttachment_ShouldPassOptionalTemplateName_ToMessageMethods()
{
Expand Down
13 changes: 5 additions & 8 deletions src/NetCore.Utilities.Email.Smtp.Tests/StartupExtensiosTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Moq;
Expand Down Expand Up @@ -45,8 +45,7 @@ public void ServiceCollection_ShouldRegisterMimeKitService()
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
collection.AddSingleton(new Mock<Microsoft.Extensions.Hosting.IHostingEnvironment>().Object);
collection.AddSingleton(new Mock<IHostingEnvironment>().Object);
collection.AddSingleton(new Mock<IHostEnvironment>().Object);
collection.UseIcgNetCoreUtilitiesEmailSmtp(configuration);
var services = collection.BuildServiceProvider();

Expand All @@ -66,8 +65,7 @@ public void ServiceCollection_ShouldRegisterMimeMessageFactory()
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
collection.AddSingleton(new Mock<Microsoft.Extensions.Hosting.IHostingEnvironment>().Object);
collection.AddSingleton(new Mock<IHostingEnvironment>().Object);
collection.AddSingleton(new Mock<IHostEnvironment>().Object);
collection.AddSingleton(new Mock<ILogger<MimeMessageFactory>>().Object);
collection.UseIcgNetCoreUtilitiesEmailSmtp(configuration);
var services = collection.BuildServiceProvider();
Expand All @@ -88,8 +86,7 @@ public void ServiceCollection_ShouldRegisterSmtpService()
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
collection.AddSingleton(new Mock<Microsoft.Extensions.Hosting.IHostingEnvironment>().Object);
collection.AddSingleton(new Mock<IHostingEnvironment>().Object);
collection.AddSingleton(new Mock<IHostEnvironment>().Object);
collection.AddSingleton(new Mock<ILogger<MimeMessageFactory>>().Object);
collection.UseIcgNetCoreUtilitiesEmailSmtp(configuration);
var services = collection.BuildServiceProvider();
Expand Down
4 changes: 2 additions & 2 deletions src/NetCore.Utilities.Email.Smtp/MimeMessageFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public interface IMimeMessageFactory
/// <inheritdoc />
public class MimeMessageFactory : IMimeMessageFactory
{
private readonly IHostingEnvironment _hostingEnvironment;
private readonly IHostEnvironment _hostingEnvironment;
private readonly IEmailTemplateFactory _emailTemplateFactory;
private readonly SmtpServiceOptions _serviceOptions;
private readonly ILogger _logger;
Expand All @@ -69,7 +69,7 @@ public class MimeMessageFactory : IMimeMessageFactory
/// <param name="logger">An instance of ILogger for recording</param>
/// <param name="hostingEnvironment">Current environment information</param>
/// <param name="emailTemplateFactory">The ICG Email Template Factory for formatting messages</param>
public MimeMessageFactory(IOptions<SmtpServiceOptions> serviceOptions, ILogger<MimeMessageFactory> logger, IHostingEnvironment hostingEnvironment, IEmailTemplateFactory emailTemplateFactory)
public MimeMessageFactory(IOptions<SmtpServiceOptions> serviceOptions, ILogger<MimeMessageFactory> logger, IHostEnvironment hostingEnvironment, IEmailTemplateFactory emailTemplateFactory)
{
_serviceOptions = serviceOptions.Value;
_logger = logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="icg.netcore.utilities.email" Version="4.0.4" />
<PackageReference Include="icg.netcore.utilities.email" Version="6.2.0" />
<PackageReference Include="MailKit" Version="2.15.0" />
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="6.0.0" />
Expand Down
64 changes: 63 additions & 1 deletion src/NetCore.Utilities.Email.Smtp/SmtpService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Microsoft.Extensions.Options;
using MimeKit;

namespace ICG.NetCore.Utilities.Email.Smtp
{
Expand Down Expand Up @@ -114,5 +116,65 @@ public bool SendMessage(string toAddress, IEnumerable<string> ccAddressList, str

return true;
}

/// <inheritdoc />
public bool SendWithReplyTo(string replyToAddress, string replyToName, string toAddress, string subject, string bodyHtml)
{
//Call full overload
return SendWithReplyTo(replyToAddress, replyToName, toAddress, null, subject, bodyHtml);
}

/// <inheritdoc />
public bool SendWithReplyTo(string replyToAddress, string replyToName, string toAddress, string subject, string bodyHtml, List<KeyValuePair<string, string>> tokens)
{
//Call full overload
return SendWithReplyTo(replyToAddress, replyToName, toAddress, null, subject, bodyHtml, null, "");
}

/// <inheritdoc />
public bool SendWithReplyTo(string replyToAddress, string replyToName, string toAddress, IEnumerable<string> ccAddressList, string subject, string bodyHtml)
{
//Call full overload
return SendWithReplyTo(replyToAddress, replyToName, toAddress, ccAddressList, subject, bodyHtml, null, "");
}

/// <inheritdoc />
public bool SendWithReplyTo(string replyToAddress, string replyToName, string toAddress, IEnumerable<string> ccAddressList, string subject, string bodyHtml, List<KeyValuePair<string, string>> tokens)
{
//Call full overload
return SendWithReplyTo(replyToAddress, replyToName, toAddress, ccAddressList, subject, bodyHtml, tokens, "");
}

/// <inheritdoc />
public bool SendWithReplyTo(string replyToAddress, string replyToName, string toAddress, IEnumerable<string> ccAddressList, string subject, string bodyHtml, List<KeyValuePair<string, string>> tokens, string templateName, string senderKeyName = "")
{
if (string.IsNullOrEmpty(replyToAddress))
throw new ArgumentNullException(nameof(replyToAddress));

if (tokens != null)
{
foreach (var item in tokens)
{
bodyHtml = bodyHtml.Replace(item.Key, item.Value);
}
}

//Get the message to send
var toSend = _mimeMessageFactory.CreateFromMessage(_serviceOptions.AdminEmail, _serviceOptions.AdminName, toAddress, ccAddressList,
subject, bodyHtml, templateName);

//Add the reply to if needed
if (!string.IsNullOrEmpty(replyToAddress))
{
var address = MailboxAddress.Parse(replyToAddress);
if(!string.IsNullOrEmpty(replyToName))
address.Name = replyToName;
toSend.ReplyTo.Add(address);
}

//Send
_mimeKitService.SendEmail(toSend);
return true;
}
}
}

0 comments on commit 96ab2df

Please sign in to comment.