|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 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.contacts; |
| 19 | + |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import org.mockito.Mockito; |
| 22 | +import org.springframework.beans.factory.annotation.Autowired; |
| 23 | +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; |
| 24 | +import org.springframework.security.test.context.support.WithMockUser; |
| 25 | +import org.springframework.test.context.bean.override.mockito.MockitoBean; |
| 26 | +import org.springframework.test.web.servlet.MockMvc; |
| 27 | + |
| 28 | +import java.util.List; |
| 29 | + |
| 30 | +import static org.hamcrest.Matchers.*; |
| 31 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 32 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; |
| 33 | + |
| 34 | +@WebMvcTest(controllers = ContactController.class) |
| 35 | +class ContactControllerTest { |
| 36 | + @Autowired |
| 37 | + private MockMvc mockMvc; |
| 38 | + |
| 39 | + @MockitoBean |
| 40 | + private ContactService contactService; |
| 41 | + |
| 42 | + private Contact sampleContact(long id, String username, String firstname, String lastname) { |
| 43 | + Contact c = new Contact(); |
| 44 | + c.setId(id); |
| 45 | + c.setUsername(username); |
| 46 | + c.setFirstname(firstname); |
| 47 | + c.setLastname(lastname); |
| 48 | + c.setComment("test"); |
| 49 | + return c; |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + @WithMockUser(username = "userA") |
| 54 | + void listContacts_asUser_ok() throws Exception { |
| 55 | + List<Contact> contacts = List.of( |
| 56 | + sampleContact(1L, "userA", "Alice", "Anderson"), |
| 57 | + sampleContact(2L, "userA", "Alan", "Archer") |
| 58 | + ); |
| 59 | + Mockito.when(contactService.getContacts()).thenReturn(contacts); |
| 60 | + |
| 61 | + mockMvc.perform(get("/contacts")) |
| 62 | + .andExpect(status().isOk()) |
| 63 | + .andExpect(view().name("contacts/list")) |
| 64 | + .andExpect(model().attributeExists("contacts")) |
| 65 | + .andExpect(model().attribute("contacts", hasSize(2))) |
| 66 | + .andExpect(model().attribute("contacts", hasItem(allOf( |
| 67 | + hasProperty("id", is(1L)), |
| 68 | + hasProperty("username", is("userA")), |
| 69 | + hasProperty("firstname", is("Alice")), |
| 70 | + hasProperty("lastname", is("Anderson")) |
| 71 | + )))); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + @WithMockUser(username = "userA") |
| 76 | + void contactDetails_asUser_ok() throws Exception { |
| 77 | + Contact contact = sampleContact(42L, "userA", "Bob", "Baker"); |
| 78 | + Mockito.when(contactService.getContact(42)).thenReturn(contact); |
| 79 | + |
| 80 | + mockMvc.perform(get("/contacts/42")) |
| 81 | + .andExpect(status().isOk()) |
| 82 | + .andExpect(view().name("contacts/details")) |
| 83 | + .andExpect(model().attributeExists("contact")) |
| 84 | + .andExpect(model().attribute("contact", allOf( |
| 85 | + hasProperty("id", is(42L)), |
| 86 | + hasProperty("username", is("userA")), |
| 87 | + hasProperty("firstname", is("Bob")), |
| 88 | + hasProperty("lastname", is("Baker")) |
| 89 | + ))); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void listContacts_unauthenticated_returns401() throws Exception { |
| 94 | + mockMvc.perform(get("/contacts")) |
| 95 | + .andExpect(status().isUnauthorized()) |
| 96 | + .andExpect(status().reason(containsString("Unauthorized"))); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + void contactDetails_unauthenticated_returns401() throws Exception { |
| 101 | + mockMvc.perform(get("/contacts/42")) |
| 102 | + .andExpect(status().isUnauthorized()) |
| 103 | + .andExpect(status().reason(containsString("Unauthorized"))); |
| 104 | + } |
| 105 | +} |
0 commit comments