public async Task GetUserById_UserDoesNotExist_ThrowsNotFoundException()
        {
            var sut = new GetUserByIdQueryHandler(_context);

            await sut.Handle(new GetUserByIdQuery { Id = Guid.NewGuid() }, CancellationToken.None)
            .ShouldThrowAsync <NotFoundException>();
        }
Ejemplo n.º 2
0
        public static UserDto GetUserById(int userId)
        {
            var     queryObject = new GetUserByIdQuery(userId);
            var     handler     = new GetUserByIdQueryHandler();
            UserDto user        = handler.Handle(queryObject).Result;

            return(user);
        }
        public async Task GetUserById_UserExists_ReturnsUserViewModel()
        {
            var sut  = new GetUserByIdQueryHandler(_context);
            var user = _context.User.First();

            var result = await sut.Handle(new GetUserByIdQuery { Id = user.Id }, CancellationToken.None);

            result.ShouldBeOfType <UserViewModel>();
            result.Id.ShouldBe(user.Id);
        }
        public async Task GetUserByIdQuery_PresentNotExistsUserId_ReturnsNull()
        {
            // ---- Arrange ----
            var query   = new GetUserByIdQuery(Guid.Empty);
            var handler = new GetUserByIdQueryHandler(_repositoryProviderMock.Object);

            // ---- Act ----
            var user = await handler.Handle(query, CancellationToken.None);

            // ---- Assert ----
            Assert.IsNull(user);
        }
Ejemplo n.º 5
0
        public async Task Handle_GivenInvalidRequest_ShouldThrowNotFoundException()
        {
            // Arrange
            var query        = new GetUserByIdQuery(Guid.NewGuid());
            var queryHandler = new GetUserByIdQueryHandler(_fixture.DbContext, _fixture.Mapper);

            // Act
            Func <Task <UserDto> > result = () => queryHandler.Handle(query, CancellationToken.None);

            // Assert
            await result.Should().ThrowAsync <NotFoundException>();
        }
Ejemplo n.º 6
0
        public void GetUserById_NotFound(Guid id)
        {
            base.UsersRespositoryMock.Setup(x => x.FindSingle(It.IsAny <Expression <Func <User, bool> > >())).ReturnsAsync(value: default);

            var handler = new GetUserByIdQueryHandler(base.UnitOfWorkMock.Object);

            var result = handler.Handle(new GetUserByIdQuery(id), default);

            Assert.IsNotNull(result);
            Assert.IsInstanceOf <Task <Response <UserVm> > >(result);
            Assert.AreEqual(StatusCodes.Status404NotFound, result.Result.StatusCode);
        }
Ejemplo n.º 7
0
        public async Task Handle_GivenValidRequest_ShouldReturnUser()
        {
            // Arrange
            var query        = new GetUserByIdQuery(_fixture.KnownUserID);
            var queryHandler = new GetUserByIdQueryHandler(_fixture.DbContext, _fixture.Mapper);

            // Act
            UserDto user = await queryHandler.Handle(query, CancellationToken.None);

            // Assert
            user.Should().NotBeNull();
            user.UserId.Should().Equals(_fixture.KnownUserID);
        }
        public void GetUserByIdQuery_PresentNotExistsUserIdAllowException_ThrowException()
        {
            // ---- Arrange ----
            var query = new GetUserByIdQuery(Guid.Empty)
            {
                ThrowIfNotExists = true
            };
            var handler = new GetUserByIdQueryHandler(_repositoryProviderMock.Object);

            // ---- Act & Assert
            var exception = Assert.ThrowsAsync <UserNotFoundByIdException>(async() =>
                                                                           await handler.Handle(query, CancellationToken.None));

            Assert.AreEqual(query.UserId, exception.UserId);
        }
Ejemplo n.º 9
0
        public void Arrange()
        {
            _user = new User
            {
                Id       = "xyz",
                IsActive = true
            };
            _userRepository = new Mock <IUserRepository>();
            _userRepository.Setup(r => r.GetById("xyz")).Returns(Task.FromResult(_user));

            _handler = new GetUserByIdQueryHandler(_userRepository.Object);

            _query = new GetUserByIdQuery {
                UserId = "xyz"
            };
        }
