public void Should_Return_User_When_Register_With_Valid_Credentials()
        {
            // Arrange
            var fakes        = new Fakes();
            var fakeConfig   = fakes.FakeConfiguration().Object;
            var tokenService = new TokenService(fakeConfig);
            var hashService  = new HashService();

            var mockUserRepository = fakes.FakeUserRepository();

            // Returns null when GetByEmail is called, so the service doesn't return Conflict.
            mockUserRepository.Setup(x => x.GetByEmail(It.IsAny <string>()))
            .Returns(Task.FromResult <User>(null));
            var fakeUserRepository = mockUserRepository.Object;

            var request = fakes.Get <RegisterUserRequest>().First();
            var user    = fakes.Get <User>().First();

            var response = fakes.Mapper.Map <UserResponse>(user);

            response.Id   = 999;                 // Expected user id
            response.Role = EUserRole.DEVELOPER; // By default, user is created as DEVELOPER

            // Act
            var service = new AuthService(fakes.Mapper, fakeUserRepository, tokenService, hashService);
            var actual  = service.Register(request).Result;

            response.CreatedAt = (actual.Data as UserResponse).CreatedAt;
            var expected = Responses.OkResponse(null, response);

            // Assert
            Assert.IsType <Response>(actual);
            Assert.NotNull(actual);
            Assert.Equal(expected, actual, new UserResponseComparer());
        }
        public void Should_Return_NotFound_When_Authenticate_With_Incorrect_Email()
        {
            // Arrange
            var fakes              = new Fakes();
            var fakeConfig         = fakes.FakeConfiguration().Object;
            var fakeUserRepository = fakes.FakeUserRepository().Object;
            var hashService        = new HashService();
            var tokenService       = new TokenService(fakeConfig);
            var authService        = new AuthService(fakes.Mapper, fakeUserRepository, tokenService, hashService);

            var request = fakes.Get <LoginUserRequest>().First();

            request.Email = "*****@*****.**";

            // Act
            var controller = new AuthController(fakes.Mapper, authService);
            var actual     = controller.Authenticate(request);

            // Assert
            Assert.NotNull(actual);
            Assert.IsType <ObjectResult>(actual.Result);

            var result = actual.Result as ObjectResult;

            Assert.NotNull(result.Value);
            Assert.Equal(404, result.StatusCode); // Not Found
            Assert.IsType <Response>(result.Value);
        }
        public void Should_Return_Forbidden_When_Archive_Without_Authorization()
        {
            // Arrange
            var logId          = 1;
            var userId         = 3; // User with id 3 isn't owner of the log with id 1
            var fakes          = new Fakes();
            var fakeHttp       = fakes.FakeHttpContextAccessor(false, userId).Object;
            var fakeRepository = fakes.FakeLogRepository().Object;
            var fakeUserRepo   = fakes.FakeUserRepository().Object;
            var hashService    = new HashService();
            var userService    = new UserService(fakes.Mapper, fakeHttp, fakeUserRepo, hashService);
            var logService     = new LogService(fakes.Mapper, fakeRepository, userService);

            // Act
            var controller = new LogController(fakes.Mapper, logService);
            var actual     = controller.Archive(logId);

            // Assert
            Assert.NotNull(actual);
            Assert.IsType <ObjectResult>(actual.Result);

            var result = actual.Result as ObjectResult;

            Assert.NotNull(result.Value);
            Assert.Equal(403, result.StatusCode); // Forbidden
            Assert.IsType <Response>(result.Value);
        }
        public void Should_Return_Ok_When_Register_With_Valid_Credentials()
        {
            // Arrange
            var fakes              = new Fakes();
            var fakeConfig         = fakes.FakeConfiguration().Object;
            var fakeUserRepository = fakes.FakeUserRepository().Object;
            var hashService        = new HashService();
            var tokenService       = new TokenService(fakeConfig);
            var authService        = new AuthService(fakes.Mapper, fakeUserRepository, tokenService, hashService);

            var request = fakes.Get <RegisterUserRequest>().First();

            request.Email = "*****@*****.**"; // Email that doesn't exist

            // Act
            var controller = new AuthController(fakes.Mapper, authService);
            var actual     = controller.Register(request);

            // Assert
            Assert.NotNull(actual);
            Assert.IsType <ObjectResult>(actual.Result);

            var result = actual.Result as ObjectResult;

            Assert.NotNull(result.Value);
            Assert.Equal(200, result.StatusCode); // Ok
            Assert.IsType <Response>(result.Value);
        }
