public async Task ShouldCreateAndAddImage()
        {
            // Arrange
            using var scope = Factory.Services.CreateScope();

            PostImageModel postImageModel = new PostImageModel
            {
                ContentType      = "media/png",
                ImageData        = new byte[0],
                ImageDescription = "image description",
                ImageTitle       = "image title"
            };

            var sut = new AddArticleImageHandler(unitOfWork, mediator, mapper, currentUserServiceMock.Object);

            // Act
            GetArticleModel sutResult = await sut.Handle(new AddArticleImageCommand(postImageModel, article.Id), new CancellationToken(false));

            // Assert
            // Should return updated model.
            Assert.NotNull(sutResult);
            Assert.Equal(article.Id, sutResult.Id);

            // Should add references to image.
            Assert.True(0 < sutResult.AssociatedImageId);

            // Added image should be valid.
            Image image = await unitOfWork.Images.GetEntityAsync(sutResult.AssociatedImageId);

            Assert.NotNull(image);
            Assert.Equal(postImageModel.ImageTitle, image.ImageTitle);
            Assert.Equal(postImageModel.ImageData, image.ImageData);
            Assert.Equal(postImageModel.ContentType, image.ContentType);
            Assert.Equal(postImageModel.ImageDescription, image.ImageDescription);
        }
示例#2
0
        public async Task ShouldCreateArticle()
        {
            // Arrange
            using var scope = Factory.Services.CreateScope();

            var postArticleModel = new PostArticleModel
            {
                BoardId = board.Id,
                Text    = "text",
                Title   = "title",
                Type    = "meta"
            };

            var sut = new AddArticleCommandHandler(unitOfWork, mediator, mapper, currentUserServiceMock.Object);

            // Act
            GetArticleModel sutResult = await sut.Handle(new AddArticleCommand(postArticleModel), new CancellationToken(false));

            // Assert
            Assert.NotNull(sutResult);

            var addedArticle = await unitOfWork.Articles.GetEntityAsync(sutResult.Id);

            Assert.NotNull(addedArticle);
            Assert.False(addedArticle.Deleted);
            Assert.Equal(addedArticle.UserId, user.Id);
            Assert.Equal(addedArticle.BoardId, board.Id);
        }
        public async Task ShouldUpdateArticle()
        {
            // Arrange
            using var scope = Factory.Services.CreateScope();

            var updateArticleModel = new PostArticleModel
            {
                BoardId = -1,                 // make sure board id can't be updated
                Text    = "updated text",
                Title   = "updated title",
                Type    = "meta"
            };

            var sut = new UpdateArticleHandler(unitOfWork, mediator, mapper, currentUserServiceMock.Object, articleOperationValidatorMock.Object);

            // Act
            GetArticleModel sutResult = await sut.Handle(new UpdateArticleCommand(article.Id, updateArticleModel), new CancellationToken(false));

            // Assert
            Assert.NotNull(sutResult);

            var updatedArticle = await unitOfWork.Articles.GetEntityAsync(sutResult.Id);

            Assert.NotNull(updatedArticle);
            Assert.False(updatedArticle.Deleted);
            Assert.Equal(updatedArticle.UserId, user.Id);
            Assert.Equal(updatedArticle.BoardId, board.Id);

            Assert.Equal(updatedArticle.Text, updateArticleModel.Text);
            Assert.Equal(updatedArticle.Title, updateArticleModel.Title);
        }
