public ActionResult SaveComment(GroupCommentFormModel newcomment) { if (ModelState.IsValid) { var userId = User.Identity.GetUserId(); var comment = Mapper.Map<GroupCommentFormModel, GroupComment>(newcomment); groupCommentService.CreateComment(comment, userId); } return RedirectToAction("DisplayComments", new { id = newcomment.GroupUpdateId }); }
public void SaveComment() { MemoryUser user = new MemoryUser("adarsh"); ApplicationUser applicationUser = getApplicationUser(); var userContext = new UserInfo { UserId = user.Id, DisplayName = user.UserName, UserIdentifier = applicationUser.Email, RoleName = Enum.GetName(typeof(UserRoles), applicationUser.RoleId) }; var testTicket = new FormsAuthenticationTicket( 1, user.Id, DateTime.Now, DateTime.Now.Add(FormsAuthentication.Timeout), false, userContext.ToString()); GroupController controller = new GroupController(groupService, groupUserService, userService, metricService, focusService, groupgoalService, groupInvitationService, securityTokenService, groupUpdateService, groupCommentService, goalStatusService, groupRequestService, followUserService, groupCommentUserService, groupUpdateSupportService, groupUpdateUserService); principal.SetupGet(x => x.Identity.Name).Returns("adarsh"); controllerContext.SetupGet(x => x.HttpContext.User).Returns(principal.Object); controllerContext.SetupGet(p => p.HttpContext.Request.IsAuthenticated).Returns(true); controller.ControllerContext = controllerContext.Object; contextBase.SetupGet(x => x.Request).Returns(httpRequest.Object); contextBase.SetupGet(x => x.Response).Returns(httpResponse.Object); genericPrincipal.Setup(x => x.Identity).Returns(identity.Object); contextBase.SetupGet(a => a.Response.Cookies).Returns(new HttpCookieCollection()); var formsAuthentication = new DefaultFormsAuthentication(); formsAuthentication.SetAuthCookie(contextBase.Object, testTicket); HttpCookie authCookie = contextBase.Object.Response.Cookies[FormsAuthentication.FormsCookieName]; var ticket = formsAuthentication.Decrypt(authCookie.Value); var goalsetterUser = new SocialGoalUser(ticket); string[] userRoles = { goalsetterUser.RoleName }; principal.Setup(x => x.Identity).Returns(goalsetterUser); // Arrange // Act GroupCommentFormModel Cmnt = new GroupCommentFormModel(); Mapper.CreateMap<GroupCommentFormModel, GroupComment>(); Cmnt.CommentText = "Mock"; var result = controller.SaveComment(Cmnt) as RedirectToRouteResult; // Assert Assert.AreEqual("DisplayComments", result.RouteValues["action"]); }