示例#5
0
        public void Should_Return_Log_When_Update_With_Owner(int logId, int loggedUserId)
        {
            // Arrange
            var fakes              = new Fakes();
            var fakeHttp           = fakes.FakeHttpContextAccessor(false, loggedUserId).Object;
            var fakeRepository     = fakes.FakeLogRepository().Object;
            var fakeUserRepository = fakes.FakeUserRepository().Object;
            var hashService        = new HashService();
            var userService        = new UserService(fakes.Mapper, fakeHttp, fakeUserRepository, hashService);

            var log = fakeRepository.GetById(logId).Result;

            log.Title = "Updated Title";

            var request  = fakes.Mapper.Map <UpdateLogRequest>(log);
            var response = fakes.Mapper.Map <LogResponse>(log);
            var expected = Responses.OkResponse(null, response);

            // Act
            var service = new LogService(fakes.Mapper, fakeRepository, userService);
            var actual  = service.Update(logId, request).Result;

            // Assert
            Assert.IsType <Response>(actual);
            Assert.NotNull(actual);
            Assert.Equal(expected, actual, new LogResponseComparer());
        }
        public void Should_Return_BadRequest_When_Delete_And_An_Error_Happens()
        {
            // Arrange
            var logId          = 1;
            var fakes          = new Fakes();
            var fakeHttp       = fakes.FakeHttpContextAccessor(true).Object;
            var fakeRepository = fakes.FakeLogRepositoryException().Object;
            var fakeUserRepo   = fakes.FakeUserRepository().Object;
            var hashService    = new HashService();
            var userService    = new UserService(fakes.Mapper, fakeHttp, fakeUserRepo, hashService);
            var logService     = new LogService(fakes.Mapper, fakeRepository, userService);

            // Act
            var controller = new LogController(fakes.Mapper, logService);
            var actual     = controller.Delete(logId);

            // Assert
            Assert.NotNull(actual);
            Assert.IsType <ObjectResult>(actual.Result);

            var result = actual.Result as ObjectResult;

            Assert.NotNull(result.Value);
            Assert.Equal(400, result.StatusCode); // Bad Request
            Assert.IsType <Response>(result.Value);
        }
示例#7
0
        public void Should_Return_Log_When_Create_With_Manager_And_Specified_UserId()
        {
            // Arrange
            var fakes              = new Fakes();
            var fakeHttp           = fakes.FakeHttpContextAccessor(true).Object;
            var fakeRepository     = fakes.FakeLogRepository().Object;
            var fakeUserRepository = fakes.FakeUserRepository().Object;
            var hashService        = new HashService();
            var userService        = new UserService(fakes.Mapper, fakeHttp, fakeUserRepository, hashService);

            var request = fakes.Get <CreateLogRequest>().First();
            var log     = fakes.Get <Log>().First();

            var response = fakes.Mapper.Map <LogResponse>(log);

            response.Id       = 999;                  // Mocked id when creating a new log
            response.Archived = false;                // By default, log is created as unarchived
            response.UserId   = request.UserId.Value; // UserId were specified, so it should be equal to the UserId in request

            // Act
            var service = new LogService(fakes.Mapper, fakeRepository, userService);
            var actual  = service.Create(request).Result;

            response.CreatedAt = (actual.Data as LogResponse).CreatedAt;
            var expected = Responses.OkResponse(null, response);

            // Assert
            Assert.IsType <Response>(actual);
            Assert.NotNull(actual);
            Assert.Equal(expected, actual, new LogResponseComparer());
        }
        public void Should_Return_Ok_When_Archive_With_Manager()
        {
            // Arrange
            var logId          = 1;
            var fakes          = new Fakes();
            var fakeHttp       = fakes.FakeHttpContextAccessor(true).Object;
            var fakeRepository = fakes.FakeLogRepository().Object;
            var fakeUserRepo   = fakes.FakeUserRepository().Object;
            var hashService    = new HashService();
            var userService    = new UserService(fakes.Mapper, fakeHttp, fakeUserRepo, hashService);
            var logService     = new LogService(fakes.Mapper, fakeRepository, userService);

            // Act
            var controller = new LogController(fakes.Mapper, logService);
            var actual     = controller.Archive(logId);

            // Assert
            Assert.NotNull(actual);
            Assert.IsType <ObjectResult>(actual.Result);

            var result = actual.Result as ObjectResult;

            Assert.NotNull(result.Value);
            Assert.Equal(200, result.StatusCode); // Ok
            Assert.IsType <Response>(result.Value);
        }
