Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 38 additions & 30 deletions DigitalLearningSolutions.Data.Tests/Services/UnlockServiceTests.cs
Original file line number Diff line number Diff line change
@@ -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<IConfiguration>();
notificationDataService = A.Fake<INotificationDataService>();
configService = A.Fake<IConfigService>();
emailService = A.Fake<IEmailService>();
featureManager = A.Fake<IFeatureManager>();

A.CallTo(() => notificationDataService.GetUnlockData(A<int>._)).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<int>._)).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]
Expand All @@ -61,7 +64,7 @@ public void Throws_an_exception_when_tracking_system_base_url_is_null()
{
// Given
A.CallTo(() => featureManager.IsEnabledAsync(A<string>._)).Returns(false);
A.CallTo(() => configService.GetConfigValue(A<string>._)).Returns(null);
A.CallTo(() => configuration[ConfigHelper.CurrentSystemBaseUrlName]).Returns("");

// Then
Assert.ThrowsAsync<ConfigValueMissingException>(async () => await notificationService.SendUnlockRequest(1));
Expand All @@ -76,8 +79,9 @@ public void Trying_to_send_unlock_request_sends_email()
notificationService.SendUnlockRequest(1);

// Then
A.CallTo(() =>
emailService.SendEmail(A<Email>._)
A.CallTo(
() =>
emailService.SendEmail(A<Email>._)
)
.MustHaveHappened();
}
Expand All @@ -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<string>._)).Returns("https://old-tracking-system.com/");
//When
notificationService.SendUnlockRequest(1);

//Then
A.CallTo(() =>
emailService.SendEmail(A<Email>.That.Matches(e => e.Body.TextBody.Contains("https://old-tracking-system.com/")))
A.CallTo(
() =>
emailService.SendEmail(
A<Email>.That.Matches(e => e.Body.TextBody.Contains("https://old-tracking-system.com/Tracking/CourseDelegates"))
)
)
.MustHaveHappened();
}
Expand All @@ -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<string>._)).Returns("https://new-tracking-system.com/");
//When
notificationService.SendUnlockRequest(1);

//Then
A.CallTo(() =>
emailService.SendEmail(A<Email>.That.Matches(e => e.Body.TextBody.Contains("https://new-tracking-system.com/")))
A.CallTo(
() =>
emailService.SendEmail(
A<Email>.That.Matches(e => e.Body.TextBody.Contains("https://new-tracking-system.com/TrackingSystem/Delegates/CourseDelegates"))
)
)
.MustHaveHappened();
}
Expand Down
14 changes: 5 additions & 9 deletions DigitalLearningSolutions.Data/Services/NotificationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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)
{
Expand Down