Ejemplo n.º 10
0
        public void GetUserById_Successfully(Guid id)
        {
            base.UsersRespositoryMock.Setup(x => x.FindSingle(It.IsAny <Expression <Func <User, bool> > >())).ReturnsAsync(new User
            {
                Id    = id,
                Name  = "test",
                Email = "test"
            });

            var handler = new GetUserByIdQueryHandler(base.UnitOfWorkMock.Object);

            var result = handler.Handle(new GetUserByIdQuery(id), default);

            Assert.IsNotNull(result);
            Assert.IsInstanceOf <Task <Response <UserVm> > >(result);
            Assert.AreEqual(StatusCodes.Status200OK, result.Result.StatusCode);
        }
Ejemplo n.º 11
0
        public async Task GivenAnId_WhenGetUserByIdQueryHandler_ThenReturnNotNull()
        {
            var user     = UserFactory.ValidUser();
            var command  = new GetUserByIdQuery(user.Id);
            var mockRepo = new Mock <IUserRepository>();

            mockRepo.Setup(db => db.GetByIdAsync(command.Id).Result).Returns(user);
            var handler = new GetUserByIdQueryHandler(mockRepo.Object);

            var result = await handler.Handle(command, CancellationToken.None);

            result.Should().NotBeNull();
            result.Id.Should().Be(user.Id);
            result.Type.Should().Be(user.Type);
            result.Location.Should().Be(user.Location);
            result.Username.Should().Be(user.Username);
        }
        public async Task ShouldReturn_OneUserExists_ReturnUserViewModel()
        {
            var fixture = new Fixture();
            var user    = fixture.Create <Task <User> >();

            var userRepository = new Mock <IUserRepository>();

            var getUserByIdQuery = new GetUserByIdQuery(user.Id);

            var getPeopleByIdQueryHandler = new GetUserByIdQueryHandler(userRepository.Object);

            userRepository.Setup(pr => pr.GetByIdAsync(user.Id))
            .Returns(user);

            var userResult = await getPeopleByIdQueryHandler.Handle(getUserByIdQuery, new System.Threading.CancellationToken());;

            Assert.NotNull(userResult);
            Assert.IsType <UserViewModel>(userResult);
        }
        public async Task GetUserByIdQuery_PresentExistsUserId_ReturnsUser()
        {
            // ---- Arrange ----
            var userId = Guid.NewGuid();

            _usersRepositoryMock.Setup(r => r.Get(userId))
            .ReturnsAsync((Guid id) => _userFactory.Get(id));

            var query   = new GetUserByIdQuery(userId);
            var handler = new GetUserByIdQueryHandler(_repositoryProviderMock.Object);

            // ---- Act ----
            var user = await handler.Handle(query, CancellationToken.None);

            // ---- Assert ----
            _usersRepositoryMock.Verify(r => r.Get(userId), Times.Once);
            Assert.NotNull(user);
            Assert.AreEqual(userId, user.Id);
        }
Ejemplo n.º 14
0
        public async Task ThreeProjectsExist_Executed_ReturnThreeUserByIdViewModels()
        {
            // Arrange
            var user = new User("Thiago Primo", "*****@*****.**", null, "-dpf!#14", "client");

            var userRepositoryMock = new Mock <IUserRepository>();

            userRepositoryMock.Setup(u => u.GetByIdAsync(user.Id).Result).Returns(user);

            var getUserByIdQuery        = new GetUserByIdQuery(user.Id);
            var getUserByIdQueryHandler = new GetUserByIdQueryHandler(userRepositoryMock.Object);

            // Act
            var userViewModel = await getUserByIdQueryHandler.Handle(getUserByIdQuery, new CancellationToken());

            //Assert
            Assert.NotNull(userViewModel);

            userRepositoryMock.Verify(u => u.GetByIdAsync(user.Id).Result, Times.Once);
        }
        public async Task UserWithIdExists_Executed_ReturnUserViewModel()
        {
            //Arrange
            var user = new User("Usuário de teste", "*****@*****.**", Convert.ToDateTime("15/12/1993"), "123456", "client");

            var userRepositoryMock = new Mock <IUserRepository>();

            var getUserByIdQuery        = new GetUserByIdQuery(4);
            var getUserByIdQueryHandler = new GetUserByIdQueryHandler(userRepositoryMock.Object);

            userRepositoryMock.Setup(u => u.GetByIdAsync(getUserByIdQuery.id).Result).Returns(user);

            //Act
            var userViewModel = await getUserByIdQueryHandler.Handle(getUserByIdQuery, new CancellationToken());

            //Assert
            Assert.NotNull(userViewModel);

            userRepositoryMock.Verify(pr => pr.GetByIdAsync(It.IsAny <int>()), Times.Once);
        }
