Skip to content

Commit

Permalink
Merge branch 'main' into feat/picture-card
Browse files Browse the repository at this point in the history
  • Loading branch information
killij committed Sep 21, 2023
2 parents dfb17d9 + b6f4a81 commit 2f8b603
Show file tree
Hide file tree
Showing 71 changed files with 1,077 additions and 151 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/app-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ jobs:

- name: Write application tag into environment variable
run: |
curl -L -i -X PATCH -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" -H "X-GitHub-Api-Version: 2022-11-28" https://api.github.com/repositories/547182049/environments/${{ inputs.workspace }}/variables/TF_VAR_CPD_IMAGE_TAG -d '{"name": "TF_VAR_CPD_IMAGE_TAG", "value": "${{ inputs.tag }}"}'
curl -L -i -X PATCH -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{ secrets.PAT }}" -H "X-GitHub-Api-Version: 2022-11-28" https://api.github.com/repositories/547182049/environments/${{ inputs.workspace }}/variables/TF_VAR_CPD_IMAGE_TAG -d '{"name": "TF_VAR_CPD_IMAGE_TAG", "value": "${{ inputs.tag }}"}'
- name: Sign out of Azure
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yml.old
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 6.0.x
dotnet-version: 7.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 6.0.x
dotnet-version: 7.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/sonarqube.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
/o:"dfe-digital" \
/d:sonar.qualitygate.wait=true \
/d:sonar.cs.vscoveragexml.reportsPaths=coverage.xml \
/d:sonar.exclusions="**/*.css,**/*.scss,**/Models/*,**/Program.cs" \
/d:sonar.exclusions="**/*.css,**/*.scss,**/Models/*,**/Program.cs,**/WebApplicationBuilderExtensions.cs" \
/d:sonar.test.exclusions="Childrens-Social-Care-CPD-Tests/**/*" \
/d:sonar.token="${{ secrets.SONAR_TOKEN }}" \
/d:sonar.host.url="https://sonarcloud.io"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<RootNamespace>Childrens_Social_Care_CPD_Tests</RootNamespace>
<Nullable>disable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="contentful.csharp" Version="7.2.12" />
<PackageReference Include="FluentAssertions" Version="6.11.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.16" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
using Childrens_Social_Care_CPD;
using Childrens_Social_Care_CPD.Configuration;
using FluentAssertions;
using NUnit.Framework;
using System;

namespace Childrens_Social_Care_CPD_Tests;
namespace Childrens_Social_Care_CPD_Tests.Configuration;

[NonParallelizable]
public class ApplicationConfigurationTests
{
private const string Value = "foo";

private void ClearEnvironmentVariables()
private static void ClearEnvironmentVariables()
{
Environment.SetEnvironmentVariable("VCS-TAG", null);
Environment.SetEnvironmentVariable("CPD_INSTRUMENTATION_CONNECTIONSTRING", null);
Expand All @@ -23,6 +23,7 @@ private void ClearEnvironmentVariables()
Environment.SetEnvironmentVariable("VCS-REF", null);
Environment.SetEnvironmentVariable("CPD_GOOGLEANALYTICSTAG", null);
Environment.SetEnvironmentVariable("CPD_DISABLE_SECURE_COOKIES", null);
Environment.SetEnvironmentVariable("CPD_FEATURE_POLLING_INTERVAL", null);
}

[SetUp]
Expand Down Expand Up @@ -206,6 +207,32 @@ public void Returns_AppVersionEnvironment_Value()
actual.Should().Be(Value);
}

[Test]
public void Returns_FeaturePollInterval_Value()
{
// arrange
Environment.SetEnvironmentVariable("CPD_FEATURE_POLLING_INTERVAL", "10000");
var sut = new ApplicationConfiguration();

// act
var actual = sut.FeaturePollingInterval;

// assert
actual.Should().Be(10000);
}

[Test]
public void Returns_FeaturePollInterval_Default_Value()
{
// arrange
var sut = new ApplicationConfiguration();

// act
var actual = sut.FeaturePollingInterval;

// assert
actual.Should().Be(0);
}

