-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feature/dp-970-incorrect-heading-supplier-in…
…formation
- Loading branch information
Showing
106 changed files
with
3,049 additions
and
927 deletions.
There are no files selected for viewing
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
74 changes: 74 additions & 0 deletions
74
Frontend/CO.CDP.OrganisationApp.Tests/CookieAcceptanceMiddlewareTests.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,74 @@ | ||
using Moq; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace CO.CDP.OrganisationApp.Tests; | ||
|
||
public class CookieAcceptanceMiddlewareTests | ||
{ | ||
private readonly Mock<ICookiePreferencesService> _cookiePreferencesServiceMock; | ||
private readonly Mock<RequestDelegate> _nextDelegateMock; | ||
private readonly CookieAcceptanceMiddleware _middleware; | ||
private readonly DefaultHttpContext _httpContext; | ||
|
||
public CookieAcceptanceMiddlewareTests() | ||
{ | ||
_cookiePreferencesServiceMock = new Mock<ICookiePreferencesService>(); | ||
_nextDelegateMock = new Mock<RequestDelegate>(); | ||
_middleware = new CookieAcceptanceMiddleware(_cookiePreferencesServiceMock.Object); | ||
_httpContext = new DefaultHttpContext(); | ||
} | ||
|
||
[Fact] | ||
public async Task InvokeAsync_ShouldCallAccept_WhenQueryParameterIsTrue() | ||
{ | ||
_httpContext.Request.QueryString = new QueryString($"?{CookieSettings.FtsHandoverParameter}=true"); | ||
|
||
await _middleware.InvokeAsync(_httpContext, _nextDelegateMock.Object); | ||
|
||
_cookiePreferencesServiceMock.Verify(s => s.Accept(), Times.Once); | ||
_nextDelegateMock.Verify(n => n(_httpContext), Times.Once); | ||
} | ||
|
||
[Fact] | ||
public async Task InvokeAsync_ShouldCallReject_WhenQueryParameterIsFalse() | ||
{ | ||
_httpContext.Request.QueryString = new QueryString($"?{CookieSettings.FtsHandoverParameter}=false"); | ||
|
||
await _middleware.InvokeAsync(_httpContext, _nextDelegateMock.Object); | ||
|
||
_cookiePreferencesServiceMock.Verify(s => s.Reject(), Times.Once); | ||
_nextDelegateMock.Verify(n => n(_httpContext), Times.Once); | ||
} | ||
|
||
[Fact] | ||
public async Task InvokeAsync_ShouldCallReset_WhenQueryParameterIsUnknown() | ||
{ | ||
_httpContext.Request.QueryString = new QueryString($"?{CookieSettings.FtsHandoverParameter}=unknown"); | ||
|
||
await _middleware.InvokeAsync(_httpContext, _nextDelegateMock.Object); | ||
|
||
_cookiePreferencesServiceMock.Verify(s => s.Reset(), Times.Once); | ||
_nextDelegateMock.Verify(n => n(_httpContext), Times.Once); | ||
} | ||
|
||
[Fact] | ||
public async Task InvokeAsync_ShouldNotCallAnyMethod_WhenQueryParameterIsMissing() | ||
{ | ||
await _middleware.InvokeAsync(_httpContext, _nextDelegateMock.Object); | ||
|
||
_cookiePreferencesServiceMock.VerifyNoOtherCalls(); | ||
_nextDelegateMock.Verify(n => n(_httpContext), Times.Once); | ||
} | ||
|
||
[Fact] | ||
public async Task InvokeAsync_ShouldNotCallAnyMethod_WhenQueryParameterHasInvalidValue() | ||
{ | ||
_httpContext.Request.QueryString = new QueryString($"?{CookieSettings.FtsHandoverParameter}=invalid"); | ||
|
||
await _middleware.InvokeAsync(_httpContext, _nextDelegateMock.Object); | ||
|
||
_cookiePreferencesServiceMock.VerifyNoOtherCalls(); | ||
_nextDelegateMock.Verify(n => n(_httpContext), Times.Once); | ||
} | ||
} | ||
|
103 changes: 103 additions & 0 deletions
103
Frontend/CO.CDP.OrganisationApp.Tests/CookiePreferencesServiceTests.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,103 @@ | ||
using Moq; | ||
using FluentAssertions; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace CO.CDP.OrganisationApp.Tests; | ||
|
||
public class CookiePreferencesServiceTests | ||
{ | ||
private readonly Mock<IHttpContextAccessor> _httpContextAccessorMock; | ||
private readonly Mock<HttpContext> _httpContextMock; | ||
private readonly Mock<HttpRequest> _httpRequestMock; | ||
private readonly Mock<HttpResponse> _httpResponseMock; | ||
private readonly Mock<IResponseCookies> _responseCookiesMock; | ||
private readonly Mock<IRequestCookieCollection> _requestCookiesMock; | ||
private readonly CookiePreferencesService _cookiePreferencesService; | ||
|
||
public CookiePreferencesServiceTests() | ||
{ | ||
_httpContextAccessorMock = new Mock<IHttpContextAccessor>(); | ||
_httpContextMock = new Mock<HttpContext>(); | ||
_httpRequestMock = new Mock<HttpRequest>(); | ||
_httpResponseMock = new Mock<HttpResponse>(); | ||
_responseCookiesMock = new Mock<IResponseCookies>(); | ||
_requestCookiesMock = new Mock<IRequestCookieCollection>(); | ||
|
||
_httpContextMock.Setup(c => c.Request).Returns(_httpRequestMock.Object); | ||
_httpContextMock.Setup(c => c.Response).Returns(_httpResponseMock.Object); | ||
_httpRequestMock.Setup(r => r.Cookies).Returns(_requestCookiesMock.Object); | ||
_httpResponseMock.Setup(r => r.Cookies).Returns(_responseCookiesMock.Object); | ||
|
||
_httpContextAccessorMock.Setup(a => a.HttpContext).Returns(_httpContextMock.Object); | ||
|
||
_cookiePreferencesService = new CookiePreferencesService(_httpContextAccessorMock.Object); | ||
} | ||
|
||
[Fact] | ||
public void Constructor_ShouldThrowException_WhenHttpContextIsNull() | ||
{ | ||
var accessor = new Mock<IHttpContextAccessor>(); | ||
accessor.Setup(a => a.HttpContext).Returns((HttpContext)null!); | ||
|
||
Action act = () => new CookiePreferencesService(accessor.Object); | ||
|
||
act.Should().Throw<InvalidOperationException>() | ||
.WithMessage("No active HTTP context."); | ||
} | ||
|
||
[Fact] | ||
public void Accept_ShouldSetCookieWithAcceptValue() | ||
{ | ||
_cookiePreferencesService.Accept(); | ||
|
||
_responseCookiesMock.Verify(c => c.Append(CookieSettings.CookieName, "Accept", It.IsAny<CookieOptions>()), Times.Once); | ||
_cookiePreferencesService.GetValue().Should().Be(CookieAcceptanceValues.Accept); | ||
_cookiePreferencesService.IsAccepted().Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void Reject_ShouldSetCookieWithRejectValue() | ||
{ | ||
_cookiePreferencesService.Reject(); | ||
|
||
_responseCookiesMock.Verify(c => c.Append(CookieSettings.CookieName, "Reject", It.IsAny<CookieOptions>()), Times.Once); | ||
_cookiePreferencesService.GetValue().Should().Be(CookieAcceptanceValues.Reject); | ||
_cookiePreferencesService.IsRejected().Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void Reset_ShouldDeleteCookie_AndSetPendingValueToUnknown() | ||
{ | ||
_cookiePreferencesService.Reset(); | ||
|
||
_responseCookiesMock.Verify(c => c.Delete(CookieSettings.CookieName), Times.Once); | ||
_cookiePreferencesService.GetValue().Should().Be(CookieAcceptanceValues.Unknown); | ||
_cookiePreferencesService.IsUnknown().Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void GetValue_ShouldReturnPendingValue_WhenSet() | ||
{ | ||
_cookiePreferencesService.Accept(); | ||
|
||
_cookiePreferencesService.GetValue().Should().Be(CookieAcceptanceValues.Accept); | ||
} | ||
|
||
[Fact] | ||
public void GetValue_ShouldReturnValueFromRequestCookie_WhenNoPendingValue() | ||
{ | ||
_requestCookiesMock.Setup(c => c.ContainsKey(CookieSettings.CookieName)).Returns(true); | ||
_requestCookiesMock.Setup(c => c[CookieSettings.CookieName]).Returns("Reject"); | ||
|
||
_cookiePreferencesService.GetValue().Should().Be(CookieAcceptanceValues.Reject); | ||
} | ||
|
||
[Fact] | ||
public void GetValue_ShouldReturnUnknown_WhenCookieIsNotPresent() | ||
{ | ||
_requestCookiesMock.Setup(c => c.ContainsKey(CookieSettings.CookieName)).Returns(false); | ||
|
||
_cookiePreferencesService.GetValue().Should().Be(CookieAcceptanceValues.Unknown); | ||
} | ||
} | ||
|
Oops, something went wrong.