Skip to content

Commit 9498477

Browse files
danielguedesbmaibin
authored andcommitted
BAEL-1914 (eugenp#4718)
* Server Sent Events example using Spring Webflux and React * spring security custom AuthenticationFailureHandler * refactor * moved SSE to branch * BAEL-1914 * remove pom properties * renamed test class * avoid git conflict
1 parent 22b7cf7 commit 9498477

1 file changed

Lines changed: 87 additions & 0 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.baeldung.springbootsecurity.form_login;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.test.context.SpringBootTest;
8+
import org.springframework.test.context.junit4.SpringRunner;
9+
import org.springframework.test.web.servlet.MockMvc;
10+
import org.springframework.test.web.servlet.MvcResult;
11+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
12+
import org.springframework.web.context.WebApplicationContext;
13+
14+
import javax.servlet.Filter;
15+
16+
import static org.junit.Assert.assertTrue;
17+
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.formLogin;
18+
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
19+
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.authenticated;
20+
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.unauthenticated;
21+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
22+
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
23+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
24+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
25+
26+
@RunWith(SpringRunner.class)
27+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = com.baeldung.springbootsecurity.form_login.SpringBootSecurityApplication.class)
28+
public class FormLoginUnitTest {
29+
30+
@Autowired
31+
private WebApplicationContext context;
32+
33+
@Autowired
34+
private Filter springSecurityFilterChain;
35+
36+
private MockMvc mvc;
37+
38+
@Before
39+
public void setup() {
40+
mvc = MockMvcBuilders
41+
.webAppContextSetup(context)
42+
.addFilters(springSecurityFilterChain)
43+
.build();
44+
}
45+
46+
@Test
47+
public void givenRequestWithoutSessionOrCsrfToken_shouldFailWith403() throws Exception {
48+
mvc
49+
.perform(post("/"))
50+
.andExpect(status().isForbidden());
51+
}
52+
53+
@Test
54+
public void givenRequestWithInvalidCsrfToken_shouldFailWith403() throws Exception {
55+
mvc
56+
.perform(post("/").with(csrf().useInvalidToken()))
57+
.andExpect(status().isForbidden());
58+
}
59+
60+
@Test
61+
public void givenRequestWithValidCsrfTokenAndWithoutSessionToken_shouldReceive302WithLocationHeaderToLoginPage() throws Exception {
62+
MvcResult mvcResult = mvc.perform(post("/").with(csrf())).andReturn();
63+
assertTrue(mvcResult.getResponse().getStatus() == 302);
64+
assertTrue(mvcResult.getResponse().getHeader("Location").contains("login"));
65+
}
66+
67+
@Test
68+
public void givenValidRequestWithValidCredentials_shouldLoginSuccessfully() throws Exception {
69+
mvc
70+
.perform(formLogin().user("baeldung").password("baeldung"))
71+
.andExpect(status().isFound())
72+
.andExpect(redirectedUrl("/"))
73+
.andExpect(authenticated().withUsername("baeldung"));
74+
}
75+
76+
@Test
77+
public void givenValidRequestWithInvalidCredentials_shouldFailWith401() throws Exception {
78+
MvcResult result = mvc
79+
.perform(formLogin().user("random").password("random"))
80+
.andExpect(status().isUnauthorized())
81+
.andDo(print())
82+
.andExpect(unauthenticated())
83+
.andReturn();
84+
85+
assertTrue(result.getResponse().getContentAsString().contains("Bad credentials"));
86+
}
87+
}

0 commit comments

Comments
 (0)