Skip to content

Commit 6afba99

Browse files
Dave Thompsonkaren-hedges
andauthored
DMP-5162 Better differentiate between 403 and 401 errors (#2982)
Co-authored-by: karen-hedges <[email protected]>
1 parent 0a330a7 commit 6afba99

20 files changed

+103
-71
lines changed

src/integrationTest/java/uk/gov/hmcts/darts/audio/controller/AudioRequestsControllerAddAudioRequestDownloadIntTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ void beforeEach() {
8787
}
8888

8989
@Test
90-
void addAudioRequestPostShouldReturnForbiddenError() throws Exception {
90+
void addAudioRequestPost_shouldReturn401Error_whenUserNotFound() throws Exception {
9191

9292
when(mockUserIdentity.getUserAccount()).thenReturn(null);
9393

@@ -97,7 +97,7 @@ void addAudioRequestPostShouldReturnForbiddenError() throws Exception {
9797
.header("Content-Type", "application/json")
9898
.content(objectMapper.writeValueAsString(audioRequestDetails));
9999

100-
mockMvc.perform(requestBuilder).andExpect(status().isForbidden());
100+
mockMvc.perform(requestBuilder).andExpect(status().isUnauthorized());
101101
}
102102

103103
@Test

src/integrationTest/java/uk/gov/hmcts/darts/audio/controller/AudioRequestsControllerAddAudioRequestPlaybackIntTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ void beforeEach() {
8686
}
8787

8888
@Test
89-
void addAudioRequestPostShouldReturnForbiddenError() throws Exception {
89+
void addAudioRequestPost_shouldReturn401Error_whenNoUserAccount() throws Exception {
9090

9191
when(mockUserIdentity.getUserAccount()).thenReturn(null);
9292

@@ -96,7 +96,7 @@ void addAudioRequestPostShouldReturnForbiddenError() throws Exception {
9696
.header("Content-Type", "application/json")
9797
.content(objectMapper.writeValueAsString(audioRequestDetails));
9898

99-
mockMvc.perform(requestBuilder).andExpect(status().isForbidden());
99+
mockMvc.perform(requestBuilder).andExpect(status().isUnauthorized());
100100
}
101101

102102
@Test

src/integrationTest/java/uk/gov/hmcts/darts/authentication/controller/impl/TokenValidatorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ void testInactiveUserCheck() throws Exception {
115115

116116
mockMvc.perform(get(ENDPOINT_URL).header("Authorization", "Bearer " + tokenDetails.getToken()))
117117
.andExpect(status().isForbidden())
118-
.andExpect(content().json("{\"type\":\"AUTHORISATION_106\",\"title\":\"Could not obtain user details\",\"status\":403}"))
118+
.andExpect(content().json("{\"type\":\"AUTHORISATION_114\",\"title\":\"User is not active\",\"status\":403}"))
119119
.andReturn();
120120
}
121121

src/integrationTest/java/uk/gov/hmcts/darts/authorisation/controller/AuthorisationControllerIntTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ void getUserStateShouldSucceedWhenUserHasADiverseSetOfAssignedGroups() throws Ex
465465

466466
@Test
467467
@WithMockSecurityContextWithEmailAddress(emailAddress = EMAIL_ADDRESS)
468-
void getUserStateShouldFailWhenUserIsInactive() throws Exception {
468+
void getUserState_shouldReturn403Error_whenUserIsInactive() throws Exception {
469469
// Given
470470
createAndSaveUser(EMAIL_ADDRESS, false);
471471

@@ -478,8 +478,8 @@ void getUserStateShouldFailWhenUserIsInactive() throws Exception {
478478
// Then
479479
JSONAssert.assertEquals("""
480480
{
481-
"type": "AUTHORISATION_106",
482-
"title": "Could not obtain user details",
481+
"type": "AUTHORISATION_114",
482+
"title": "User is not active",
483483
"status": 403
484484
}""",
485485
mvcResult.getResponse().getContentAsString(),
@@ -489,22 +489,22 @@ void getUserStateShouldFailWhenUserIsInactive() throws Exception {
489489

490490
@Test
491491
@WithMockSecurityContextWithEmailAddress(emailAddress = "[email protected]")
492-
void getUserStateShouldFailWhenUserEmailDoesNotExistInDatabase() throws Exception {
492+
void getUserState_shouldReturn401Error_whenUserEmailDoesNotExistInDatabase() throws Exception {
493493
// Given
494494
createAndSaveUser(EMAIL_ADDRESS, true);
495495

496496
// When
497497
MvcResult mvcResult = mockMvc.perform(
498498
get(ENDPOINT))
499-
.andExpect(status().isForbidden())
499+
.andExpect(status().isUnauthorized())
500500
.andReturn();
501501

502502
// Then
503503
JSONAssert.assertEquals("""
504504
{
505505
"type": "AUTHORISATION_106",
506506
"title": "Could not obtain user details",
507-
"status": 403
507+
"status": 401
508508
}""",
509509
mvcResult.getResponse().getContentAsString(),
510510
JSONCompareMode.NON_EXTENSIBLE

src/integrationTest/java/uk/gov/hmcts/darts/cases/controller/CaseControllerGetAnnotationsIntTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,16 @@ void givenSuperUserReturnForbidden() throws Exception {
207207
verifyNoMoreInteractions(mockUserIdentity);
208208
}
209209

210+
@Test
211+
void casesGetAnnotationsEndpoint_shouldReturn401Error_whenUserNotFound() throws Exception {
212+
CourtCaseEntity courtCaseEntity = dartsDatabase.createCase("Bristol", "case1");
213+
214+
mockMvc.perform(get("/cases/" + courtCaseEntity.getId() + "/annotations")
215+
.header("user_id", "9999999999"))
216+
217+
.andExpect(status().isUnauthorized());
218+
}
219+
210220
@Test
211221
void givenUserRequestsNonExistingHearingThenReturn404() throws Exception {
212222
UserAccountEntity testUser = dartsDatabase.getUserAccountStub()

src/integrationTest/java/uk/gov/hmcts/darts/cases/controller/CaseControllerGetCaseByIdTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ private void setupData() {
6363
}
6464

6565
@Test
66-
void casesSearchGetEndpointShouldReturnForbiddenError() throws Exception {
66+
void casesSearchGetEndpoint_shouldReturn401Error_whenUserNotFound() throws Exception {
6767
setupData();
6868
when(mockUserIdentity.getUserAccount()).thenReturn(null);
6969

7070
MockHttpServletRequestBuilder requestBuilder = get(endpointUrl, getCaseId(SOME_CASE_NUMBER, SOME_COURTHOUSE));
7171

72-
mockMvc.perform(requestBuilder).andExpect(status().isForbidden());
72+
mockMvc.perform(requestBuilder).andExpect(status().isUnauthorized());
7373
}
7474

7575
@Test

src/integrationTest/java/uk/gov/hmcts/darts/cases/controller/CaseControllerGetCaseHearingsTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ void setUp() {
7878
}
7979

8080
@Test
81-
void caseHearingsGetEndpointShouldReturnForbidden() throws Exception {
81+
void caseHearingsGet_shouldReturn401Error_whenUserNotFound() throws Exception {
8282
Mockito.reset(authentication);
8383

8484
// a user that does not exist in the db
@@ -90,12 +90,12 @@ void caseHearingsGetEndpointShouldReturnForbidden() throws Exception {
9090

9191
MockHttpServletRequestBuilder requestBuilder = get(endpointUrl, hearingEntity.getCourtCase().getId());
9292

93-
MvcResult response = mockMvc.perform(requestBuilder).andExpect(status().isForbidden()).andReturn();
93+
MvcResult response = mockMvc.perform(requestBuilder).andExpect(status().isUnauthorized()).andReturn();
9494

9595
String actualResponse = response.getResponse().getContentAsString();
9696

9797
String expectedResponse = """
98-
{"type":"AUTHORISATION_106","title":"Could not obtain user details","status":403}
98+
{"type":"AUTHORISATION_106","title":"Could not obtain user details","status":401}
9999
""";
100100
JSONAssert.assertEquals(expectedResponse, actualResponse, JSONCompareMode.NON_EXTENSIBLE);
101101
}
@@ -252,7 +252,7 @@ void caseHearingsWithInactiveUser() throws Exception {
252252
MockHttpServletRequestBuilder requestBuilder = get(endpointUrl, hearingEntity.getCourtCase().getId());
253253

254254
mockMvc.perform(requestBuilder).andExpect(status().isForbidden()).andExpect(jsonPath("$.type").value(
255-
AuthorisationError.USER_DETAILS_INVALID.getType()));
255+
AuthorisationError.USER_NOT_ACTIVE.getType()));
256256
}
257257

258258
}

src/integrationTest/java/uk/gov/hmcts/darts/cases/controller/CaseControllerGetEventByCaseIdTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,15 @@ void setUp() {
9090
}
9191

9292
@Test
93-
void casesGetEventsEndpointShouldReturnForbiddenError() throws Exception {
93+
void casesGetEventsEndpoint_shouldReturn401Error_whenNoUserExists() throws Exception {
9494

9595
when(mockUserIdentity.getUserAccount()).thenReturn(null);
9696

9797
MockHttpServletRequestBuilder requestBuilder = get(ENDPOINT_URL, getCaseId(SOME_CASE_NUMBER, SOME_COURTHOUSE))
9898
.queryParam("page_number", "1")
9999
.queryParam("page_size", "1");
100100

101-
mockMvc.perform(requestBuilder).andExpect(MockMvcResultMatchers.status().isForbidden());
101+
mockMvc.perform(requestBuilder).andExpect(MockMvcResultMatchers.status().isUnauthorized());
102102
}
103103

104104
@Test

src/integrationTest/java/uk/gov/hmcts/darts/cases/controller/CaseControllerSearchPostTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -503,10 +503,10 @@ void casesSearchPostEndpointJudgeName() throws Exception {
503503
}
504504

505505
@Test
506-
void casesSearchPostEndpointJudgeNameInactive() throws Exception {
507-
user = dartsDatabase.getUserAccountStub().createJudgeUser();
506+
void casesSearchPost_shouldReturn403Error_whenUserIsInactive() throws Exception {
507+
user = dartsDatabase.getUserAccountStub().getIntegrationTestUserAccountEntity();
508508
user.setActive(false);
509-
setupUserAccountAndSecurityGroup(swanseaCourthouse);
509+
userAccountRepository.save(user);
510510

511511
String requestBody = """
512512
{
@@ -521,8 +521,7 @@ void casesSearchPostEndpointJudgeNameInactive() throws Exception {
521521
.contentType(MediaType.APPLICATION_JSON_VALUE)
522522
.content(requestBody);
523523
mockMvc.perform(requestBuilder).andExpect(status().isForbidden()).andExpect(jsonPath("$.type").value(
524-
AuthorisationError.USER_DETAILS_INVALID.getType()));
525-
524+
AuthorisationError.USER_NOT_ACTIVE.getType()));
526525
}
527526

528527
@Test

src/integrationTest/java/uk/gov/hmcts/darts/common/controller/CourthouseApiTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ void courthousesGet_ThreeCourthousesAssignedToUser() throws Exception {
289289
}
290290

291291
@Test
292-
void courthousesGet_ThreeCourthousesAssignedToUserInactive() throws Exception {
292+
void courthousesGet_shouldReturn403Error_whenUserInactive() throws Exception {
293293
String courthouseName = "courthousetest";
294294
UserAccountEntity userAccountEntity = userStub.createAuthorisedIntegrationTestUser(false, courthouseName);
295295
userAccountEntity.setActive(false);
@@ -300,7 +300,7 @@ void courthousesGet_ThreeCourthousesAssignedToUserInactive() throws Exception {
300300
MockHttpServletRequestBuilder requestBuilder = get("/courthouses")
301301
.contentType(MediaType.APPLICATION_JSON_VALUE);
302302
mockMvc.perform(requestBuilder).andExpect(status().isForbidden()).andExpect(jsonPath("$.type").value(
303-
AuthorisationError.USER_DETAILS_INVALID.getType()));
303+
AuthorisationError.USER_NOT_ACTIVE.getType()));
304304
}
305305

306306
@Test

0 commit comments

Comments
 (0)