Exemplo n.º 1
0
        public void DeletePost()
        {
            new PostRepository().Add(postOne);

            var retorno = postController.DeletePost(postOne.IdPost);

            Assert.IsType <OkObjectResult>(retorno);
        }
        public async void Task_Delete_Post_Return_OkResult()
        {
            var postId = 2;

            //Act
            var data = await _controller.DeletePost(postId);

            //Assert
            Assert.IsType <OkResult>(data);
        }
Exemplo n.º 3
0
        public void DeletePost_Test()
        {
            // arrange
            mockRepo.Setup(repo => repo.Posts.FindByCondition(u => u.ID == It.IsAny <int>())).Returns(GetPosts());
            mockRepo.Setup(repo => repo.Posts.Delete(GetPost()));
            // act
            var controllerJsonResult = postController.DeletePost(It.IsAny <int>());
            var nullExpected         = postController.DeletePost(0);

            // assert
            Assert.NotNull(controllerJsonResult);
            Assert.IsType <JsonResult>(controllerJsonResult);
        }
Exemplo n.º 4
0
        public void Failed_To_Delete_An_Invalid_Post_Test()
        {
            var mockService = GetService();

            mockService
            .Setup(service => service.GetPostOnly(It.IsAny <Guid>()))
            .Returns(async() =>
            {
                Post response = null;
                return(await Task.FromResult(response));
            });


            var controller = new PostController(mockService.Object);

            controller.ObjectValidator = GetValidator();

            var          controllerContext = GetControllerContext();
            Claim        claim             = new Claim(ClaimTypes.Name, "1");
            List <Claim> claimList         = new List <Claim>()
            {
                claim
            };
            ClaimsIdentity identity = new ClaimsIdentity(claimList);

            controllerContext.HttpContext.User = new ClaimsPrincipal(identity);

            controller.ControllerContext = controllerContext;

            var actionResult = controller.DeletePost(Guid.NewGuid()).Result as NotFoundResult;

            Assert.NotNull(actionResult);
        }
Exemplo n.º 5
0
        public void Cant_Delete_Post_With_Uncorrect_Id()
        {
            // Arrange
            IPostRepository repository = MockPostRepository.GetRepository(MockPostRepository.STDN);
            PostController  target     = new PostController(repository);

            // Act
            ActionResult result = target.DeletePost(MockPostRepository.STDN + 1, "#r");

            // Assert
            Assert.IsNull(result);
        }
Exemplo n.º 6
0
        public async void Task_Delete_Return_BadRequestResult()
        {
            //Arrange
            var controller = new PostController(repository);
            int?postId     = null;

            //Act
            var data = await controller.DeletePost(postId);

            //Assert
            Assert.IsType <BadRequestResult>(data);
        }
Exemplo n.º 7
0
        public async void Task_Delete_Post_Return_NotFoundResult()
        {
            //Arrange
            var controller = new PostController(repository);
            var postId     = 5;

            //Act
            var data = await controller.DeletePost(postId);

            //Assert
            Assert.IsType <NotFoundResult>(data);
        }
Exemplo n.º 8
0
        public async void Task_Delete_Post_Return_OkResult()
        {
            //Arrange
            var controller = new PostController(logger, postRepository);
            var postId     = 2;

            //Act
            var data = await controller.DeletePost(postId);

            //Assert
            Assert.IsType <OkResult>(data);
        }
Exemplo n.º 9
0
        public void Delete_Post_With_Correct_Id()
        {
            // Arrange
            IPostRepository repository = MockPostRepository.GetRepository(MockPostRepository.STDN);
            PostController  target     = new PostController(repository);

            // Act
            ActionResult result = target.DeletePost(MockPostRepository.STDN, "#r");

            // Assert
            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(RedirectResult));
        }
Exemplo n.º 10
0
        public void TestDeletePost()
        {
            var postId = post3.Id;
            var mock   = new Mock <PostDAO>();

            mock.Setup(dao => dao.Delete(It.IsAny <int>()));
            var postController = new PostController(mock.Object);

            var actionResult = postController.DeletePost(postId);

            mock.Verify((dao) => dao.Delete(postId));
            Assert.IsInstanceOf <NoContentResult>(actionResult);
        }
