|
| 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.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.web.servlet.MockMvc; |
| 25 | + |
| 26 | +import static org.hamcrest.Matchers.*; |
| 27 | +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; |
| 28 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 29 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
| 30 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; |
| 31 | + |
| 32 | +@WebMvcTest(controllers = GreetingController.class) |
| 33 | +class GreetingControllerTest { |
| 34 | + @Autowired |
| 35 | + private MockMvc mockMvc; |
| 36 | + |
| 37 | + @Test |
| 38 | + @WithMockUser |
| 39 | + void home_returnsIndexView() throws Exception { |
| 40 | + mockMvc.perform(get("/")) |
| 41 | + .andExpect(status().isOk()) |
| 42 | + .andExpect(view().name("index")) |
| 43 | + .andExpect(model().attributeExists("greeting")) |
| 44 | + .andExpect(model().attribute("greeting", instanceOf(Greeting.class))); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + @WithMockUser |
| 49 | + void greeting_returnsResultView() throws Exception { |
| 50 | + mockMvc.perform(post("/greeting") |
| 51 | + .with(csrf()) |
| 52 | + .param("name", "TestUser")) |
| 53 | + .andExpect(status().isOk()) |
| 54 | + .andExpect(view().name("result")) |
| 55 | + .andExpect(model().attributeExists("result")) |
| 56 | + .andExpect(model().attribute("result", instanceOf(Greeting.class))); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + void home_unauthenticated_returnsUnauthorized() throws Exception { |
| 61 | + mockMvc.perform(get("/")) |
| 62 | + .andExpect(status().isUnauthorized()); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void greeting_unauthenticated_returnsUnauthorized() throws Exception { |
| 67 | + mockMvc.perform(post("/greeting") |
| 68 | + .with(csrf()) |
| 69 | + .param("name", "TestUser")) |
| 70 | + .andExpect(status().isUnauthorized()); |
| 71 | + } |
| 72 | +} |
0 commit comments