79.1 Delete user (#9)
This commit is contained in:
@ -91,6 +91,17 @@ public class UserResource {
|
|||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("{id}")
|
||||||
|
public HttpResponse deleteUser(@PathVariable long id){
|
||||||
|
userService.deleteUser(id);
|
||||||
|
return HttpResponse.builder()
|
||||||
|
.httpStatusCode(OK.value())
|
||||||
|
.httpStatus(OK)
|
||||||
|
.reason(OK.getReasonPhrase())
|
||||||
|
.message("User deleted successfully")
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
private void authenticate(String username, String password) {
|
private void authenticate(String username, String password) {
|
||||||
Authentication auth = new UsernamePasswordAuthenticationToken(username, password);
|
Authentication auth = new UsernamePasswordAuthenticationToken(username, password);
|
||||||
authenticationManager.authenticate(auth);
|
authenticationManager.authenticate(auth);
|
||||||
|
|||||||
@ -520,4 +520,37 @@ class UserResourceUnSecureTest extends BaseUserTest {
|
|||||||
.hasFieldOrPropertyWithValue("message", "Password reset successfully. Check your email for new password");
|
.hasFieldOrPropertyWithValue("message", "Password reset successfully. Check your email for new password");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
class DeleteUserTests {
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
user = userRepository
|
||||||
|
.findAll()
|
||||||
|
.stream()
|
||||||
|
.findAny()
|
||||||
|
.orElseGet(() -> userRepository.save(createRandomUser()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void deleteUser() {
|
||||||
|
|
||||||
|
//given
|
||||||
|
long id = user.getId();
|
||||||
|
|
||||||
|
//when
|
||||||
|
var responseEntity = restTemplate.exchange("/user/{id}", HttpMethod.DELETE, null, HttpResponse.class, id);
|
||||||
|
|
||||||
|
//then
|
||||||
|
log.debug("Response Entity: {}", responseEntity);
|
||||||
|
assertThat(responseEntity.getStatusCode()).isEqualTo(OK);
|
||||||
|
assertThat(responseEntity.getBody())
|
||||||
|
.isNotNull()
|
||||||
|
.hasNoNullFieldsOrProperties()
|
||||||
|
.hasFieldOrPropertyWithValue("httpStatus", OK)
|
||||||
|
.hasFieldOrPropertyWithValue("message", "User deleted successfully");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user