2024-04-03 12:05:44 -07:00

121 lines
4.2 KiB
C#

using FabApprovalWorkerService.Clients;
using FabApprovalWorkerService.Services;
using Microsoft.Extensions.Logging;
using Moq;
using System.Net.Mail;
namespace FabApprovalWorkerServiceTests;
internal class SmtpServiceTests {
private static readonly List<MailAddress> ADDRESS_LIST = new List<MailAddress>() {
new MailAddress("fake@email.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 SendMailWithEmptyccRecipientsShouldThrowException() {
_smtpService = new SmtpService(_mockLogger.Object, _mockSmtpClient.Object);
Assert.ThrowsAsync<ArgumentNullException>(async Task () => {
await _smtpService.SendEmail(ADDRESS_LIST, new List<MailAddress>(), "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"));
string? env = Environment.GetEnvironmentVariable("FabApprovalEnvironmentName");
if (env is not null && !env.ToLower().Equals("development"))
_mockSmtpClient.Verify(s => s.Send(It.IsAny<MailMessage>()));
}
}