示例#4
0
        public async Task <ActionResult> Edit(ArticleEditViewModel editModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(editModel));
            }

            GetArticleModel updatedModel = await Mediator.Send(new UpdateArticleCommand(editModel.ArticleId, editModel.PostArticleModel));

            return(RedirectToAction("Details", new { id = updatedModel.Id }));
        }
        public async Task <IViewComponentResult> InvokeAsync(GetArticleModel articleModel, string imageDataUrl, bool displayText = false)
        {
            // TODO: test with deleted boards
            var           getBoardQuery = new GetBoardQuery(articleModel.BoardId);
            GetBoardModel board         = await getBoardQuery.DefaultIfExceptionAsync(_mediator);

            // TODO: returns null, which throws exception in view, if user is deleted
            var getuserQuery        = new GetPublicUserQuery(articleModel.UserId);
            GetPublicUserModel user = await getuserQuery.DefaultIfExceptionAsync(_mediator);

            string jwt = _apiJwtManager.GetToken();

            bool loggedIn           = false;
            bool saved              = false;
            bool userUpvoted        = false;
            bool userDownvoted      = false;
            bool userCreatedArticle = false;

            try
            {
                var loggedInUser = await _mediator.Send(new GetAuthenticatedUserQuery());

                if (loggedInUser != null)
                {
                    loggedIn = true;
                }
                saved              = loggedInUser.SavedArticles.Any(articleId => articleId == articleModel.Id);
                userUpvoted        = loggedInUser.LikedArticles.Any(articleId => articleId == articleModel.Id);
                userDownvoted      = loggedInUser.DislikedArticles.Any(articleId => articleId == articleModel.Id);
                userCreatedArticle = loggedInUser.ArticleIds.Any(articleId => articleId == articleModel.Id);
            }
            catch { }

            var viewModel = new ArticleCardViewModel
            {
                Article            = articleModel,
                Board              = board,
                User               = user,
                Jwt                = jwt,
                LoggedIn           = loggedIn,
                Saved              = saved,
                UserUpvoted        = userUpvoted,
                UserDownvoted      = userDownvoted,
                UserCreatedArticle = userCreatedArticle,
                ImageDataUrl       = imageDataUrl,
                DisplayText        = displayText,
                BaseUrl            = _baseUrl
            };

            return(View(viewModel));
        }
示例#6
0
        public async Task ShouldReturnArticle()
        {
            // Arrange
            using var scope = Factory.Services.CreateScope();

            var sut = new GetArticleHandler(deletedArticleValidatorMock.Object, unitOfWork, mediator, mapper, currentUserServiceMock.Object);

            // Act
            GetArticleModel sutResult = await sut.Handle(new GetArticleQuery(article.Id), new CancellationToken(false));

            // Assert
            Assert.NotNull(sutResult);

            Assert.Equal(article.Title, sutResult.Title);
            Assert.Equal(article.Text, sutResult.Text);
            Assert.Equal(article.Type, sutResult.Type);
            Assert.Equal(article.Id, sutResult.Id);
        }
示例#7
0
        public async Task <ActionResult> Create(ArticleCreateViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(viewModel));
            }

            // Create the article
            GetArticleModel model = await Mediator.Send(new AddArticleCommand(viewModel.Article));

            // If image attatched
            var file = Request.Form.Files.FirstOrDefault();

            if (file != null)
            {
                PostImageModel imageModel = _imageFileReader.ConvertImageFileToImageModel(file);

                // Add the image to the article
                await Mediator.Send(new AddArticleImageCommand(imageModel, model.Id));
            }

            return(RedirectToAction("Details", new { model.Id }));
        }
示例#8
0
        public async Task ShouldDownvoteArticleAndUser()
        {
            // Arrange
            using var scope = Factory.Services.CreateScope();

            Assert.Equal(article.UserId, user.Id);
            var originalArticleKarma = article.Karma;
            var originalUserKarma    = user.Karma;

            var sut = new VoteArticleCommandHandler(unitOfWork, mediator, mapper, currentUserServiceMock.Object);

            // Act
            GetArticleModel sutResult = await sut.Handle(new VoteArticleCommand(article.Id, false), new CancellationToken(false));

            // Assert
            Assert.NotNull(sutResult);

            var downvotedArticle = await unitOfWork.Articles.GetEntityAsync(sutResult.Id);

            Assert.NotNull(downvotedArticle);

            Assert.Equal(originalArticleKarma - 1, downvotedArticle.Karma);
            Assert.Equal(originalUserKarma - 1, user.Karma);
        }
