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 ADDRESS_LIST = new List() { new MailAddress("chase.tucker@infineon.com") }; private Mock> _mockLogger; private Mock _mockSmtpClient; private SmtpService _smtpService; [SetUp] public void Setup() { _mockLogger = new Mock>(); _mockSmtpClient = new Mock(); } [Test] public void SmtpServiceWithNullLoggerShouldThrowException() { Assert.Throws(() => new SmtpService(null, _mockSmtpClient.Object)); } [Test] public void SmtpServiceWithNullSmtpClientShouldThrowException() { Assert.Throws(() => new SmtpService(_mockLogger.Object, null)); } [Test] public void SendMailWithNullRecipientsShouldThrowException() { _smtpService = new SmtpService(_mockLogger.Object, _mockSmtpClient.Object); Assert.ThrowsAsync(async Task () => { await _smtpService.SendEmail(null, ADDRESS_LIST, "subject", "body"); }); } [Test] public void SendMailWithEmptyRecipientsShouldThrowException() { _smtpService = new SmtpService(_mockLogger.Object, _mockSmtpClient.Object); Assert.ThrowsAsync(async Task () => { await _smtpService.SendEmail(new List (), ADDRESS_LIST, "subject", "body"); }); } [Test] public void SendMailWithNullccRecipientsShouldThrowException() { _smtpService = new SmtpService(_mockLogger.Object, _mockSmtpClient.Object); Assert.ThrowsAsync(async Task () => { await _smtpService.SendEmail(ADDRESS_LIST, null, "subject", "body"); }); } [Test] public void SendMailWithNullSubjectShouldThrowException() { _smtpService = new SmtpService(_mockLogger.Object, _mockSmtpClient.Object); Assert.ThrowsAsync(async Task () => { await _smtpService.SendEmail(ADDRESS_LIST, ADDRESS_LIST, null, "body"); }); } [Test] public void SendMailWithEmptySubjectShouldThrowException() { _smtpService = new SmtpService(_mockLogger.Object, _mockSmtpClient.Object); Assert.ThrowsAsync(async Task () => { await _smtpService.SendEmail(ADDRESS_LIST, ADDRESS_LIST, "", "body"); }); } [Test] public void SendMailWithNullBodyShouldThrowException() { _smtpService = new SmtpService(_mockLogger.Object, _mockSmtpClient.Object); Assert.ThrowsAsync(async Task () => { await _smtpService.SendEmail(ADDRESS_LIST, ADDRESS_LIST, "subject", null); }); } [Test] public void SendMailWithEmptyBodyShouldThrowException() { _smtpService = new SmtpService(_mockLogger.Object, _mockSmtpClient.Object); Assert.ThrowsAsync(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; Boolean.TryParse(Environment.GetEnvironmentVariable("FabApprovalShouldSendEmail"), out shouldSendEmail); if (shouldSendEmail) _mockSmtpClient.Verify(s => s.Send(It.IsAny())); } }