public async Task CreatePost_WithIncorrectFile_ShouldThrowInvalidOperationException()
        {
            var blogRepo = new Mock <IRepository <Blog> >();
            var userRepo = new Mock <IRepository <ApplicationUser> >();

            blogRepo.Setup(x => x.All()).Returns(this.GetTestData().AsQueryable);
            userRepo.Setup(x => x.All()).Returns(new List <ApplicationUser>()
            {
                new ApplicationUser()
                {
                    Id = "1"
                }
            }.AsQueryable);

            this._blogService = new BlogService(blogRepo.Object, userRepo.Object, null, null,
                                                null, null, null);

            var postDto = new CreateBlogPostDTO()
            {
                Title     = "asdasd",
                Content   = "asdads",
                BlogImage = null
            };

            await Assert.ThrowsAsync <InvalidOperationException>(() =>
                                                                 this._blogService.CreatePost(postDto, "1", true));
        }
Esempio n. 2
0
        public async Task CreatePost(CreateBlogPostDTO postDto, string userId, bool skipMethodForTest = false)
        {
            var userObj = this._userRepository.All().Single(x => x.Id == userId);

            string blogImageUrl = "";

            if (string.IsNullOrEmpty(postDto.Content) || string.IsNullOrEmpty(postDto.Title) ||
                postDto.BlogImage == null)
            {
                throw new InvalidOperationException("Fields cannot be null or empty.");
            }

            if (!skipMethodForTest)
            {
                blogImageUrl = this._imageService.AddToCloudinaryAndReturnBlogImageUrl(postDto.BlogImage);
            }

            var postObj = new Blog
            {
                Content        = postDto.Content,
                Title          = postDto.Title,
                AuthorUserName = userObj.UserName,
                BlogImage      = $"{blogImageUrl}",
                PublishedOn    = DateTime.Now,
                UserId         = userObj.Id
            };

            userObj.Blogs.Add(postObj);

            await this._blogRepository.SaveChangesAsync();
        }
Esempio n. 3
0
        public async Task <ActionResult <CreateBlogPostDTO> > CreatePost([FromForm] CreateBlogPostDTO postDto)
        {
            var userId = HttpContext.User.Claims.First(x => x.Type == "UserID").Value;

            if (!ModelState.IsValid)
            {
                return(this.NoContent());
            }

            _logger.LogInfo("Creating a new post...");

            await this._blogService.CreatePost(postDto, userId);

            _logger.LogInfo($"Post with content {postDto.Content} successfully created.");

            return(this.CreatedAtAction("CreatePost", new { text = postDto.Content }));
        }
        public void CreatePost_WithCorrectData_ShouldCreatePostSuccessfully()
        {
            string errorMessagePrefix = "BlogService CreatePost() method does not work properly.";

            var blogRepo = new Mock <IRepository <Blog> >();
            var userRepo = new Mock <IRepository <ApplicationUser> >();
            var fileMock = new Mock <IFormFile>();

            const string content  = "Hello World from a Fake File";
            const string fileName = "profileImg.jpg";
            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);
            blogRepo.Setup(x => x.All()).Returns(this.GetTestData().AsQueryable);
            userRepo.Setup(x => x.All()).Returns(new List <ApplicationUser>()
            {
                new ApplicationUser()
                {
                    Id = "1"
                }
            }.AsQueryable);

            this._blogService = new BlogService(blogRepo.Object, userRepo.Object, null, null,
                                                null, null, null);

            var postDto = new CreateBlogPostDTO()
            {
                Title     = "George",
                Content   = "qwerty qwerty qwerty",
                BlogImage = fileMock.Object
            };

            var passed = this._blogService.CreatePost(postDto, "1", true).IsCompletedSuccessfully;

            Assert.True(passed, errorMessagePrefix);
        }
        public async Task CreatePost_WithIncorrectUserId_ShouldThrowInvalidOperationException()
        {
            var userRepo = new Mock <IRepository <ApplicationUser> >();

            userRepo.Setup(x => x.All()).Returns(new List <ApplicationUser>()
            {
                new ApplicationUser()
                {
                    Id = "1"
                }
            }.AsQueryable);

            this._blogService = new BlogService(null, userRepo.Object, null, null,
                                                null, null, null);

            var postDto = new CreateBlogPostDTO();

            await Assert.ThrowsAsync <InvalidOperationException>(() =>
                                                                 this._blogService.CreatePost(postDto, "1", true));
        }