Log out user properly after changing own username

This commit is contained in:
Denis Natusch 2023-11-21 22:09:14 +01:00
parent 3d5793297b
commit 79895d715b
No known key found for this signature in database
GPG key ID: 5E57BD8EDACFA985
2 changed files with 47 additions and 1 deletions

View file

@ -65,8 +65,12 @@ public class UserController {
@PostMapping("/profile")
@PreAuthorize("isAuthenticated()")
public String editProfile(@LoggedIn UserAccount userAccount, @Valid ProfileForm form) {
String redirect = "redirect:/logout";
User user = userManagement.getUserByAccount(userAccount).get();
if (form.getUsername().equals(user.getUsername())) {
redirect = "redirect:/profile";
}
user.setUsername(form.getUsername());
user.setFullName(form.getFullName());
user.setAddress(form.getAddress());
@ -78,7 +82,7 @@ public class UserController {
userManagement.save(user);
// by default the user gets logged out by salespoint after changing the user name
return "redirect:/profile";
return redirect;
}
@GetMapping("/profile/disable")

View file

@ -0,0 +1,42 @@
package catering.users;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
@AutoConfigureMockMvc
@SpringBootTest
class UserControllerIntegrationTests {
@Autowired
MockMvc mvc;
@BeforeEach
void init() throws Exception {
MockHttpServletRequestBuilder createCustomer = post("/register")
.param("username", "dieter")
.param("password", "123")
.param("fullName", "Dieter")
.param("address", "Baum Weg");
mvc.perform(createCustomer);
}
@Test
@WithMockUser(username = "dieter", password = "123")
void redirectAfterChangingOwnUsernameWithDifferentUsername() throws Exception {
MockHttpServletRequestBuilder createMessage = post("/profile")
.param("username", "diete")
.param("password", "123")
.param("fullName", "Dieter")
.param("address", "Baum Weg");
mvc.perform(createMessage).andExpect(redirectedUrl("/logout"));
}
}