public ActionResult Edit(int?id, int?itemId, int?page) { if (id == null || itemId == null) { return(BadRequest()); } if (page == null || page < 1) { page = 1; } int userId = User.Identity.GetUserId <int>(); if (!this.commentService.CanEdit(id.Value, userId) && !User.IsInRole(CommonConstants.ModeratorRole)) { return(new HttpUnauthorizedResult()); } CommentFormServiceModel model = this.commentService.GetForm(id.Value); if (model == null) { return(BadRequest()); } model.Page = page; model.ItemId = itemId; return(View(model)); }
public IActionResult Edit(int id, int publicationId, int page, CommentFormServiceModel model) { int userId = int.Parse(this.userManager.GetUserId(User)); bool canEdit = this.commentService.CanEdit(id, userId); if (!canEdit && !User.IsInRole(WebConstants.ModeratorRole)) { return(Unauthorized()); } bool success = this.commentService.Edit(id, model.Content); if (!success) { return(BadRequest()); } TempData.AddSuccessMessage(string.Format(WebConstants.SuccessfullEntityOperation, Comment, WebConstants.Edited)); return(RedirectToAction( nameof(PublicationsController.Details), Publications, new { area = string.Empty, id = publicationId, page })); }
public void EditGet_ShouldReturnView() { // Arrange Mock <ICommentService> commentService = new Mock <ICommentService>(); Mock <UserManager <User> > userManager = UserManagerMock.New; Mock <ClaimsPrincipal> claimsMock = new Mock <ClaimsPrincipal>(); Mock <HttpContext> mockHttpContext = new Mock <HttpContext>(); CommentFormServiceModel formModel = this.GetCommentFormServiceModel(); commentService .Setup(c => c.CanEdit(It.IsAny <int>(), It.IsAny <int>())) .Returns(true); commentService .Setup(c => c.GetForm(It.IsAny <int>())) .Returns(formModel); userManager .Setup(u => u.GetUserId(It.IsAny <ClaimsPrincipal>())) .Returns("1"); claimsMock .Setup(c => c.IsInRole(It.IsAny <string>())) .Returns(true); mockHttpContext .Setup(m => m.User) .Returns(claimsMock.Object); CommentsController commentsController = new CommentsController(commentService.Object, userManager.Object) { ControllerContext = new ControllerContext { HttpContext = mockHttpContext.Object } }; const int commentId = 1; const int publicationId = 1; const int page = 1; // Act IActionResult result = commentsController.Edit(commentId, publicationId, page); // Assert Assert.IsType <ViewResult>(result); object model = (result as ViewResult).Model; Assert.IsType <CommentFormServiceModel>(model); CommentFormServiceModel returnModel = model as CommentFormServiceModel; this.AssertComment(formModel, returnModel); }
public void GetForm_WithExistingCommentId_ShouldReturnNull() { // Arrange StarStuffDbContext db = this.Database; CommentService commentService = new CommentService(db); const int commentId = 1; // Act CommentFormServiceModel result = commentService.GetForm(commentId); // Assert Assert.Null(result); }
public void EditGet_WithNotExistingCommentId_ShouldReturnBadRequest() { // Arrange Mock <ICommentService> commentService = new Mock <ICommentService>(); Mock <UserManager <User> > userManager = UserManagerMock.New; Mock <ClaimsPrincipal> claimsMock = new Mock <ClaimsPrincipal>(); Mock <HttpContext> mockHttpContext = new Mock <HttpContext>(); CommentFormServiceModel fromModel = null; commentService .Setup(c => c.CanEdit(It.IsAny <int>(), It.IsAny <int>())) .Returns(true); commentService .Setup(c => c.GetForm(It.IsAny <int>())) .Returns(fromModel); userManager .Setup(u => u.GetUserId(It.IsAny <ClaimsPrincipal>())) .Returns("1"); claimsMock .Setup(c => c.IsInRole(It.IsAny <string>())) .Returns(true); mockHttpContext .Setup(m => m.User) .Returns(claimsMock.Object); CommentsController commentsController = new CommentsController(commentService.Object, userManager.Object) { ControllerContext = new ControllerContext { HttpContext = mockHttpContext.Object } }; const int commentId = 1; const int publicationId = 1; const int page = 1; // Act IActionResult result = commentsController.Edit(commentId, publicationId, page); // Assert Assert.IsType <BadRequestResult>(result); }
public void GetForm_WithExistingCommentId_ShouldReturnCorrectResult() { // Arrange StarStuffDbContext db = this.Database; CommentService commentService = new CommentService(db); this.SeedCommanets(db); const int commentId = 1; Comment expected = db.Comments.Find(commentId); // Act CommentFormServiceModel actual = commentService.GetForm(commentId); // Assert this.CompareComments(expected, actual); }
public IActionResult Create(int id, int page, CommentFormServiceModel model) { int userId = int.Parse(this.userManager.GetUserId(User)); bool success = this.commentService.Create(id, userId, model.Content); if (!success) { return(BadRequest()); } TempData.AddSuccessMessage(string.Format(WebConstants.SuccessfullEntityOperation, Comment, WebConstants.Added)); return(RedirectToAction( nameof(PublicationsController.Details), Publications, new { area = string.Empty, id, page })); }
public ActionResult Edit(int?id, CommentFormServiceModel model) { if (id == null || model.ItemId == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(View(model)); } if (model.Page == null || model.Page < 1) { model.Page = 1; } int userId = User.Identity.GetUserId <int>(); if (!this.commentService.CanEdit(id.Value, userId) && !User.IsInRole(CommonConstants.ModeratorRole)) { return(new HttpUnauthorizedResult()); } bool success = this.commentService.Edit(id.Value, model.Content); if (!success) { return(BadRequest()); } TempData.AddSuccessMessage(string.Format( WebConstants.SuccessfullEntityOperation, Comment, WebConstants.Edited)); return(RedirectToAction( nameof(ItemsController.Details), Items, new { id = model.ItemId, model.Page })); }
public ActionResult Create(int?id, int?itemId, int?page) { if (id == null || itemId == null) { return(BadRequest()); } if (page == null || page < 1) { page = 1; } CommentFormServiceModel model = new CommentFormServiceModel() { Page = page, ItemId = itemId }; return(View(model)); }
public IActionResult Edit(int id, int publicationId, int page) { int userId = int.Parse(this.userManager.GetUserId(User)); bool canEdit = this.commentService.CanEdit(id, userId); if (!canEdit && !User.IsInRole(WebConstants.ModeratorRole)) { return(Unauthorized()); } CommentFormServiceModel model = this.commentService.GetForm(id); if (model == null) { return(BadRequest()); } return(View(model)); }
public ActionResult Create(int?answerId, int?questionId, int?page) { if (answerId == null || questionId == null || this.questionService.IsLocked(questionId.Value)) { return(BadRequest()); } if (page == null || page < 1) { page = 1; } CommentFormServiceModel model = new CommentFormServiceModel(); model.AnswerId = answerId.Value; model.RedirectInfo.Page = page; model.RedirectInfo.QuestionId = questionId; return(View(model)); }
public ActionResult Create(CommentFormServiceModel model) { if (model.RedirectInfo.QuestionId == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(View(model)); } if (model.RedirectInfo.Page == null || model.RedirectInfo.Page < 1) { model.RedirectInfo.Page = 1; } int authorId = User.Identity.GetUserId <int>(); bool success = this.commentService.Create(model.AnswerId, authorId, model.Content); if (!success) { return(BadRequest()); } TempData.AddSuccessMessage(string.Format( WebConstants.SuccessfullEntityOperation, Comment, WebConstants.Added)); return(RedirectToAction( nameof(QuestionsController.Details), Questions, new { id = model.RedirectInfo.QuestionId, model.RedirectInfo.Page })); }
private void CompareComments(Comment expected, CommentFormServiceModel actual) { Assert.Equal(expected.Content, actual.Content); }
private void AssertComment(CommentFormServiceModel expected, CommentFormServiceModel actual) { Assert.Equal(expected.Content, actual.Content); }