Add user tests

This commit is contained in:
Eren Asker 2023-11-28 10:03:43 +01:00 committed by Denis Natusch
parent 743e459d41
commit 6e4fe14339
No known key found for this signature in database
GPG key ID: 5E57BD8EDACFA985
2 changed files with 174 additions and 4 deletions

View file

@ -1,5 +1,5 @@
// SPDX-License-Identifier: AGPL-3.0-or-later // SPDX-License-Identifier: AGPL-3.0-or-later
// SPDX-FileCopyrightText: 2023 swt23w23 // SPDX-FileCopyrightText: 2023-2024 swt23w23
package catering.users; package catering.users;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -7,11 +7,13 @@ import static org.assertj.core.api.Assertions.tuple;
import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; 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.model;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl; 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.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -19,10 +21,10 @@ import org.salespointframework.useraccount.UserAccountManagement;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.test.context.support.WithAnonymousUser;
import org.springframework.security.test.context.support.WithMockUser; import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.security.test.context.support.WithAnonymousUser;
@AutoConfigureMockMvc @AutoConfigureMockMvc
@SpringBootTest @SpringBootTest
@ -52,6 +54,23 @@ class UserControllerIntegrationTests {
createTestUser("mona"); createTestUser("mona");
createTestUser("john"); createTestUser("john");
createTestUser("stefan"); createTestUser("stefan");
createTestUser("jacob");
createTestUser("moana");
createTestUser("paul");
}
@Test
void unauthorizedTest() throws Exception {
mvc.perform(get("/unauthorized"))
.andExpect(status().isOk())
.andExpect(view().name("unauthorized"));
}
@Test
void loginPageTest() throws Exception {
mvc.perform(get("/login"))
.andExpect(status().isOk())
.andExpect(view().name("login"));
} }
@Test @Test
@ -237,4 +256,56 @@ class UserControllerIntegrationTests {
.andExpect(content().string(containsString("Ungültiger Name"))) .andExpect(content().string(containsString("Ungültiger Name")))
.andExpect(content().string(containsString("Ungültige Addresse"))); .andExpect(content().string(containsString("Ungültige Addresse")));
} }
@Test
@WithMockUser(username = "admin", password = "admin", roles = "ADMIN")
void viewCustomers() throws Exception {
mvc.perform(get("/customers"))
.andExpect(status().isOk())
.andExpect(view().name("customers"))
.andExpect(model().attributeExists("customers"));
}
@Test
@WithMockUser(username = "admin", password = "admin", roles = "ADMIN")
void editCustomerDetails() throws Exception {
assertThat(userManagement.getUsers().findAll())
.extracting("username", "fullName", "address")
.contains(tuple("moana", "Moana Klaus", "Baum Weg"));
mvc.perform(post("/customers/edit/" + userManagement.getUserByName("moana").get().getId())
.param("username", "moan")
.param("fullName", "Moan Klaus")
.param("address", "Baum Straße"))
.andExpect(redirectedUrl("/customers"));
assertThat(userManagement.getUsers().findAll())
.extracting("username", "fullName", "address")
.contains(tuple("moan", "Moan Klaus", "Baum Straße"));
mvc.perform(get("/customers/edit/" + userManagement.getUserByName("moan").get().getId()))
.andExpect(status().isOk())
.andExpect(view().name("edit-customer"));
}
@Test
@WithMockUser(username = "admin", roles = "ADMIN")
void removeCustomerTest() throws Exception {
assertTrue(userAccountManagement.findByUsername("paul").isPresent());
mvc.perform(get("/customers/remove/" + userManagement.getUserByName("paul").get().getId()))
.andExpect(status().is3xxRedirection())
.andExpect(redirectedUrl("/customers"));
assertThat(userAccountManagement.findDisabled())
.extracting("username")
.contains("paul");
}
@Test
@WithMockUser(username = "jacob", password = "123", roles = "CUSTOMER")
void disableUserAccount() throws Exception {
assertTrue(userAccountManagement.findByUsername("jacob").isPresent());
mvc.perform(get("/profile/disable"))
.andExpect(status().is3xxRedirection())
.andExpect(redirectedUrl("/logout"));
assertThat(userAccountManagement.findDisabled())
.extracting("username")
.contains("jacob");
}
} }

View file

@ -0,0 +1,99 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
// SPDX-FileCopyrightText: 2023-2024 swt23w23
package catering.users;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Objects;
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;
@SpringBootTest
public class UserUnitTests {
private User user;
@Autowired
private UserManagement userManagement;
@BeforeEach
void setUp() {
user = userManagement.getUserByName("testUser")
.orElseGet(() -> userManagement.createCustomer("testUser", "Baum Weg", "123", "Mario Götze"));
}
@Test
void getAddress() {
assertThat(user.getAddress()).isEqualTo("Baum Weg");
}
@Test
void setAddress() {
user.setAddress("Braum Weg");
assertThat(user.getAddress()).isEqualTo("Braum Weg");
}
@Test
void getUsername() {
User otherUser = userManagement.createCustomer("testUser0", "password", "Apfel Weg", "Marco Reus");
assertThat(otherUser.getUsername()).isEqualTo("testUser0");
}
@Test
void setUsername() {
user.setUsername("testUser1");
assertThat(user.getUsername()).isEqualTo("testUser1");
}
@Test
void getFullName() {
assertThat(user.getFullName()).isEqualTo("Mario Götze");
}
@Test
void setFullName() {
user.setFullName("Manuel Neuer");
assertThat(user.getFullName()).isEqualTo("Manuel Neuer");
}
@Test
void isEnabled() {
assertThat(user.isEnabled()).isTrue();
}
@Test
void hasRole() {
assertThat(user.hasRole("CUSTOMER")).isTrue();
assertThat(user.hasRole("ADMIN")).isFalse();
}
@Test
void differentUserDifferentHashCode() {
User otherUser = userManagement.createCustomer("carlos", "123", "Baum Weg", "Marco Reus");
assertThat(user.hashCode()).isNotEqualTo(otherUser.hashCode());
}
@Test
void usersNotEqual() {
User otherUser = userManagement.createCustomer("maria", "123", "Baum Weg", "Marco Reus");
assertThat(user).isNotEqualTo(otherUser);
}
@Test
void usersNotEqualToDifferentClass() {
assertThat(user).isNotEqualTo(new Object());
}
@Test
void differentAddressesNotEqual() {
assertThat(user.getAddress()).isNotEqualTo("Baum Straße");
}
@Test
void differentUserNamesNotEqual() {
assertThat(user.getUsername()).isNotEqualTo("TestUser");
}
}