public void Delete_ByUser()
        {
            var controller = new PostController(database);

            controller.Request       = new HttpRequestMessage();
            controller.Configuration = new HttpConfiguration();

            LiteDatabase db  = database;
            var          col = db.GetCollection <User>();

            col.Delete(123);
            col.Insert(new User {
                username = "******", password = "******", id = 123, status = "user"
            });
            IBlogService bs = new BlogService();

            bs.AddPost(new BlogService.PostWCF {
                User = "******", Date = "01.01.2001", Content = "some content 100g chicken", Title = ""
            });
            var response = controller.Delete(JObject.Parse("{ \"id\":\"123\",\"delete\":\"125\"}"));

            Assert.AreNotEqual(null, response);
            var tmp = response as BadRequestErrorMessageResult;

            Assert.AreEqual("You don't have sufficient rights to delete posts!", tmp.Message);
        }
Beispiel #2
0
        public void GivenAValidPost_WhenITryAndDeleteThePost_ThenItIsDeleted()
        {
            MockHttpContext.SetupProperty(h => h.User);
            var controller = new PostController(_postServiceMock.Object, _dashboardServiceMock, _blogService.Object, null);

            controller.Delete(_userName, 2);
            _postServiceMock.Verify(p => p.Delete(2), Times.Once());
        }
Beispiel #3
0
        public void GivenAValidPost_WhenITryAndDeleteThePost_ThenIGetASuccessfulResult()
        {
            MockHttpContext.SetupProperty(h => h.User);
            var controller = new PostController(_postServiceMock.Object, _dashboardServiceMock, _blogService.Object, null);

            var result = (HttpStatusCodeResult) controller.Delete(_userName, 2);
            Assert.That(result.StatusCode, Is.EqualTo(200));
        }
        public void Remove_ExistingGuidPassed_RemovesOneItem()
        {
            // Act
            var okResponse = _controller.Delete(1);

            // Assert
            Assert.Equal(2, _service.GetAll(null).Count());
        }
Beispiel #5
0
        public void Given_valid_post_id_When_Delete_Returns_DeleteView()
        {
            //Given
            var id = 1;

            var model = new SinglePostViewModel();

            _postBuilder.Setup(x => x.BuildPostViewModel(It.IsAny <int>())).Returns(model);

            //When
            var result = _postController.Delete(id) as ViewResult;

            //Then

            Assert.NotNull(result);
            Assert.IsInstanceOf <SinglePostViewModel>(result.Model);
            Assert.AreEqual(String.Empty, result.ViewName);
        }
Beispiel #6
0
        public void GivenAnInvalidPost_WhenITryAndDeleteThePost_ThenIGetAnError()
        {
            MockHttpContext.SetupProperty(h => h.User);
            _postServiceMock.Setup(p => p.Delete(It.IsAny<int>())).Throws<Exception>();
            var controller = new PostController(_postServiceMock.Object, _dashboardServiceMock, _blogService.Object, null);

            var result = (HttpStatusCodeResult)controller.Delete(_userName, 2);
            Assert.That(result.StatusCode, Is.EqualTo(500));
        }
        public void Delete()
        {
            var  id       = 4;
            Post testPost = _posts.Find(x => x.Id == id);
            var  result   = _postController.Delete(4);

            _postRepositoryMock.Verify(m => m.Delete(testPost));
            Assert.IsNotInstanceOfType(result, typeof(ViewResult));
        }
        public async Task Delete_Get_ModelIsValid_AsksForDeleteView()
        {
            Mock <PostService> mock       = new Mock <PostService>();
            PostController     controller = GetNewPostController(mock.Object, null, null);

            ViewResult result = (await controller.Delete(1)) as ViewResult;

            Assert.AreEqual("Delete", result.ViewName);
        }
        public void deletePost()
        {
            var result = (NoContentResult)controller.Delete(2);

            Assert.Equal(result.StatusCode, controller.NoContent().StatusCode);
            // rather than test this way wrap the db stuff in a class and test that the class method is called
            mockSet.Verify(m => m.Find(It.IsAny <long>()), Times.Once);
            mockSet.Verify(m => m.Remove(It.IsAny <Post>()), Times.Once);
            mockContext.Verify(m => m.SaveChanges(), Times.Once);
        }
        public async Task Delete_Get_ModelIsInvalid_AsksForErrorView()
        {
            Mock <PostService> mock = new Mock <PostService>();

            mock.Setup(m => m.FindByIdAsync(It.IsAny <int?>())).Throws(new ValidationException("FindByIdAsync method throws Exception", ""));
            PostController controller = GetNewPostController(mock.Object, null, null);

            ViewResult result = (await controller.Delete(1)) as ViewResult;

            Assert.AreEqual("Error", result.ViewName);
        }
