public async Task PutUser_UserNotExist_ReturnsNotFound()
        {
            // Arrange
            var mockConfiguration = new Mock <Microsoft.Extensions.Configuration.IConfiguration>();
            var mockLogger        = new Mock <ILogger <AccountController> >();
            var mockUmService     = new Mock <IUserManagementService>();
            var mockHelperService = new Mock <IHelperService>();

            var mapperConfig = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new AutomapperProfiles.UserProfile());
            });
            var mapper = mapperConfig.CreateMapper();

            var controller = new UsersController(mockConfiguration.Object, mockLogger.Object,
                                                 mockUmService.Object, mapper, mockHelperService.Object);

            var a = new UpdateUserModel
            {
                Id        = "a",
                FirstName = "a",
                LastName  = "b",
                Email     = "[email protected]",
            };

            mockUmService.Setup(umService => umService.FindUserAsync("a"))
            .ReturnsAsync((ApplicationUser)null)
            .Verifiable();

            // Act
            var result = await controller.PutUser("a", a);

            // Assert
            var actionResult = Assert.IsType <NotFoundResult>(result);

            mockUmService.Verify();
        }
        public async Task PostUser_EmailInUse_ReturnsBadRequest()
        {
            // Arrange
            var mockConfiguration = new Mock <Microsoft.Extensions.Configuration.IConfiguration>();
            var mockLogger        = new Mock <ILogger <AccountController> >();
            var mockUmService     = new Mock <IUserManagementService>();
            var mockHelperService = new Mock <IHelperService>();

            var mapperConfig = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new AutomapperProfiles.UserProfile());
            });
            var mapper = mapperConfig.CreateMapper();

            var controller = new UsersController(mockConfiguration.Object, mockLogger.Object,
                                                 mockUmService.Object, mapper, mockHelperService.Object);

            var a = new UserModel
            {
                FirstName = "a",
                LastName  = "b",
                Email     = "[email protected]",
            };

            mockUmService.Setup(umService => umService.IsEmailInUseAsync("[email protected]"))
            .ReturnsAsync(true)
            .Verifiable();

            // Act
            var result = await controller.PostUser(a);

            // Assert
            var actionResult = Assert.IsType <ActionResult <UserModel> >(result);
            var returnValue  = Assert.IsType <BadRequestObjectResult>(actionResult.Result);

            mockUmService.Verify();
        }
        public async Task ChangeUserPassword_UserExists_ReturnsOk()
        {
            // Arrange
            var mockConfiguration = new Mock <Microsoft.Extensions.Configuration.IConfiguration>();
            var mockLogger        = new Mock <ILogger <AccountController> >();
            var mockUmService     = new Mock <IUserManagementService>();
            var mockMapper        = new Mock <IMapper>();
            var mockHelperService = new Mock <IHelperService>();

            var controller = new UsersController(mockConfiguration.Object, mockLogger.Object,
                                                 mockUmService.Object, mockMapper.Object, mockHelperService.Object);

            var user = new ApplicationUser();

            mockUmService.Setup(umService => umService.FindUserAsync("a"))
            .ReturnsAsync(user)
            .Verifiable();

            mockUmService.Setup(umService => umService.ChangePasswordAsync(user, "password"))
            .ReturnsAsync(IdentityResult.Success)
            .Verifiable();

            var passwordChangeModel = new PasswordChangeModel()
            {
                Password        = "******",
                ConfirmPassword = "******"
            };

            // Act
            var result = await controller.ChangeUserPassword("a", passwordChangeModel);

            // Assert
            var actionResult = Assert.IsType <OkResult>(result);

            mockUmService.Verify();
        }
        public async Task DeleteUser_UserExists_ReturnsNoContent()
        {
            // Arrange
            var mockConfiguration = new Mock <Microsoft.Extensions.Configuration.IConfiguration>();
            var mockLogger        = new Mock <ILogger <AccountController> >();
            var mockUmService     = new Mock <IUserManagementService>();
            var mockMapper        = new Mock <IMapper>();
            var mockHelperService = new Mock <IHelperService>();

            var controller = new UsersController(mockConfiguration.Object, mockLogger.Object,
                                                 mockUmService.Object, mockMapper.Object, mockHelperService.Object);

            mockUmService.Setup(umService => umService.DeleteUserAsync("a"))
            .ReturnsAsync(IdentityResult.Success)
            .Verifiable();

            // Act
            var result = await controller.DeleteUser("a");

            // Assert
            var actionResult = Assert.IsType <NoContentResult>(result);

            mockUmService.Verify();
        }
        public async Task PutUser_UserHasChangedInDatabase_ReturnsBadRequest()
        {
            // Arrange
            var mockConfiguration = new Mock <Microsoft.Extensions.Configuration.IConfiguration>();
            var mockLogger        = new Mock <ILogger <AccountController> >();
            var mockUmService     = new Mock <IUserManagementService>();
            var mockHelperService = new Mock <IHelperService>();

            var mockMapper = new Mock <IMapper>();

            var controller = new UsersController(mockConfiguration.Object, mockLogger.Object,
                                                 mockUmService.Object, mockMapper.Object, mockHelperService.Object);

            var aOld = new UpdateUserModel
            {
                Id        = "a",
                FirstName = "a",
                LastName  = "b",
                Email     = "[email protected]",
                Role      = "employee"
            };

            var aNew = new UpdateUserModel
            {
                Id        = "a",
                FirstName = "b",
                LastName  = "c",
                Email     = "[email protected]",
                Role      = "supervisor"
            };

            ApplicationUser oldUser = new ApplicationUser
            {
                Id        = "a",
                FirstName = "a",
                LastName  = "b",
                Email     = "[email protected]"
            };

            ApplicationUser newUser = new ApplicationUser
            {
                Id        = "a",
                FirstName = "b",
                LastName  = "c",
                Email     = "[email protected]"
            };

            mockUmService.Setup(umService => umService.FindUserAsync("a"))
            .ReturnsAsync(oldUser)
            .Verifiable();

            mockUmService.Setup(umService => umService.IsEmailInUseAsync("[email protected]", "a"))
            .ReturnsAsync(false)
            .Verifiable();

            mockMapper.Setup(mapper => mapper.Map(aNew, oldUser))
            .Callback <UpdateUserModel, ApplicationUser>((userModel, user) =>
            {
                user.FirstName = userModel.FirstName;
                user.LastName  = userModel.LastName;
                user.Email     = userModel.Email;
            })
            .Returns(newUser)
            .Verifiable();

            DbUpdateConcurrencyException dbUpdateConcurrencyException =
                new DbUpdateConcurrencyException("Exception",
                                                 new List <IUpdateEntry>()
            {
                new Mock <IUpdateEntry>().Object
            });

            mockUmService.Setup(umService => umService.UpdateUserAsync(oldUser, "supervisor", It.IsAny <byte[]>()))
            .ThrowsAsync(dbUpdateConcurrencyException)
            .Verifiable();

            mockHelperService.Setup(helperService => helperService.RetrieveEntity(dbUpdateConcurrencyException))
            .ReturnsAsync((PropertyValues)null);

            // Act
            var result = await controller.PutUser("a", aNew);

            // Assert
            var actionResult = Assert.IsType <BadRequestObjectResult>(result);

            mockUmService.Verify();
        }
        public async Task PutUser_UpdatedUserCorrect_ReturnsNoContent()
        {
            // Arrange
            var mockConfiguration = new Mock <Microsoft.Extensions.Configuration.IConfiguration>();
            var mockLogger        = new Mock <ILogger <AccountController> >();
            var mockUmService     = new Mock <IUserManagementService>();
            var mockHelperService = new Mock <IHelperService>();

            //var mapperConfig = new MapperConfiguration(cfg =>
            //{
            //    cfg.AddProfile(new AutomapperProfiles.UserProfile());
            //});
            //var mapper = mapperConfig.CreateMapper();

            var mockMapper = new Mock <IMapper>();

            var controller = new UsersController(mockConfiguration.Object, mockLogger.Object,
                                                 mockUmService.Object, mockMapper.Object, mockHelperService.Object);

            var aOld = new UpdateUserModel
            {
                Id        = "a",
                FirstName = "a",
                LastName  = "b",
                Email     = "[email protected]",
                Role      = "employee"
            };

            var aNew = new UpdateUserModel
            {
                Id        = "a",
                FirstName = "b",
                LastName  = "c",
                Email     = "[email protected]",
                Role      = "supervisor"
            };

            ApplicationUser oldUser = new ApplicationUser
            {
                Id        = "a",
                FirstName = "a",
                LastName  = "b",
                Email     = "[email protected]"
            };

            ApplicationUser newUser = new ApplicationUser
            {
                Id        = "a",
                FirstName = "b",
                LastName  = "c",
                Email     = "[email protected]"
            };

            mockUmService.Setup(umService => umService.FindUserAsync("a"))
            .ReturnsAsync(oldUser)
            .Verifiable();

            mockUmService.Setup(umService => umService.IsEmailInUseAsync("[email protected]", "a"))
            .ReturnsAsync(false)
            .Verifiable();

            mockMapper.Setup(mapper => mapper.Map(aNew, oldUser))
            .Callback <UpdateUserModel, ApplicationUser>((userModel, user) =>
            {
                user.FirstName = userModel.FirstName;
                user.LastName  = userModel.LastName;
                user.Email     = userModel.Email;
            })
            .Returns(newUser)
            .Verifiable();

            mockUmService.Setup(umService => umService.UpdateUserAsync(oldUser, "supervisor", It.IsAny <byte[]>()))
            .ReturnsAsync(new IdentityResult())
            .Verifiable();

            // Act
            var result = await controller.PutUser("a", aNew);

            // Assert
            var actionResult = Assert.IsType <NoContentResult>(result);

            mockUmService.Verify();
        }
        public async Task GetUsers_TwoUsers_ReturnsListOfUsers()
        {
            // Arrange
            var mockConfiguration = new Mock <Microsoft.Extensions.Configuration.IConfiguration>();
            var mockLogger        = new Mock <ILogger <AccountController> >();
            var mockUmService     = new Mock <IUserManagementService>();
            var mockHelperService = new Mock <IHelperService>();

            var mapperConfig = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new AutomapperProfiles.UserProfile());
            });
            var mapper = mapperConfig.CreateMapper();

            var a = new ApplicationUser
            {
                Id        = "a",
                FirstName = "a",
                LastName  = "b",
                Email     = "[email protected]",
                UserName  = "******"
            };

            var b = new ApplicationUser
            {
                Id        = "b",
                FirstName = "b",
                LastName  = "c",
                Email     = "[email protected]",
                UserName  = "******"
            };

            mockUmService.Setup(umService => umService.GetAllUsersCountAsync("a"))
            .ReturnsAsync(2)
            .Verifiable();

            mockUmService.Setup(umService => umService.GetUsersAsync(0, 2, "Fname", "a"))
            .ReturnsAsync(new List <ApplicationUser>()
            {
                a, b
            })
            .Verifiable();

            mockUmService.Setup(umService => umService.GetUserRoleAsync(It.IsAny <string>(), It.IsAny <bool>()))
            .ReturnsAsync("Employee")
            .Verifiable();

            var controller = new UsersController(mockConfiguration.Object, mockLogger.Object,
                                                 mockUmService.Object, mapper, mockHelperService.Object);

            // Act
            var result = await controller.GetUsers(0, 2, "Fname", "a");

            // Assert
            var actionResult = Assert.IsType <ActionResult <PageUsersModel> >(result);
            var returnValue  = Assert.IsType <PageUsersModel>(actionResult.Value);

            mockUmService.Verify();

            Assert.Equal(2, returnValue.TotalUsers);
            Assert.Collection(returnValue.Users,
                              user => Assert.Equal("b", user.LastName),
                              user => Assert.Equal("c", user.LastName)
                              );
        }