Skip to content

Commit

Permalink
Merge branch 'main' into feature/dp-970-incorrect-heading-supplier-in…
Browse files Browse the repository at this point in the history
…formation
  • Loading branch information
JBaigGoaco committed Dec 10, 2024
2 parents 4bf2c80 + 9810ef5 commit 543813f
Show file tree
Hide file tree
Showing 106 changed files with 3,049 additions and 927 deletions.
15 changes: 9 additions & 6 deletions Frontend/CO.CDP.OrganisationApp.Tests/AuthorizationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ private HttpClient BuildHttpClient(List<string> userOrganisationScopes, List<str
var organisation = new UserOrganisation(
id: OrganisationId,
name: "Org name",
type: Tenant.WebApiClient.OrganisationType.Organisation,
roles:
[
Tenant.WebApiClient.PartyRole.Supplier,
Expand Down Expand Up @@ -81,7 +82,8 @@ [ organisation ]
id: OrganisationId,
identifier: new Identifier("asd", "asd", "asd", new Uri("http://whatever")),
name: "Org name",
roles: [ Organisation.WebApiClient.PartyRole.Supplier, Organisation.WebApiClient.PartyRole.Tenderer ],
type: Organisation.WebApiClient.OrganisationType.Organisation,
roles: [Organisation.WebApiClient.PartyRole.Supplier, Organisation.WebApiClient.PartyRole.Tenderer],
details: new Details(approval: null, pendingRoles: [])
)
);
Expand All @@ -107,7 +109,8 @@ [ organisation ]
services.AddTransient<IPersonClient, PersonClient>(_ => PersonClient.Object);
services.AddSingleton(Session.Object);
services.AddTransient<IAuthenticationSchemeProvider, FakeSchemeProvider>();
services.Configure<OpenIdConnectOptions>(OpenIdConnectDefaults.AuthenticationScheme, options => {
services.Configure<OpenIdConnectOptions>(OpenIdConnectDefaults.AuthenticationScheme, options =>
{
options.ClientId = "123";
options.Authority = "https://whatever";
});
Expand Down Expand Up @@ -183,7 +186,7 @@ public async Task TestAuthorizationIsUnsuccessful_WhenUserIsNotAllowedToAccessRe
[Fact]
public async Task TestCanSeeUsersLinkOnOrganisationPage_WhenUserIsAllowedToAccessResourceAsAdminUser()
{
var httpClient = BuildHttpClient([ OrganisationPersonScopes.Admin, OrganisationPersonScopes.Viewer ], []);
var httpClient = BuildHttpClient([OrganisationPersonScopes.Admin, OrganisationPersonScopes.Viewer], []);

var request = new HttpRequestMessage(HttpMethod.Get, $"/organisation/{OrganisationId}");

Expand All @@ -200,7 +203,7 @@ public async Task TestCanSeeUsersLinkOnOrganisationPage_WhenUserIsAllowedToAcces
[Fact]
public async Task TestCannotSeeUsersLinkOnOrganisationPage_WhenUserIsNotAllowedToAccessResourceAsEditorUser()
{
var httpClient = BuildHttpClient([ OrganisationPersonScopes.Editor ], []);
var httpClient = BuildHttpClient([OrganisationPersonScopes.Editor], []);

var request = new HttpRequestMessage(HttpMethod.Get, $"/organisation/{OrganisationId}");

Expand All @@ -219,8 +222,8 @@ public async Task TestCannotSeeUsersLinkOnOrganisationPage_WhenUserIsNotAllowedT
public static IEnumerable<object[]> SupportAdminAccessTestCases()
{
yield return new object[] { $"/organisation/{OrganisationId}", new[] { "Organisation name", "Organisation identifier", "Organisation email", "Complete supplier information" }, new[] { "Change", "Users" }, HttpStatusCode.OK };
yield return new object[] { $"/organisation/{OrganisationId}/address/uk?frm-overview", new string[] {}, new string[] {}, HttpStatusCode.NotFound };
yield return new object[] { $"/organisation/{OrganisationId}/users/user-summary", new string[] {}, new string[] {}, HttpStatusCode.NotFound };
yield return new object[] { $"/organisation/{OrganisationId}/address/uk?frm-overview", new string[] { }, new string[] { }, HttpStatusCode.NotFound };
yield return new object[] { $"/organisation/{OrganisationId}/users/user-summary", new string[] { }, new string[] { }, HttpStatusCode.NotFound };
}

[Theory]
Expand Down
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 Frontend/CO.CDP.OrganisationApp.Tests/CookiePreferencesServiceTests.cs
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);
}
}

Loading

0 comments on commit 543813f

Please sign in to comment.