示例#1
0
            public async Task WhenCreateMessageGetsCalled()
            {
                Setup();

                _createMessageRequest = new Fixture().Create <CreateMessageRequest>();

                AuthService.Setup(service => service.AuthorizeSelf(It.IsAny <string>(), It.IsAny <Guid>()))
                .Returns(false);

                _result = await MessagesController.CreateMessage(_createMessageRequest);
            }
示例#2
0
        public async Task IntegrityTest()
        {
            _controller.User.SetId(Dummies.Alice.Id);
            var res = await _controller.ListMessages(Dummies.GuildA.Id);

            var items = (IEnumerable <ApiManagedMessage>)((ObjectResult)res).Value;

            Assert.Empty(items);

            await Assert.ThrowsAsync <MessageNotFoundException>(async() =>
                                                                await _controller.CreateMessage(Dummies.GuildA.Id, new ApiManagedMessage(new GuildManagedMessage(1, 1, 1000, null !))));

            // Impossible to test due to inability to create mocked entities (IAsyncEnumerable duality), even with moq.
        }
示例#3
0
        public async Task CreateMessage_InvalidRequest_ReturnsUnauthorizedResult()
        {
            var user = new ClaimsPrincipal(new ClaimsIdentity(new Claim[]
            {
                new Claim(ClaimTypes.NameIdentifier, "1"),
            }));
            var messageForCreation = new MessageForCreationDto()
            {
                SenderId    = 1,
                RecipientId = 1
            };
            var mapperMock     = new Mock <IMapper>();
            var repositoryMock = new Mock <IRepositoryWrapper>();

            repositoryMock.Setup(r => r.UserRepository.GetUserAsync(1)).ReturnsAsync(new User
            {
                Id = 1
            });
            repositoryMock.Setup(r => r.UserRepository.GetUserAsync(2)).ReturnsAsync(new User
            {
                Id = 2
            });
            repositoryMock.Setup(r => r.SaveAllAsync()).ReturnsAsync(true);
            repositoryMock.Setup(r => r.MessageRepository.Add(It.IsAny <Message>()));
            mapperMock.Setup(m => m.Map <Message>(It.IsAny <MessageForCreationDto>())).Returns(new Message());
            mapperMock.Setup(m => m.Map <MessageToReturnDto>(It.IsAny <Message>())).Returns(new MessageToReturnDto());


            var controllerMock = new MessagesController(repositoryMock.Object, mapperMock.Object);

            controllerMock.ControllerContext = new ControllerContext()
            {
                HttpContext = new DefaultHttpContext()
                {
                    User = user
                }
            };
            var result = await controllerMock.CreateMessage(2, messageForCreation);

            Assert.IsType <UnauthorizedResult>(result);
        }
示例#4
0
            public async Task WhenCreateMessageGetsCalled()
            {
                Setup();

                var fixture = new Fixture();

                _conversationParticipants = fixture.CreateMany <Guid>().ToList();
                _createMessageRequest     = fixture.Build <CreateMessageRequest>()
                                            .With(request => request.SenderId, _conversationParticipants.Last())
                                            .Create();

                AuthService.Setup(service => service.GetUserIdFromToken(It.IsAny <string>())).Returns(_createMessageRequest.SenderId.ToString());
                AuthService.Setup(service => service.AuthorizeSelf(It.IsAny <string>(), It.IsAny <Guid>()))
                .Returns(true);
                ConversationsService.Setup(service => service.GetConversationParticipants(It.IsAny <Guid>()))
                .ReturnsAsync(_conversationParticipants);
                MessagesService.Setup(repository => repository.CreateMessage(It.IsAny <Message>()))
                .Callback <Message>(msg => _usedMessage = msg);

                _result = await MessagesController.CreateMessage(_createMessageRequest);
            }