|
| 1 | +/* |
| 2 | + * Copyright (C) 2026 Dominik Schadow, dominikschadow@gmail.com |
| 3 | + * |
| 4 | + * This file is part of the Java Security project. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package de.dominikschadow.javasecurity.sessionhandling.greetings; |
| 19 | + |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import org.springframework.beans.factory.annotation.Autowired; |
| 22 | +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; |
| 23 | +import org.springframework.security.test.context.support.WithMockUser; |
| 24 | +import org.springframework.test.context.bean.override.mockito.MockitoBean; |
| 25 | +import org.springframework.test.web.servlet.MockMvc; |
| 26 | + |
| 27 | +import static org.mockito.Mockito.when; |
| 28 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 29 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; |
| 30 | + |
| 31 | +@WebMvcTest(GreetingController.class) |
| 32 | +class GreetingControllerTest { |
| 33 | + |
| 34 | + @Autowired |
| 35 | + private MockMvc mockMvc; |
| 36 | + |
| 37 | + @MockitoBean |
| 38 | + private GreetingService greetingService; |
| 39 | + |
| 40 | + @Test |
| 41 | + @WithMockUser |
| 42 | + void index_shouldReturnIndexView() throws Exception { |
| 43 | + mockMvc.perform(get("/")) |
| 44 | + .andExpect(status().isOk()) |
| 45 | + .andExpect(view().name("index")) |
| 46 | + .andExpect(model().attributeExists("sessionId")); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + @WithMockUser(roles = "USER") |
| 51 | + void greetUser_shouldReturnUserViewWithGreeting() throws Exception { |
| 52 | + when(greetingService.greetUser()).thenReturn("Hello User!"); |
| 53 | + |
| 54 | + mockMvc.perform(get("/user/user")) |
| 55 | + .andExpect(status().isOk()) |
| 56 | + .andExpect(view().name("user/user")) |
| 57 | + .andExpect(model().attributeExists("sessionId")) |
| 58 | + .andExpect(model().attribute("greeting", "Hello User!")); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + @WithMockUser(roles = "ADMIN") |
| 63 | + void greetAdmin_shouldReturnAdminViewWithGreeting() throws Exception { |
| 64 | + when(greetingService.greetAdmin()).thenReturn("Hello Admin!"); |
| 65 | + |
| 66 | + mockMvc.perform(get("/admin/admin")) |
| 67 | + .andExpect(status().isOk()) |
| 68 | + .andExpect(view().name("admin/admin")) |
| 69 | + .andExpect(model().attributeExists("sessionId")) |
| 70 | + .andExpect(model().attribute("greeting", "Hello Admin!")); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void index_withoutAuthentication_shouldReturnUnauthorized() throws Exception { |
| 75 | + mockMvc.perform(get("/")) |
| 76 | + .andExpect(status().isUnauthorized()); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void greetUser_withoutAuthentication_shouldReturnUnauthorized() throws Exception { |
| 81 | + mockMvc.perform(get("/user/user")) |
| 82 | + .andExpect(status().isUnauthorized()); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + void greetAdmin_withoutAuthentication_shouldReturnUnauthorized() throws Exception { |
| 87 | + mockMvc.perform(get("/admin/admin")) |
| 88 | + .andExpect(status().isUnauthorized()); |
| 89 | + } |
| 90 | +} |
0 commit comments