diff --git a/spring-security-custom-voter/pom.xml b/spring-security-custom-voter/pom.xml index 800fe356b4..727efa48ab 100644 --- a/spring-security-custom-voter/pom.xml +++ b/spring-security-custom-voter/pom.xml @@ -21,6 +21,7 @@ UTF-8 UTF-8 + 3.0.1 1.8 @@ -43,6 +44,45 @@ spring-boot-starter-test test + + + + + junit + junit + test + + + + org.hamcrest + hamcrest-core + test + + + + org.hamcrest + hamcrest-library + test + + + + org.springframework + spring-test + 4.3.3.RELEASE + + + + org.springframework.security + spring-security-test + 4.1.3.RELEASE + test + + + + org.springframework.security + spring-security-web + 4.1.3.RELEASE + diff --git a/spring-security-custom-voter/src/test/java/org/baeldung/web/LiveTest.java b/spring-security-custom-voter/src/test/java/org/baeldung/web/LiveTest.java new file mode 100644 index 0000000000..3ddeab5920 --- /dev/null +++ b/spring-security-custom-voter/src/test/java/org/baeldung/web/LiveTest.java @@ -0,0 +1,42 @@ +package org.baeldung.web; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.formLogin; +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; +import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.authenticated; +import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration @WebAppConfiguration @SpringBootTest public class LiveTest { + @Autowired private WebApplicationContext context; + + private MockMvc mvc; + + @Before public void setup() { + mvc = MockMvcBuilders.webAppContextSetup(context).apply(springSecurity()).build(); + } + + @Test public void givenUnauthenticatedUser_whenAccessingMainPage_thenRedirect() throws Exception { + mvc.perform(get("/")).andExpect(status().is3xxRedirection()); + } + + @Test public void givenValidUsernameAndPassword_whenLogin_thenOK() throws Exception { + mvc.perform(formLogin("/login").user("user").password("pass")).andExpect(authenticated()); + } + + @Test public void givenAuthenticatedAdmin_whenAccessingMainPage_thenOK() throws Exception { + mvc.perform(get("/").with(user("admin").password("pass").roles("ADMIN"))).andExpect(status().isOk()); + } +}