Esempio n. 1
0
        public void Should_resolve_policy_violation_handler_for_exception_from_container()
        {
            // Arrange
            var expectedActionResult = new ViewResult {
                ViewName = "SomeViewName"
            };
            var violationHandler = new DenyAnonymousAccessPolicyViolationHandler(expectedActionResult);

            FakeIoC.GetAllInstancesProvider = () => new List <IPolicyViolationHandler>
            {
                violationHandler
            };

            SecurityConfigurator.Configure(policy =>
            {
                policy.ResolveServicesUsing(FakeIoC.GetAllInstances);
                policy.GetAuthenticationStatusFrom(StaticHelper.IsAuthenticatedReturnsFalse);
                policy.For <BlogController>(x => x.Index()).DenyAnonymousAccess();
            });

            var securityHandler = new SecurityHandler();

            // Act
            var result = securityHandler.HandleSecurityFor(NameHelper.Controller <BlogController>(), "Index", SecurityContext.Current);

            // Assert
            Assert.That(result, Is.EqualTo(expectedActionResult));
        }
Esempio n. 2
0
        public void Should_resolve_policy_violation_handler_for_exception_from_container()
        {
            // Arrange
            var          controllerName = NameHelper.Controller <BlogController>();
            const string actionName     = "Index";

            var events = new List <ISecurityEvent>();

            SecurityDoctor.Register(events.Add);
            var expectedActionResult = new ViewResult {
                ViewName = "SomeViewName"
            };
            var violationHandler = new DenyAnonymousAccessPolicyViolationHandler(expectedActionResult);

            FakeIoC.GetAllInstancesProvider = () => new List <IPolicyViolationHandler>
            {
                violationHandler
            };

            SecurityConfigurator.Configure(policy =>
            {
                policy.ResolveServicesUsing(FakeIoC.GetAllInstances);
                policy.GetAuthenticationStatusFrom(StaticHelper.IsAuthenticatedReturnsFalse);
                policy.For <BlogController>(x => x.Index()).DenyAnonymousAccess();
            });

            var securityHandler = new SecurityHandler();

            // Act
            var result = securityHandler.HandleSecurityFor(controllerName, actionName, SecurityContext.Current);

            // Assert
            Assert.That(result, Is.EqualTo(expectedActionResult));
            Assert.That(events.Any(e => e.Message == "Handling security for {0} action {1}.".FormatWith(controllerName, actionName)));
            Assert.That(events.Any(e => e.Message == "Finding policy violation handler using convention {0}.".FormatWith(typeof(FindByPolicyNameConvention))));
            Assert.That(events.Any(e => e.Message == "Found policy violation handler {0}.".FormatWith(violationHandler.GetType().FullName)));
            Assert.That(events.Any(e => e.Message == "Handling violation with {0}.".FormatWith(violationHandler.GetType().FullName)));
            Assert.That(events.Any(e => e.Message == "Done enforcing policies. Violation occured!"));
        }