Exemplo n.º 11
0
        public void Success_To_Delete_A_Post_Test()
        {
            var  mockService = GetService();
            Post oldPost     = new Post();

            oldPost.Id           = Guid.NewGuid();
            oldPost.Title        = "old title";
            oldPost.Body         = "old body";
            oldPost.AuthorId     = 1;
            oldPost.CreatedOn    = DateTime.Now;
            oldPost.LastUpdateOn = DateTime.Now;

            mockService
            .Setup(service => service.GetPostOnly(It.IsAny <Guid>()))
            .Returns(async() =>
            {
                return(await Task.FromResult(oldPost));
            });

            mockService
            .Setup(service => service.Delete(It.IsAny <Post>()))
            .Returns(async(Post post) =>
            {
                return(await Task.FromResult(post));
            });

            var controller = new PostController(mockService.Object);

            controller.ObjectValidator = GetValidator();

            var          controllerContext = GetControllerContext();
            Claim        claim             = new Claim(ClaimTypes.Name, "1");
            List <Claim> claimList         = new List <Claim>()
            {
                claim
            };
            ClaimsIdentity identity = new ClaimsIdentity(claimList);

            controllerContext.HttpContext.User = new ClaimsPrincipal(identity);

            controller.ControllerContext = controllerContext;

            var actionResult = controller.DeletePost(oldPost.Id).Result as OkObjectResult;

            Assert.NotNull(actionResult);

            var post = actionResult.Value as Post;

            Assert.Equal(oldPost, post);
        }
Exemplo n.º 12
0
        public void DeletePost_Failed()
        {
            int postId = 2;

            _postsRepoMock.Setup(m => m.DeletePost(postId, _loginUser)).Returns(false);
            PostController controller = new PostController(_postsRepoMock.Object, _loginUser);

            JsonResult result = controller.DeletePost(postId);

            Assert.IsNotNull(result);
            Assert.IsNotNull(result.Data);
            Assert.IsTrue(result.Data is JsonResponseVM);
            Assert.IsTrue((result.Data as JsonResponseVM).Result == "ERROR");
        }
Exemplo n.º 13
0
        public void TestDeletePostWhenIdIsUnknown()
        {
            var postId = post3.Id;
            var mock   = new Mock <PostDAO>();

            mock
            .Setup(dao => dao.Delete(It.IsAny <int>()))
            .Throws <ArgumentException>();

            var postController = new PostController(mock.Object);

            var actionResult = postController.DeletePost(postId);

            mock.Verify((dao) => dao.Delete(postId));
            Assert.IsInstanceOf <NotFoundResult>(actionResult);
        }
        public void TestDeletePostReturnUnauthorized()
        {
            var contextMock = new Mock <HttpContext>();

            var claims = new ClaimsIdentity(new Claim[]
            {
                new Claim(ClaimTypes.Name, "abc"),
                new Claim(ClaimTypes.Role, "member"),
                new Claim("user_id", "5d247a04eff1030d7c520aa1")
            });

            contextMock.Setup(x => x.User).Returns(new ClaimsPrincipal(claims));
            mockPostService.Setup(x => x.GetById(It.IsAny <string>())).Returns(post);
            mockPostService.Setup(x => x.Delete(It.IsAny <string>())).Returns(true);
            var _postController = new PostController(mockPostService.Object);

            _postController.ControllerContext.HttpContext = contextMock.Object;
            IActionResult deletePost = _postController.DeletePost("5d247a04eff1030d7c5209a1");
            var           type       = deletePost.GetType();

            Assert.AreEqual(type.Name, "UnauthorizedResult");
        }
        public void TestDeletePostReturnOkObjectResult()
        {
            var contextMock = new Mock <HttpContext>();

            var claims = new ClaimsIdentity(new Claim[]
            {
                new Claim(ClaimTypes.Name, "abc"),
                new Claim(ClaimTypes.Role, "member"),
                new Claim("user_id", "5d0b2b0b1c9d440000d8e9a1")
            });

            contextMock.Setup(x => x.User).Returns(new ClaimsPrincipal(claims));
            mockPostService.Setup(x => x.GetById(It.IsAny <string>())).Returns(post);
            mockPostService.Setup(x => x.Delete(It.IsAny <string>())).Returns(true);
            var _postController = new PostController(mockPostService.Object);

            _postController.ControllerContext.HttpContext = contextMock.Object;
            IActionResult deletePost     = _postController.DeletePost("5d247a04eff1030d7c5209a1");
            var           okObjectResult = deletePost as OkObjectResult;
            bool          checkDelete    = (bool)okObjectResult.Value;

            Assert.IsTrue(checkDelete);
        }