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: Add application configuration auto discovery from annotations
Merge pull request #306 from DFE-Digital/feat/application-health
- Loading branch information
Showing
16 changed files
with
649 additions
and
228 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
Childrens-Social-Care-CPD-Tests/Configuration/ConfigurationInformationTests.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,92 @@ | ||
using NUnit.Framework; | ||
using Childrens_Social_Care_CPD.Configuration; | ||
using FluentAssertions; | ||
using System.Linq; | ||
using NSubstitute; | ||
|
||
namespace Childrens_Social_Care_CPD_Tests.Configuration; | ||
|
||
public partial class ConfigurationInformationTests | ||
{ | ||
private IApplicationConfiguration _applicationConfiguration; | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
_applicationConfiguration = Substitute.For<IApplicationConfiguration>(); | ||
_applicationConfiguration.AzureEnvironment.Returns("dev"); | ||
} | ||
|
||
[Test] | ||
public void Required_Values_Are_Detected() | ||
{ | ||
// arrange | ||
_applicationConfiguration.AppVersion.Returns("foo"); | ||
|
||
// act | ||
var sut = new ConfigurationInformation(_applicationConfiguration); | ||
var actual = sut.ConfigurationInfo.Single(x => x.Name == "AppVersion"); | ||
|
||
// assert | ||
actual.Required.Should().BeTrue(); | ||
actual.HasValue.Should().BeTrue(); | ||
} | ||
|
||
[TestCase("")] | ||
[TestCase(null)] | ||
public void Missing_Values_Are_Detected(string value) | ||
{ | ||
// arrange | ||
_applicationConfiguration.AppVersion.Returns(value); | ||
|
||
// act | ||
var sut = new ConfigurationInformation(_applicationConfiguration); | ||
var actual = sut.ConfigurationInfo.Single(x => x.Name == "AppVersion"); | ||
|
||
// assert | ||
actual.Required.Should().BeTrue(); | ||
actual.HasValue.Should().BeFalse(); | ||
} | ||
|
||
[Test] | ||
public void Extraneous_Values_Are_Detected() | ||
{ | ||
// arrange | ||
_applicationConfiguration.ClarityProjectId.Returns("foo"); | ||
|
||
// act | ||
var sut = new ConfigurationInformation(_applicationConfiguration); | ||
var actual = sut.ConfigurationInfo.Single(x => x.Name == "ClarityProjectId"); | ||
|
||
// assert | ||
actual.Required.Should().BeFalse(); | ||
actual.Extraneous.Should().BeTrue(); | ||
} | ||
|
||
[Test] | ||
public void Ignored_Values_Are_Detected() | ||
{ | ||
// act | ||
var sut = new ConfigurationInformation(_applicationConfiguration); | ||
var actual = sut.ConfigurationInfo.SingleOrDefault(x => x.Name == "ContentfulPreviewHost"); | ||
|
||
// assert | ||
actual.Should().BeNull(); | ||
} | ||
|
||
[Test] | ||
public void Sensitive_Values_Are_Obfuscated() | ||
{ | ||
// arrange | ||
var value = "sensitive value"; | ||
_applicationConfiguration.AzureEnvironment.Returns(ApplicationEnvironment.Production); | ||
_applicationConfiguration.AppInsightsConnectionString.Returns(value); | ||
|
||
// act | ||
var sut = new ConfigurationInformation(_applicationConfiguration); | ||
var actual = sut.ConfigurationInfo.Single(x => x.Name == "AppInsightsConnectionString"); | ||
|
||
// assert | ||
actual.Value.Should().NotBe(value); | ||
} | ||
} |
133 changes: 133 additions & 0 deletions
133
Childrens-Social-Care-CPD-Tests/Configuration/RequiredForEnvironmentTests.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,133 @@ | ||
using NUnit.Framework; | ||
using Childrens_Social_Care_CPD.Configuration; | ||
using FluentAssertions; | ||
using System.Linq; | ||
|
||
namespace Childrens_Social_Care_CPD_Tests.Configuration; | ||
|
||
public class RequiredForEnvironmentTests | ||
{ | ||
private class TestConfiguration | ||
{ | ||
public string PropertyHasNoRules { get; set; } | ||
|
||
[RequiredForEnvironment("dev")] | ||
public string PropertyHasSingleEnv { get; set; } | ||
|
||
[RequiredForEnvironment("*")] | ||
public string PropertyHasAllEnv { get; set; } | ||
|
||
[RequiredForEnvironment("dev")] | ||
[RequiredForEnvironment("test")] | ||
[RequiredForEnvironment("prod")] | ||
public string PropertyHasMultipleEnv { get; set; } | ||
|
||
[RequiredForEnvironment("dev", Hidden = false)] | ||
[RequiredForEnvironment("*")] | ||
public string PropertyHasEnvironmentOverrideValue { get; set; } | ||
} | ||
|
||
[Test] | ||
public void No_Rule_Property_Is_Ignored() | ||
{ | ||
// arrange | ||
var config = new TestConfiguration(); | ||
|
||
// act | ||
var sut = config.RulesForEnvironment("dev"); | ||
var actual = sut.Where(x => x.Key.Name == "PropertyHasNoRules"); | ||
|
||
// assert | ||
actual.Should().HaveCount(0); | ||
} | ||
|
||
[Test] | ||
public void Single_Environment_Property_Returns_Rule_For_Correct_Environment() | ||
{ | ||
// arrange | ||
var config = new TestConfiguration(); | ||
|
||
// act | ||
var sut = config.RulesForEnvironment("dev"); | ||
var actual = sut.SingleOrDefault(x => x.Key.Name == "PropertyHasSingleEnv"); | ||
|
||
// assert | ||
actual.Value.Should().NotBeNull(); | ||
} | ||
|
||
[Test] | ||
public void Single_Environment_Property_Returns_No_Rule_For_Wrong_Environment() | ||
{ | ||
// arrange | ||
var config = new TestConfiguration(); | ||
|
||
// act | ||
var sut = config.RulesForEnvironment(" "); | ||
var actual = sut.SingleOrDefault(x => x.Key.Name == "PropertyHasSingleEnv"); | ||
|
||
// assert | ||
actual.Value.Should().BeNull(); | ||
} | ||
|
||
[Test] | ||
public void Asterisk_Matches_Any_Environment() | ||
{ | ||
// arrange | ||
var config = new TestConfiguration(); | ||
|
||
// act | ||
var sut = config.RulesForEnvironment("test"); | ||
var actual = sut.SingleOrDefault(x => x.Key.Name == "PropertyHasAllEnv"); | ||
|
||
// assert | ||
actual.Value.Should().NotBeNull(); | ||
} | ||
|
||
[TestCase("dev")] | ||
[TestCase("test")] | ||
[TestCase("prod")] | ||
public void Multiple_Rules_For_Different_Environments_For_Same_Property_Return_Single_Rule(string env) | ||
{ | ||
// arrange | ||
var config = new TestConfiguration(); | ||
|
||
// act | ||
var sut = config.RulesForEnvironment(env); | ||
var actual = sut.SingleOrDefault(x => x.Key.Name == "PropertyHasMultipleEnv"); | ||
|
||
// assert | ||
actual.Value.Should().NotBeNull(); | ||
} | ||
|
||
[Test] | ||
public void All_Environment_Matcher_Can_Be_Overidden() | ||
{ | ||
// arrange | ||
var config = new TestConfiguration(); | ||
|
||
// act | ||
var sut = config.RulesForEnvironment("dev"); | ||
var actual = sut.SingleOrDefault(x => x.Key.Name == "PropertyHasEnvironmentOverrideValue"); | ||
|
||
// assert | ||
actual.Value.Should().NotBeNull(); | ||
actual.Value.Environment.Should().Be("dev"); | ||
actual.Value.Hidden.Should().Be(false); | ||
} | ||
|
||
[Test] | ||
public void Override_For_Non_Matching_Environment_Does_Not_Override_Catchall_Rule() | ||
{ | ||
// arrange | ||
var config = new TestConfiguration(); | ||
|
||
// act | ||
var sut = config.RulesForEnvironment("text"); | ||
var actual = sut.SingleOrDefault(x => x.Key.Name == "PropertyHasEnvironmentOverrideValue"); | ||
|
||
// assert | ||
actual.Value.Should().NotBeNull(); | ||
actual.Value.Environment.Should().Be("*"); | ||
actual.Value.Hidden.Should().Be(true); | ||
} | ||
} |
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
Oops, something went wrong.