示例#9
0
        public void Should_Return_All_User_Archived_Logs_When_GetArchivedLogsByUserId_With_Manager(int userId)
        {
            // Arrange
            var loggedUserId   = userId;
            var fakes          = new Fakes();
            var fakeHttp       = fakes.FakeHttpContextAccessor(false, loggedUserId).Object;
            var fakeRepository = fakes.FakeLogRepository().Object;
            var fakeUserRepo   = fakes.FakeUserRepository().Object;
            var hashService    = new HashService();
            var userService    = new UserService(fakes.Mapper, fakeHttp, fakeUserRepo, hashService);

            var logs     = fakeRepository.GetAllArchivedByUserId(userId).Result;
            var response = fakes.Mapper.Map <LogResponse[]>(logs);
            var expected = Responses.OkResponse(null, response);

            // Act
            var service      = new LogService(fakes.Mapper, fakeRepository, userService);
            var actual       = service.GetArchivedLogsByUserId(userId).Result;
            var logResponses = actual.Data as LogResponse[];

            // Assert
            Assert.IsType <Response>(actual);
            Assert.NotNull(actual);
            Assert.Equal(expected, actual, new LogResponseListComparer());
            Array.ForEach(logResponses, log => Assert.True(log.Archived));
        }
示例#10
0
        public void Should_Return_Log_When_Archive_With_Manager(int logId)
        {
            // Arrange
            var fakes              = new Fakes();
            var fakeHttp           = fakes.FakeHttpContextAccessor(true).Object;
            var fakeRepository     = fakes.FakeLogRepository().Object;
            var fakeUserRepository = fakes.FakeUserRepository().Object;
            var hashService        = new HashService();
            var userService        = new UserService(fakes.Mapper, fakeHttp, fakeUserRepository, hashService);

            var log = fakeRepository.GetById(logId).Result;

            log.Archived = !log.Archived; // Should change log archiving state
            var response = fakes.Mapper.Map <LogResponse>(log);
            var expected = Responses.OkResponse(null, response);

            // Act
            var service = new LogService(fakes.Mapper, fakeRepository, userService);
            var actual  = service.Archive(logId).Result;

            // Assert
            Assert.IsType <Response>(actual);
            Assert.NotNull(actual);
            Assert.Equal(expected, actual, new LogResponseComparer());
        }
        public void Should_Return_NotFound_When_Delete_With_An_Incorrect_Id()
        {
            // Arrange
            var logId          = 9999;
            var fakes          = new Fakes();
            var fakeHttp       = fakes.FakeHttpContextAccessor(true).Object;
            var fakeRepository = fakes.FakeLogRepository().Object;
            var fakeUserRepo   = fakes.FakeUserRepository().Object;
            var hashService    = new HashService();
            var userService    = new UserService(fakes.Mapper, fakeHttp, fakeUserRepo, hashService);
            var logService     = new LogService(fakes.Mapper, fakeRepository, userService);

            // Act
            var controller = new LogController(fakes.Mapper, logService);
            var actual     = controller.Delete(logId);

            // Assert
            Assert.NotNull(actual);
            Assert.IsType <ObjectResult>(actual.Result);

            var result = actual.Result as ObjectResult;

            Assert.NotNull(result.Value);
            Assert.Equal(404, result.StatusCode); // Not Found
            Assert.IsType <Response>(result.Value);
        }
示例#12
0
        public void Should_Return_Conflict_When_Create_With_Existing_Email()
        {
            // Arrange
            var fakes          = new Fakes();
            var fakeHttp       = fakes.FakeHttpContextAccessor(true).Object;
            var fakeRepository = fakes.FakeUserRepository().Object;
            var hashService    = new HashService();
            var userService    = new UserService(fakes.Mapper, fakeHttp, fakeRepository, hashService);

            var request = fakes.Get <CreateUserRequest>().First();

            // Act
            var controller = new UserController(fakes.Mapper, userService);
            var actual     = controller.Post(request);

            // Assert
            Assert.NotNull(actual);
            Assert.IsType <ObjectResult>(actual.Result);

            var result = actual.Result as ObjectResult;

            Assert.NotNull(result.Value);
            Assert.Equal(409, result.StatusCode); // Conflict
            Assert.IsType <Response>(result.Value);
        }
