public async Task RecipientExistsQueryHandler_ShouldReturnFalse_WhenRecipientDoesNotExist() { // Arrange RecipientExistsQuery request = new RecipientExistsQuery { RecipientId = 1 }; _unitOfWorkMock .Setup(m => m.Recipients.Exists(request.RecipientId, It.IsAny <CancellationToken>())) .ReturnsAsync(false); RecipientExistsQuery.Handler handler = new RecipientExistsQuery.Handler(_unitOfWorkMock.Object); // Act bool exists = await handler.Handle(request); // Assert Assert.False(exists); }
public async Task <ActionResult <IEnumerable <ChatMessageResource> > > GetMessagesWithRecipient([FromRoute] int recipientId, [FromQuery] GetMessagesWithRecipientQueryParams boundaries, CancellationToken cancellationToken = default) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } // Check if the given recipient exists RecipientExistsQuery existsQuery = new RecipientExistsQuery { RecipientId = recipientId }; bool exists = await _mediator.Send(existsQuery, cancellationToken); if (!exists) { return(NotFound(new ErrorResource { StatusCode = StatusCodes.Status404NotFound, Message = $"Recipient with ID '{recipientId}' does not exist" })); } // Get messages with given recipient GetMessagesWithRecipientQuery fetchQuery = new GetMessagesWithRecipientQuery { RecipientId = recipientId, Limit = boundaries.Limit, Before = boundaries.Before, After = boundaries.After, }; IEnumerable <ChatMessageResource> messages = await _mediator.Send(fetchQuery, cancellationToken); return(Ok(messages)); }
public async Task <ActionResult <ChatMessageResource> > SendMessage([FromBody] SendMessageBody body, CancellationToken cancellationToken = default) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } // Check if recipient exists RecipientExistsQuery recipientExistsQuery = new RecipientExistsQuery { RecipientId = body.RecipientId }; bool recipientExists = await _mediator.Send(recipientExistsQuery, cancellationToken); if (!recipientExists) { return(NotFound(new ErrorResource { StatusCode = StatusCodes.Status404NotFound, Message = $"Recipient with ID '{body.RecipientId}' does not exist" })); } // Check if the the user wants to message himself IsOwnRecipientQuery isOwnRecipientQuery = new IsOwnRecipientQuery { RecipientId = body.RecipientId }; bool isOwnRecipient = await _mediator.Send(isOwnRecipientQuery, cancellationToken); if (isOwnRecipient) { return(StatusCode(StatusCodes.Status403Forbidden, new ErrorResource { StatusCode = StatusCodes.Status403Forbidden, Message = "You cannot write messages to yourself" })); } if (body.ParentId != null) { // Check if parent message exists MessageExistsQuery parentMessageExistsQuery = new MessageExistsQuery { MessageId = body.ParentId.Value }; bool parentMessageExists = await _mediator.Send(parentMessageExistsQuery, cancellationToken); if (!parentMessageExists) { return(NotFound(new ErrorResource { StatusCode = StatusCodes.Status404NotFound, Message = $"Parent message with ID '{body.ParentId}' does not exist" })); } // Check if the user should have access to the parent message CanAccessMessageQuery canAccessParentMessageQuery = new CanAccessMessageQuery { MessageId = body.ParentId.Value }; bool canAccessParentMessage = await _mediator.Send(canAccessParentMessageQuery, cancellationToken); if (!canAccessParentMessage) { return(StatusCode(StatusCodes.Status403Forbidden, new ErrorResource { StatusCode = StatusCodes.Status403Forbidden, Message = "You cannot answer messages from a foreign chat" })); } } // Send message to their recipients SendMessageCommand sendMessageCommand = _mapper.Map <SendMessageBody, SendMessageCommand>(body); ChatMessageResource message = await _mediator.Send(sendMessageCommand, cancellationToken); return(CreatedAtAction(nameof(GetMessageById), new { messageId = message.MessageId }, message)); }