Exemplo n.º 1
0
        private UserManageController SetupController(int test)
        {
            switch (test)
            {
            case 1:
                // user list and is admin true
                userServiceMock = SetupMockService(test);
                break;

            case 2:
                // user == null
                userServiceMock = SetupMockService(test);
                break;

            case 3:
                // user != null
                userServiceMock = SetupMockService(test);
                break;
            }

            controller = new UserManageController(userServiceMock.Object, cache)
            {
                ControllerContext = new ControllerContext()
                {
                    HttpContext = new DefaultHttpContext()
                    {
                        User = new ClaimsPrincipal()
                    }
                },
                TempData = new Mock <ITempDataDictionary>().Object
            };

            return(controller);
        }
Exemplo n.º 2
0
        public async Task Good_Delete()
        {
            // Arrange
            var id   = Guid.NewGuid().ToString();
            var user = new ApplicationUser()
            {
                Id           = id,
                UserName     = "******",
                Email        = "*****@*****.**",
                PasswordHash = ""
            };
            var identityResult = IdentityResult.Success;
            var service        = Substitute.For <IUserService>();
            var controller     = new UserManageController(service);

            service.FindUserAsync(id).Returns(Task.FromResult(user));
            service.DeleteUserAsync(user).Returns(Task.FromResult(identityResult));

            // Act
            var result = await controller.Delete(id) as RedirectToRouteResult;

            var areaName       = result.RouteValues.Values.ElementAt(0);
            var actionName     = result.RouteValues.Values.ElementAt(1);
            var controllerName = result.RouteValues.Values.ElementAt(2);

            // Assert
            Assert.That(result, !Is.Null);
            Assert.That("Index", Is.EqualTo(actionName));
            Assert.That("UserManage", Is.EqualTo(controllerName));
            Assert.That("admin", Is.EqualTo(areaName));
        }
Exemplo n.º 3
0
        public async Task Delete_Not_Success()
        {
            // Arrange
            var id   = Guid.NewGuid().ToString();
            var user = new ApplicationUser()
            {
                Id           = id,
                UserName     = "******",
                Email        = "*****@*****.**",
                PasswordHash = ""
            };
            var identityResult = IdentityResult.Failed("User not exist.");
            var service        = Substitute.For <IUserService>();
            var controller     = new UserManageController(service);

            service.FindUserAsync(id).Returns(Task.FromResult(user));
            service.DeleteUserAsync(user).Returns(Task.FromResult(identityResult));

            // Act
            var result = await controller.Delete(id) as HttpStatusCodeResult;

            var statusCode = result.StatusCode;

            service.FindUserAsync(id).Returns(Task.FromResult(user));

            // Assert
            Assert.That(result, !Is.Null);
            Assert.That(400, Is.EqualTo(statusCode));
        }
Exemplo n.º 4
0
        public void Index()
        {
            // Arrange
            var service    = Substitute.For <IUserService>();
            var controller = new UserManageController(service);

            // Act
            var result   = controller.Index() as ViewResult;
            var viewName = result.ViewName;

            // Assert
            Assert.That(result, !Is.Null);
            Assert.That("Index", Is.EqualTo(viewName));
        }
Exemplo n.º 5
0
        public void ListUsers()
        {
            // Arrange
            var users = new List <ApplicationUser>()
            {
                new ApplicationUser()
                {
                    UserName = "******", Email = "*****@*****.**", PasswordHash = ""
                },
                new ApplicationUser()
                {
                    UserName = "******", Email = "*****@*****.**", PasswordHash = ""
                }
            };
            var usersViewModel = new List <UserViewModel>()
            {
                new UserViewModel()
                {
                    UserName = "******", Email = "*****@*****.**", Roles = new List <string>()
                    {
                        "superAdmin"
                    }
                },
                new UserViewModel()
                {
                    UserName = "******", Email = "*****@*****.**", Roles = new List <string>()
                    {
                        "admin"
                    }
                }
            };
            var service    = Substitute.For <IUserService>();
            var controller = new UserManageController(service);

            service.UserList().Returns(users);
            service.UsersToViewModels(users).Returns(usersViewModel);

            // Act
            var result   = controller.ListUsers() as PartialViewResult;
            var viewName = result.ViewName;
            var model    = result.Model;

            // Assert
            Assert.That(result, !Is.Null);
            Assert.That(model, !Is.Null);
            Assert.That("_UserPartial", Is.EqualTo(viewName));
        }
Exemplo n.º 6
0
        public async Task Delete_User_Is_Null()
        {
            // Arrange
            var             id         = "wdhjhfjdhj";
            ApplicationUser user       = null;
            var             service    = Substitute.For <IUserService>();
            var             controller = new UserManageController(service);

            // Act
            var result = await controller.Delete(id) as HttpNotFoundResult;

            var statusCode = result.StatusCode;

            service.FindUserAsync(id).Returns(Task.FromResult(user));

            // Assert
            Assert.That(result, !Is.Null);
            Assert.That(404, Is.EqualTo(statusCode));
        }