Exemplo n.º 1
0
        public void CreatePost_ReturnsOk()
        {
            // Arrange
            var post = SetupPost();

            _postRepositoryMock
            .Setup(mock => mock.CreatePost(It.IsAny <Post>()))
            .Returns(post);

            // Act
            var result = _postController.CreatePost(post);

            // Assert
            Assert.IsAssignableFrom <ObjectResult>(result);
            Assert.Equal((int)HttpStatusCode.OK, (result as ObjectResult).StatusCode);

            var resultBody = Assert.IsType <Post>((result as ObjectResult).Value);

            Assert.Equal((int)StatusType.Draft, resultBody.StatusId);
            Assert.Equal("some title", resultBody.Title);
        }
Exemplo n.º 2
0
        public void TestCreatePost()
        {
            var mock = new Mock <PostDAO>();

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

            var actionResult = postController.CreatePost(post4);

            mock.Verify((dao) => dao.Create(post4));
            Assert.IsInstanceOf <NoContentResult>(actionResult);
        }
Exemplo n.º 3
0
        public async Task CreatePost_UsingInvalidUserId_ReturnsBadRequest()
        {
            // Arrange
            PostToCreateDto post = new PostToCreateDto {
                Text = "Test."
            };

            _mockRepo.Setup(repo => repo.GetUserById(-1));

            // Act
            var response = await _postController.CreatePost(post);

            // Assert
            Assert.IsAssignableFrom <BadRequestObjectResult>(response);
        }
Exemplo n.º 4
0
        public void TestCreatePostWhenIdIsSet()
        {
            var mock = new Mock <PostDAO>();

            mock
            .Setup(dao => dao.Create(It.IsAny <Post>()))
            .Throws <ArgumentException>();

            var postController = new PostController(mock.Object);

            var actionResult = postController.CreatePost(post4);

            mock.Verify((dao) => dao.Create(post4));
            Assert.IsInstanceOf <BadRequestResult>(actionResult);
        }
Exemplo n.º 5
0
        public void Success_To_Create_A_Post_Test()
        {
            var mockService = GetService();

            mockService
            .Setup(service => service.Create(It.IsAny <Post>()))
            .Returns(async(Post post) =>
            {
                var dto       = new PostDto();
                dto.Id        = Guid.NewGuid();
                dto.Author    = new AuthorDto();
                dto.AuthorId  = 1;
                dto.CreatedOn = post.CreatedOn;
                dto.Title     = post.Title;
                dto.Body      = post.Body;
                return(await Task.FromResult(dto));
            });
            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 postDto      = GetPostDto();
            var actionResult = controller.CreatePost(postDto).Result as CreatedResult;

            Assert.NotNull(actionResult);
            Assert.IsType <string>(actionResult.Location);

            var post = actionResult.Value as PostDto;

            Assert.NotNull(post);
            Assert.Equal(1, post.AuthorId);
            Assert.IsType <Guid>(post.Id);
            Assert.Equal(postDto.Title, post.Title);
            Assert.Equal(postDto.Body, post.Body);
            Assert.Equal(postDto.CreatedOn, post.CreatedOn);
        }
Exemplo n.º 6
0
        public void CreatePost_WithFile()
        {
            UserPost newPost = new UserPost
            {
                Verified       = false,
                RecordStatusId = (int)RecordStatus.Active,
                IdUser         = 2,
                Text           = "Test",
                PostId         = 1
            };

            Mock <HttpContextBase>    contextMock = new Mock <HttpContextBase>();
            Mock <HttpPostedFileBase> file        = new Mock <HttpPostedFileBase>();

            MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes("Hello"));

            file.Setup(x => x.InputStream).Returns(stream);
            file.Setup(x => x.ContentLength).Returns((int)stream.Length);
            file.Setup(x => x.FileName).Returns("Test.js");
            string basePath = @"c:\yourPath\App";

            contextMock.Setup(x => x.Server.MapPath(It.IsAny <String>())).Returns(basePath);
            file.Setup(x => x.SaveAs(It.IsAny <String>())).Verifiable();


            string folderPath     = basePath + HardcodedValues.UserFiles + _loginUser.UserId;
            var    fakeFileSystem =
                new MockFileSystem(new Dictionary <string, MockFileData> {
                { folderPath, new MockDirectoryData() }
            });

            _postsRepoMock.Setup(m => m.CreatePost(newPost)).Returns(true);
            PostController controller = new PostController(_postsRepoMock.Object, _loginUser, fakeFileSystem);

            RequestContext rc = new RequestContext(contextMock.Object, new RouteData());

            controller.ControllerContext = new ControllerContext(rc, controller);

            JsonResult result = controller.CreatePost(newPost, file.Object);

            Assert.IsNotNull(result);
            Assert.IsNotNull(result.Data);
            Assert.IsTrue(result.Data is JsonResponseVM);
            Assert.IsTrue((result.Data as JsonResponseVM).Result == "OK");
            Assert.IsTrue(!String.IsNullOrEmpty((result.Data as JsonResponseVM).Msg));
        }
