public void ShouldThrowIfMemberIsNotMessageOwner()
        {
            // Arrange
            _messageRepositoryMock.Setup(x => x.GetMessageWithOwnerAndForwardMessageAsync(It.IsAny <Guid>()))
            .ReturnsAsync(new Message {
                OwnerId = new Guid("F19AD922-B0DB-4686-8CB4-F51902800CAE")
            })
            .Verifiable();
            _memberRepositoryMock.Setup(x => x.GetMemberBySaasUserIdAsync(It.IsAny <string>()))
            .ReturnsAsync(new Member {
                Id = new Guid("ABF2CA08-5374-4CED-BE87-6EA93A8B90DA")
            })
            .Verifiable();

            var request = new DeleteMessageAttachmentRequest("864EB62D-D833-47FA-8A88-DDBFE76AE6A7", new Guid("A455F139-09E6-4EF5-B55A-D4C94D05DFDE"), new Guid("609B190D-063C-4C2E-A7BD-5E8F98E8D4D9"));

            // Act
            Func <Task> act = async() => { await _messageService.DeleteMessageAttachmentAsync(request); };

            // Assert
            act.Should().Throw <NetKitChatAccessForbiddenException>()
            .And.Message.Should().Be($"Unable to delete message attachment. Message {nameof(request.MessageId)}:{request.MessageId} owner required.");

            VerifyMocks();
        }
        public void ShouldThrowIfAttachmentNotFound()
        {
            // Arrange
            var request = new DeleteMessageAttachmentRequest("864EB62D-D833-47FA-8A88-DDBFE76AE6A7", new Guid("A455F139-09E6-4EF5-B55A-D4C94D05DFDE"), new Guid("609B190D-063C-4C2E-A7BD-5E8F98E8D4D9"));

            var member = new Member
            {
                Id = new Guid("ABF2CA08-5374-4CED-BE87-6EA93A8B90DA")
            };

            _messageRepositoryMock.Setup(x => x.GetMessageWithOwnerAndForwardMessageAsync(It.IsAny <Guid>()))
            .ReturnsAsync(new Message {
                OwnerId = member.Id
            })
            .Verifiable();

            _memberRepositoryMock.Setup(x => x.GetMemberBySaasUserIdAsync(It.IsAny <string>()))
            .ReturnsAsync(member)
            .Verifiable();

            _attachmentRepositoryMock.Setup(x => x.GetAttachmentAsync(It.IsAny <Guid>()))
            .ReturnsAsync((Attachment)null)
            .Verifiable();

            // Act
            Func <Task> act = async() => { await _messageService.DeleteMessageAttachmentAsync(request); };

            // Assert
            act.Should().Throw <NetKitChatNotFoundException>()
            .And.Message.Should().Be($"Unable to delete message attachment. Attachment {nameof(request.AttachmentId)}:{request.AttachmentId} is not found.");

            VerifyMocks();
        }
