public void if_user_has_specified_role_pass() { var attr = new AuthorizeAttributeHelper("Customer", "Web"); var mockHttpContext = new Mock <HttpContextBase>(); var routeData = new RouteData(); routeData.DataTokens.Add("area", "Customer"); routeData.Values.Add("controller", "Fake"); mockHttpContext.Setup(c => c.User.Identity.IsAuthenticated).Returns(true); mockHttpContext.Setup(c => c.User.IsInRole("Web")).Returns(true); mockHttpContext.Setup(x => x.Request.RequestContext.RouteData).Returns(routeData); var mockRepo = new Mock <IUsersRepository>(); var user = new User(); user.Roles.Add(new Role() { Name = "Web" }); mockRepo.Setup(x => x.GetUserByLogin("test.test.com")).Returns(user); attr.SetUsersRepository(mockRepo.Object); var result = attr.PublicAuthorizeCore(mockHttpContext.Object); Assert.That(result, Is.True); }
public void no_area_passes_not_authenticated_requests() { var attr = new AuthorizeAttributeHelper("Customer", "Web"); var mockHttpContext = new Mock <HttpContextBase>(); var routeData = new RouteData(); routeData.Values.Add("controller", "Fake"); mockHttpContext.Setup(x => x.Request.RequestContext.RouteData).Returns(routeData); var result = attr.PublicAuthorizeCore(mockHttpContext.Object); Assert.That(result, Is.True); }
public void if_area_is_not_listed_then_do_pass() { var attr = new AuthorizeAttributeHelper("Customer", "Web"); var mockHttpContext = new Mock <HttpContextBase>(); var routeData = new RouteData(); routeData.DataTokens.Add("area", "Underwriter"); routeData.Values.Add("controller", "Fake"); mockHttpContext.Setup(x => x.Request.RequestContext.RouteData).Returns(routeData); var result = attr.PublicAuthorizeCore(mockHttpContext.Object); Assert.That(result, Is.True); }
public void returns_not_authorized_when_area_doesnot_match() { var attr = new AuthorizeAttributeHelper("Underwriter", "Web", true); var mockHttpContext = new Mock <RequestContext>(); var context = new Mock <AuthorizationContext>(); var routeData = new RouteData(); routeData.DataTokens.Add("area", "Web"); routeData.Values.Add("controller", "Customers"); context.Setup(x => x.HttpContext.Request.RawUrl).Returns("google.ru"); context.Setup(x => x.RouteData).Returns(routeData); context.Object.RequestContext = mockHttpContext.Object; mockHttpContext.Object.RouteData = routeData; attr.PublicHandleUnauthorizedRequest(context.Object); Assert.That(context.Object.Result, Is.InstanceOf <HttpUnauthorizedResult>()); }