示例#13
0
        public void Should_Return_Ok_When_Create_With_Manager()
        {
            // Arrange
            var fakes          = new Fakes();
            var fakeHttp       = fakes.FakeHttpContextAccessor(true).Object;
            var fakeRepository = fakes.FakeUserRepository().Object;
            var hashService    = new HashService();
            var userService    = new UserService(fakes.Mapper, fakeHttp, fakeRepository, hashService);

            var request = fakes.Get <CreateUserRequest>().First();

            request.Email = "*****@*****.**";  // Email that doesn't exist in test data

            // Act
            var controller = new UserController(fakes.Mapper, userService);
            var actual     = controller.Post(request);

            // Assert
            Assert.NotNull(actual);
            Assert.IsType <ObjectResult>(actual.Result);

            var result = actual.Result as ObjectResult;

            Assert.NotNull(result.Value);
            Assert.Equal(200, result.StatusCode); // Ok
            Assert.IsType <Response>(result.Value);
        }
        public void Should_Return_Ok_When_Post_With_Manager_And_Specified_UserId()
        {
            // Arrange
            var userId         = 1;
            var fakes          = new Fakes();
            var fakeHttp       = fakes.FakeHttpContextAccessor(true).Object;
            var fakeRepository = fakes.FakeLogRepository().Object;
            var fakeUserRepo   = fakes.FakeUserRepository().Object;
            var hashService    = new HashService();
            var userService    = new UserService(fakes.Mapper, fakeHttp, fakeUserRepo, hashService);
            var logService     = new LogService(fakes.Mapper, fakeRepository, userService);

            var request = fakes.Get <CreateLogRequest>().First();

            request.UserId = userId;

            // Act
            var controller = new LogController(fakes.Mapper, logService);
            var actual     = controller.Post(request);

            // Assert
            Assert.NotNull(actual);
            Assert.IsType <ObjectResult>(actual.Result);

            var result = actual.Result as ObjectResult;

            Assert.NotNull(result.Value);
            Assert.Equal(200, result.StatusCode); // Ok
            Assert.IsType <Response>(result.Value);
        }
示例#15
0
        public void Should_Return_Forbidden_When_Delete_Without_Authorization()
        {
            // Arrange
            var userId         = 1;
            var loggedUserId   = userId - 1;
            var fakes          = new Fakes();
            var fakeHttp       = fakes.FakeHttpContextAccessor(false, loggedUserId).Object;
            var fakeRepository = fakes.FakeUserRepository().Object;
            var hashService    = new HashService();
            var userService    = new UserService(fakes.Mapper, fakeHttp, fakeRepository, hashService);

            // Act
            var controller = new UserController(fakes.Mapper, userService);
            var actual     = controller.Delete(userId);

            // Assert
            Assert.NotNull(actual);
            Assert.IsType <ObjectResult>(actual.Result);

            var result = actual.Result as ObjectResult;

            Assert.NotNull(result.Value);
            Assert.Equal(403, result.StatusCode); // Forbidden
            Assert.IsType <Response>(result.Value);
        }
        public void Should_Return_User_And_Token_When_Authenticate_With_Correct_Credentials()
        {
            // Arrange
            var fakes              = new Fakes();
            var fakeConfig         = fakes.FakeConfiguration().Object;
            var fakeUserRepository = fakes.FakeUserRepository().Object;
            var tokenService       = new TokenService(fakeConfig);
            var hashService        = new HashService();

            var request = fakes.Get <LoginUserRequest>().First();
            var user    = fakes.Get <User>().First();

            var response = new LoginUserResponse();

            response.User      = fakes.Mapper.Map <UserResponse>(user);
            response.ExpiresIn = tokenService.GetExpirationInSeconds();

            // Act
            var service = new AuthService(fakes.Mapper, fakeUserRepository, tokenService, hashService);
            var actual  = service.Authenticate(request).Result;
            var data    = actual.Data as LoginUserResponse;

            response.Token = data.Token;  // Should use the generated token for comparison
            var expected = Responses.OkResponse(null, response);

            // Assert
            Assert.IsType <Response>(actual);
            Assert.NotNull(actual);
            Assert.Equal(expected, actual, new LoginUserResponseComparer());
            Assert.True(tokenService.IsValid(data.Token));
        }
