Skip to content

Commit

Permalink
Merge branch 'main' into firewall-logs
Browse files Browse the repository at this point in the history
  • Loading branch information
albal authored Oct 4, 2023
2 parents 46218b8 + 337c23c commit 4c62814
Show file tree
Hide file tree
Showing 38 changed files with 1,246 additions and 92 deletions.
165 changes: 165 additions & 0 deletions Childrens-Social-Care-CPD-Tests/ConfigurationHealthCheckTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
using Castle.Core.Logging;
using Childrens_Social_Care_CPD;
using Childrens_Social_Care_CPD.Configuration;
using FluentAssertions;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Logging;
using NSubstitute;
using NSubstitute.Extensions;
using NUnit.Framework;
using System.Threading.Tasks;

namespace Childrens_Social_Care_CPD_Tests;

public class ConfigurationHealthCheckTests
{
private ILogger<ConfigurationHealthCheck> _logger;
private IApplicationConfiguration _applicationConfiguration;

[SetUp]
public void Setup()
{
_logger = Substitute.For<ILogger<ConfigurationHealthCheck>>();
_applicationConfiguration = Substitute.For<IApplicationConfiguration>();
_applicationConfiguration.ReturnsForAll("foo");
}

[Test]
public async Task Passes_When_All_Values_Set_And_Cookies_Are_Secured()
{
// arrange
var sut = new ConfigurationHealthCheck(_logger, _applicationConfiguration);

// act
var result = await sut.CheckHealthAsync(null, default);

// assert
result.Status.Should().Be(HealthStatus.Healthy);
}

[Test]
public async Task Fails_When_Disable_Cookies_Is_True()
{
// arrange
_applicationConfiguration.DisableSecureCookies.Returns(true);
var sut = new ConfigurationHealthCheck(_logger, _applicationConfiguration);

// act
var result = await sut.CheckHealthAsync(null, default);

// assert
result.Status.Should().Be(HealthStatus.Unhealthy);
}

[Test]
public async Task Fails_When_AppInsightsConnectionString_Is_Not_Set()
{
// arrange
_applicationConfiguration.AppInsightsConnectionString.Returns(string.Empty);
var sut = new ConfigurationHealthCheck(_logger, _applicationConfiguration);

// act
var result = await sut.CheckHealthAsync(null, default);

// assert
result.Status.Should().Be(HealthStatus.Unhealthy);
}

[Test]
public async Task Fails_When_AppVersion_Is_Not_Set()
{
// arrange
_applicationConfiguration.AppVersion.Returns(string.Empty);
var sut = new ConfigurationHealthCheck(_logger, _applicationConfiguration);

// act
var result = await sut.CheckHealthAsync(null, default);

// assert
result.Status.Should().Be(HealthStatus.Unhealthy);
}

[Test]
public async Task Fails_When_AzureEnvironment_Is_Not_Set()
{
// arrange
_applicationConfiguration.AzureEnvironment.Returns(string.Empty);
var sut = new ConfigurationHealthCheck(_logger, _applicationConfiguration);

// act
var result = await sut.CheckHealthAsync(null, default);

// assert
result.Status.Should().Be(HealthStatus.Unhealthy);
}

[Test]
public async Task Fails_When_ClarityProjectId_Is_Not_Set()
{
// arrange
_applicationConfiguration.ClarityProjectId.Returns(string.Empty);
var sut = new ConfigurationHealthCheck(_logger, _applicationConfiguration);

// act
var result = await sut.CheckHealthAsync(null, default);

// assert
result.Status.Should().Be(HealthStatus.Unhealthy);
}

[Test]
public async Task Fails_When_ContentfulDeliveryApiKey_Is_Not_Set()
{
// arrange
_applicationConfiguration.ContentfulDeliveryApiKey.Returns(string.Empty);
var sut = new ConfigurationHealthCheck(_logger, _applicationConfiguration);

// act
var result = await sut.CheckHealthAsync(null, default);

// assert
result.Status.Should().Be(HealthStatus.Unhealthy);
}

[Test]
public async Task Fails_When_ContentfulEnvironment_Is_Not_Set()
{
// arrange
_applicationConfiguration.ContentfulEnvironment.Returns(string.Empty);
var sut = new ConfigurationHealthCheck(_logger, _applicationConfiguration);

// act
var result = await sut.CheckHealthAsync(null, default);

// assert
result.Status.Should().Be(HealthStatus.Unhealthy);
}

[Test]
public async Task Fails_When_ContentfulSpaceId_Is_Not_Set()
{
// arrange
_applicationConfiguration.ContentfulSpaceId.Returns(string.Empty);
var sut = new ConfigurationHealthCheck(_logger, _applicationConfiguration);

// act
var result = await sut.CheckHealthAsync(null, default);

// assert
result.Status.Should().Be(HealthStatus.Unhealthy);
}

[Test]
public async Task Fails_When_GoogleTagManagerKey_Is_Not_Set()
{
// arrange
_applicationConfiguration.GoogleTagManagerKey.Returns(string.Empty);
var sut = new ConfigurationHealthCheck(_logger, _applicationConfiguration);

// act
var result = await sut.CheckHealthAsync(null, default);

// assert
result.Status.Should().Be(HealthStatus.Unhealthy);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class EntityResolverTests
[TestCase("areaOfPracticeList", typeof(AreaOfPracticeList))]
[TestCase("applicationFeature", typeof(ApplicationFeature))]
[TestCase("applicationFeatures", typeof(ApplicationFeatures))]
[TestCase("audioResource", typeof(AudioResource))]
[TestCase("columnLayout", typeof(ColumnLayout))]
[TestCase("content", typeof(Content))]
[TestCase("contentLink", typeof(ContentLink))]
Expand All @@ -29,6 +30,7 @@ public class EntityResolverTests
[TestCase("roleList", typeof(RoleList))]
[TestCase("sideMenu", typeof(SideMenu))]
[TestCase("textBlock", typeof(TextBlock))]
[TestCase("videoResource", typeof(VideoResource))]
public void Resolves_Correctly(string contentTypeId, Type expectedType)
{
var resolver = new EntityResolver();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public partial class PartialsFactoryTests
{
new object[] { new AreaOfPractice(), "_AreaOfPractice" },
new object[] { new AreaOfPracticeList(), "_AreaOfPracticeList" },
new object[] { new AudioResource(), "_AudioResource" },
new object[] { new ColumnLayout(), "_ColumnLayout" },
new object[] { new Content(), "_Content" },
new object[] { new ContentLink(), "_ContentLink" },
Expand All @@ -28,6 +29,7 @@ public partial class PartialsFactoryTests
new object[] { new RoleList(), "_RoleList" },
new object[] { new SideMenu(), "_SideMenu" },
new object[] { new TextBlock(), "_TextBlock" },
new object[] { new VideoResource(), "_VideoResource" },
};

[TestCaseSource(nameof(Successful_Resolves))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace Childrens_Social_Care_CPD_Tests.Controllers;
Expand All @@ -17,7 +18,7 @@ public class ContentControllerServerTests
{
private CpdTestServerApplication _application;
private HttpClient _httpClient;
private static string ContentUrl = "/";
private static readonly string _contentUrl = "/";

[SetUp]
public void SetUp()
Expand All @@ -36,10 +37,10 @@ public async Task Content_Will_Contain_Warning_If_Data_Is_Self_Referential()
var content = new Content();
content.Items = new List<IContent> { content };
var contentCollection = new ContentfulCollection<Content>() { Items = new List<Content>() { content } };
_application.CpdContentfulClient.GetEntries(Arg.Any<QueryBuilder<Content>>(), default).Returns(contentCollection);
_application.CpdContentfulClient.GetEntries(Arg.Any<QueryBuilder<Content>>(), Arg.Any<CancellationToken>()).Returns(contentCollection);

// act
var response = await _httpClient.GetAsync(ContentUrl);
var response = await _httpClient.GetAsync(_contentUrl);
var responseContent = await response.Content.ReadAsStringAsync();

// assert
Expand All @@ -51,13 +52,15 @@ public async Task Content_Will_Contain_Warning_If_Data_Is_Self_Referential()
public async Task Content_Will_Contain_Warning_If_Data_Has_An_Unknown_Content_Type()
{
// arrange
var content = new Content();
content.Items = new List<IContent> { new TestingContentItem() };
var content = new Content
{
Items = new List<IContent> { new TestingContentItem() }
};
var contentCollection = new ContentfulCollection<Content>() { Items = new List<Content>() { content } };
_application.CpdContentfulClient.GetEntries(Arg.Any<QueryBuilder<Content>>(), default).Returns(contentCollection);
_application.CpdContentfulClient.GetEntries(Arg.Any<QueryBuilder<Content>>(), Arg.Any<CancellationToken>()).Returns(contentCollection);

// act
var response = await _httpClient.GetAsync(ContentUrl);
var response = await _httpClient.GetAsync(_contentUrl);
var responseContent = await response.Content.ReadAsStringAsync();

// assert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using NSubstitute;
using NUnit.Framework;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace Childrens_Social_Care_CPD_Tests.Controllers;
Expand All @@ -22,6 +23,7 @@ public class ContentControllerTests
private HttpContext _httpContext;
private HttpRequest _httpRequest;
private ICpdContentfulClient _contentfulClient;
private CancellationTokenSource _cancellationTokenSource;

private void SetContent(Content content)
{
Expand All @@ -32,8 +34,10 @@ private void SetContent(Content content)
: contentCollection.Items = new List<Content> { content };

_contentfulClient
.GetEntries(Arg.Any<QueryBuilder<Content>>(), default)
.GetEntries(Arg.Any<QueryBuilder<Content>>(), Arg.Any<CancellationToken>())
.Returns(contentCollection);

_cancellationTokenSource = new CancellationTokenSource();
}

[SetUp]
Expand All @@ -50,9 +54,11 @@ public void SetUp()

_contentfulClient = Substitute.For<ICpdContentfulClient>();

_contentController = new ContentController(_contentfulClient);
_contentController.ControllerContext = controllerContext;
_contentController.TempData = Substitute.For<ITempDataDictionary>();
_contentController = new ContentController(_contentfulClient)
{
ControllerContext = controllerContext,
TempData = Substitute.For<ITempDataDictionary>()
};
}

[Test]
Expand All @@ -62,7 +68,7 @@ public async Task Index_Returns_404_When_No_Content_Found()
SetContent(null);

// act
var actual = await _contentController.Index("home");
var actual = await _contentController.Index(_cancellationTokenSource.Token, "home");

// assert
actual.Should().BeOfType<NotFoundResult>();
Expand All @@ -75,7 +81,7 @@ public async Task Index_Returns_View()
SetContent(new Content());

// act
var actual = await _contentController.Index("home");
var actual = await _contentController.Index(_cancellationTokenSource.Token, "home");

// assert
actual.Should().BeOfType<ViewResult>();
Expand All @@ -94,7 +100,7 @@ public async Task Index_Sets_The_ViewState_ContextModel()
SetContent(rootContent);

// act
await _contentController.Index("home");
await _contentController.Index(_cancellationTokenSource.Token, "home");
var actual = _contentController.ViewData["ContextModel"] as ContextModel;

// assert
Expand All @@ -112,21 +118,21 @@ public async Task Index_Sets_The_ContextModel_Preferences_Set_Value_Correctly(bo
SetContent(new Content());

// act
await _contentController.Index("home", preferenceSet);
await _contentController.Index(_cancellationTokenSource.Token, "home", preferenceSet);
var actual = _contentController.ViewData["ContextModel"] as ContextModel;

// assert
actual.Should().NotBeNull();
actual.PreferenceSet.Should().Be(preferenceSet);
}

public static object[] SideMenuContent =
private static readonly object[] _sideMenuContent =
{
new object[] { new SideMenu() },
new object[] { null },
};

[TestCaseSource(nameof(SideMenuContent))]
[TestCaseSource(nameof(_sideMenuContent))]
public async Task Index_Sets_The_ContextModel_UseContainers_From_SideMenu_Value_Correctly(SideMenu sideMenu)
{
// arrange
Expand All @@ -138,7 +144,7 @@ public async Task Index_Sets_The_ContextModel_UseContainers_From_SideMenu_Value_
SetContent(rootContent);

// act
await _contentController.Index("home");
await _contentController.Index(_cancellationTokenSource.Token, "home");
var actual = _contentController.ViewData["ContextModel"] as ContextModel;

// assert
Expand All @@ -152,10 +158,10 @@ public async Task Index_Trims_Trailing_Slashes()
// arrange
SetContent(new Content());
var query = "";
await _contentfulClient.GetEntries(Arg.Do<QueryBuilder<Content>>(value => query = value.Build()));
await _contentfulClient.GetEntries(Arg.Do<QueryBuilder<Content>>(value => query = value.Build()), Arg.Any<CancellationToken>());

// act
var actual = await _contentController.Index("home/");
var actual = await _contentController.Index(_cancellationTokenSource.Token, "home/");

// assert
query.Should().Contain("fields.id=home&");
Expand Down
Loading

0 comments on commit 4c62814

Please sign in to comment.