コード例 #1
0
 public void GivenAFilter_WhenTheUserIsNotLoggedIn_ThenTheFilterReturnsFalse()
 {
     AuthorizationContext filterContext = CreateFilterContext();
     var model = new UserViewModel {IsLoggedIn = false};
     _mockHttpContext.Setup(h => h.User).Returns(model);
     var loggedInUserAttribute = new AuthorizeLoggedInUserAttribute();
     loggedInUserAttribute.OnAuthorization(filterContext);
     Assert.That(filterContext.Result, Is.TypeOf<RedirectResult>());
 }
コード例 #2
0
        public void GivenAFilter_WhenTheBlogIdIsEmpty_ThenTheFilterReturnsFalse()
        {
            RouteData routeData = string.Format("~/{0}/edit/25/1", Nickname).GetRouteData("GET");
            _requestContext.Setup(r => r.RouteData).Returns(routeData);

            AuthorizationContext filterContext = CreateFilterContext(routeData);
            var httpRequest = new Mock<HttpRequestBase>();
            var model = new UserViewModel { IsLoggedIn = false };
            _mockHttpContext.Setup(h => h.User).Returns(model);
            _mockHttpContext.Setup(h => h.Request).Returns(httpRequest.Object);


            _blogOwnerAttribute.OnAuthorization(filterContext);
            Assert.That(filterContext.Result, Is.TypeOf<RedirectResult>());
        }
コード例 #3
0
ファイル: UserController.cs プロジェクト: kevinrjones/mblog
        public virtual ActionResult Create(UserViewModel userViewModel)
        {
            List<ErrorDetails> errorDetails;
            if (!ModelState.IsValid)
            {
                return View("Register");
            }
            errorDetails = _userService.IsUserRegistrationValid(userViewModel.Name, userViewModel.Email);
            if (errorDetails.Count != 0)
            {
                foreach (ErrorDetails errorDetail in errorDetails)
                {
                    ModelState.AddModelError(errorDetail.FieldName, errorDetail.Message);
                }
                return View("Register");
            }

            User user = _userService.CreateUser(userViewModel.Name, userViewModel.Email, userViewModel.Password);
            UpdateCookiesAndContext(user);
            return RedirectToAction("index", "Dashboard");
        }
コード例 #4
0
        public void GivenAFilter_WhenTheUserDoesNotOwnTheBlog_ThenTheFilterReturnsFalse()
        {
            const int blogId = 1;
            RouteData routeData = string.Format("~/{0}/edit/{1}/25", "wrong-nickname", blogId).GetRouteData("GET");
            _mockBlogService.Setup(r => r.GetBlog(Nickname)).Returns(new Blog {Id = blogId});
            _requestContext.Setup(r => r.RouteData).Returns(routeData);

            AuthorizationContext filterContext = CreateFilterContext(routeData);

            var model = new UserViewModel {IsLoggedIn = true};
            _mockHttpContext.Setup(h => h.User).Returns(model);

            _blogOwnerAttribute.OnAuthorization(filterContext);
            Assert.That(filterContext.Result, Is.TypeOf<RedirectResult>());
        }
コード例 #5
0
 private bool UserOwnsBlog(UserViewModel sessionUser, string nickname)
 {
     var user = UserService.GetUser(sessionUser.Email);
     var blog = BlogService.GetBlog(nickname);
     return blog != null && user!= null && blog.UserId == user.Id;
 }
コード例 #6
0
 protected bool IsLoggedInUser(UserViewModel user)
 {
     return (user != null && user.IsLoggedIn);
 }
コード例 #7
0
        public void GivenAnAuthenticatedUser_WhenILogin_ThenIGetTheRedirectView()
        {
            var userViewModel = new UserViewModel {IsLoggedIn = true};

            MockHttpContext.Setup(h => h.User).Returns(userViewModel);
            var result = _sessionController.New() as RedirectToRouteResult;

            Assert.That(result, Is.Not.Null);
            Assert.That(result.RouteValues["action"], Is.EqualTo("Index").IgnoreCase);
        }