diff --git a/DigitalLearningSolutions.Data.Tests/Services/UnlockServiceTests.cs b/DigitalLearningSolutions.Data.Tests/Services/UnlockServiceTests.cs index a2391dab88..6719212677 100644 --- a/DigitalLearningSolutions.Data.Tests/Services/UnlockServiceTests.cs +++ b/DigitalLearningSolutions.Data.Tests/Services/UnlockServiceTests.cs @@ -1,49 +1,52 @@ namespace DigitalLearningSolutions.Data.Tests.Services { - using System.Configuration; - using System.Threading.Tasks; + using DigitalLearningSolutions.Data.Helpers; using DigitalLearningSolutions.Data.Models; using DigitalLearningSolutions.Data.Models.Email; using DigitalLearningSolutions.Data.Services; - using DigitalLearningSolutions.Web.Helpers; using FakeItEasy; using Microsoft.Extensions.Configuration; using Microsoft.FeatureManagement; using NUnit.Framework; - using ConfigHelper = DigitalLearningSolutions.Data.Helpers.ConfigHelper; public class UnlockServiceTests { private IConfiguration configuration = null!; - private IConfigService configService = null!; private IEmailService emailService = null!; + private IFeatureManager featureManager = null!; private INotificationDataService notificationDataService = null!; private NotificationService notificationService = null!; - private IFeatureManager featureManager = null!; [SetUp] public void Setup() { configuration = A.Fake(); notificationDataService = A.Fake(); - configService = A.Fake(); emailService = A.Fake(); featureManager = A.Fake(); - A.CallTo(() => notificationDataService.GetUnlockData(A._)).Returns(new UnlockData - { - ContactEmail = "recipient@example.com", - ContactForename = "Forename", - CourseName = "Activity Name", - CustomisationId = 22, - DelegateEmail = "cc@example.com", - DelegateName = "Delegate Name" - }); - - notificationService = new NotificationService(configuration, notificationDataService, configService, emailService, featureManager); - - A.CallTo(() => configuration[ConfigHelper.AppRootPathName]).Returns("https://new-tracking-system.com/"); - A.CallTo(() => configuration[ConfigHelper.CurrentSystemBaseUrlName]).Returns("https://old-tracking-system.com/"); + A.CallTo(() => notificationDataService.GetUnlockData(A._)).Returns( + new UnlockData + { + ContactEmail = "recipient@example.com", + ContactForename = "Forename", + CourseName = "Activity Name", + CustomisationId = 22, + DelegateEmail = "cc@example.com", + DelegateName = "Delegate Name", + } + ); + + notificationService = new NotificationService( + configuration, + notificationDataService, + emailService, + featureManager + ); + + A.CallTo(() => configuration[ConfigHelper.AppRootPathName]).Returns("https://new-tracking-system.com"); + A.CallTo(() => configuration[ConfigHelper.CurrentSystemBaseUrlName]) + .Returns("https://old-tracking-system.com"); } [Test] @@ -61,7 +64,7 @@ public void Throws_an_exception_when_tracking_system_base_url_is_null() { // Given A.CallTo(() => featureManager.IsEnabledAsync(A._)).Returns(false); - A.CallTo(() => configService.GetConfigValue(A._)).Returns(null); + A.CallTo(() => configuration[ConfigHelper.CurrentSystemBaseUrlName]).Returns(""); // Then Assert.ThrowsAsync(async () => await notificationService.SendUnlockRequest(1)); @@ -76,8 +79,9 @@ public void Trying_to_send_unlock_request_sends_email() notificationService.SendUnlockRequest(1); // Then - A.CallTo(() => - emailService.SendEmail(A._) + A.CallTo( + () => + emailService.SendEmail(A._) ) .MustHaveHappened(); } @@ -102,13 +106,15 @@ public void Trying_to_send_unlock_request_send_email_with_correct_old_url() //Given A.CallTo(() => featureManager.IsEnabledAsync("RefactoredTrackingSystem")) .Returns(false); - A.CallTo(() => configService.GetConfigValue(A._)).Returns("https://old-tracking-system.com/"); //When notificationService.SendUnlockRequest(1); //Then - A.CallTo(() => - emailService.SendEmail(A.That.Matches(e => e.Body.TextBody.Contains("https://old-tracking-system.com/"))) + A.CallTo( + () => + emailService.SendEmail( + A.That.Matches(e => e.Body.TextBody.Contains("https://old-tracking-system.com/Tracking/CourseDelegates")) + ) ) .MustHaveHappened(); } @@ -119,13 +125,15 @@ public void trying_to_send_unlock_request_send_email_with_correct_new_url() //Given A.CallTo(() => featureManager.IsEnabledAsync("RefactoredTrackingSystem")) .Returns(true); - A.CallTo(() => configService.GetConfigValue(A._)).Returns("https://new-tracking-system.com/"); //When notificationService.SendUnlockRequest(1); //Then - A.CallTo(() => - emailService.SendEmail(A.That.Matches(e => e.Body.TextBody.Contains("https://new-tracking-system.com/"))) + A.CallTo( + () => + emailService.SendEmail( + A.That.Matches(e => e.Body.TextBody.Contains("https://new-tracking-system.com/TrackingSystem/Delegates/CourseDelegates")) + ) ) .MustHaveHappened(); } diff --git a/DigitalLearningSolutions.Data/Services/NotificationService.cs b/DigitalLearningSolutions.Data/Services/NotificationService.cs index c097d97589..dfda13f263 100644 --- a/DigitalLearningSolutions.Data/Services/NotificationService.cs +++ b/DigitalLearningSolutions.Data/Services/NotificationService.cs @@ -16,7 +16,6 @@ public interface INotificationService public class NotificationService : INotificationService { - private readonly IConfigService configService; private readonly IConfiguration configuration; private readonly IEmailService emailService; private readonly IFeatureManager featureManager; @@ -25,14 +24,12 @@ public class NotificationService : INotificationService public NotificationService( IConfiguration configuration, INotificationDataService notificationDataService, - IConfigService configService, IEmailService emailService, IFeatureManager featureManager ) { this.configuration = configuration; this.notificationDataService = notificationDataService; - this.configService = configService; this.emailService = emailService; this.featureManager = featureManager; } @@ -53,19 +50,18 @@ public async Task SendUnlockRequest(int progressId) var baseUrlConfigOption = refactoredTrackingSystemEnabled ? configuration.GetAppRootPath() - : configService.GetConfigValue(ConfigHelper.CurrentSystemBaseUrlName); - if (baseUrlConfigOption == null) + : configuration.GetCurrentSystemBaseUrl(); + if (string.IsNullOrEmpty(baseUrlConfigOption)) { + var missingConfigValue = refactoredTrackingSystemEnabled ? "AppRootPath" : "CurrentSystemBaseUrl"; throw new ConfigValueMissingException( - configService.GetConfigValueMissingExceptionMessage( - refactoredTrackingSystemEnabled ? "AppRootPath" : "CurrentSystemBaseUrl" - ) + $"Encountered an error while trying to send an email: The value of {missingConfigValue} is null" ); } var baseUrl = refactoredTrackingSystemEnabled ? $"{baseUrlConfigOption}/TrackingSystem/Delegates/CourseDelegates" - : $"{baseUrlConfigOption}/CourseDelegates"; + : $"{baseUrlConfigOption}/Tracking/CourseDelegates"; var unlockUrl = new UriBuilder(baseUrl) {