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>());
 }
        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>());
        }
Exemplo n.º 3
0
        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");
        }
        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>());
        }
 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;
 }
 protected bool IsLoggedInUser(UserViewModel user)
 {
     return (user != null && user.IsLoggedIn);
 }
Exemplo n.º 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);
        }