Mike Phares 7be540964a Pull Request 33523 suggestions
Pull Request 33520 suggestions

Injected AppSettings instead of using GetEnvironmentVariable at Services level

When debugging only
app.Services.GetRequiredService<IPCRBService>();

Get ready to use VSCode IDE

Align .editorconfig files
2024-12-03 10:48:07 -07:00

112 lines
3.8 KiB
C#

using System.Net.Mail;
using FabApprovalWorkerService.Clients;
using FabApprovalWorkerService.Services;
using Microsoft.Extensions.Logging;
using Moq;
namespace FabApprovalWorkerServiceTests;
internal class SmtpServiceTests {
private static readonly List<MailAddress> ADDRESS_LIST = new List<MailAddress>() {
new MailAddress("chase.tucker@infineon.com")
};
private Mock<ILogger<SmtpService>> _mockLogger;
private Mock<ISmtpClientWrapper> _mockSmtpClient;
private SmtpService _smtpService;
[SetUp]
public void Setup() {
_mockLogger = new Mock<ILogger<SmtpService>>();
_mockSmtpClient = new Mock<ISmtpClientWrapper>();
}
[Test]
public void SmtpServiceWithNullLoggerShouldThrowException() {
Assert.Throws<ArgumentNullException>(() => new SmtpService(null, _mockSmtpClient.Object));
}
[Test]
public void SmtpServiceWithNullSmtpClientShouldThrowException() {
Assert.Throws<ArgumentNullException>(() => new SmtpService(_mockLogger.Object, null));
}
[Test]
public void SendMailWithNullRecipientsShouldThrowException() {
_smtpService = new SmtpService(_mockLogger.Object, _mockSmtpClient.Object);
Assert.ThrowsAsync<ArgumentNullException>(async Task () => {
await _smtpService.SendEmail(null, ADDRESS_LIST, "subject", "body");
});
}
[Test]
public void SendMailWithEmptyRecipientsShouldThrowException() {
_smtpService = new SmtpService(_mockLogger.Object, _mockSmtpClient.Object);
Assert.ThrowsAsync<ArgumentNullException>(async Task () => {
await _smtpService.SendEmail(new List<MailAddress>(), ADDRESS_LIST, "subject", "body");
});
}
[Test]
public void SendMailWithNullccRecipientsShouldThrowException() {
_smtpService = new SmtpService(_mockLogger.Object, _mockSmtpClient.Object);
Assert.ThrowsAsync<ArgumentNullException>(async Task () => {
await _smtpService.SendEmail(ADDRESS_LIST, null, "subject", "body");
});
}
[Test]
public void SendMailWithNullSubjectShouldThrowException() {
_smtpService = new SmtpService(_mockLogger.Object, _mockSmtpClient.Object);
Assert.ThrowsAsync<ArgumentNullException>(async Task () => {
await _smtpService.SendEmail(ADDRESS_LIST, ADDRESS_LIST, null, "body");
});
}
[Test]
public void SendMailWithEmptySubjectShouldThrowException() {
_smtpService = new SmtpService(_mockLogger.Object, _mockSmtpClient.Object);
Assert.ThrowsAsync<ArgumentNullException>(async Task () => {
await _smtpService.SendEmail(ADDRESS_LIST, ADDRESS_LIST, "", "body");
});
}
[Test]
public void SendMailWithNullBodyShouldThrowException() {
_smtpService = new SmtpService(_mockLogger.Object, _mockSmtpClient.Object);
Assert.ThrowsAsync<ArgumentNullException>(async Task () => {
await _smtpService.SendEmail(ADDRESS_LIST, ADDRESS_LIST, "subject", null);
});
}
[Test]
public void SendMailWithEmptyBodyShouldThrowException() {
_smtpService = new SmtpService(_mockLogger.Object, _mockSmtpClient.Object);
Assert.ThrowsAsync<ArgumentNullException>(async Task () => {
await _smtpService.SendEmail(ADDRESS_LIST, ADDRESS_LIST, "subject", "");
});
}
[Test]
public async Task SendEmailWithValidArgsShouldSendMailThroughClient() {
_smtpService = new SmtpService(_mockLogger.Object, _mockSmtpClient.Object);
Assert.True(await _smtpService.SendEmail(ADDRESS_LIST, ADDRESS_LIST, "subject", "body"));
bool shouldSendEmail = false;
bool.TryParse(Environment.GetEnvironmentVariable("FabApprovalShouldSendEmail"), out shouldSendEmail);
if (shouldSendEmail)
_mockSmtpClient.Verify(s => s.Send(It.IsAny<MailMessage>()));
}
}