Unit test auth

This commit is contained in:
Meutel 2023-11-17 10:01:00 +01:00
parent ae4214c6ed
commit a6fc9c482a
3 changed files with 29 additions and 7 deletions

View File

@ -49,6 +49,11 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>

View File

@ -17,6 +17,7 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMock
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.oauth2Login;
import net.meutel.recettes.api.model.RecetteParam;
import net.meutel.recettes.api.service.ParametersService;
@ -32,8 +33,14 @@ public class ParameterControllerTest {
private ParametersService service;
@Test
public void listRecetteParamsByType_any_emptyArray() throws Exception {
public void listRecetteParamsByType_noAuth_302() throws Exception {
this.mockMvc.perform(get("/parameters/TEST"))
.andExpect(status().is3xxRedirection());
}
@Test
public void listRecetteParamsByType_any_emptyArray() throws Exception {
this.mockMvc.perform(get("/parameters/TEST").with(oauth2Login()))
.andExpect(status().isOk())
.andExpect(jsonPath("$").isArray())
.andExpect(jsonPath("$").isEmpty());
@ -46,7 +53,7 @@ public class ParameterControllerTest {
.map(n -> new RecetteParam().name(n).atType("TEST").id(n))
.collect(toList()));
this.mockMvc.perform(get("/parameters/TEST"))
this.mockMvc.perform(get("/parameters/TEST").with(oauth2Login()))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$").isArray())

View File

@ -3,6 +3,7 @@ package net.meutel.recettes.api.controller;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.mockito.Mockito.when;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.oauth2Login;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ -29,31 +30,40 @@ public class ReceipeControllerTest {
@MockBean
private ReceipeService service;
@Test
public void getReceipeById_noAuth_302() throws Exception {
when(service.getReceipeById("TEST"))
.thenThrow(new ItemNotFoundException("Receipe", "TEST"));
this.mockMvc.perform(get("/receipes/TEST"))
.andExpect(status().is3xxRedirection());
}
@Test
public void getReceipeById_ItemNotFound_404() throws Exception {
when(service.getReceipeById("TEST"))
.thenThrow(new ItemNotFoundException("Receipe", "TEST"));
this.mockMvc.perform(get("/receipes/TEST"))
this.mockMvc.perform(get("/receipes/TEST").with(oauth2Login()))
.andExpect(status().isNotFound());
}
@Test
public void getReceipeById_exixts_success() throws Exception {
public void getReceipeById_exists_success() throws Exception {
Receipe r1 = new Receipe()
.name("pates carbo");
when(service.getReceipeById("TEST"))
.thenReturn(r1);
this.mockMvc.perform(get("/receipes/TEST"))
this.mockMvc.perform(get("/receipes/TEST").with(oauth2Login()))
.andExpect(status().isOk())
.andExpect(jsonPath("$.name", equalTo("pates carbo")));
}
@Test
public void findReceipes_none_empty() throws Exception {
this.mockMvc.perform(get("/receipes"))
this.mockMvc.perform(get("/receipes").with(oauth2Login()))
.andExpect(status().isOk())
.andExpect(jsonPath("$").isArray())
.andExpect(jsonPath("$").isEmpty());
@ -68,7 +78,7 @@ public class ReceipeControllerTest {
when(service.findReceipes())
.thenReturn(List.of(r1, r2));
this.mockMvc.perform(get("/receipes"))
this.mockMvc.perform(get("/receipes").with(oauth2Login()))
.andExpect(status().isOk())
.andExpect(jsonPath("$").isArray())
.andExpect(jsonPath("$", hasSize(2)))