-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b7bd5e8
commit 69e7b18
Showing
1 changed file
with
37 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,19 @@ | ||
package ch.puzzle.okr.controller; | ||
|
||
import static ch.puzzle.okr.test.TestHelper.*; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.*; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
|
||
import ch.puzzle.okr.dto.UnitDto; | ||
import ch.puzzle.okr.mapper.UnitMapper; | ||
import ch.puzzle.okr.models.Unit; | ||
import ch.puzzle.okr.multitenancy.TenantContext; | ||
import ch.puzzle.okr.service.authorization.AuthorizationService; | ||
import ch.puzzle.okr.service.authorization.UnitAuthorizationService; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.util.List; | ||
import org.hamcrest.Matchers; | ||
import org.hamcrest.core.Is; | ||
import org.junit.jupiter.api.BeforeEach; | ||
|
@@ -24,24 +31,20 @@ | |
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; | ||
import org.springframework.web.server.ResponseStatusException; | ||
|
||
import java.util.List; | ||
|
||
import static ch.puzzle.okr.test.TestHelper.*; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.*; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
|
||
@WithMockUser(value = "spring") | ||
@ExtendWith(MockitoExtension.class) | ||
@WebMvcTest(UnitController.class) | ||
class UnitControllerITIT { | ||
private final String URL_BASE = "/api/v2/units"; | ||
private final ObjectMapper objectMapper = new ObjectMapper(); | ||
@Autowired private MockMvc mvc; | ||
@MockitoBean private UnitAuthorizationService unitAuthorizationService; | ||
@MockitoBean private AuthorizationService authorizationService; | ||
@MockitoBean private UnitMapper unitMapper; | ||
@Autowired | ||
private MockMvc mvc; | ||
@MockitoBean | ||
private UnitAuthorizationService unitAuthorizationService; | ||
@MockitoBean | ||
private AuthorizationService authorizationService; | ||
@MockitoBean | ||
private UnitMapper unitMapper; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
|
@@ -87,9 +90,9 @@ void shouldReturnNewUnitWithCurrentUserAsOwner() throws Exception { | |
|
||
mvc | ||
.perform(post(URL_BASE) | ||
.content(unitJson) | ||
.with(SecurityMockMvcRequestPostProcessors.jwt().jwt(glJwtToken())) | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.content(unitJson) | ||
.with(SecurityMockMvcRequestPostProcessors.jwt().jwt(glJwtToken())) | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(MockMvcResultMatchers.status().isOk()) | ||
.andExpect(jsonPath("$.owner.email", Is.is("[email protected]"))) | ||
.andExpect(jsonPath("$.unitName", Is.is("FTE"))); | ||
|
@@ -99,27 +102,28 @@ void shouldReturnNewUnitWithCurrentUserAsOwner() throws Exception { | |
void shouldReturn401ForInvalidUserWhenCreatingUnit() throws Exception { | ||
UnitDto unitDTO = new UnitDto(null, "TestUnit", null); | ||
String unitJson = objectMapper.writeValueAsString(unitDTO); | ||
when(unitAuthorizationService.createUnit(any(Unit.class))).thenThrow(new ResponseStatusException(HttpStatus.UNAUTHORIZED)); | ||
when(unitAuthorizationService.createUnit(any(Unit.class))) | ||
.thenThrow(new ResponseStatusException(HttpStatus.UNAUTHORIZED)); | ||
mvc | ||
.perform(post(URL_BASE) | ||
.with(SecurityMockMvcRequestPostProcessors.csrf()) | ||
.content(unitJson) | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.with(SecurityMockMvcRequestPostProcessors.csrf()) | ||
.content(unitJson) | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(MockMvcResultMatchers.status().isUnauthorized()); | ||
} | ||
|
||
@Test | ||
void shouldReturn200ForUserWhenUpdatingUnit() throws Exception { | ||
UnitDto unitDTO = new UnitDto(null, "TestUnit", null); | ||
String unitJson = objectMapper.writeValueAsString(unitDTO); | ||
when(unitAuthorizationService.updateUnit(any(),any(Unit.class))).thenReturn(FTE_UNIT); | ||
when(unitAuthorizationService.updateUnit(any(), any(Unit.class))).thenReturn(FTE_UNIT); | ||
|
||
mvc | ||
.perform(put(URL_BASE + "/100") | ||
.with(SecurityMockMvcRequestPostProcessors.csrf()) | ||
.with(SecurityMockMvcRequestPostProcessors.jwt().jwt(glJwtToken())) | ||
.content(unitJson) | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.with(SecurityMockMvcRequestPostProcessors.csrf()) | ||
.with(SecurityMockMvcRequestPostProcessors.jwt().jwt(glJwtToken())) | ||
.content(unitJson) | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(MockMvcResultMatchers.status().isOk()) | ||
.andExpect(jsonPath("$.unitName", Is.is("FTE"))); | ||
} | ||
|
@@ -128,13 +132,14 @@ void shouldReturn200ForUserWhenUpdatingUnit() throws Exception { | |
void shouldReturn403ForWrongUserWhenUpdatingUnit() throws Exception { | ||
UnitDto unitDTO = new UnitDto(null, "TestUnit", null); | ||
String unitJson = objectMapper.writeValueAsString(unitDTO); | ||
when(unitAuthorizationService.updateUnit(any(),any(Unit.class))).thenThrow(new ResponseStatusException(HttpStatus.FORBIDDEN)); | ||
when(unitAuthorizationService.updateUnit(any(), any(Unit.class))) | ||
.thenThrow(new ResponseStatusException(HttpStatus.FORBIDDEN)); | ||
mvc | ||
.perform(put(URL_BASE + "/100") | ||
.with(SecurityMockMvcRequestPostProcessors.csrf()) | ||
.with(SecurityMockMvcRequestPostProcessors.jwt().jwt(bbtJwtToken())) | ||
.content(unitJson) | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.with(SecurityMockMvcRequestPostProcessors.csrf()) | ||
.with(SecurityMockMvcRequestPostProcessors.jwt().jwt(bbtJwtToken())) | ||
.content(unitJson) | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(MockMvcResultMatchers.status().isForbidden()); | ||
} | ||
|
||
|
@@ -153,9 +158,9 @@ void shouldReturn403ForWrongUserWhenDeletingUnit() throws Exception { | |
void shouldReturn200ForRightUserWhenDeletingUnit() throws Exception { | ||
mvc | ||
.perform(delete(URL_BASE + "/101") | ||
.with(SecurityMockMvcRequestPostProcessors.csrf()) | ||
.with(SecurityMockMvcRequestPostProcessors.jwt().jwt(glJwtToken())) | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.with(SecurityMockMvcRequestPostProcessors.csrf()) | ||
.with(SecurityMockMvcRequestPostProcessors.jwt().jwt(glJwtToken())) | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(MockMvcResultMatchers.status().isOk()); | ||
verify(unitAuthorizationService, times(1)).deleteUnitById(101L); | ||
} | ||
|