Esempio n. 1
0
        public async Task given_invalid_username_send_message_should_throw_an_exception
            ([Frozen] Mock <IUserRepository> userRepository,
            SendMessageHandler handler)
        {
            var  sendMessage = new SendMessage("", "test");
            User user        = null;

            userRepository.Setup(r => r.GetByUserNameAsync("")).ReturnsAsync(user);

            var exception = await Record.ExceptionAsync(async() => await handler.HandleAsync(sendMessage));

            exception.Should().NotBeNull();
            exception.Should().BeOfType <UserNotFoundException>();
        }
Esempio n. 2
0
        public async Task given_valid_parameters_send_message_should_success
            ([Frozen] Mock <IUserRepository> userRepository,
            [Frozen] Mock <IUserContext> userContext,
            [Frozen] Mock <IBlockUserRepository> blockUserRepository,
            User toUser,
            SendMessageHandler handler)
        {
            var sendMessage = new SendMessage(toUser.UserName, "test");

            userRepository.Setup(r => r.GetByUserNameAsync(toUser.UserName)).ReturnsAsync(toUser);
            blockUserRepository.Setup(r => r.IsBlockedUser(toUser.Id, userContext.Object.Id)).ReturnsAsync(false);
            userContext.Setup(r => r.Id).Returns(Guid.NewGuid());
            var exception = await Record.ExceptionAsync(async() => await handler.HandleAsync(sendMessage));

            exception.Should().BeNull();
        }
Esempio n. 3
0
        public async Task given_blocked_user_id_send_message_should_throw_an_exception
            ([Frozen] Mock <IUserRepository> userRepository,
            [Frozen] Mock <IUserContext> userContext,
            [Frozen] Mock <IBlockUserRepository> blockUserRepository,
            User toUser,
            SendMessageHandler handler)
        {
            var sendMessage = new SendMessage("", "test");

            userRepository.Setup(r => r.GetByUserNameAsync("")).ReturnsAsync(toUser);
            blockUserRepository.Setup(r => r.IsBlockedUser(toUser.Id, userContext.Object.Id)).ReturnsAsync(true);

            var exception = await Record.ExceptionAsync(async() => await handler.HandleAsync(sendMessage));

            exception.Should().NotBeNull();
            exception.Should().BeOfType <BlockedUserException>();
        }
        public async Task <IActionResult> Message(MessageBindingModel messageBindingModel)
        {
            //get sender ID
            var senderGambler = await new GamblerQueryHandler().HandleAsync(new GamblerQuery
            {
                Name = messageBindingModel.sender
            });

            Gambler sender = senderGambler.ToList().FirstOrDefault(g => g.Name == messageBindingModel.sender);

            if (sender == null)
            {
                return(RedirectToAction(StaticConstants.All, StaticConstants.ApiMessages, new { error = StaticConstants.SenderDontExistError }));
            }

            //get receiver ID
            var receiverGambler = await new GamblerQueryHandler().HandleAsync(new GamblerQuery
            {
                Name = messageBindingModel.receiver
            });

            Gambler receiver = receiverGambler.ToList().FirstOrDefault(g => g.Name == messageBindingModel.receiver);

            if (receiver == null)
            {
                return(RedirectToAction(StaticConstants.All, StaticConstants.ApiMessages, new { error = StaticConstants.ReceiverDontExistError }));
            }

            SendMessageCommand command = new SendMessageCommand {
                Body          = messageBindingModel.body,
                Subject       = messageBindingModel.subject,
                Id            = Guid.NewGuid(),
                FromGamblerId = sender.Id,
                ToGamblerId   = receiver.Id
            };

            var handler = new SendMessageHandler();
            await handler.HandleAsync(command);

            return(RedirectToAction(StaticConstants.All, StaticConstants.ApiMessages, new { message = StaticConstants.MessageSentSuccessMessage }));
        }