add example for security with profiles (#9185)

Co-authored-by: Mihai Lepadat <mihai.lepadat@irian.at>
This commit is contained in:
Mihai238
2020-06-09 21:49:03 +02:00
committed by GitHub
parent c387fe5dd6
commit 26c11b3e85
6 changed files with 119 additions and 0 deletions
@@ -0,0 +1,28 @@
package com.baeldung.securityprofile;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringRunner.class)
@WebMvcTest(value = EmployeeController.class)
@ActiveProfiles("test")
public class EmployeeControllerNoSecurityUnitTest {
@Autowired
private MockMvc mockMvc;
@Test
public void whenSecurityDisabled_shouldBeOk() throws Exception {
this.mockMvc.perform(get("/employees"))
.andExpect(status().isOk());
}
}
@@ -0,0 +1,28 @@
package com.baeldung.securityprofile;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringRunner.class)
@WebMvcTest(value = EmployeeController.class)
@ActiveProfiles("prod")
public class EmployeeControllerUnitTest {
@Autowired
private MockMvc mockMvc;
@Test
public void whenSecurityEnabled_shouldBeForbidden() throws Exception {
this.mockMvc.perform(get("/employees"))
.andExpect(status().isForbidden());
}
}