mirror of
https://github.com/st-tu-dresden-praktikum/swt23w23
synced 2024-07-19 21:04:36 +02:00
Log out user properly after changing own username
This commit is contained in:
parent
3d5793297b
commit
79895d715b
|
@ -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")
|
||||
|
|
|
@ -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"));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue