generated from DFE-Digital/govuk-dotnet-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Enable health checks and add health check around the configuration
Merge pull request #293 from DFE-Digital/feat/add-healthcheck
- Loading branch information
Showing
4 changed files
with
221 additions
and
0 deletions.
There are no files selected for viewing
165 changes: 165 additions & 0 deletions
165
Childrens-Social-Care-CPD-Tests/ConfigurationHealthCheckTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using Childrens_Social_Care_CPD.Configuration; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
|
||
namespace Childrens_Social_Care_CPD; | ||
|
||
public class ConfigurationHealthCheck : IHealthCheck | ||
{ | ||
private readonly ILogger _logger; | ||
private readonly IApplicationConfiguration _applicationConfiguration; | ||
|
||
public ConfigurationHealthCheck(ILogger<ConfigurationHealthCheck> logger, IApplicationConfiguration applicationConfiguration) | ||
{ | ||
_logger = logger; | ||
_applicationConfiguration = applicationConfiguration; | ||
} | ||
|
||
private bool CheckSetting(string name, string value) | ||
{ | ||
if (string.IsNullOrWhiteSpace(value)) | ||
{ | ||
_logger.LogError("Configuration setting {propertyName} does not have a value", name); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) | ||
{ | ||
var healthy = CheckSetting("AppInsightsConnectionString", _applicationConfiguration.AppInsightsConnectionString) | ||
&& CheckSetting("AppVersion", _applicationConfiguration.AppVersion) | ||
&& CheckSetting("AzureEnvironment", _applicationConfiguration.AzureEnvironment) | ||
&& CheckSetting("ClarityProjectId", _applicationConfiguration.ClarityProjectId) | ||
&& CheckSetting("ContentfulDeliveryApiKey", _applicationConfiguration.ContentfulDeliveryApiKey) | ||
&& CheckSetting("ContentfulEnvironment", _applicationConfiguration.ContentfulEnvironment) | ||
&& CheckSetting("ContentfulSpaceId", _applicationConfiguration.ContentfulSpaceId) | ||
&& CheckSetting("GoogleTagManagerKey", _applicationConfiguration.GoogleTagManagerKey); | ||
|
||
if (_applicationConfiguration.DisableSecureCookies) | ||
{ | ||
_logger.LogError("DisableSecureCookies should not be enabled for standard environments"); | ||
healthy = false; | ||
} | ||
|
||
if (!healthy) | ||
{ | ||
return Task.FromResult(HealthCheckResult.Unhealthy("One or more application configuration settings is missing or incorrect")); | ||
} | ||
|
||
return Task.FromResult(HealthCheckResult.Healthy("Application configuration is OK")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters