chat deleting

This commit is contained in:
Stepan Usatiuk
2023-12-22 12:56:31 +01:00
parent f2bfd9bdd2
commit 9d38035205
2 changed files with 42 additions and 0 deletions

View File

@@ -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);

View File

@@ -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,