Fix modify profile issue

Closes: #141
This commit is contained in:
Denis Natusch 2024-01-14 12:50:38 +01:00
parent ff8bbdca15
commit da19356496
No known key found for this signature in database
GPG key ID: 5E57BD8EDACFA985
2 changed files with 49 additions and 1 deletions

View file

@ -94,7 +94,7 @@ public class UserController {
if ( if (
(!result.hasErrors()) && (!result.hasErrors()) &&
(!userManagement.getUserByName(form.getUsername()).isEmpty()) && (!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"); result.rejectValue("username", "username already in use");
} }

View file

@ -59,6 +59,8 @@ class UserControllerIntegrationTests {
createTestUser("paul"); createTestUser("paul");
createTestUser("mark"); createTestUser("mark");
createTestUser("eva"); createTestUser("eva");
createTestUser("sarah");
createTestUser("paul");
} }
@Test @Test
@ -353,4 +355,50 @@ class UserControllerIntegrationTests {
).andExpect(content().string(containsString("Ungültiger Nutzername"))); ).andExpect(content().string(containsString("Ungültiger Nutzername")));
assertTrue(userAccountManagement.findByUsername("eva").isPresent()); 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("/"));
}
} }