Skip to content

Commit

Permalink
VIH-11075 Sonar fixes (#1438)
Browse files Browse the repository at this point in the history
* BFF basic fixes

* FE fixes

* Remove redundant function

* Fix sonar lint analysis

* More code coverage

* FE basic fixes

* More fixes

* Fix for empty function warnings

* Additional small fix

* Split ReferenceDataController into multiple controllers

* More fixes

* Whitespace changes to trigger sonar checks

* Comment to trigger sonar checks

* Add ignore rule for method parameter count

* Revert "Whitespace changes to trigger sonar checks"

This reverts commit 9253160.

* Revert "Comment to trigger sonar checks"

This reverts commit 70cca0d.
  • Loading branch information
oliver-scott authored Oct 30, 2024
1 parent c6a411e commit eea8e5d
Show file tree
Hide file tree
Showing 191 changed files with 1,110 additions and 1,040 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<PackageReference Include="nunit" Version="4.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.25.0.90414">
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.32.0.97167">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
Expand Down
63 changes: 0 additions & 63 deletions AdminWebsite/AdminWebsite.IntegrationTests/Helper/ApiUriFactory.cs

This file was deleted.

6 changes: 3 additions & 3 deletions AdminWebsite/AdminWebsite.IntegrationTests/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@
},
"SonarAnalyzer.CSharp": {
"type": "Direct",
"requested": "[9.25.0.90414, )",
"resolved": "9.25.0.90414",
"contentHash": "T5zhLMv1uC98eDXZtLNOcS+fIlRJZ8VNlWLaXVlgmI7wPuYwTPDpfaKXB0UfTMFAvnMCAsG2kFpdRszUY3kh5g=="
"requested": "[9.32.0.97167, )",
"resolved": "9.32.0.97167",
"contentHash": "Yxk86RV+8ynJpUhku1Yw2hITFmnmXKkXJ73cIFSy85ol5SnWREQg9RuTyV8nI7V7+pyLKpCfRmD7P0widsgjkg=="
},
"AspNetCore.HealthChecks.Uris": {
"type": "Transitive",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ namespace AdminWebsite.Testing.Common.Builders
public class ClaimsPrincipalBuilder
{
private const string Username = "[email protected]";

private readonly List<Claim> _claims;

private static class ClaimTypeNames
{
public const string PreferredUsername = "preferred_username";
}

public ClaimsPrincipalBuilder()
{
_claims = new List<Claim>
{
new Claim("preferred_username", Username),
new(ClaimTypeNames.PreferredUsername, Username),
new Claim(ClaimTypes.NameIdentifier, "userId"),
new Claim("name", "John Doe")
};
Expand All @@ -26,9 +31,9 @@ public ClaimsPrincipalBuilder WithRole(string role)

public ClaimsPrincipalBuilder WithUsername(string username)
{
var usernameClaimIndex = _claims.FindIndex(x => x.Type == "preferred_username");
var usernameClaimIndex = _claims.FindIndex(x => x.Type == ClaimTypeNames.PreferredUsername);
_claims.RemoveAt(usernameClaimIndex);
return WithClaim("preferred_username", username);
return WithClaim(ClaimTypeNames.PreferredUsername, username);
}

public ClaimsPrincipalBuilder WithClaim(string claimType, string value)
Expand All @@ -39,7 +44,7 @@ public ClaimsPrincipalBuilder WithClaim(string claimType, string value)

public ClaimsPrincipal Build()
{
var identity = new ClaimsIdentity(_claims, "TestAuthType", "preferred_username", ClaimTypes.Role);
var identity = new ClaimsIdentity(_claims, "TestAuthType", ClaimTypeNames.PreferredUsername, ClaimTypes.Role);
var claimsPrincipal = new ClaimsPrincipal(identity);
return claimsPrincipal;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<PackageReference Include="nunit" Version="4.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.25.0.90414">
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.32.0.97167">
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,62 +89,6 @@ public async Task Should_book_hearing()
_mocker.Mock<IBookingsApiClient>().Verify(x => x.BookNewHearingWithCodeAsync(It.IsAny<BookNewHearingRequestV2>()), Times.Once);
}

private BookingDetailsRequest InitHearingForTest()
{
// request with existing person, new user, existing user in AD but not in persons table
var bookNewHearingRequest = new BookingDetailsRequest
{
Participants = new List<AdminWebsite.Contracts.Requests.ParticipantRequest>
{
new ()
{
CaseRoleName = "CaseRole", ContactEmail = "[email protected]",
HearingRoleName = "HearingRole", DisplayName = "display name1",
FirstName = "fname", MiddleNames = "", LastName = "lname1", Username = "[email protected]",
OrganisationName = "", Representee = "", TelephoneNumber = ""
},
new ()
{
CaseRoleName = "CaseRole", ContactEmail = "[email protected]",
HearingRoleName = "HearingRole", DisplayName = "display name2",
FirstName = "fname2", MiddleNames = "", LastName = "lname2", OrganisationName = "",
Representee = "", TelephoneNumber = "", Username = "[email protected]"
},
new ()
{
CaseRoleName = "CaseRole", ContactEmail = "[email protected]",
HearingRoleName = "HearingRole", DisplayName = "display name3",
FirstName = "fname3", MiddleNames = "", LastName = "lname3", OrganisationName = "",
Representee = "", TelephoneNumber = "", Username = "[email protected]"
},
new ()
{
CaseRoleName = "Panel Member", ContactEmail = "[email protected]",
HearingRoleName = "HearingRole", DisplayName = "display name4",
FirstName = "fname4", MiddleNames = "", LastName = "lname4", OrganisationName = "",
Representee = "", TelephoneNumber = "", Username = "[email protected]"
},
new ()
{
CaseRoleName = "Judge", ContactEmail = "[email protected]",
HearingRoleName = "Judge", DisplayName = "Judge Fudge",
FirstName = "Jack", MiddleNames = "", LastName = "Fudge",
Username = "[email protected]", OrganisationName = "", Representee = "",
TelephoneNumber = ""
}
},
Endpoints = new List<EndpointRequest>
{
new ()
{DisplayName = "displayname1", DefenceAdvocateContactEmail = "[email protected]"},
new ()
{DisplayName = "displayname2", DefenceAdvocateContactEmail = "[email protected]"},
}
};

return SetUpRequest(bookNewHearingRequest);
}

private BookingDetailsRequest InitHearingForV2Test()
{
// request with existing person, new user, existing user in AD but not in persons table
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using AdminWebsite.Models;
using System.Net;
using AdminWebsite.Models;
using AdminWebsite.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using BookingsApi.Client;
using BookingsApi.Contract.V1.Requests.Enums;
using Autofac.Extras.Moq;
using BookingsApi.Contract.V1.Requests;
using BookingsApi.Contract.V1.Responses;
using VideoApi.Contract.Responses;
using ParticipantResponse = BookingsApi.Contract.V1.Responses.ParticipantResponse;
Expand Down Expand Up @@ -58,5 +60,88 @@ public async Task Should_update_status_of_hearing_to_cancelled_given_status_and_
.And.BeAssignableTo<UpdateBookingStatusResponse>()
.Subject.Success.Should().BeTrue();
}

[Test]
public async Task Should_return_bad_request_when_bookings_api_returns_bad_request()
{
// Arrange
var bookingId = Guid.NewGuid();
var exception = new BookingsApiException<string>("BadRequest",
(int)HttpStatusCode.BadRequest,
"BadRequest",
null,
"BadRequest",
null);
_mocker.Mock<IBookingsApiClient>().Setup(x => x.CancelBookingAsync(bookingId, It.IsAny<CancelBookingRequest>()))
.ThrowsAsync(exception);

// Act
var response = await _controller.CancelBooking(bookingId, "Reason");

// Assert
var result = response as BadRequestObjectResult;
result.StatusCode.Should().Be(StatusCodes.Status400BadRequest);
}

[Test]
public async Task Should_return_not_found_when_bookings_api_returns_not_found()
{
// Arrange
var bookingId = Guid.NewGuid();
var exception = new BookingsApiException<string>("NotFound",
(int)HttpStatusCode.NotFound,
"NotFound",
null,
"NotFound",
null);
_mocker.Mock<IBookingsApiClient>().Setup(x => x.CancelBookingAsync(bookingId, It.IsAny<CancelBookingRequest>()))
.ThrowsAsync(exception);

// Act
var response = await _controller.CancelBooking(bookingId, "Reason");

// Assert
var result = response as NotFoundObjectResult;
result.StatusCode.Should().Be(StatusCodes.Status404NotFound);
}

[Test]
public async Task Should_return_bad_request_when_bookings_api_returns_forbidden()
{
// Arrange
var bookingId = Guid.NewGuid();
var exception = new BookingsApiException<string>("Forbidden",
(int)HttpStatusCode.Forbidden,
"Forbidden",
null,
"Forbidden",
null);
_mocker.Mock<IBookingsApiClient>().Setup(x => x.CancelBookingAsync(bookingId, It.IsAny<CancelBookingRequest>()))
.ThrowsAsync(exception);

// Act
var response = await _controller.CancelBooking(bookingId, "Reason");

// Assert
var result = response as ObjectResult;
result.StatusCode.Should().Be(StatusCodes.Status400BadRequest);
}

[Test]
public async Task Should_return_bad_request_when_other_exception_thrown()
{
// Arrange
var bookingId = Guid.NewGuid();
var exception = new InvalidOperationException();
_mocker.Mock<IBookingsApiClient>().Setup(x => x.CancelBookingAsync(bookingId, It.IsAny<CancelBookingRequest>()))
.ThrowsAsync(exception);

// Act
var response = await _controller.CancelBooking(bookingId, "Reason");

// Assert
var result = response as ObjectResult;
result.StatusCode.Should().Be(StatusCodes.Status400BadRequest);
}
}
}
Loading

0 comments on commit eea8e5d

Please sign in to comment.