コード例 #1
0
 public void UserProfile()
 {
     var userManager = new UserManager<ApplicationUser>(new TestUserStore());
     MemoryUser user = new MemoryUser("adarsh");
     ApplicationUser applicationUser = new ApplicationUser()
     {
         Activated = true,
         Email = "*****@*****.**",
         FirstName = "Adarsh",
         LastName = "Vikraman",
         UserName="******",
         RoleId = 0,
         Id = "402bd590-fdc7-49ad-9728-40efbfe512ec"
     };
     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());           
     userRepository.Setup(x => x.Get(It.IsAny<Expression<Func<ApplicationUser, bool>>>())).Returns(applicationUser);
     AccountController controller = new AccountController(userService, userProfileService, goalService, updateService, commentService, followRequestService, followUserService, securityTokenService,userManager);
     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);
     UserProfile prfil = new UserProfile()
     {
         FirstName="Adarsh",
         LastName="Vikraman",
         DateOfBirth = DateTime.Now,
         Gender = true,
         Address = "a",
         City = "a",
         State = "a",
         Country = "a",
         ZipCode = 2344545,
         ContactNo = 1223344556,
         UserId = "402bd590-fdc7-49ad-9728-40efbfe512ec"
     };
     userProfileRepository.Setup(x => x.Get(It.IsAny<Expression<Func<UserProfile, bool>>>())).Returns(prfil);
     IEnumerable<FollowRequest> fake = new List<FollowRequest> {
     new FollowRequest { FollowRequestId =1, FromUserId = "402bd590-fdc7-49ad-9728-40efbfe512ec", ToUserId = "402bd590-fdc7-49ad-9728-40efbfe512ed"},
     new FollowRequest { FollowRequestId =2, FromUserId = "402bd590-fdc7-49ad-9728-40efbfe512ec", ToUserId = "402bd590-fdc7-49ad-9728-40efbfe512ee"},
     };
     followRequestRepository.Setup(x => x.GetMany(It.IsAny<Expression<Func<FollowRequest, bool>>>())).Returns(fake);
     IEnumerable<FollowUser> fakeuser = new List<FollowUser> {
     new FollowUser {FollowUserId =1, Accepted = false,FromUserId = "402bd590-fdc7-49ad-9728-40efbfe512ec", ToUserId = "402bd590-fdc7-49ad-9728-40efbfe512ed"},
     new FollowUser {FollowUserId =2, Accepted = false,FromUserId = "402bd590-fdc7-49ad-9728-40efbfe512ec", ToUserId = "402bd590-fdc7-49ad-9728-40efbfe512ee" },           
     };
     followUserRepository.Setup(x => x.GetMany(It.IsAny<Expression<Func<FollowUser, bool>>>())).Returns(fakeuser);
     ViewResult result = controller.UserProfile("402bd590-fdc7-49ad-9728-40efbfe512ec") as ViewResult;
     Assert.IsNotNull(result);
     Assert.IsInstanceOfType(typeof(UserProfileViewModel), result.ViewData.Model, "WrongType");
     var data = result.ViewData.Model as UserProfileViewModel;
     Assert.AreEqual("adarsh", data.UserName);
 }