示例#17
0
        public void Should_Return_User_When_Update_With_Manager()
        {
            // Arrange
            var fakes          = new Fakes();
            var fakeHttp       = fakes.FakeHttpContextAccessor(true).Object;
            var fakeRepository = fakes.FakeUserRepository().Object;
            var hashService    = new HashService();

            var request = fakes.Get <UpdateUserRequest>().First();

            request.Name = "Updated Title";

            var user = fakes.Get <User>().First();

            user.Name = request.Name;
            var response = fakes.Mapper.Map <UserResponse>(user);
            var expected = Responses.OkResponse(null, response);

            // Act
            var service = new UserService(fakes.Mapper, fakeHttp, fakeRepository, hashService);
            var actual  = service.Update(user.Id, request).Result;

            // Assert
            Assert.IsType <Response>(actual);
            Assert.NotNull(actual);
            Assert.Equal(expected, actual, new UserResponseComparer());
        }
        public void Should_Return_Conflict_When_Register_With_Email_Already_In_Use()
        {
            // Arrange
            var fakes              = new Fakes();
            var fakeConfig         = fakes.FakeConfiguration().Object;
            var fakeUserRepository = fakes.FakeUserRepository().Object;
            var hashService        = new HashService();
            var tokenService       = new TokenService(fakeConfig);
            var authService        = new AuthService(fakes.Mapper, fakeUserRepository, tokenService, hashService);

            var request = fakes.Get <RegisterUserRequest>().First();

            // Act
            var controller = new AuthController(fakes.Mapper, authService);
            var actual     = controller.Register(request);

            // Assert
            Assert.NotNull(actual);
            Assert.IsType <ObjectResult>(actual.Result);

            var result = actual.Result as ObjectResult;

            Assert.NotNull(result.Value);
            Assert.Equal(409, result.StatusCode); // Conflict
            Assert.IsType <Response>(result.Value);
        }
示例#19
0
        public void Should_Return_User_When_Create_With_Manager()
        {
            // Arrange
            var fakes       = new Fakes();
            var fakeHttp    = fakes.FakeHttpContextAccessor(true).Object;
            var hashService = new HashService();

            var mockRepository = fakes.FakeUserRepository();

            // Returns null when GetByEmail is called, so the service doesn't return Conflict.
            mockRepository.Setup(x => x.GetByEmail(It.IsAny <string>()))
            .Returns(Task.FromResult <User>(null));
            var fakeRepository = mockRepository.Object;

            var request = fakes.Get <CreateUserRequest>().First();
            var user    = fakes.Get <User>().First();

            var response = fakes.Mapper.Map <UserResponse>(user);

            response.Id   = 999;                 // Mocked id when creating a new user
            response.Role = EUserRole.DEVELOPER; // By default, user is created as DEVELOPER

            // Act
            var service = new UserService(fakes.Mapper, fakeHttp, fakeRepository, hashService);
            var actual  = service.Create(request).Result;

            response.CreatedAt = (actual.Data as UserResponse).CreatedAt;
            var expected = Responses.OkResponse(null, response);

            // Assert
            Assert.IsType <Response>(actual);
            Assert.NotNull(actual);
            Assert.Equal(expected, actual, new UserResponseComparer());
        }
示例#20
0
        public void Should_Return_Ok_When_Update_With_Manager()
        {
            // Arrange
            var userId         = 1;
            var fakes          = new Fakes();
            var fakeHttp       = fakes.FakeHttpContextAccessor(true).Object;
            var fakeRepository = fakes.FakeUserRepository().Object;
            var hashService    = new HashService();
            var userService    = new UserService(fakes.Mapper, fakeHttp, fakeRepository, hashService);

            var user    = fakeRepository.GetById(userId).Result;
            var request = fakes.Mapper.Map <UpdateUserRequest>(user);

            // Act
            var controller = new UserController(fakes.Mapper, userService);
            var actual     = controller.Put(user.Id, request);

            // Assert
            Assert.NotNull(actual);
            Assert.IsType <ObjectResult>(actual.Result);

            var result = actual.Result as ObjectResult;

            Assert.NotNull(result.Value);
            Assert.Equal(200, result.StatusCode); // Ok
            Assert.IsType <Response>(result.Value);
        }