示例#9
0
        public async Task <ViewResult> Details(int id, PagingParams pagingParams)
        {
            var commentModel = await Mediator.Send(new GetCommentQuery(id));

            var board = await Mediator.Send(new GetBoardQuery(commentModel.BoardId));

            GetArticleModel parentArticle;

            try
            {
                parentArticle = await Mediator.Send(new GetArticleQuery(commentModel.ParentArticleId));
            }
            catch (NotFoundException)
            {
                parentArticle = new GetArticleModel();
            }

            GetCommentModel parentComment;

            try
            {
                parentComment = await Mediator.Send(new GetCommentQuery(commentModel.ParentCommentId));
            }
            catch (NotFoundException)
            {
                parentComment = new GetCommentModel();
            }

            var childComments = await Mediator.Send(new GetCommentsByIdsQuery(commentModel.CommentIds, pagingParams));

            var user = await Mediator.Send(new GetPublicUserQuery(commentModel.UserId));

            GetPrivateUserModel privateUser = null;

            try
            {
                privateUser = await Mediator.Send(new GetAuthenticatedUserQuery());
            }
            catch (NotFoundException) { }
            catch (UnauthorizedException) { }

            var loggedIn     = privateUser != null && privateUser.Id != 0;
            var savedComment = loggedIn
                                ? privateUser.SavedComments.Contains(id)
                                : false;


            var model = new CommentDetailsViewModel
            {
                Board            = board,
                ChildCommentPage = new FrontendPage <GetCommentModel>(childComments),
                Comment          = commentModel,
                LoggedIn         = loggedIn,
                ParentArticle    = parentArticle,
                ParentComment    = parentComment,
                PostCommentModel = new PostCommentModel(),
                User             = user,
                UserSavedComment = savedComment,
                UserWroteComment = commentModel.UserId == user.Id
            };

            return(View(model));
        }
        public async Task <IViewComponentResult> InvokeAsync(GetCommentModel commentModel)
        {
            // TODO: test with deleted boards
            var getBoardQuery = new GetBoardQuery(commentModel.BoardId);
            var board         = await getBoardQuery.DefaultIfExceptionAsync(_mediator);

            var             getArticleQuery = new GetArticleQuery(commentModel.ParentArticleId);
            GetArticleModel parentArticle   = await getArticleQuery.DefaultIfExceptionAsync(_mediator);

            var             getCommentQuery = new GetCommentQuery(commentModel.ParentCommentId);
            GetCommentModel parentComment   = await getCommentQuery.DefaultIfExceptionAsync(_mediator);

            // TODO: will most likely return null when user is deleted
            var getUserQuery        = new GetPublicUserQuery(commentModel.UserId);
            GetPublicUserModel user = await getUserQuery.DefaultIfExceptionAsync(_mediator);


            string jwt = _apiJwtManager.GetToken();

            bool loggedIn           = false;
            bool saved              = false;
            bool userUpvoted        = false;
            bool userDownvoted      = false;
            bool userCreatedComment = false;

            try
            {
                var loggedInUser = await _mediator.Send(new GetAuthenticatedUserQuery());

                if (loggedInUser != null)
                {
                    loggedIn = true;
                }
                saved              = loggedInUser.SavedComments.Any(commentId => commentId == commentModel.Id);
                userUpvoted        = loggedInUser.LikedComments.Any(commentId => commentId == commentModel.Id);
                userDownvoted      = loggedInUser.DislikedComments.Any(commentId => commentId == commentModel.Id);
                userCreatedComment = commentModel.UserId == loggedInUser.Id;
            }
            catch (System.Exception)
            {
                loggedIn = false;
            }


            var model = new CommentCardViewModel
            {
                Comment            = commentModel,
                Board              = board,
                ParentArticle      = parentArticle,
                ParentComment      = parentComment,
                User               = user,
                Jwt                = jwt,
                LoggedIn           = loggedIn,
                Saved              = saved,
                UserUpvoted        = userUpvoted,
                UserDownvoted      = userDownvoted,
                UserCreatedComment = userCreatedComment
            };

            return(View(model));
        }