Skip to content

Commit

Permalink
TMI2-390 - Login to Spotlight from DD checks page (#72)
Browse files Browse the repository at this point in the history
* TMI2-390 - Added call to check if scheme has an internal application form

* TMI2-390 - Added missing tests

* TMI2-390 - Fixed formatting
  • Loading branch information
kiramarstonTCO authored Nov 16, 2023
1 parent 4b818ef commit cdb6c4b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import gov.cabinetoffice.gap.adminbackend.dtos.schemes.SchemeDTO;
import gov.cabinetoffice.gap.adminbackend.dtos.schemes.SchemePatchDTO;
import gov.cabinetoffice.gap.adminbackend.dtos.schemes.SchemePostDTO;
import gov.cabinetoffice.gap.adminbackend.entities.ApplicationFormEntity;
import gov.cabinetoffice.gap.adminbackend.entities.GrantAdmin;
import gov.cabinetoffice.gap.adminbackend.services.ApplicationFormService;
import gov.cabinetoffice.gap.adminbackend.services.GrantAdvertService;
Expand Down Expand Up @@ -38,6 +39,7 @@
import javax.validation.constraints.NotNull;
import java.util.Collections;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;

@Tag(name = "Schemes", description = "API for handling grant schemes.")
Expand Down Expand Up @@ -213,4 +215,33 @@ public ResponseEntity<List<SchemeDTO>> getAdminsSchemes(final @PathVariable Stri
return ResponseEntity.ok().body(Collections.emptyList());
}

@GetMapping("/{schemeId}/hasInternalApplicationForm")
@Operation(summary = "Retrieve grant scheme which matches the given id.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Found scheme which matched the given id.",
content = @Content(mediaType = "application/json",
schema = @Schema(implementation = SchemeDTO.class))),
@ApiResponse(responseCode = "404", description = "No scheme found with matching id.",
content = @Content(mediaType = "application/json")),
@ApiResponse(responseCode = "403", description = "You do not have permissions to access this scheme.",
content = @Content(mediaType = "application/json")) })
public ResponseEntity<Boolean> hasInternalApplicationForm(@PathVariable final Integer schemeId) {
SchemeDTO scheme = null;
ApplicationFormEntity application = null;
try {
scheme = this.schemeService.getSchemeBySchemeId(schemeId);
}
catch (EntityNotFoundException enfe) {
return ResponseEntity.notFound().build();
}

try {
application = this.applicationFormService.getApplicationFromSchemeId(schemeId);
return ResponseEntity.ok(true);
}
catch (NoSuchElementException exception) {
return ResponseEntity.ok(false);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.web.servlet.MockMvc;
Expand All @@ -33,10 +35,13 @@
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;
import java.util.Collections;
import java.util.NoSuchElementException;
import java.util.Optional;

import static gov.cabinetoffice.gap.adminbackend.testdata.ApplicationFormTestData.SAMPLE_APPLICATION_FORM_ENTITY;
import static gov.cabinetoffice.gap.adminbackend.testdata.SchemeTestData.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
Expand Down Expand Up @@ -403,4 +408,36 @@ void updateGrantOwnership() throws Exception {

}

@Test
void hasInternalApplicationForm_HappyPath() throws Exception {
when(schemeService.getSchemeBySchemeId(SAMPLE_SCHEME_ID)).thenReturn(SCHEME_DTO_EXAMPLE);
when(applicationFormService.getApplicationFromSchemeId(SAMPLE_SCHEME_ID))
.thenReturn(SAMPLE_APPLICATION_FORM_ENTITY);
mockMvc.perform(get("/schemes/1/hasInternalApplicationForm")).andExpect(status().isOk())
.andExpect(content().string("true"));
}

@Test
void hasInternalApplicationForm_HappyPathNoInternalApplicationForm() throws Exception {
when(schemeService.getSchemeBySchemeId(SAMPLE_SCHEME_ID)).thenReturn(SCHEME_DTO_EXAMPLE);
when(applicationFormService.getApplicationFromSchemeId(SAMPLE_SCHEME_ID))
.thenThrow(new NoSuchElementException());
mockMvc.perform(get("/schemes/1/hasInternalApplicationForm")).andExpect(status().isOk())
.andExpect(content().string("false"));
}

@Test
void hasInternalApplicationForm_SchemeNotFound() throws Exception {
when(schemeService.getSchemeBySchemeId(SAMPLE_SCHEME_ID)).thenThrow(new EntityNotFoundException());
mockMvc.perform(get("/schemes/1/hasInternalApplicationForm")).andExpect(status().isNotFound())
.andExpect(content().string(""));
}

@Test
void hasInternalApplicationForm_NoPermission() throws Exception {
when(schemeService.getSchemeBySchemeId(SAMPLE_SCHEME_ID)).thenThrow(new AccessDeniedException(""));
mockMvc.perform(get("/schemes/1/hasInternalApplicationForm")).andExpect(status().isForbidden())
.andExpect(content().string("{\"error\":{\"message\":\"\"}}"));
}

}

0 comments on commit cdb6c4b

Please sign in to comment.