Exemplo n.º 7
0
        public async Task CreatePost_Pass()
        {
            var fileMock = new Mock <IFormFile>();
            //Setup mock file using a memory stream
            var content  = "Hello World from a Fake File";
            var fileName = "test.mp4";
            var ms       = new MemoryStream();
            var writer   = new StreamWriter(ms);

            writer.Write(content);
            writer.Flush();
            ms.Position = 0;
            fileMock.Setup(_ => _.OpenReadStream()).Returns(ms);
            fileMock.Setup(_ => _.FileName).Returns(fileName);
            fileMock.Setup(_ => _.Length).Returns(ms.Length);
            var postMockOject = new Mock <IPost>();

            postMockOject.Setup(r => r.Post(It.IsAny <Post>())).Returns(Task.CompletedTask);
            var mailSeviceObject = new Mock <IMailService>();
            var loggerObject     = new Mock <ILogger <PostController> >();
            var userObject       = new Mock <IUser>();

            userObject.Setup(r => r.GetCurrentUser()).Returns(Guid.NewGuid().ToString);
            var photoUploadObject = new  Mock <IPhoto>();

            photoUploadObject.Setup(r => r.UploadImageDb(It.IsAny <Photo>())).ReturnsAsync(Guid.NewGuid());
            var youTubeObject = new Mock <IYoutube>();

            youTubeObject.Setup(r => r.UploadVideo(It.IsAny <UploadViewModel>(), fileMock.Object))
            .ReturnsAsync(GetUpload());
            var videoObject = new Mock <IVideo>();

            videoObject.Setup(r => r.Post(It.IsAny <Videos>())).ReturnsAsync(Guid.NewGuid);
            var mapper = new Mock <IMapper>();
            var photoAccessorObject = new Mock <IPhotoAccessor>();

            photoAccessorObject.Setup(r => r.AddPhoto(fileMock.Object)).Returns(Getupload());
            var redisObject    = new Mock <IRedis>();
            var postController = new PostController(postMockOject.Object, mailSeviceObject.Object, loggerObject.Object, userObject.Object, photoUploadObject.Object, youTubeObject.Object, mapper.Object, photoAccessorObject.Object, videoObject.Object, redisObject.Object);
            var result         = await postController.CreatePost(It.IsAny <PostModel>());

            Assert.IsType <ObjectResult>(result);
        }
Exemplo n.º 8
0
        public async void CreatePost_GivenValidPostPresent_WhenApiSuccessResponseReturned_OkResultReturned()
        {
            // Arrange
            const int postId        = 23;
            var       createPostDto = new CreatePostDto
            {
                Title = "test title"
            };

            mockMediator.Setup(m => m.Send(It.Is <CreatePost.Command>(c => c.Post == createPostDto), CancellationToken.None))
            .ReturnsAsync(ApiResponse <PostDto> .Ok(new PostDto {
                Id = postId
            }));

            // Act
            var result = (OkObjectResult)(await sut.CreatePost(createPostDto)).Result;

            // Assert
            mockMediator.Verify(x => x.Send(It.Is <CreatePost.Command>(y => y.Post.Title == createPostDto.Title), CancellationToken.None));
            Assert.IsType <PostDto>(result.Value);
        }
Exemplo n.º 9
0
        public void CreatePost()
        {
            UserPost newPost = new UserPost
            {
                Verified       = false,
                RecordStatusId = (int)RecordStatus.Active,
                IdUser         = 2,
                Text           = "Test",
                PostId         = 1
            };

            _postsRepoMock.Setup(m => m.CreatePost(newPost)).Returns(true);
            PostController controller = new PostController(_postsRepoMock.Object, _loginUser);

            JsonResult result = controller.CreatePost(newPost);

            Assert.IsNotNull(result);
            Assert.IsNotNull(result.Data);
            Assert.IsTrue(result.Data is JsonResponseVM);
            Assert.IsTrue((result.Data as JsonResponseVM).Result == "OK");
            Assert.IsTrue((result.Data as JsonResponseVM).PostId == newPost.PostId);
        }
Exemplo n.º 10
0
        public void CreatePost_ModelStateNotValid()
        {
            UserPost newPost = new UserPost
            {
                Verified       = false,
                RecordStatusId = (int)RecordStatus.Active,
                IdUser         = 2,
                Text           = "Test",
                PostId         = 1
            };

            _postsRepoMock.Setup(m => m.CreatePost(newPost)).Returns(true);
            PostController controller = new PostController(_postsRepoMock.Object, _loginUser);

            controller.ModelState.AddModelError("UnitTest", "TestError");

            JsonResult result = controller.CreatePost(newPost);

            Assert.IsNotNull(result);
            Assert.IsNotNull(result.Data);
            Assert.IsTrue(result.Data is JsonResponseVM);
            Assert.IsTrue((result.Data as JsonResponseVM).Result == "ERROR");
            Assert.IsTrue((result.Data as JsonResponseVM).Msg == "TestError");
        }
Exemplo n.º 11
0
        public void CreatePost()
        {
            var retorno = postController.CreatePost(postOne.Title, postOne.Description);

            Assert.IsType <OkObjectResult>(retorno);
        }