From de900d2eeb10bb610727d9721826c5764879ee80 Mon Sep 17 00:00:00 2001 From: Denis Natusch Date: Mon, 15 Jan 2024 01:20:42 +0100 Subject: [PATCH] Add more username tests These test validate, that no username validation will be performed, if the form has an error. --- .../users/UserControllerIntegrationTests.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/test/java/catering/users/UserControllerIntegrationTests.java b/src/test/java/catering/users/UserControllerIntegrationTests.java index e697378..2bb5b2c 100644 --- a/src/test/java/catering/users/UserControllerIntegrationTests.java +++ b/src/test/java/catering/users/UserControllerIntegrationTests.java @@ -61,6 +61,8 @@ class UserControllerIntegrationTests { createTestUser("eva"); createTestUser("sarah"); createTestUser("paul"); + createTestUser("maria"); + createTestUser("adam"); } @Test @@ -401,4 +403,45 @@ class UserControllerIntegrationTests { .andExpect(redirectedUrl("/")); } + @Test + @WithMockUser(username = "maria", password = "123") + void alreadyExistingNameCheckWillNotBePerformedIfFormHasErrorProfile() throws Exception { + assertTrue(userManagement.getUserByName("maria").isPresent()); + MockHttpServletRequestBuilder createMessage = post("/profile") + .param("username", "admin") + .param("password", "\0") + .param("address", "Baum Straße"); + assertThat(mvc.perform(createMessage).andReturn().getResponse().getContentAsString()) + .contains("Ungültiges Passwort") + .contains("Ungültiger Name") + .doesNotContain("Ungültiger Nutzername"); + } + + @Test + @WithMockUser(username = "admin", password = "admin", roles = "ADMIN") + void alreadyExistingNameCheckWillNotBePerformedIfFormHasErrorCustomer() throws Exception { + assertThat(userManagement.getUsers().findAll()) + .extracting("username", "fullName", "address") + .contains(tuple("adam", "Adam Klaus", "Baum Weg")); + assertThat(mvc.perform(post("/customers/edit/" + userManagement.getUserByName("adam").get().getId()) + .param("username", "adam") + .param("address", "Baum Straße")) + .andReturn().getResponse().getContentAsString()) + .contains("Ungültiger Name") + .doesNotContain("Ungültiger Nutzername"); + } + + @Test + @WithAnonymousUser + void alreadyExistingNameCheckWillNotBePerformedIfFormHasErrorRegistration() throws Exception { + MockHttpServletRequestBuilder createCustomer = post("/register") + .param("username", "admin") + .param("password", "\0") + .param("fullName", "Tom Klaus") + .param("address", "Baum Weg"); + assertThat(mvc.perform(createCustomer).andReturn().getResponse().getContentAsString()) + .contains("Ungültiges Passwort") + .doesNotContain("Ungültiger Nutzername"); + } + }