mirror of
https://github.com/usatiuk/y.git
synced 2025-10-28 18:37:47 +01:00
chat deleting
This commit is contained in:
@@ -56,6 +56,16 @@ public class ChatController {
|
||||
return chatMapper.makeDto(chat);
|
||||
}
|
||||
|
||||
@DeleteMapping(path = "/by-id/{id}")
|
||||
@ResponseStatus(HttpStatus.NO_CONTENT)
|
||||
public void delete(Principal principal, @PathVariable Long id) {
|
||||
var chat = chatService.readById(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Chat not found"));
|
||||
if (!Objects.equals(chat.getCreator().getUuid(), principal.getName()))
|
||||
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "User isn't creator of the chat");
|
||||
chatService.deleteById(id);
|
||||
}
|
||||
|
||||
|
||||
@GetMapping(path = "/my")
|
||||
public Stream<ChatTo> getMy(Principal principal) {
|
||||
return chatService.readByMember(principal.getName()).stream().map(chatMapper::makeDto);
|
||||
|
||||
@@ -76,6 +76,38 @@ public class ChatControllerTest extends DemoDataDbTest {
|
||||
Assertions.assertIterableEquals(Stream.of(chat1, chat2).map(chatMapper::makeDto).toList(), Arrays.asList(toResponse));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldDeleteChat() {
|
||||
Long chat1Id = chat1.getId();
|
||||
var response = restTemplate.exchange(addr + "/chat/by-id/" + chat1Id, HttpMethod.DELETE,
|
||||
new HttpEntity<>(createAuthHeaders(person1Auth)),
|
||||
Object.class);
|
||||
|
||||
Assertions.assertNotNull(response);
|
||||
Assertions.assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
|
||||
|
||||
Assertions.assertFalse(chatRepository.existsById(chat1Id));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotDeleteChatUnauthorized() {
|
||||
Long chatId = chat2.getId();
|
||||
var response = restTemplate.exchange(addr + "/chat/by-id/" + chatId, HttpMethod.DELETE,
|
||||
new HttpEntity<>(createAuthHeaders(person1Auth)),
|
||||
ErrorTo.class);
|
||||
|
||||
Assertions.assertNotNull(response);
|
||||
Assertions.assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode());
|
||||
|
||||
var toResponse = response.getBody();
|
||||
Assertions.assertNotNull(toResponse);
|
||||
|
||||
Assertions.assertEquals(HttpStatus.FORBIDDEN.value(), toResponse.code());
|
||||
|
||||
Assertions.assertTrue(chatRepository.existsById(chatId));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void shouldNotChatUnauthorized() {
|
||||
var response = restTemplate.exchange(addr + "/chat/by-id/" + chat1.getId(), HttpMethod.GET,
|
||||
|
||||
Reference in New Issue
Block a user