feat(account): add project login page
This commit is contained in:
+85
@@ -0,0 +1,85 @@
|
||||
package com.lab.labtimesheet.feature.account.controller;
|
||||
|
||||
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
|
||||
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.authenticated;
|
||||
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.unauthenticated;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
|
||||
|
||||
import com.lab.labtimesheet.config.TestcontainersConfiguration;
|
||||
import com.lab.labtimesheet.feature.account.service.BootstrapService;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.mock.web.MockHttpSession;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
@Import(TestcontainersConfiguration.class)
|
||||
@SpringBootTest
|
||||
@AutoConfigureMockMvc
|
||||
@ActiveProfiles("test")
|
||||
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
|
||||
class AuthenticationWebIntegrationTest {
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Autowired
|
||||
private BootstrapService bootstrap;
|
||||
|
||||
@BeforeEach
|
||||
void initializeAdmin() {
|
||||
bootstrap.bootstrap("admin@example.com", "Admin", "correct horse battery staple");
|
||||
}
|
||||
|
||||
@Test
|
||||
void projectLoginPageSupportsFailureNormalizedSuccessAndLogout() throws Exception {
|
||||
mockMvc.perform(get("/login"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(view().name("accounts/login"))
|
||||
.andExpect(content().string(Matchers.containsString("Sign in")))
|
||||
.andExpect(content().string(Matchers.containsString("action=\"/login\"")));
|
||||
|
||||
mockMvc.perform(post("/login")
|
||||
.with(csrf())
|
||||
.param("username", " ADMIN@EXAMPLE.COM ")
|
||||
.param("password", "incorrect password"))
|
||||
.andExpect(status().is3xxRedirection())
|
||||
.andExpect(redirectedUrl("/login?error"))
|
||||
.andExpect(unauthenticated());
|
||||
|
||||
mockMvc.perform(get("/login").param("error", ""))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(view().name("accounts/login"))
|
||||
.andExpect(content().string(Matchers.containsString("Invalid email or password")));
|
||||
|
||||
var login = mockMvc.perform(post("/login")
|
||||
.with(csrf())
|
||||
.param("username", " ADMIN@EXAMPLE.COM ")
|
||||
.param("password", "correct horse battery staple"))
|
||||
.andExpect(status().is3xxRedirection())
|
||||
.andExpect(redirectedUrl("/"))
|
||||
.andExpect(authenticated().withUsername("admin@example.com"))
|
||||
.andReturn();
|
||||
var session = (MockHttpSession) login.getRequest().getSession(false);
|
||||
|
||||
mockMvc.perform(post("/logout").session(session).with(csrf()))
|
||||
.andExpect(status().is3xxRedirection())
|
||||
.andExpect(redirectedUrl("/login?logout"))
|
||||
.andExpect(unauthenticated());
|
||||
|
||||
mockMvc.perform(get("/login").param("logout", ""))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(view().name("accounts/login"))
|
||||
.andExpect(content().string(Matchers.containsString("You have signed out")));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user