+
Privacy notice for Find information about academies and trusts
diff --git a/DfE.FindInformationAcademiesTrusts/Setup/ConfigurationVariables.cs b/DfE.FindInformationAcademiesTrusts/Setup/ConfigurationVariables.cs
index 9e9fd92eb..521c4539c 100644
--- a/DfE.FindInformationAcademiesTrusts/Setup/ConfigurationVariables.cs
+++ b/DfE.FindInformationAcademiesTrusts/Setup/ConfigurationVariables.cs
@@ -1,5 +1,6 @@
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
+using DfE.FindInformationAcademiesTrusts.Extensions;
using DfE.FindInformationAcademiesTrusts.Options;
using Microsoft.FeatureManagement;
diff --git a/DfE.FindInformationAcademiesTrusts/Setup/LoggingSetup.cs b/DfE.FindInformationAcademiesTrusts/Setup/LoggingSetup.cs
index eb41dcfcd..bfcb3197c 100644
--- a/DfE.FindInformationAcademiesTrusts/Setup/LoggingSetup.cs
+++ b/DfE.FindInformationAcademiesTrusts/Setup/LoggingSetup.cs
@@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis;
+using DfE.FindInformationAcademiesTrusts.Extensions;
using Microsoft.ApplicationInsights.Extensibility;
using Serilog;
using Serilog.Extensions.Hosting;
diff --git a/DfE.FindInformationAcademiesTrusts/Setup/PostBuildSetup.cs b/DfE.FindInformationAcademiesTrusts/Setup/PostBuildSetup.cs
index 288a17542..918cab53f 100644
--- a/DfE.FindInformationAcademiesTrusts/Setup/PostBuildSetup.cs
+++ b/DfE.FindInformationAcademiesTrusts/Setup/PostBuildSetup.cs
@@ -1,5 +1,6 @@
using System.Diagnostics.CodeAnalysis;
using DfE.FindInformationAcademiesTrusts.Configuration;
+using DfE.FindInformationAcademiesTrusts.Extensions;
using Microsoft.AspNetCore.CookiePolicy;
using Serilog;
diff --git a/DfE.FindInformationAcademiesTrusts/Setup/SecurityServicesSetup.cs b/DfE.FindInformationAcademiesTrusts/Setup/SecurityServicesSetup.cs
index 9d297335f..9e79b2349 100644
--- a/DfE.FindInformationAcademiesTrusts/Setup/SecurityServicesSetup.cs
+++ b/DfE.FindInformationAcademiesTrusts/Setup/SecurityServicesSetup.cs
@@ -2,6 +2,8 @@
using System.Diagnostics.CodeAnalysis;
using Azure.Identity;
using DfE.FindInformationAcademiesTrusts.Authorization;
+using DfE.FindInformationAcademiesTrusts.Configuration;
+using DfE.FindInformationAcademiesTrusts.Extensions;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.DataProtection;
@@ -52,7 +54,7 @@ private static void AddIdentityServices(WebApplicationBuilder builder)
CookieAuthenticationDefaults.AuthenticationScheme,
options =>
{
- options.Cookie.Name = ".FindInformationAcademiesTrusts.Login";
+ options.Cookie.Name = FiatCookies.Login;
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
options.Cookie.SameSite = SameSiteMode.None;
@@ -62,7 +64,7 @@ private static void AddIdentityServices(WebApplicationBuilder builder)
private static void AddAntiForgeryCookies(WebApplicationBuilder builder)
{
- builder.Services.AddAntiforgery(opts => { opts.Cookie.Name = ".FindInformationAcademiesTrusts.Antiforgery"; });
+ builder.Services.AddAntiforgery(opts => { opts.Cookie.Name = FiatCookies.Antiforgery; });
}
private static void AddDataProtectionServices(WebApplicationBuilder builder)
diff --git a/tests/DFE.FindInformationAcademiesTrusts.CypressTests/cypress/e2e/regression/cookies.cy.ts b/tests/DFE.FindInformationAcademiesTrusts.CypressTests/cypress/e2e/regression/cookies.cy.ts
index 71566e5f3..7ce1af38b 100644
--- a/tests/DFE.FindInformationAcademiesTrusts.CypressTests/cypress/e2e/regression/cookies.cy.ts
+++ b/tests/DFE.FindInformationAcademiesTrusts.CypressTests/cypress/e2e/regression/cookies.cy.ts
@@ -31,7 +31,7 @@ describe('Cookie page and consent tests', () => {
cy.getCookie('_ga').should('exist');
//check mandatory cookies exist after saving
- cy.getCookie('.FindInformationAcademiesTrust.CookieConsent').should('exist');
+ cy.getCookie('.FindInformationAcademiesTrusts.CookieConsent').should('exist');
cy.getCookie('ASLBSA').should('exist');
cy.getCookie('ASLBSACORS').should('exist');
cy.getCookie('.FindInformationAcademiesTrusts.Antiforgery').should('exist');
@@ -55,7 +55,7 @@ describe('Cookie page and consent tests', () => {
cy.getCookie('_ga').should('not.exist');
//check mandatory cookies do not exist after saving
- cy.getCookie('.FindInformationAcademiesTrust.CookieConsent').should('exist');
+ cy.getCookie('.FindInformationAcademiesTrusts.CookieConsent').should('exist');
cy.getCookie('ASLBSA').should('exist');
cy.getCookie('ASLBSACORS').should('exist');
cy.getCookie('.FindInformationAcademiesTrusts.Antiforgery').should('exist');
diff --git a/tests/DfE.FindInformationAcademiesTrusts.UnitTests/CookiesHelperTests.cs b/tests/DfE.FindInformationAcademiesTrusts.UnitTests/CookiesHelperTests.cs
index c6e269fee..803065492 100644
--- a/tests/DfE.FindInformationAcademiesTrusts.UnitTests/CookiesHelperTests.cs
+++ b/tests/DfE.FindInformationAcademiesTrusts.UnitTests/CookiesHelperTests.cs
@@ -16,7 +16,7 @@ private void SetTempDataCookieDeleted()
[Fact]
public void OptionalCookiesAreAccepted_is_true_when_Accepted_cookie_exists()
{
- _mockContext.SetupAcceptedCookie();
+ _mockContext.MockRequestCookies.SetupAcceptedCookie();
var result = CookiesHelper.OptionalCookiesAreAccepted(_mockContext.Object, _mockTempData.Object);
result.Should().BeTrue();
}
@@ -24,7 +24,7 @@ public void OptionalCookiesAreAccepted_is_true_when_Accepted_cookie_exists()
[Fact]
public void OptionalCookiesAreAccepted_is_false_when_Rejected_cookie_exists()
{
- _mockContext.SetupRejectedCookie();
+ _mockContext.MockRequestCookies.SetupRejectedCookie();
var result = CookiesHelper.OptionalCookiesAreAccepted(_mockContext.Object, _mockTempData.Object);
result.Should().BeFalse();
}
@@ -35,7 +35,7 @@ public void OptionalCookiesAreAccepted_is_false_when_Rejected_cookie_exists()
[InlineData(null)]
public void OptionalCookiesAreAccepted_is_false_when_DeleteCookieTempData_exists(bool? cookieAccepted)
{
- _mockContext.SetupConsentCookie(cookieAccepted);
+ _mockContext.MockRequestCookies.SetupConsentCookie(cookieAccepted);
SetTempDataCookieDeleted();
var result = CookiesHelper.OptionalCookiesAreAccepted(_mockContext.Object, _mockTempData.Object);
@@ -81,7 +81,7 @@ public void ShowCookieBanner_is_true_when_consent_cookie_does_not_exist_and_temp
[InlineData(false)]
public void ShowCookieBanner_is_false_when_consent_cookie_exists_and_temp_data_does_not_exist(bool cookieAccepted)
{
- _mockContext.SetupConsentCookie(cookieAccepted);
+ _mockContext.MockRequestCookies.SetupConsentCookie(cookieAccepted);
var result = CookiesHelper.ShowCookieBanner(_mockContext.Object, _mockTempData.Object);
result.Should().BeFalse();
@@ -100,7 +100,7 @@ public void ShowCookieBanner_is_false_when_consent_cookie_does_not_exist_and_tem
[InlineData(false)]
public void ShowCookieBanner_is_false_when_consent_cookie_exists_and_temp_data_exists(bool cookieAccepted)
{
- _mockContext.SetupConsentCookie(cookieAccepted);
+ _mockContext.MockRequestCookies.SetupConsentCookie(cookieAccepted);
SetTempDataCookieDeleted();
var result = CookiesHelper.ShowCookieBanner(_mockContext.Object, _mockTempData.Object);
diff --git a/tests/DfE.FindInformationAcademiesTrusts.UnitTests/EnvironmentExtensionsTests.cs b/tests/DfE.FindInformationAcademiesTrusts.UnitTests/Extensions/EnvironmentExtensionsTests.cs
similarity index 96%
rename from tests/DfE.FindInformationAcademiesTrusts.UnitTests/EnvironmentExtensionsTests.cs
rename to tests/DfE.FindInformationAcademiesTrusts.UnitTests/Extensions/EnvironmentExtensionsTests.cs
index a08a493f5..1552282a8 100644
--- a/tests/DfE.FindInformationAcademiesTrusts.UnitTests/EnvironmentExtensionsTests.cs
+++ b/tests/DfE.FindInformationAcademiesTrusts.UnitTests/Extensions/EnvironmentExtensionsTests.cs
@@ -1,6 +1,7 @@
+using DfE.FindInformationAcademiesTrusts.Extensions;
using Microsoft.AspNetCore.Hosting;
-namespace DfE.FindInformationAcademiesTrusts.UnitTests;
+namespace DfE.FindInformationAcademiesTrusts.UnitTests.Extensions;
public class EnvironmentExtensionsTests
{
diff --git a/tests/DfE.FindInformationAcademiesTrusts.UnitTests/Mocks/MockHttpContext.cs b/tests/DfE.FindInformationAcademiesTrusts.UnitTests/Mocks/MockHttpContext.cs
index 81c711934..920ca2b4f 100644
--- a/tests/DfE.FindInformationAcademiesTrusts.UnitTests/Mocks/MockHttpContext.cs
+++ b/tests/DfE.FindInformationAcademiesTrusts.UnitTests/Mocks/MockHttpContext.cs
@@ -7,8 +7,8 @@ namespace DfE.FindInformationAcademiesTrusts.UnitTests.Mocks;
public class MockHttpContext : Mock
{
- private readonly Mock _mockResponseCookies = new();
- private readonly Mock _mockRequestCookies = new();
+ public MockResponseCookies MockResponseCookies { get; } = new();
+ public MockRequestCookies MockRequestCookies { get; } = new();
private readonly Mock _mockRequest = new();
private readonly Mock _mockFeatureCollection = new();
private readonly ClaimsIdentity _claimsIdentity = new();
@@ -16,19 +16,14 @@ public class MockHttpContext : Mock
public MockHttpContext()
{
Mock mockResponse = new();
- mockResponse.Setup(m => m.Cookies).Returns(_mockResponseCookies.Object);
+ mockResponse.Setup(m => m.Cookies).Returns(MockResponseCookies.Object);
ClaimsPrincipal user = new();
user.AddIdentity(_claimsIdentity);
- _mockRequest.Setup(m => m.Cookies).Returns(_mockRequestCookies.Object);
+ _mockRequest.Setup(m => m.Cookies).Returns(MockRequestCookies.Object);
_mockRequest.Setup(m => m.Query[It.IsAny()]).Returns("");
- _mockRequestCookies.Setup(m => m[It.IsAny()]).Returns("False");
- _mockRequestCookies.Setup(m => m.ContainsKey(".FindInformationAcademiesTrusts.Login")).Returns(true);
- _mockRequestCookies.Setup(m => m[".FindInformationAcademiesTrusts.Login"]).Returns("You are logged in");
- _mockRequestCookies.Setup(m => m.Keys).Returns(new List());
-
Setup(m => m.Request).Returns(_mockRequest.Object);
Setup(m => m.Response).Returns(mockResponse.Object);
Setup(m => m.Features).Returns(_mockFeatureCollection.Object);
@@ -40,42 +35,6 @@ public void AddUserClaim(string type, string value)
_claimsIdentity.AddClaim(new Claim(type, value));
}
- public void SetupConsentCookie(bool? accepted)
- {
- if (accepted is true)
- {
- SetupAcceptedCookie();
- }
- else if (accepted is false)
- {
- SetupRejectedCookie();
- }
- }
-
- public void SetupAcceptedCookie()
- {
- _mockRequestCookies.Setup(m => m.Keys).Returns(new List { CookiesHelper.ConsentCookieName });
- _mockRequestCookies.Setup(m => m.ContainsKey(CookiesHelper.ConsentCookieName)).Returns(true);
- _mockRequestCookies.Setup(m => m[CookiesHelper.ConsentCookieName]).Returns("True");
- }
-
- public void SetupRejectedCookie()
- {
- _mockRequestCookies.Setup(m => m.Keys).Returns(new List());
- _mockRequestCookies.Setup(m => m.ContainsKey(CookiesHelper.ConsentCookieName)).Returns(true);
- _mockRequestCookies.Setup(m => m[CookiesHelper.ConsentCookieName]).Returns("False");
- }
-
- public void SetupOptionalCookies()
- {
- _mockRequestCookies.Setup(m => m.Keys).Returns(new List { "ai_user", "ai_session", "_gid", "_ga" });
-
- _mockRequestCookies.Setup(m => m.ContainsKey("ai_user")).Returns(true);
- _mockRequestCookies.Setup(m => m["ai_user"]).Returns("True");
- _mockRequestCookies.Setup(m => m.ContainsKey("ai_session")).Returns(true);
- _mockRequestCookies.Setup(m => m["ai_session"]).Returns("True");
- }
-
public void SetQueryReturnPath(string path)
{
_mockRequest.Setup(m => m.Query[CookiesHelper.ReturnPathQuery]).Returns(path);
@@ -110,23 +69,4 @@ public void SetNotFoundUrl(string host, string path, string query)
_mockRequest.Setup(m => m.Host).Returns(new HostString(host));
}
-
- public void VerifySecureCookieAdded(string key, string value)
- {
- _mockResponseCookies.Verify(
- m => m.Append(key, value,
- It.Is(c => c.Secure == true && c.HttpOnly == true)), Times.Once);
- }
-
- public void VerifyCookieDeleted(string key)
- {
- _mockResponseCookies.Verify(
- m => m.Delete(key), Times.Once);
- }
-
- public void VerifyNoCookiesDeleted()
- {
- _mockResponseCookies.Verify(
- m => m.Delete(It.IsAny()), Times.Exactly(0));
- }
}
diff --git a/tests/DfE.FindInformationAcademiesTrusts.UnitTests/Mocks/MockRequestCookies.cs b/tests/DfE.FindInformationAcademiesTrusts.UnitTests/Mocks/MockRequestCookies.cs
new file mode 100644
index 000000000..9a00e925e
--- /dev/null
+++ b/tests/DfE.FindInformationAcademiesTrusts.UnitTests/Mocks/MockRequestCookies.cs
@@ -0,0 +1,46 @@
+using DfE.FindInformationAcademiesTrusts.Configuration;
+using Microsoft.AspNetCore.Http;
+
+namespace DfE.FindInformationAcademiesTrusts.UnitTests.Mocks;
+
+public class MockRequestCookies : Mock
+{
+ public Dictionary Data { get; } = new();
+
+ public MockRequestCookies()
+ {
+ Setup(m => m.Keys).Returns(Data.Keys);
+ Setup(m => m.ContainsKey(It.IsAny())).Returns((string key) => Data.ContainsKey(key));
+ Setup(m => m[It.IsAny()]).Returns((string key) => Data.TryGetValue(key, out var value) ? value : null);
+ }
+
+ public void SetupConsentCookie(bool? accepted)
+ {
+ if (accepted is true)
+ {
+ SetupAcceptedCookie();
+ }
+ else if (accepted is false)
+ {
+ SetupRejectedCookie();
+ }
+ }
+
+ public void SetupAcceptedCookie()
+ {
+ Data.Add(FiatCookies.CookieConsent, "True");
+ }
+
+ public void SetupRejectedCookie()
+ {
+ Data.Add(FiatCookies.CookieConsent, "False");
+ }
+
+ public void SetupOptionalCookies()
+ {
+ Data.Add("ai_user", "True");
+ Data.Add("ai_session", "True");
+ Data.Add("_gid", "True");
+ Data.Add("_ga", "True");
+ }
+}
diff --git a/tests/DfE.FindInformationAcademiesTrusts.UnitTests/Mocks/MockResponseCookies.cs b/tests/DfE.FindInformationAcademiesTrusts.UnitTests/Mocks/MockResponseCookies.cs
new file mode 100644
index 000000000..467e3b87b
--- /dev/null
+++ b/tests/DfE.FindInformationAcademiesTrusts.UnitTests/Mocks/MockResponseCookies.cs
@@ -0,0 +1,33 @@
+using Microsoft.AspNetCore.Http;
+
+namespace DfE.FindInformationAcademiesTrusts.UnitTests.Mocks;
+
+public class MockResponseCookies : Mock
+{
+ public void VerifySecureCookieAdded(string key, string value)
+ {
+ Verify(m => m.Append(key, value,
+ It.Is(c => c.Secure == true && c.HttpOnly == true)), Times.Once);
+ }
+
+ public void VerifyCookieDeleted(string key)
+ {
+ Verify(m => m.Delete(key), Times.Once);
+ }
+
+ public void VerifyCookieDeleted(string key, CookieOptions options)
+ {
+ Verify(m => m.Delete(key, It.Is(cookieOptions =>
+ cookieOptions.HttpOnly == options.HttpOnly
+ && cookieOptions.IsEssential == options.IsEssential
+ && cookieOptions.SameSite == options.SameSite
+ && cookieOptions.Secure == options.Secure
+ )),
+ Times.Once);
+ }
+
+ public void VerifyNoCookiesDeleted()
+ {
+ Verify(m => m.Delete(It.IsAny()), Times.Exactly(0));
+ }
+}
diff --git a/tests/DfE.FindInformationAcademiesTrusts.UnitTests/Pages/CookiesModelTests.cs b/tests/DfE.FindInformationAcademiesTrusts.UnitTests/Pages/CookiesModelTests.cs
index 142efa195..8bd0b2544 100644
--- a/tests/DfE.FindInformationAcademiesTrusts.UnitTests/Pages/CookiesModelTests.cs
+++ b/tests/DfE.FindInformationAcademiesTrusts.UnitTests/Pages/CookiesModelTests.cs
@@ -1,3 +1,4 @@
+using DfE.FindInformationAcademiesTrusts.Configuration;
using DfE.FindInformationAcademiesTrusts.Pages;
using DfE.FindInformationAcademiesTrusts.UnitTests.Mocks;
using Microsoft.AspNetCore.Http;
@@ -92,7 +93,7 @@ public void OnPost_should_replace_return_path_when_it_is_invalid(string? path)
[InlineData(false, false)]
public void OnGet_should_not_change_consent_when_it_is_provided(bool consent, bool? accepted)
{
- _mockHttpContext.SetupConsentCookie(accepted);
+ _mockHttpContext.MockRequestCookies.SetupConsentCookie(accepted);
_sut.Consent = consent;
_sut.OnGet();
_sut.Consent.Should().Be(consent);
@@ -107,7 +108,7 @@ public void OnGet_should_not_change_consent_when_it_is_provided(bool consent, bo
[InlineData(false, false)]
public void OnPost_should_not_change_consent_when_it_is_provided(bool consent, bool? accepted)
{
- _mockHttpContext.SetupConsentCookie(accepted);
+ _mockHttpContext.MockRequestCookies.SetupConsentCookie(accepted);
_sut.Consent = consent;
_sut.OnPost();
_sut.Consent.Should().Be(consent);
@@ -118,7 +119,7 @@ public void OnPost_should_not_change_consent_when_it_is_provided(bool consent, b
[InlineData(false)]
public void OnGet_should_set_consent_when_it_not_provided_and_the_cookie_has_been_set(bool accepted)
{
- _mockHttpContext.SetupConsentCookie(accepted);
+ _mockHttpContext.MockRequestCookies.SetupConsentCookie(accepted);
_sut.OnGet();
_sut.Consent.Should().Be(accepted);
}
@@ -183,7 +184,7 @@ public void OnGet_adds_consent_cookie_when_consent_is_given(bool consent, string
{
_sut.Consent = consent;
_sut.OnGet();
- _mockHttpContext.VerifySecureCookieAdded(CookiesHelper.ConsentCookieName, value);
+ _mockHttpContext.MockResponseCookies.VerifySecureCookieAdded(FiatCookies.CookieConsent, value);
}
[Theory]
@@ -193,7 +194,7 @@ public void OnPost_adds_consent_cookie_when_consent_is_given(bool consent, strin
{
_sut.Consent = consent;
_sut.OnPost();
- _mockHttpContext.VerifySecureCookieAdded(CookiesHelper.ConsentCookieName, value);
+ _mockHttpContext.MockResponseCookies.VerifySecureCookieAdded(FiatCookies.CookieConsent, value);
}
// (Cookie appended Delete called if needed/TempData is not null)
@@ -204,10 +205,10 @@ public void OnPost_adds_consent_cookie_when_consent_is_given(bool consent, strin
[InlineData("_gid")]
public void OnGet_removes_optional_cookie_when_consent_is_false(string cookieName)
{
- _mockHttpContext.SetupOptionalCookies();
+ _mockHttpContext.MockRequestCookies.SetupOptionalCookies();
_sut.Consent = false;
_sut.OnGet();
- _mockHttpContext.VerifyCookieDeleted(cookieName);
+ _mockHttpContext.MockResponseCookies.VerifyCookieDeleted(cookieName);
}
[Theory]
@@ -217,48 +218,48 @@ public void OnGet_removes_optional_cookie_when_consent_is_false(string cookieNam
[InlineData("_gid")]
public void OnPost_removes_optional_cookie_when_consent_is_false(string cookieName)
{
- _mockHttpContext.SetupOptionalCookies();
+ _mockHttpContext.MockRequestCookies.SetupOptionalCookies();
_sut.Consent = false;
_sut.OnPost();
- _mockHttpContext.VerifyCookieDeleted(cookieName);
+ _mockHttpContext.MockResponseCookies.VerifyCookieDeleted(cookieName);
}
[Fact]
public void OnGet_does_not_remove_cookies_when_consent_is_true()
{
- _mockHttpContext.SetupOptionalCookies();
- _mockHttpContext.SetupAcceptedCookie();
+ _mockHttpContext.MockRequestCookies.SetupOptionalCookies();
+ _mockHttpContext.MockRequestCookies.SetupAcceptedCookie();
_sut.Consent = true;
_sut.OnGet();
- _mockHttpContext.VerifyNoCookiesDeleted();
+ _mockHttpContext.MockResponseCookies.VerifyNoCookiesDeleted();
}
[Fact]
public void OnPost_does_not_remove_cookies_when_consent_is_true()
{
- _mockHttpContext.SetupOptionalCookies();
- _mockHttpContext.SetupAcceptedCookie();
+ _mockHttpContext.MockRequestCookies.SetupOptionalCookies();
+ _mockHttpContext.MockRequestCookies.SetupAcceptedCookie();
_sut.Consent = true;
_sut.OnPost();
- _mockHttpContext.VerifyNoCookiesDeleted();
+ _mockHttpContext.MockResponseCookies.VerifyNoCookiesDeleted();
}
[Fact]
public void OnGet_does_not_remove_cookies_when_there_are_no_optional_cookies_to_remove()
{
- _mockHttpContext.SetupRejectedCookie();
+ _mockHttpContext.MockRequestCookies.SetupRejectedCookie();
_sut.Consent = false;
_sut.OnGet();
- _mockHttpContext.VerifyNoCookiesDeleted();
+ _mockHttpContext.MockResponseCookies.VerifyNoCookiesDeleted();
}
[Fact]
public void OnPost_does_not_remove_cookies_when_there_are_no_optional_cookies_to_remove()
{
- _mockHttpContext.SetupRejectedCookie();
+ _mockHttpContext.MockRequestCookies.SetupRejectedCookie();
_sut.Consent = false;
_sut.OnPost();
- _mockHttpContext.VerifyNoCookiesDeleted();
+ _mockHttpContext.MockResponseCookies.VerifyNoCookiesDeleted();
}
//TempData