From 69e8b5a7e2d7a063b482f4c6bc9f280d62012875 Mon Sep 17 00:00:00 2001 From: tidusjar Date: Sat, 6 Nov 2021 22:00:59 +0000 Subject: [PATCH] test(mass-email): :white_check_mark: Added tests covering the mass email functionality --- .../Senders/MassEmailSenderTests.cs | 147 ++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 src/Ombi.Core.Tests/Senders/MassEmailSenderTests.cs diff --git a/src/Ombi.Core.Tests/Senders/MassEmailSenderTests.cs b/src/Ombi.Core.Tests/Senders/MassEmailSenderTests.cs new file mode 100644 index 000000000..c90797922 --- /dev/null +++ b/src/Ombi.Core.Tests/Senders/MassEmailSenderTests.cs @@ -0,0 +1,147 @@ +using Microsoft.Extensions.Logging; +using MockQueryable.Moq; +using Moq; +using Moq.AutoMock; +using NUnit.Framework; +using Ombi.Core.Authentication; +using Ombi.Core.Models; +using Ombi.Core.Senders; +using Ombi.Notifications; +using Ombi.Notifications.Models; +using Ombi.Settings.Settings.Models.Notifications; +using Ombi.Store.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Ombi.Core.Tests.Senders +{ + [TestFixture] + public class MassEmailSenderTests + { + + private MassEmailSender _subject; + private AutoMocker _mocker; + + [SetUp] + public void Setup() + { + _mocker = new AutoMocker(); + _subject = _mocker.CreateInstance(); + } + + [Test] + public async Task SendMassEmail_SingleUser() + { + var model = new MassEmailModel + { + Body = "Test", + Subject = "Subject", + Users = new List + { + new Store.Entities.OmbiUser + { + Id = "a" + } + } + }; + + _mocker.Setup>(x => x.Users).Returns(new List + { + new OmbiUser + { + Id = "a", + Email = "Test@test.com" + } + }.AsQueryable().BuildMock().Object); + + var result = await _subject.SendMassEmail(model); + + _mocker.Verify(x => x.SendAdHoc(It.Is(m => m.Subject == model.Subject + && m.Message == model.Body + && m.To == "Test@test.com"), It.IsAny()), Times.Once); + } + + [Test] + public async Task SendMassEmail_MultipleUsers() + { + var model = new MassEmailModel + { + Body = "Test", + Subject = "Subject", + Users = new List + { + new OmbiUser + { + Id = "a" + }, + new OmbiUser + { + Id = "b" + } + } + }; + + _mocker.Setup>(x => x.Users).Returns(new List + { + new OmbiUser + { + Id = "a", + Email = "Test@test.com" + }, + new OmbiUser + { + Id = "b", + Email = "b@test.com" + } + }.AsQueryable().BuildMock().Object); + + var result = await _subject.SendMassEmail(model); + + _mocker.Verify(x => x.SendAdHoc(It.Is(m => m.Subject == model.Subject + && m.Message == model.Body + && m.To == "Test@test.com"), It.IsAny()), Times.Once); + _mocker.Verify(x => x.SendAdHoc(It.Is(m => m.Subject == model.Subject + && m.Message == model.Body + && m.To == "b@test.com"), It.IsAny()), Times.Once); + } + + [Test] + public async Task SendMassEmail_UserNoEmail() + { + var model = new MassEmailModel + { + Body = "Test", + Subject = "Subject", + Users = new List + { + new OmbiUser + { + Id = "a" + } + } + }; + + _mocker.Setup>(x => x.Users).Returns(new List + { + new OmbiUser + { + Id = "a", + } + }.AsQueryable().BuildMock().Object); + + var result = await _subject.SendMassEmail(model); + _mocker.Verify>( + x => x.Log( + LogLevel.Information, + It.IsAny(), + It.IsAny(), + It.IsAny(), + It.IsAny>()), + Times.Once); + + _mocker.Verify(x => x.SendAdHoc(It.IsAny(), It.IsAny()), Times.Never); + } + } +}