public async Task PutUser_ModelInvalid_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);

            controller.ModelState.AddModelError("LastName", "Last name is missing.");

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

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

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

            mockUmService.Verify();
        }
        public async Task PutUser_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 UpdateUserModel
            {
                Id        = "a",
                FirstName = "a",
                LastName  = "b",
                Email     = "[email protected]",
                Role      = "employee"
            };

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

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

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

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

            // Assert
            var actionResult = Assert.IsType <BadRequestObjectResult>(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();
        }