mirror of
https://github.com/fabioformosa/quartz-manager.git
synced 2025-12-27 04:33:20 +09:00
#62 added a missing test to the UserController
This commit is contained in:
@@ -20,8 +20,9 @@ import static it.fabioformosa.quartzmanager.api.common.config.QuartzManagerPaths
|
||||
@RequestMapping(value = QUARTZ_MANAGER_AUTH_PATH, produces = MediaType.APPLICATION_JSON_VALUE)
|
||||
public class UserController {
|
||||
|
||||
public static final String WHOAMI_URL = "/whoami";
|
||||
|
||||
@GetMapping("/whoami")
|
||||
@GetMapping(WHOAMI_URL)
|
||||
public ResponseEntity<Object> getLoggedUser() {
|
||||
SecurityContext context = SecurityContextHolder.getContext();
|
||||
if (context != null && context.getAuthentication() != null)
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package it.fabioformosa.quartzmanager.api.security.controllers;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.security.test.context.support.WithMockUser;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
|
||||
import static it.fabioformosa.quartzmanager.api.common.config.QuartzManagerPaths.QUARTZ_MANAGER_AUTH_PATH;
|
||||
import static it.fabioformosa.quartzmanager.api.security.controllers.UserController.WHOAMI_URL;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@SpringBootTest
|
||||
@AutoConfigureMockMvc
|
||||
@TestPropertySource(properties = {
|
||||
"quartz-manager.security.accounts.in-memory.enabled=true",
|
||||
"quartz-manager.security.accounts.in-memory.users[0].username=admin",
|
||||
"quartz-manager.security.accounts.in-memory.users[0].password=admin",
|
||||
"quartz-manager.security.accounts.in-memory.users[0].roles[0]=admin",
|
||||
})
|
||||
class UserControllerTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Test
|
||||
@WithMockUser("admin")
|
||||
void givenAnUser_whenCalledTheWhoamiEndpoint_thenShouldReturn2xx() throws Exception {
|
||||
mockMvc.perform(MockMvcRequestBuilders.get(QUARTZ_MANAGER_AUTH_PATH + WHOAMI_URL))
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenAnAnonymousUser_whenCalledTheWhoamiEndpoint_thenShouldReturnNotFound() throws Exception {
|
||||
mockMvc.perform(MockMvcRequestBuilders.get(QUARTZ_MANAGER_AUTH_PATH + WHOAMI_URL))
|
||||
.andExpect(status().isUnauthorized());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user