Ejemplo n.º 16
0
        public void UserIsCreatedWithCorrectInformation()
        {
            UserDetailModel createdUser = null;
            UnProcessableEntityException couldNotCreateUserException  = null;
            EntityNotFoundException      createdUserNotFoundException = null;

            var command = new CreateUserCommand
            {
                FirstName = "Marco",
                LastName  = "Polo",
                Password  = "******",
                Username  = "******"
            };

            try
            {
                var createdUserId = new CreateUserCommandHandler(_fixture.LandmarkContext, new FakeDatetimeProvider()).Handle(command, CancellationToken.None).GetAwaiter().GetResult();
                createdUser = new GetUserByIdQueryHandler(_fixture.LandmarkContext).Handle(new GetUserByIdQuery {
                    Id = createdUserId
                }, CancellationToken.None).GetAwaiter()
                              .GetResult();
            }
            catch (UnProcessableEntityException ex)
            {
                couldNotCreateUserException = ex;
            }
            catch (EntityNotFoundException ex)
            {
                createdUserNotFoundException = ex;
            }

            couldNotCreateUserException.ShouldBeNull();
            createdUserNotFoundException.ShouldBeNull();
            createdUser.ShouldSatisfyAllConditions(
                () => createdUser.FirstName.ShouldBe("Marco"),
                () => createdUser.LastName.ShouldBe("Polo"),
                () => createdUser.UserName.ShouldBe("marco.polo")
                );
        }
Ejemplo n.º 17
0
        public void UserIsNotCreatedAndExceptionWithDetailsIsThrown()
        {
            UserDetailModel createdUser;
            UnProcessableEntityException couldNotCreateUserException  = null;
            EntityNotFoundException      createdUserNotFoundException = null;

            try
            {
                var command = new CreateUserCommand
                {
                    LastName = "Polo",
                    Password = "******",
                    Username = "******"
                };
                var createdUserId = new CreateUserCommandHandler(_fixture.LandmarkContext, new FakeDatetimeProvider()).Handle(command, CancellationToken.None).GetAwaiter().GetResult();
                createdUser = new GetUserByIdQueryHandler(_fixture.LandmarkContext).Handle(new GetUserByIdQuery {
                    Id = createdUserId
                }, CancellationToken.None).GetAwaiter()
                              .GetResult();
            }
            catch (UnProcessableEntityException ex)
            {
                couldNotCreateUserException = ex;
            }
            catch (EntityNotFoundException ex)
            {
                createdUserNotFoundException = ex;
            }

            couldNotCreateUserException.ShouldNotBeNull();
            couldNotCreateUserException.ModelStateErrors.Count().ShouldBe(1);
            var firstError = couldNotCreateUserException.ModelStateErrors.First();

            firstError.ShouldSatisfyAllConditions(
                () => { firstError.PropertyName.ShouldBe(nameof(CreateUserCommand.FirstName)); });
        }
 public GetUserByIdQueryTests()
 {
     _query = new GetUserByIdQueryHandler(DbContext);
 }
Ejemplo n.º 19
0
 public GetUserByEmailTest()
 {
     // Arrange
     Context = ContextFactory.Create();
     Handler = new GetUserByIdQueryHandler(Context);
 }
 public void SetUp()
 {
     _userCollection  = Database.GetCollection <UserEntity>("User");
     _classUnderTests = new GetUserByIdQueryHandler(Database);
 }
 public GetUserByIdQueryHandlerTests()
 {
     this.sut = new GetUserByIdQueryHandler(this.Context);
 }