示例#21
0
        public void Should_Return_Forbidden_When_Get_Without_Manager()
        {
            // Arrange
            var fakes          = new Fakes();
            var fakeHttp       = fakes.FakeHttpContextAccessor().Object;
            var fakeRepository = fakes.FakeUserRepository().Object;
            var hashService    = new HashService();

            var expected = Responses.ForbiddenResponse();

            // Act
            var service = new UserService(fakes.Mapper, fakeHttp, fakeRepository, hashService);
            var actual  = service.Get().Result;

            // Assert
            Assert.IsType <Response>(actual);
            Assert.NotNull(actual);
            Assert.Equal(expected, actual, new ResponseComparer());
        }
示例#22
0
        public void Should_Return_Ok_When_Delete_With_Manager(int userId)
        {
            // Arrange
            var fakes          = new Fakes();
            var fakeHttp       = fakes.FakeHttpContextAccessor(true).Object;
            var fakeRepository = fakes.FakeUserRepository().Object;
            var hashService    = new HashService();

            var expected = Responses.OkResponse("Usuário deletado");

            // Act
            var service = new UserService(fakes.Mapper, fakeHttp, fakeRepository, hashService);
            var actual  = service.Delete(userId).Result;

            // Assert
            Assert.IsType <Response>(actual);
            Assert.NotNull(actual);
            Assert.Equal(expected, actual, new ResponseComparer());
        }
示例#23
0
        public void Should_Return_NotFound_When_GetUserById_With_An_Incorrect_Id()
        {
            // Arrange
            var userId         = 0;
            var fakes          = new Fakes();
            var fakeHttp       = fakes.FakeHttpContextAccessor(true).Object;
            var fakeRepository = fakes.FakeUserRepository().Object;
            var hashService    = new HashService();

            var expected = Responses.NotFoundResponse("Usuário não encontrado");

            // Act
            var service = new UserService(fakes.Mapper, fakeHttp, fakeRepository, hashService);
            var actual  = service.GetUserById(userId).Result;

            // Assert
            Assert.IsType <Response>(actual);
            Assert.NotNull(actual);
            Assert.Equal(expected, actual, new ResponseComparer());
        }
示例#24
0
        public void Should_Return_Conflict_When_Create_With_Existing_Email()
        {
            // Arrange
            var fakes          = new Fakes();
            var fakeHttp       = fakes.FakeHttpContextAccessor(true).Object;
            var fakeRepository = fakes.FakeUserRepository().Object;
            var hashService    = new HashService();

            var request  = fakes.Get <CreateUserRequest>().First();
            var expected = Responses.ConflictResponse("Esse email já está sendo utilizado");

            // Act
            var service = new UserService(fakes.Mapper, fakeHttp, fakeRepository, hashService);
            var actual  = service.Create(request).Result;

            // Assert
            Assert.IsType <Response>(actual);
            Assert.NotNull(actual);
            Assert.Equal(expected, actual, new ResponseComparer());
        }
        public void Should_Return_Conflict_When_Register_With_Email_Already_In_Use()
        {
            // Arrange
            var fakes              = new Fakes();
            var fakeConfig         = fakes.FakeConfiguration().Object;
            var fakeUserRepository = fakes.FakeUserRepository().Object;
            var tokenService       = new TokenService(fakeConfig);
            var hashService        = new HashService();

            var request  = fakes.Get <RegisterUserRequest>().First();
            var expected = Responses.ConflictResponse("Esse email já está sendo utilizado");

            // Act
            var service = new AuthService(fakes.Mapper, fakeUserRepository, tokenService, hashService);
            var actual  = service.Register(request).Result;

            // Assert
            Assert.IsType <Response>(actual);
            Assert.NotNull(actual);
            Assert.Equal(expected, actual, new ResponseComparer());
        }