[Test]
public void Returns_AzureEnvironment_Default_Value()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using Childrens_Social_Care_CPD.Configuration;
using Microsoft.Extensions.Logging;
using NSubstitute;
using NUnit.Framework;
using System.Threading;
using System.Threading.Tasks;
namespace Childrens_Social_Care_CPD_Tests.Configuration;

public class FeaturesConfigBackgroundServiceTests
{
private ILogger<FeaturesConfigBackgroundService> _logger;
private IApplicationConfiguration _applicationConfiguration;
private IFeaturesConfigUpdater _featuresConfigUpdater;

[SetUp]
public void Setup()
{
_logger = Substitute.For<ILogger<FeaturesConfigBackgroundService>>();

_applicationConfiguration = Substitute.For<IApplicationConfiguration>();
_featuresConfigUpdater = Substitute.For<IFeaturesConfigUpdater>();
}

[TestCase(1)]
[TestCase(2)]
public async Task Calls_Updater_At_Specified_Interval(int interval)
{
// arrange
_applicationConfiguration.FeaturePollingInterval.Returns(interval);
var featuresConfigBackgroundService = new FeaturesConfigBackgroundService(
_logger,
_applicationConfiguration,
_featuresConfigUpdater
);

// act
using (var cancellationTokenSource = new CancellationTokenSource())
{
var task = featuresConfigBackgroundService.StartAsync(cancellationTokenSource.Token);
await Task.Delay((int)(interval * 1100));
cancellationTokenSource.Cancel();
task.Wait();
}

// assert
await _featuresConfigUpdater.Received(1).UpdateFeaturesAsync(Arg.Any<CancellationToken>());
}

[Test]
public async Task Returns_If_Interval_Is_Zero()
{
// arrange
_applicationConfiguration.FeaturePollingInterval.Returns(0);
var featuresConfigBackgroundService = new FeaturesConfigBackgroundService(
_logger,
_applicationConfiguration,
_featuresConfigUpdater
);

// act
using (var cancellationTokenSource = new CancellationTokenSource())
{
var task = featuresConfigBackgroundService.StartAsync(cancellationTokenSource.Token);
await Task.Delay(50);
cancellationTokenSource.Cancel();
task.Wait();
}

// assert
await _featuresConfigUpdater.DidNotReceive().UpdateFeaturesAsync(Arg.Any<CancellationToken>());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Childrens_Social_Care_CPD.Configuration;
using FluentAssertions;
using NUnit.Framework;

namespace Childrens_Social_Care_CPD_Tests.Configuration;

public class FeaturesConfigTests
{
[Test]
public void UpdateFeaturesConfig_Adds_New_Feature()
{
// arrange
var featureName = "foo";
var featuresConfig = new FeaturesConfig();

// act
featuresConfig.AddOrUpdateFeature(featureName, true);

// assert
featuresConfig.IsEnabled(featureName).Should().BeTrue();
}

[Test]
public void IsEnabled_Returns_False_For_Non_Existant_Features()
{
// arrange
var featuresConfig = new FeaturesConfig();

// assert
featuresConfig.IsEnabled("foo").Should().BeFalse();
}

[Test]
public void UpdateFeaturesConfig_Updates_Features()
{
// arrange
var featureName = "foo";
var featuresConfig = new FeaturesConfig();

// act
featuresConfig.AddOrUpdateFeature(featureName, false);
featuresConfig.AddOrUpdateFeature(featureName, true);

// assert
featuresConfig.IsEnabled("foo").Should().BeTrue();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using Childrens_Social_Care_CPD.Configuration;
using Childrens_Social_Care_CPD.Contentful;
using Childrens_Social_Care_CPD.Contentful.Models;
using Contentful.Core.Models;
using Contentful.Core.Search;
using Microsoft.Extensions.Logging;
using NSubstitute;
using NUnit.Framework;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Threading;
using NSubstitute.ExceptionExtensions;

namespace Childrens_Social_Care_CPD_Tests.Configuration;

public class FeaturesConfigurationUpdaterTest
{
private ILogger<FeaturesConfigUpdater> _logger;
private ICpdContentfulClient _contentfulClient;
private IFeaturesConfig _featuresConfig;

[SetUp]
public void Setup()
{
_logger = Substitute.For<ILogger<FeaturesConfigUpdater>>();
_contentfulClient = Substitute.For<ICpdContentfulClient>();
_featuresConfig = Substitute.For<IFeaturesConfig>();
}

[Test]
public async Task Updates_Features()
{
// arrange
var featureName = "foo";
var applicationFeatures = new ApplicationFeatures
{
Features = new List<ApplicationFeature>
{
new ApplicationFeature
{
Name = featureName,
IsEnabled = true
}
}
};

_contentfulClient
.GetEntries(Arg.Any<QueryBuilder<ApplicationFeatures>>(), Arg.Any<CancellationToken>())
.Returns(
new ContentfulCollection<ApplicationFeatures>
{
Items = new List<ApplicationFeatures> {
applicationFeatures
}
}
);

var featuresConfigUpdater = new FeaturesConfigUpdater(_logger, _contentfulClient, _featuresConfig);

// act
using (var cancellationTokenSource = new CancellationTokenSource())
{
await featuresConfigUpdater.UpdateFeaturesAsync(cancellationTokenSource.Token);
}

// assert
_featuresConfig.Received().AddOrUpdateFeature(featureName, true);
}


[Test]
public async Task Poll_Ignores_Empty_Response()
{
// arrange
_contentfulClient
.GetEntries(Arg.Any<QueryBuilder<ApplicationFeatures>>(), Arg.Any<CancellationToken>())
.Returns(
new ContentfulCollection<ApplicationFeatures>
{
Items = new List<ApplicationFeatures>()
}
);

var featuresConfigUpdater = new FeaturesConfigUpdater(_logger, _contentfulClient, _featuresConfig);

// act
using (var cancellationTokenSource = new CancellationTokenSource())
{
await featuresConfigUpdater.UpdateFeaturesAsync(cancellationTokenSource.Token);
}

// assert
_featuresConfig.DidNotReceive().AddOrUpdateFeature(Arg.Any<string>(), Arg.Any<bool>());
}

[Test]
public async Task Poll_catches_exceptions()
{
// arrange
var exception = new TestException();
_contentfulClient
.GetEntries(Arg.Any<QueryBuilder<ApplicationFeatures>>(), Arg.Any<CancellationToken>())
.Throws(exception);

var featuresConfigUpdater = new FeaturesConfigUpdater(_logger, _contentfulClient, _featuresConfig);

// act
using (var cancellationTokenSource = new CancellationTokenSource())
{
await featuresConfigUpdater.UpdateFeaturesAsync(cancellationTokenSource.Token);
}

// assert
_logger.Received().LogError(exception, "Exception querying for feature configuration. Does the ApplicationFeatures model exist in Contentful?");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -366,5 +366,25 @@ public void Calls_Base_SyncNextResult()
_contentfulClient.Received().SyncNextResult(Arg.Any<string>(), default);
}

[Test]
public void Calls_Base_GetTag()
{
// act
_cpdClient.GetTag(string.Empty, default);

// assert
_contentfulClient.Received().GetTag(Arg.Any<string>(), default);
}

[Test]
public void Calls_Base_GetTags()
{
// act
_cpdClient.GetTags(string.Empty, default);

// assert
_contentfulClient.Received().GetTags(Arg.Any<string>(), default);
}

#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ namespace Childrens_Social_Care_CPD_Tests.Contentful;
public class EntityResolverTests
{
[Test]
[TestCase("areaOfPractice", typeof(AreaOfPractice))]
[TestCase("areaOfPracticeList", typeof(AreaOfPracticeList))]
[TestCase("applicationFeature", typeof(ApplicationFeature))]
[TestCase("applicationFeatures", typeof(ApplicationFeatures))]
[TestCase("columnLayout", typeof(ColumnLayout))]
[TestCase("content", typeof(Content))]
[TestCase("contentLink", typeof(ContentLink))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public partial class PartialsFactoryTests
{
public static object[] Successful_Resolves =
{
new object[] { new AreaOfPractice(), "_AreaOfPractice" },
new object[] { new AreaOfPracticeList(), "_AreaOfPracticeList" },
new object[] { new ColumnLayout(), "_ColumnLayout" },
new object[] { new Content(), "_Content" },
new object[] { new ContentLink(), "_ContentLink" },
Expand Down
Loading

0 comments on commit 2f8b603

Please sign in to comment.