Example #3
0
        public async Task DeleteMessageAttachmentAsync(DeleteMessageAttachmentRequest request)
        {
            try
            {
                var member = await _memberService.GetMemberSummaryBySaasUserIdAsync(request.SaasUserId);

                var message = await _messageService.GetMessageByIdAsync(request.MessageId);

                if (message.Sender.Id != member.Id)
                {
                    throw new Exception(String.Format(LanguageResources.Msg_AccessPermission, message.Id));
                }

                await _messageService.DeleteMessageAttachmentAsync(request);

                await _messageNotificationHub.OnDeleteMessageAttachment(member, message);
            }
            catch (ServiceException ex)
            {
                if (ex.Errors.Any(x => x.Description == "Message does not exist."))
                {
                    _logger.Event(PropertyNames.EventId).With.Message("Exception: Message does not exist. MessageId: {messageId}", request.MessageId).Exception(ex).AsError();
                    throw new Exception(String.Format(LanguageResources.Msg_NotFound, request.MessageId));
                }
                if (ex.Errors.Any(x => x.Description == "Attachment does not exist."))
                {
                    _logger.Event(PropertyNames.EventId).With.Message("Exception: Attachemnt does not exist. AttachmentId: {attachmentId}", request.AttachmentId).Exception(ex).AsError();
                    throw new Exception(String.Format(LanguageResources.Msg_AttachmentNotFound, request.AttachmentId));
                }
            }
        }
        public void ShouldThrowIfMessageDoesNotContainAttachment()
        {
            // Arrange
            var request = new DeleteMessageAttachmentRequest("864EB62D-D833-47FA-8A88-DDBFE76AE6A7", new Guid("A455F139-09E6-4EF5-B55A-D4C94D05DFDE"), new Guid("609B190D-063C-4C2E-A7BD-5E8F98E8D4D9"));

            var member = new Member
            {
                Id = new Guid("ABF2CA08-5374-4CED-BE87-6EA93A8B90DA")
            };

            _messageRepositoryMock.Setup(x => x.GetMessageWithOwnerAndForwardMessageAsync(It.IsAny <Guid>()))
            .ReturnsAsync(new Message {
                Id = new Guid("5684E61A-05B2-46C6-97DC-5136AC6D68CE"), OwnerId = member.Id
            })
            .Verifiable();

            _memberRepositoryMock.Setup(x => x.GetMemberBySaasUserIdAsync(It.IsAny <string>()))
            .ReturnsAsync(member)
            .Verifiable();

            _attachmentRepositoryMock.Setup(x => x.GetAttachmentAsync(It.IsAny <Guid>()))
            .ReturnsAsync(new Attachment {
                MessageId = new Guid("6AB05E21-5EEE-4407-B798-886D2ED04A4C")
            })
            .Verifiable();

            // Act
            Func <Task> act = async() => { await _messageService.DeleteMessageAttachmentAsync(request); };

            // Assert
            act.Should().Throw <NetKitChatInvalidOperationException>()
            .And.Message.Should().Be($"Unable to delete message attachment. Message {nameof(request.MessageId)}:{request.MessageId} does not contain attachment {nameof(request.AttachmentId)}:{request.AttachmentId}.");

            VerifyMocks();
        }
Example #5
0
        public async Task <IActionResult> DeleteMessageAttachmentAsync(Guid messageId, Guid attachmentId)
        {
            var request = new DeleteMessageAttachmentRequest(GetCurrentSaasUserId(), messageId, attachmentId);
            await _messageSocketService.DeleteMessageAttachmentAsync(request);

            return(Ok());
        }
        public async Task DeleteMessageAttachmentAsync(DeleteMessageAttachmentRequest request)
        {
            var message = await _messageService.GetMessageByIdAsync(request.MessageId);

            await _messageService.DeleteMessageAttachmentAsync(request);

            await _messageNotificationService.OnDeleteMessageAttachment(message);
        }
