public void GivenUserHasRoles_WhenIAuthorize_ThenRolesAreLoaded()
        {
            HttpContext context = new HttpContext(new HttpRequest("", "http://tempuri.org", ""), new HttpResponse(new StringWriter()));
            HttpContext.Current = context;
            AuthenticateAndAuthorizeAttribute target = new AuthenticateAndAuthorizeAttribute();
            ControllerContext controllerContext = new ControllerContext(new HttpContextWrapper(context), new RouteData(), new TestController());
            ActionDescriptor action = new ReflectedActionDescriptor(typeof(TestController).GetMethod("Index"), "Index", new ReflectedControllerDescriptor(typeof(TestController)));
            AuthorizationContext authContext = new AuthorizationContext(controllerContext, action);
            context.User = new System.Security.Claims.ClaimsPrincipal(new System.Security.Claims.ClaimsIdentity[]
            {
                new System.Security.Claims.ClaimsIdentity(new System.Security.Claims.Claim[]
                {
                    new System.Security.Claims.Claim(System.Security.Claims.ClaimTypes.NameIdentifier, "Bob")
                }, "Test")
            });
            controllerContext.RouteData.Values.Add("action", "bob");
            controllerContext.RouteData.Values.Add("controller", "fred");

            target.OnAuthorization(authContext);

            Assert.IsInstanceOfType(context.User, typeof(EducationSecurityPrincipal));
            User user = ((EducationSecurityPrincipal)context.User).Identity.User;
            Assert.IsNotNull(user.UserRoles.FirstOrDefault().Role);
            Assert.AreEqual(SecurityRoles.DataAdmin, user.UserRoles.First().Role.Name);
        }
Example #2
0
        public void GivenUserHasRoles_WhenIAuthorize_ThenRolesAreLoaded()
        {
            HttpContext context = new HttpContext(new HttpRequest("", "http://tempuri.org", ""), new HttpResponse(new StringWriter()));

            HttpContext.Current = context;
            AuthenticateAndAuthorizeAttribute target = new AuthenticateAndAuthorizeAttribute();
            ControllerContext    controllerContext   = new ControllerContext(new HttpContextWrapper(context), new RouteData(), new TestController());
            ActionDescriptor     action      = new ReflectedActionDescriptor(typeof(TestController).GetMethod("Index"), "Index", new ReflectedControllerDescriptor(typeof(TestController)));
            AuthorizationContext authContext = new AuthorizationContext(controllerContext, action);

            context.User = new System.Security.Claims.ClaimsPrincipal(new System.Security.Claims.ClaimsIdentity[]
            {
                new System.Security.Claims.ClaimsIdentity(new System.Security.Claims.Claim[]
                {
                    new System.Security.Claims.Claim(System.Security.Claims.ClaimTypes.NameIdentifier, "Bob")
                }, "Test")
            });
            controllerContext.RouteData.Values.Add("action", "bob");
            controllerContext.RouteData.Values.Add("controller", "fred");

            target.OnAuthorization(authContext);

            Assert.IsInstanceOfType(context.User, typeof(EducationSecurityPrincipal));
            User user = ((EducationSecurityPrincipal)context.User).Identity.User;

            Assert.IsNotNull(user.UserRoles.FirstOrDefault().Role);
            Assert.AreEqual(SecurityRoles.DataAdmin, user.UserRoles.First().Role.Name);
        }
 public void TestInitialize()
 {
     MockAccountManager    = MockRepository.GenerateMock <IAccountManager>();
     MockDependecyResolver = MockRepository.GenerateMock <IDependencyResolver>();
     MockDependecyResolver.Expect(m => m.GetService(typeof(IAccountManager))).Return(MockAccountManager);
     DependencyResolver.SetResolver(MockDependecyResolver);
     Target = new AuthenticateAndAuthorizeAttribute();
 }
 public void TestInitialize()
 {
     MockAccountManager = MockRepository.GenerateMock<IAccountManager>();
     MockDependecyResolver = MockRepository.GenerateMock<IDependencyResolver>();
     MockDependecyResolver.Expect(m => m.GetService(typeof(IAccountManager))).Return(MockAccountManager);
     DependencyResolver.SetResolver(MockDependecyResolver);
     Target = new AuthenticateAndAuthorizeAttribute();
 }
 public void WhenRegisterGlobalFilters_ThenUserIdentityMapAttributeOrderLessThanDefaultAuthenticateAndAuthorizeAttributeOrder()
 {
     UserIdentityMapAttribute userIdentityMapAttribute = Filters.Select(f => f.Instance).OfType<UserIdentityMapAttribute>().Single();
     AuthenticateAndAuthorizeAttribute otherAttribute = new AuthenticateAndAuthorizeAttribute();
     Assert.IsTrue(userIdentityMapAttribute.Order < otherAttribute.Order);
 }