From da19356496c862cd2eeceb137a83e7c51af0d333 Mon Sep 17 00:00:00 2001 From: Denis Natusch Date: Sun, 14 Jan 2024 12:50:38 +0100 Subject: [PATCH] Fix modify profile issue Closes: #141 --- .../java/catering/users/UserController.java | 2 +- .../users/UserControllerIntegrationTests.java | 48 +++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/main/java/catering/users/UserController.java b/src/main/java/catering/users/UserController.java index 5404c70..bcfd8f8 100644 --- a/src/main/java/catering/users/UserController.java +++ b/src/main/java/catering/users/UserController.java @@ -94,7 +94,7 @@ public class UserController { if ( (!result.hasErrors()) && (!userManagement.getUserByName(form.getUsername()).isEmpty()) && - (userManagement.getUserByAccount(userAccount).get().getUsername() != form.getUsername()) + (!userManagement.getUserByAccount(userAccount).get().getUsername().equals(form.getUsername())) ) { result.rejectValue("username", "username already in use"); } diff --git a/src/test/java/catering/users/UserControllerIntegrationTests.java b/src/test/java/catering/users/UserControllerIntegrationTests.java index a6456b6..e697378 100644 --- a/src/test/java/catering/users/UserControllerIntegrationTests.java +++ b/src/test/java/catering/users/UserControllerIntegrationTests.java @@ -59,6 +59,8 @@ class UserControllerIntegrationTests { createTestUser("paul"); createTestUser("mark"); createTestUser("eva"); + createTestUser("sarah"); + createTestUser("paul"); } @Test @@ -353,4 +355,50 @@ class UserControllerIntegrationTests { ).andExpect(content().string(containsString("Ungültiger Nutzername"))); assertTrue(userAccountManagement.findByUsername("eva").isPresent()); } + + @Test + @WithMockUser(username = "admin", password = "admin", roles = "ADMIN") + void editCustomerDetailsExeptUsername() throws Exception { + assertThat(userManagement.getUsers().findAll()) + .extracting("username", "fullName", "address") + .contains(tuple("sarah", "Sarah Klaus", "Baum Weg")); + mvc.perform(post("/customers/edit/" + userManagement.getUserByName("sarah").get().getId()) + // this form of construction is necessary, + // because we belive MockMvc internally uses the provided strings directly + // this leads to different behavor between tests and browser + .param("username", new String("sarah")) + .param("fullName", "Sarah Klaus") + .param("address", "Baum Straße")) + .andExpect(redirectedUrl("/customers")); + assertThat(userManagement.getUsers().findAll()) + .extracting("username", "fullName", "address") + .contains(tuple("sarah", "Sarah Klaus", "Baum Straße")); + mvc.perform(get("/customers/edit/" + userManagement.getUserByName("sarah").get().getId())) + .andExpect(status().isOk()) + .andExpect(view().name("edit-customer")); + } + + @Test + @WithMockUser(username = "paul", password = "123") + void updateEverythingInProfileExceptUsername() throws Exception { + assertTrue(userManagement.getUserByName("paul").isPresent()); + MockHttpServletRequestBuilder createMessage = post("/profile") + .param("username", new String("paul")) // explained above + .param("password", "paul") + .param("fullName", "Paul") + .param("address", "Hotel"); + mvc.perform(createMessage) + .andExpect(status().is3xxRedirection()) + .andExpect(redirectedUrl("/profile")); + assertThat(userManagement.getUsers().findAll()) + .extracting("username", "fullName", "address") + .contains(tuple("paul", "Paul", "Hotel")); + MockHttpServletRequestBuilder login = post("/login") + .param("username", "paul") + .param("password", "paul"); + mvc.perform(login) + .andExpect(status().is3xxRedirection()) + .andExpect(redirectedUrl("/")); + } + }