Example #7
0
 public async Task DeleteMessageAttachmentAsync(DeleteMessageAttachmentRequest request)
 {
     await CheckAccessTokenAndExecute(new TaskReference(async() =>
     {
         request.SaasUserId = Context.GetSaasUserId();
         await _messageSocketService.DeleteMessageAttachmentAsync(request);
     }),
                                      request.RequestId);
 }
        public async Task ShouldDeleteAttachment()
        {
            // Arrange
            var request = new DeleteMessageAttachmentRequest("864EB62D-D833-47FA-8A88-DDBFE76AE6A7", new Guid("A455F139-09E6-4EF5-B55A-D4C94D05DFDE"), new Guid("609B190D-063C-4C2E-A7BD-5E8F98E8D4D9"));

            var member = new Member
            {
                Id = new Guid("ABF2CA08-5374-4CED-BE87-6EA93A8B90DA")
            };

            var message = new Message
            {
                Id      = new Guid("5684E61A-05B2-46C6-97DC-5136AC6D68CE"),
                OwnerId = member.Id
            };

            _messageRepositoryMock.Setup(x => x.GetMessageWithOwnerAndForwardMessageAsync(It.Is <Guid>(messageId => messageId.Equals(request.MessageId))))
            .ReturnsAsync(message)
            .Verifiable();

            _memberRepositoryMock.Setup(x => x.GetMemberBySaasUserIdAsync(It.Is <string>(saasUserId => saasUserId.Equals(request.SaasUserId))))
            .ReturnsAsync(member)
            .Verifiable();

            var attachment = new Attachment
            {
                Id        = request.AttachmentId,
                MessageId = message.Id,
                FileName  = "attachment file name"
            };

            _attachmentRepositoryMock.Setup(x => x.GetAttachmentAsync(It.Is <Guid>(attachmentId => attachmentId.Equals(request.AttachmentId))))
            .ReturnsAsync(attachment)
            .Verifiable();

            _attachmentRepositoryMock.Setup(x => x.DeleteAttachmentAsync(It.Is <Guid>(attachmentId => attachmentId.Equals(request.AttachmentId))))
            .Returns(Task.CompletedTask)
            .Verifiable();

            _cloudAttachmentProviderMock.Setup(x => x.DeleteMessageAttachmentAsync(It.Is <string>(attachmentFileName => attachmentFileName.Equals(attachment.FileName))))
            .Returns(Task.CompletedTask)
            .Verifiable();

            // Act
            await _messageService.DeleteMessageAttachmentAsync(request);

            // Assert
            VerifyMocks();
        }
        public void ShouldThrowIfMessageDoesNotExist()
        {
            // Arrange
            _messageRepositoryMock.Setup(x => x.GetMessageWithOwnerAndForwardMessageAsync(It.IsAny <Guid>()))
            .ReturnsAsync((Message)null)
            .Verifiable();

            var request = new DeleteMessageAttachmentRequest("864EB62D-D833-47FA-8A88-DDBFE76AE6A7", new Guid("A455F139-09E6-4EF5-B55A-D4C94D05DFDE"), new Guid("609B190D-063C-4C2E-A7BD-5E8F98E8D4D9"));

            // Act
            Func <Task> act = async() => { await _messageService.DeleteMessageAttachmentAsync(request); };

            // Assert
            act.Should().Throw <NetKitChatNotFoundException>()
            .And.Message.Should().Be($"Unable to delete message attachment. Message {nameof(request.MessageId)}:{request.MessageId} is not found.");

            VerifyMocks();
        }
        public async Task DeleteMessageAttachmentAsync(DeleteMessageAttachmentRequest request)
        {
            var message = await UnitOfWork.MessageRepository.GetMessageByIdAsync(request.MessageId);

            Ensure.That(message).WithException(x => new NotFoundException(new ErrorDto(ErrorCode.NotFound, "Message does not exist."))).IsNotNull();
            var attachment = await UnitOfWork.AttachmentRepository.GetAttachmentByIdAsync(request.AttachmentId);

            Ensure.That(attachment).WithException(x => new NotFoundException(new ErrorDto(ErrorCode.NotFound, "Attachment does not exist."))).IsNotNull();
            if (attachment.MessageId != message.Id)
            {
                throw new ConflictException(new ErrorDto(ErrorCode.ConflictError, "Message does not contain this attachment."));
            }

            // Delete message attachment from database
            await UnitOfWork.AttachmentRepository.DeleteAttachmentAsync(attachment.Id);

            // Delete attachment from blob storage
            await _contentStorage.DeleteContentAsync(attachment.FileName, _cloudStorageConfiguration.MessageAttachmentsContainer);
        }
Example #11
0
        public async Task DeleteMessageAttachmentAsync(DeleteMessageAttachmentRequest request)
        {
            var message = await UnitOfWork.MessageRepository.GetMessageWithOwnerAndForwardMessageAsync(request.MessageId);

            if (message == null)
            {
                throw new NetKitChatNotFoundException($"Unable to delete message attachment. Message {nameof(request.MessageId)}:{request.MessageId} is not found.");
            }

            var member = await UnitOfWork.MemberRepository.GetMemberBySaasUserIdAsync(request.SaasUserId);

            if (member == null)
            {
                throw new NetKitChatNotFoundException($"Unable to delete message attachment. Member {nameof(request.SaasUserId)}:{request.SaasUserId} is not found.");
            }

            if (member.Id != message.OwnerId)
            {
                throw new NetKitChatAccessForbiddenException($"Unable to delete message attachment. Message {nameof(request.MessageId)}:{request.MessageId} owner required.");
            }

            var attachment = await UnitOfWork.AttachmentRepository.GetAttachmentAsync(request.AttachmentId);

            if (attachment == null)
            {
                throw new NetKitChatNotFoundException($"Unable to delete message attachment. Attachment {nameof(request.AttachmentId)}:{request.AttachmentId} is not found.");
            }

            if (attachment.MessageId != message.Id)
            {
                throw new NetKitChatInvalidOperationException($"Unable to delete message attachment. Message {nameof(request.MessageId)}:{request.MessageId} does not contain attachment {nameof(request.AttachmentId)}:{request.AttachmentId}.");
            }

            // Delete message attachment from database
            await UnitOfWork.AttachmentRepository.DeleteAttachmentAsync(attachment.Id);

            // Delete attachment from cloud
            await _cloudAttachmentProvider.DeleteMessageAttachmentAsync(attachment.FileName);
        }