Beispiel #11
0
        public async void DeletePostTest()
        {
            PostController postController = new PostController(new PostRepository(_dataContext));

            var postToDelete = (Post)((OkObjectResult)await postController.Get("test title updated")).Value;
            await postController.Delete(postToDelete.Id);

            var postDeleted = (Post)((OkObjectResult)await postController.Get("test title updated")).Value;

            Assert.Null(postDeleted);
        }
        public void DeleteForbidden()
        {
            var postId         = 2;
            var postController = new PostController(GetMockService());

            postController.ControllerContext = GetMockContext();
            var result = postController.Delete(postId).Result;

            Assert.IsInstanceOfType(result, typeof(ObjectResult));
            Assert.AreEqual(((ObjectResult)result).StatusCode, 403);
        }
        public void DeleteNotFound()
        {
            var postId         = 15;
            var postController = new PostController(GetMockService());

            postController.ControllerContext = GetMockContext();

            var result = postController.Delete(postId).Result;

            Assert.IsInstanceOfType(result, typeof(NotFoundResult));
        }
        public void DeletePostUnauthorized()
        {
            //Arrange
            PostController postController = new PostController();

            // Act
            var actionResult = postController.Delete("", 1).Result;

            // Assert
            Assert.IsType <UnauthorizedResult>(actionResult);
        }
        public async Task Delete_Get_ModelIsInvalid_RetrievesExceptionMessageFromModel()
        {
            Mock <PostService> mock = new Mock <PostService>();

            mock.Setup(m => m.FindByIdAsync(It.IsAny <int?>())).Throws(new ValidationException("FindByIdAsync method throws Exception", ""));
            PostController controller = GetNewPostController(mock.Object, null, null);

            ViewResult result = (await controller.Delete(1)) as ViewResult;

            string[] model = result.ViewData.Model as string[];
            Assert.AreEqual("FindByIdAsync method throws Exception", model[0]);
        }
        public static string deletePost(int id)
        {
            var    post   = PostController.GetByID(id);
            string result = "";

            if (post != null)
            {
                // Delete thumbnail image
                if (!string.IsNullOrEmpty(post.Image))
                {
                    string fileImage = HttpContext.Current.Server.MapPath(post.Image);
                    File.Delete(fileImage);
                }

                // Delete image gallery

                var postImage = PostImageController.GetByPostID(post.ID);

                if (postImage.Count > 0)
                {
                    foreach (var img in postImage)
                    {
                        if (!string.IsNullOrEmpty(img.Image))
                        {
                            string fileImage = HttpContext.Current.Server.MapPath(img.Image);
                            File.Delete(fileImage);

                            // Delete in database
                            string deletePostImage = PostImageController.Delete(img.ID);
                        }
                    }
                }


                string deletePost = PostController.Delete(id);

                if (!string.IsNullOrEmpty(deletePost))
                {
                    result = "success";
                }
                else
                {
                    result = "failed";
                }
            }
            else
            {
                result = "notfound";
            }

            return(result);
        }
        public void DeleteOk()
        {
            var postMock       = mockData.First();
            var postController = new PostController(GetMockService());
            var userInfo       = new UserInfo {
                Mail = "*****@*****.**", Password = "******"
            };

            postController.ControllerContext = GetMockContext(userInfo);
            var result = postController.Delete(postMock.Id).Result;

            Assert.IsInstanceOfType(result, typeof(OkResult));
        }
        public void RemovePost()
        {
            var postId          = 1;
            var postManagerMock = new Mock <IPostManager>();

            postManagerMock.Setup(pm => pm.Remove(It.IsAny <int>()));

            var controller = new PostController(postManagerMock.Object);

            controller.Delete(postId);

            postManagerMock.Verify(p => p.Remove(postId), Times.Once);
        }