示例#26
0
        public void Should_Return_All_Users_When_Get_With_Manager()
        {
            // Arrange
            var fakes          = new Fakes();
            var fakeHttp       = fakes.FakeHttpContextAccessor(true).Object;
            var fakeRepository = fakes.FakeUserRepository().Object;
            var hashService    = new HashService();

            var users    = fakeRepository.GetAll().Result;
            var response = fakes.Mapper.Map <UserResponse[]>(users);
            var expected = Responses.OkResponse(null, response);

            // Act
            var service = new UserService(fakes.Mapper, fakeHttp, fakeRepository, hashService);
            var actual  = service.Get().Result;

            // Assert
            Assert.IsType <Response>(actual);
            Assert.NotNull(actual);
            Assert.Equal(expected, actual, new UserResponseListComparer());
        }
示例#27
0
        public void Should_Return_Forbidden_When_Delete_Without_Authorization(int userId)
        {
            // Arrange
            var fakes          = new Fakes();
            var loggedUserId   = userId - 1;
            var fakeHttp       = fakes.FakeHttpContextAccessor(false, loggedUserId).Object;
            var fakeRepository = fakes.FakeUserRepository().Object;
            var hashService    = new HashService();

            var expected = Responses.ForbiddenResponse(
                "Não é possível deletar informações de outro usuário");

            // Act
            var service = new UserService(fakes.Mapper, fakeHttp, fakeRepository, hashService);
            var actual  = service.Delete(userId).Result;

            // Assert
            Assert.IsType <Response>(actual);
            Assert.NotNull(actual);
            Assert.Equal(expected, actual, new ResponseComparer());
        }
示例#28
0
        public void Should_Return_BadRequest_When_GetAll_And_Exception_Is_Thrown()
        {
            // Arrange
            var fakes          = new Fakes();
            var fakeHttp       = fakes.FakeHttpContextAccessor().Object;
            var fakeRepository = fakes.FakeLogRepositoryException().Object;
            var fakeUserRepo   = fakes.FakeUserRepository().Object;
            var hashService    = new HashService();
            var userService    = new UserService(fakes.Mapper, fakeHttp, fakeUserRepo, hashService);

            var expected = Responses.BadRequestResponse("Test Exception");

            // Act
            var service = new LogService(fakes.Mapper, fakeRepository, userService);
            var actual  = service.GetAll().Result;

            // Assert
            Assert.ThrowsAnyAsync <Exception>(() => service.GetAll());
            Assert.IsType <Response>(actual);
            Assert.NotNull(actual);
            Assert.Equal(expected, actual, new ResponseComparer());
        }
示例#29
0
        public void Should_Return_User_When_GetUserById_With_Owner(int userId)
        {
            // Arrange
            var fakes          = new Fakes();
            var loggedUserId   = userId;
            var fakeHttp       = fakes.FakeHttpContextAccessor(false, loggedUserId).Object;
            var fakeRepository = fakes.FakeUserRepository().Object;
            var hashService    = new HashService();

            var user     = fakeRepository.GetById(userId).Result;
            var response = fakes.Mapper.Map <UserResponse>(user);
            var expected = Responses.OkResponse(null, response);

            // Act
            var service = new UserService(fakes.Mapper, fakeHttp, fakeRepository, hashService);
            var actual  = service.GetUserById(userId).Result;

            // Assert
            Assert.IsType <Response>(actual);
            Assert.NotNull(actual);
            Assert.Equal(expected, actual, new UserResponseComparer());
        }
示例#30
0
        public void Should_Return_NotFound_When_Update_With_An_Incorrect_Id()
        {
            // Arrange
            var logId              = 9999;
            var fakes              = new Fakes();
            var fakeHttp           = fakes.FakeHttpContextAccessor(true).Object;
            var fakeRepository     = fakes.FakeLogRepository().Object;
            var fakeUserRepository = fakes.FakeUserRepository().Object;
            var hashService        = new HashService();
            var userService        = new UserService(fakes.Mapper, fakeHttp, fakeUserRepository, hashService);

            var request  = new UpdateLogRequest();
            var expected = Responses.NotFoundResponse("Log não encontrado");

            // Act
            var service = new LogService(fakes.Mapper, fakeRepository, userService);
            var actual  = service.Update(logId, request).Result;

            // Assert
            Assert.IsType <Response>(actual);
            Assert.NotNull(actual);
            Assert.Equal(expected, actual, new ResponseComparer());
        }