From 22bb4226ead2d62e8c2c2c05be47d7da621402e2 Mon Sep 17 00:00:00 2001 From: tidusjar Date: Sat, 12 Feb 2022 21:37:51 +0000 Subject: [PATCH] fix(notifications): :bug: This is a fix for some of the duplicate notification issues #3825 --- .../NotificationServiceTests.cs | 42 +++++++++++++++++++ .../Ombi.Notifications.Tests.csproj | 1 + src/Ombi.Notifications/BaseNotification.cs | 16 +++---- src/Ombi.Notifications/NotificationService.cs | 4 +- 4 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 src/Ombi.Notifications.Tests/NotificationServiceTests.cs diff --git a/src/Ombi.Notifications.Tests/NotificationServiceTests.cs b/src/Ombi.Notifications.Tests/NotificationServiceTests.cs new file mode 100644 index 000000000..fedd7707e --- /dev/null +++ b/src/Ombi.Notifications.Tests/NotificationServiceTests.cs @@ -0,0 +1,42 @@ +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; +using Moq.AutoMock; +using NUnit.Framework; +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Ombi.Notifications.Tests +{ + [TestFixture] + public class NotificationServiceTests + { + + private NotitficationServiceTestFacade _subject; + + [SetUp] + public void Setup() + { + var mocker = new AutoMocker(); + mocker.Use(NullLogger.Instance); + _subject = mocker.CreateInstance(); + } + + [Test] + public void PopulateAgentsTests() + { + Assert.That(_subject.Agents, Has.Count.EqualTo(12)); + Assert.That(_subject.Agents.DistinctBy(x => x.NotificationName).ToList(), Has.Count.EqualTo(12)); + } + } + + + public class NotitficationServiceTestFacade : NotificationService + { + public NotitficationServiceTestFacade(IServiceProvider provider, ILogger log) : base(provider, log) + { + } + + public List Agents => base.NotificationAgents; + } +} diff --git a/src/Ombi.Notifications.Tests/Ombi.Notifications.Tests.csproj b/src/Ombi.Notifications.Tests/Ombi.Notifications.Tests.csproj index b3edbcf6d..e936c9072 100644 --- a/src/Ombi.Notifications.Tests/Ombi.Notifications.Tests.csproj +++ b/src/Ombi.Notifications.Tests/Ombi.Notifications.Tests.csproj @@ -13,6 +13,7 @@ + diff --git a/src/Ombi.Notifications/BaseNotification.cs b/src/Ombi.Notifications/BaseNotification.cs index 9397767bf..b52f0a0cd 100644 --- a/src/Ombi.Notifications/BaseNotification.cs +++ b/src/Ombi.Notifications/BaseNotification.cs @@ -33,7 +33,7 @@ namespace Ombi.Notifications AlbumRepository = album; UserNotificationPreferences = notificationUserPreferences; _userManager = um; - Settings.ClearCache(); + Settings?.ClearCache(); } protected ISettingsService Settings { get; } @@ -64,7 +64,11 @@ namespace Ombi.Notifications public async Task NotifyAsync(NotificationOptions model, Settings.Settings.Models.Settings settings) { - if (settings == null) await NotifyAsync(model); + if (settings == null) + { + await NotifyAsync(model); + return; + } var notificationSettings = (T)settings; @@ -114,15 +118,13 @@ namespace Ombi.Notifications case NotificationType.IssueComment: await IssueComment(model, notificationSettings); break; - case NotificationType.AdminNote: + case NotificationType.PartiallyAvailable: + await PartiallyAvailable(model, notificationSettings); break; + case NotificationType.AdminNote: case NotificationType.WelcomeEmail: - break; case NotificationType.Newsletter: break; - case NotificationType.PartiallyAvailable: - await PartiallyAvailable(model, notificationSettings); - break; default: throw new ArgumentOutOfRangeException(); } diff --git a/src/Ombi.Notifications/NotificationService.cs b/src/Ombi.Notifications/NotificationService.cs index 8a24382aa..f22800cdc 100644 --- a/src/Ombi.Notifications/NotificationService.cs +++ b/src/Ombi.Notifications/NotificationService.cs @@ -23,7 +23,7 @@ namespace Ombi.Notifications PopulateAgents(); } - private List NotificationAgents { get; } + protected List NotificationAgents { get; } private ILogger Log { get; } /// @@ -55,7 +55,7 @@ namespace Ombi.Notifications } - private void PopulateAgents() + protected void PopulateAgents() { var baseSearchType = typeof(BaseNotification<>).Name;