Beispiel #19
0
        public void Delete_Doesnt_Found_Post()
        {
            // Arrange
            IPostRepository repository = MockPostRepository.GetRepository(MockPostRepository.STDN);
            PostController  target     = new PostController(repository);

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

            // Assert
            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(RedirectResult));
        }
        public void DeleteWholePost()
        {
            // Arrange
            LoginData loginData = GetAdvancedPost();

            PostController postController = new PostController();

            // Act
            postController.Delete(loginData.sessionkey, 1);

            //Assert
            var comment    = DatabaseService.Instance.GetComment(1);
            var subComment = DatabaseService.Instance.GetComment(3);

            Assert.Null(comment);
            Assert.Null(subComment);
        }
        public async Task Delete_Get_ModelIsValid_RetrievesPostFromModel()
        {
            Mock <PostService> mock = new Mock <PostService>();

            mock.Setup(m => m.FindByIdAsync(It.IsAny <int?>())).ReturnsAsync((int?_id) => new PostDTO {
                Id    = _id.Value,
                Title = "Programmer"
            });
            PostController controller = GetNewPostController(mock.Object, null, null);

            ViewResult result = (await controller.Delete(2)) as ViewResult;

            PostViewModel model = result.ViewData.Model as PostViewModel;

            Assert.AreEqual(2, model.Id);
            Assert.AreEqual("Programmer", model.Title);
        }
        public void DeletePost()
        {
            // Arrange
            LoginData loginData = CreatePost();

            PostController postController = new PostController();

            var getPostResult = postController.Get(loginData.sessionkey, 1).Result;
            var createdPost   = ((getPostResult as OkObjectResult).Value as ComplexAnswer).data as Post;

            // Act
            postController.Delete(loginData.sessionkey, createdPost.postId);

            // Assert
            var getPostNotFoundResult = postController.Get(loginData.sessionkey, 1).Result;

            Assert.IsType <NotFoundObjectResult>(getPostNotFoundResult);
        }
Beispiel #23
0
        public async void Delete_Errors()
        {
            PostControllerMockFacade mock = new PostControllerMockFacade();
            var mockResult = new Mock <ActionResponse>();

            mockResult.SetupGet(x => x.Success).Returns(false);
            mock.ServiceMock.Setup(x => x.Delete(It.IsAny <int>())).Returns(Task.FromResult <ActionResponse>(mockResult.Object));
            PostController controller = new PostController(mock.ApiSettingsMoc.Object, mock.LoggerMock.Object, mock.TransactionCoordinatorMock.Object, mock.ServiceMock.Object, mock.ModelMapperMock.Object);

            controller.ControllerContext             = new ControllerContext();
            controller.ControllerContext.HttpContext = new DefaultHttpContext();

            IActionResult response = await controller.Delete(default(int));

            response.Should().BeOfType <ObjectResult>();
            (response as ObjectResult).StatusCode.Should().Be((int)HttpStatusCode.UnprocessableEntity);
            mock.ServiceMock.Verify(x => x.Delete(It.IsAny <int>()));
        }
Beispiel #24
0
        //  [Fact]
        public void TestDeletePost()
        {
            var builder = new DbContextOptionsBuilder <PostContext>()
                          .UseInMemoryDatabase(databaseName: "database_name2");

            var context = new PostContext(builder.Options);

            var posts = Enumerable.Range(1, 10)
                        .Select(i => new Post {
                id = i, userid = i, title = $"Sample{i}", body = "Wrox Press"
            });

            context.Posts.AddRange(posts);
            int changed = context.SaveChanges();

            var controller = new PostController(context, CreateMockPostClientService());

            controller.Delete(1);
            Assert.Equal(9, context.Posts.Count());
        }
        public void Delete_ByAdmin()
        {
            var controller = new PostController(database);

            controller.Request       = new HttpRequestMessage();
            controller.Configuration = new HttpConfiguration();

            LiteDatabase db  = database;
            var          col = db.GetCollection <User>();

            col.Delete(123);
            col.Insert(new User {
                username = "******", password = "******", id = 123, status = "admin"
            });
            var response = controller.Delete(JObject.Parse("{ \"id\":\"123\",\"delete\":\"125\"}"));

            Assert.AreNotEqual(null, response);
            var tmp = response as OkNegotiatedContentResult <string>;

            Assert.AreEqual("Post has been deleted!", tmp.Content);
        }
        public static string deletePost(int id)
        {
            var    post   = PostController.GetByID(id);
            string result = "";

            if (post != null)
            {
                // Delete image gallery
                var postImage = PostImageController.GetByPostID(post.ID);
                if (postImage.Count > 0)
                {
                    foreach (var img in postImage)
                    {
                        if (!string.IsNullOrEmpty(img.Image))
                        {
                            // Delete in database
                            string deletePostImage = PostImageController.Delete(img.ID);
                        }
                    }
                }

                string deletePost = PostController.Delete(id);

                if (!string.IsNullOrEmpty(deletePost))
                {
                    result = "success";
                }
                else
                {
                    result = "failed";
                }
            }
            else
            {
                result = "notfound";
            }

            return(result);
        }
        public void Delete_Returns_A_View()
        {
            var result = underTest.Delete(1);

            Assert.IsType <ViewResult>(result);
        }