Ejemplo n.º 1
0
        public void Should_use_cache_lifecycle_specified_when_adding_a_policy()
        {
            // Arrange
            const Cache defaultCacheLifecycle   = Cache.PerHttpSession;
            const Cache specifiedCacheLifecycle = Cache.PerHttpRequest;

            var context         = TestDataFactory.CreateSecurityContext(false);
            var securityPolicy  = new IgnorePolicy();
            var policyContainer = new PolicyContainer(TestDataFactory.ValidControllerName, TestDataFactory.ValidActionName, TestDataFactory.CreateValidPolicyAppender());

            policyContainer.SecurityConfigurationProvider = () => TestDataFactory.CreateValidSecurityConfiguration(configuration => configuration.Advanced.SetDefaultResultsCacheLifecycle(defaultCacheLifecycle));
            policyContainer.AddPolicy(securityPolicy).Cache <IgnorePolicy>(specifiedCacheLifecycle);

            // Act
            var results1 = policyContainer.EnforcePolicies(context);
            var results2 = policyContainer.EnforcePolicies(context);

            SecurityCache.ClearCache(Lifecycle.HybridHttpContext);;

            var results3 = policyContainer.EnforcePolicies(context);
            var results4 = policyContainer.EnforcePolicies(context);

            // Assert
            Assert.That(results1.Single(), Is.EqualTo(results2.Single()));
            Assert.That(results3.Single(), Is.EqualTo(results4.Single()));

            Assert.That(results1.Single(), Is.Not.EqualTo(results3.Single()), "Results should not be equal across requests.");
        }
Ejemplo n.º 2
0
        public void Should_return_the_same_results()
        {
            // Arrange
            var context         = TestDataFactory.CreateSecurityContext(false);
            var firstPolicy     = new IgnorePolicy();
            var policyContainer = new PolicyContainer(TestDataFactory.ValidControllerName, TestDataFactory.ValidActionName, TestDataFactory.CreateValidPolicyAppender());

            policyContainer.SecurityConfigurationProvider = () => TestDataFactory.CreateValidSecurityConfiguration(configuration => configuration.Advanced.SetDefaultResultsCacheLifecycle(Cache.PerHttpSession));
            policyContainer.AddPolicy(firstPolicy);

            // Act
            var results1 = policyContainer.EnforcePolicies(context);
            var results2 = policyContainer.EnforcePolicies(context);

            SecurityCache.ClearCache(Lifecycle.HybridHttpSession);;

            var results3 = policyContainer.EnforcePolicies(context);
            var results4 = policyContainer.EnforcePolicies(context);

            // Assert
            Assert.That(results1.Single(), Is.EqualTo(results2.Single()));
            Assert.That(results3.Single(), Is.EqualTo(results4.Single()));

            Assert.That(results1.Single(), Is.Not.EqualTo(results3.Single()), "Results should not be equal across sessions.");
        }
Ejemplo n.º 3
0
        public void Should_load_lazy_policy_with_cache_key_exactly_twice_during_execution_with_caching_on()
        {
            // Arrange
            var callsToContainer = 0;
            var policy           = new LazyLoadedPolicyWithCacheKey();

            FakeIoC.GetAllInstancesProvider = () =>
            {
                callsToContainer++;
                return(new List <object> {
                    policy
                });
            };
            SecurityConfigurator.Configure(configuration =>
            {
                configuration.GetAuthenticationStatusFrom(TestDataFactory.ValidIsAuthenticatedFunction);
                configuration.ResolveServicesUsing(FakeIoC.GetAllInstances);
                configuration.Advanced.SetDefaultResultsCacheLifecycle(Cache.PerHttpRequest);
            });
            var context         = new MockSecurityContext();
            var policyContainer = new PolicyContainer(TestDataFactory.ValidControllerName, TestDataFactory.ValidActionName, TestDataFactory.CreateValidPolicyAppender());

            policyContainer.AddPolicy <LazyLoadedPolicyWithCacheKey>();

            // Act
            policyContainer.EnforcePolicies(context);
            policyContainer.EnforcePolicies(context);

            // Assert
            Assert.That(callsToContainer, Is.EqualTo(2));
            Assert.That(policy.CacheKeyCallCount, Is.EqualTo(2), "Did not get the custom cache key the expected amount of times");
            Assert.That(policy.EnforceCallCount, Is.EqualTo(1), "Did not call enforce the expected amount of times");
        }
Ejemplo n.º 4
0
        public void Should_return_results()
        {
            // Arrange
            var roles = new List <object> {
                UserRole.Owner
            }.ToArray();
            const bool   isAuthenticated = true;
            const string failureOccured  = "Failure occured";
            var          context         = TestDataFactory.CreateSecurityContext(isAuthenticated, roles);

            var policy = new Mock <ISecurityPolicy>();

            policy.Setup(x => x.Enforce(It.IsAny <ISecurityContext>())).Returns(PolicyResult.CreateFailureResult(policy.Object, failureOccured));

            var policyContainer = new PolicyContainer(TestDataFactory.ValidControllerName, TestDataFactory.ValidActionName, TestDataFactory.CreateValidPolicyAppender());

            policyContainer.SecurityConfigurationProvider = TestDataFactory.CreateValidSecurityConfiguration;
            policyContainer.AddPolicy(policy.Object);

            // Act
            var results = policyContainer.EnforcePolicies(context);

            // Assert
            Assert.That(results.Count(), Is.EqualTo(1));
            Assert.That(results.Single().ViolationOccured, Is.True);
            Assert.That(results.Single().Message, Is.EqualTo(failureOccured));
        }
Ejemplo n.º 5
0
        public void Should_enforce_policies_with_context()
        {
            // Arrange
            var roles = new List <object> {
                UserRole.Owner
            }.ToArray();
            const bool isAuthenticated = true;

            var context = new Mock <ISecurityContext>();

            context.Setup(x => x.CurrenUserAuthenticated()).Returns(isAuthenticated);
            context.Setup(x => x.CurrenUserRoles()).Returns(roles);

            var policy = new Mock <ISecurityPolicy>();

            policy.Setup(x => x.Enforce(It.Is <ISecurityContext>(c => c.CurrenUserAuthenticated() == isAuthenticated && c.CurrenUserRoles() == roles))).Returns(PolicyResult.CreateSuccessResult(policy.Object));

            var policyContainer = new PolicyContainer(TestDataFactory.ValidControllerName, TestDataFactory.ValidActionName, TestDataFactory.CreateValidPolicyAppender());

            policyContainer.SecurityConfigurationProvider = TestDataFactory.CreateValidSecurityConfiguration;
            policyContainer.AddPolicy(policy.Object);

            // Act
            policyContainer.EnforcePolicies(context.Object);

            // Assert
            policy.VerifyAll();
        }
Ejemplo n.º 6
0
        public void Should_stop_on_first_violation_and_return_1_result()
        {
            // Arrange
            PolicyExecutionMode.StopOnFirstViolation(true);

            var context = TestDataFactory.CreateSecurityContext(false);

            var firstPolicy = new Mock <ISecurityPolicy>();

            firstPolicy.Setup(x => x.Enforce(It.IsAny <ISecurityContext>())).Returns(PolicyResult.CreateFailureResult(firstPolicy.Object, "Failure occured"));

            var secondPolicy = new Mock <ISecurityPolicy>();

            secondPolicy.Setup(x => x.Enforce(It.IsAny <ISecurityContext>())).Returns(PolicyResult.CreateSuccessResult(secondPolicy.Object));

            var policyContainer = new PolicyContainer(TestDataFactory.ValidControllerName, TestDataFactory.ValidActionName, TestDataFactory.CreateValidPolicyAppender());

            policyContainer.AddPolicy(firstPolicy.Object).AddPolicy(secondPolicy.Object);

            // Act
            var results = policyContainer.EnforcePolicies(context);

            // Assert
            Assert.That(results.Count(), Is.EqualTo(1));
            Assert.That(results.Single().ViolationOccured, Is.True);
        }
Ejemplo n.º 7
0
        public void Should_stop_on_first_violation_and_return_2_results()
        {
            // Arrange
            var context = TestDataFactory.CreateSecurityContext(false);

            var firstPolicy = new TestPolicy();

            var secondPolicy = new Mock <ISecurityPolicy>();

            secondPolicy.Setup(x => x.Enforce(It.IsAny <ISecurityContext>())).Returns(PolicyResult.CreateFailureResult(secondPolicy.Object, "Failure occured"));

            var thirdPolicy = new TestPolicy();

            var policyContainer = new PolicyContainer(TestDataFactory.ValidControllerName, TestDataFactory.ValidActionName, TestDataFactory.CreateValidPolicyAppender());

            policyContainer.SecurityConfigurationProvider = TestDataFactory.CreateValidSecurityConfiguration;
            policyContainer.AddPolicy(firstPolicy).AddPolicy(secondPolicy.Object).AddPolicy(thirdPolicy);

            // Act
            var results = policyContainer.EnforcePolicies(context);

            // Assert
            Assert.That(results.Count(), Is.EqualTo(2));
            Assert.That(results.First().ViolationOccured, Is.False);
            Assert.That(results.Last().ViolationOccured, Is.True);
        }
Ejemplo n.º 8
0
        public void Should_return_unique_results()
        {
            // Arrange
            var firstPolicy     = new IgnorePolicy();
            var policyContainer = new PolicyContainer(TestDataFactory.ValidControllerName, TestDataFactory.ValidActionName, TestDataFactory.CreateValidPolicyAppender());

            policyContainer.SecurityConfigurationProvider = () => TestDataFactory.CreateValidSecurityConfiguration(configuration => configuration.Advanced.SetDefaultResultsCacheLifecycle(Cache.DoNotCache));
            policyContainer.AddPolicy(firstPolicy);

            // Act
            var context  = TestDataFactory.CreateSecurityContext(false);
            var results1 = policyContainer.EnforcePolicies(context);
            var results2 = policyContainer.EnforcePolicies(context);

            // Assert
            Assert.That(results1.Single(), Is.Not.EqualTo(results2.Single()));
        }
Ejemplo n.º 9
0
        public void Should_invoke_the_isautheticated_and_roles_functions()
        {
            // Arrange
            var context = MockRepository.GenerateMock <ISecurityContext>();

            context.Expect(x => x.CurrenUserAuthenticated()).Return(true).Repeat.Once();
            context.Expect(x => x.CurrenUserRoles()).Return(new List <object> {
                UserRole.Owner
            }.ToArray()).Repeat.Once();
            context.Replay();

            var policyContainer = new PolicyContainer(TestDataFactory.ValidControllerName, TestDataFactory.ValidActionName, TestDataFactory.CreateValidPolicyAppender());

            policyContainer.AddPolicy(new TestPolicy());

            // Act
            policyContainer.EnforcePolicies(context);

            // Assert
            context.VerifyAllExpectations();
        }
Ejemplo n.º 10
0
        public void Should_enforce_policies_with_context()
        {
            // Arrange
            var roles = new List<object> { UserRole.Owner }.ToArray();
            const bool isAuthenticated = true;

            var context = new Mock<ISecurityContext>();
            context.Setup(x => x.CurrenUserAuthenticated()).Returns(isAuthenticated);
            context.Setup(x => x.CurrenUserRoles()).Returns(roles);

            var policy = new Mock<ISecurityPolicy>();
            policy.Setup(x => x.Enforce(It.Is<ISecurityContext>(c => c.CurrenUserAuthenticated() == isAuthenticated && c.CurrenUserRoles() == roles))).Returns(PolicyResult.CreateSuccessResult(policy.Object));

            var policyContainer = new PolicyContainer(TestDataFactory.ValidControllerName, TestDataFactory.ValidActionName, TestDataFactory.CreateValidPolicyAppender());
            policyContainer.AddPolicy(policy.Object);

            // Act
            policyContainer.EnforcePolicies(context.Object);

            // Assert
            policy.VerifyAll();
        }
Ejemplo n.º 11
0
        public void Should_use_cache_lifecycle_specified_when_adding_a_policy()
        {
            // Arrange
            const Cache defaultCacheLifecycle = Cache.PerHttpSession;
            const Cache specifiedCacheLifecycle = Cache.PerHttpRequest;

            var context = TestDataFactory.CreateSecurityContext(false);
            context.Runtime.As<SecurityRuntime>().DefaultResultsCacheLifecycle = defaultCacheLifecycle;
            var securityPolicy = new IgnorePolicy();
            var policyContainer = new PolicyContainer(TestDataFactory.ValidControllerName, TestDataFactory.ValidActionName, TestDataFactory.CreateValidPolicyAppender());
            policyContainer.AddPolicy(securityPolicy).Cache<IgnorePolicy>(specifiedCacheLifecycle);

            // Act
            var results1 = policyContainer.EnforcePolicies(context);
            var results2 = policyContainer.EnforcePolicies(context);

            SecurityCache.ClearCache(Lifecycle.HybridHttpContext); ;

            var results3 = policyContainer.EnforcePolicies(context);
            var results4 = policyContainer.EnforcePolicies(context);

            // Assert
            Assert.That(results1.Single(), Is.EqualTo(results2.Single()));
            Assert.That(results3.Single(), Is.EqualTo(results4.Single()));

            Assert.That(results1.Single(), Is.Not.EqualTo(results3.Single()), "Results should not be equal across requests.");
        }
Ejemplo n.º 12
0
        public void Should_stop_on_first_violation_and_return_2_results()
        {
            // Arrange
            var context = TestDataFactory.CreateSecurityContext(false);

            var firstPolicy = new TestPolicy();

            var secondPolicy = new Mock<ISecurityPolicy>();
            secondPolicy.Setup(x => x.Enforce(It.IsAny<ISecurityContext>())).Returns(PolicyResult.CreateFailureResult(secondPolicy.Object, "Failure occured"));

            var thirdPolicy = new TestPolicy();

            var policyContainer = new PolicyContainer(TestDataFactory.ValidControllerName, TestDataFactory.ValidActionName, TestDataFactory.CreateValidPolicyAppender());
            policyContainer.AddPolicy(firstPolicy).AddPolicy(secondPolicy.Object).AddPolicy(thirdPolicy);

            // Act
            var results = policyContainer.EnforcePolicies(context);

            // Assert
            Assert.That(results.Count(), Is.EqualTo(2));
            Assert.That(results.First().ViolationOccured, Is.False);
            Assert.That(results.Last().ViolationOccured, Is.True);
        }
Ejemplo n.º 13
0
        public void Should_return_results()
        {
            // Arrange
            var roles = new List<object> { UserRole.Owner }.ToArray();
            const bool isAuthenticated = true;
            const string failureOccured = "Failure occured";
            var context = TestDataFactory.CreateSecurityContext(isAuthenticated, roles);

            var policy = new Mock<ISecurityPolicy>();
            policy.Setup(x => x.Enforce(It.IsAny<ISecurityContext>())).Returns(PolicyResult.CreateFailureResult(policy.Object, failureOccured));

            var policyContainer = new PolicyContainer(TestDataFactory.ValidControllerName, TestDataFactory.ValidActionName, TestDataFactory.CreateValidPolicyAppender());
            policyContainer.AddPolicy(policy.Object);

            // Act
            var results = policyContainer.EnforcePolicies(context);

            // Assert
            Assert.That(results.Count(), Is.EqualTo(1));
            Assert.That(results.Single().ViolationOccured, Is.True);
            Assert.That(results.Single().Message, Is.EqualTo(failureOccured));
        }
Ejemplo n.º 14
0
        public void Should_invoke_the_isautheticated_and_roles_functions()
        {
            // Arrange
            var context = MockRepository.GenerateMock<ISecurityContext>();
            context.Expect(x => x.Runtime).Return(TestDataFactory.CreateSecurityRuntime()).Repeat.Once();
            context.Expect(x => x.CurrentUserIsAuthenticated()).Return(true).Repeat.Once();
            context.Expect(x => x.CurrentUserRoles()).Return(new List<object> { UserRole.Owner }.ToArray()).Repeat.Once();
            context.Replay();

            var policyContainer = new PolicyContainer(TestDataFactory.ValidControllerName, TestDataFactory.ValidActionName, TestDataFactory.CreateValidPolicyAppender());
            policyContainer.AddPolicy(new TestPolicy());

            // Act
            policyContainer.EnforcePolicies(context);

            // Assert
            context.VerifyAllExpectations();
        }
Ejemplo n.º 15
0
        public void Should_load_lazy_policy_with_cache_key_exactly_twice_during_execution_with_caching_on()
        {
            // Arrange
            var callsToContainer = 0;
            var policy = new LazyLoadedPolicyWithCacheKey();
            FakeIoC.GetAllInstancesProvider = () =>
            {
                callsToContainer++;
                return new List<object> { policy };
            };
            SecurityConfigurator.Configure(configuration =>
            {
                configuration.GetAuthenticationStatusFrom(TestDataFactory.ValidIsAuthenticatedFunction);
                configuration.ResolveServicesUsing(FakeIoC.GetAllInstances);
                configuration.Advanced.SetDefaultResultsCacheLifecycle(Cache.PerHttpRequest);
            });
            var context = new MockSecurityContext(runtime: SecurityConfiguration.Current.Runtime);
            var policyContainer = new PolicyContainer(TestDataFactory.ValidControllerName, TestDataFactory.ValidActionName, TestDataFactory.CreateValidPolicyAppender());
            policyContainer.AddPolicy<LazyLoadedPolicyWithCacheKey>();

            // Act
            policyContainer.EnforcePolicies(context);
            policyContainer.EnforcePolicies(context);

            // Assert
            Assert.That(callsToContainer, Is.EqualTo(2));
            Assert.That(policy.CacheKeyCallCount, Is.EqualTo(2), "Did not get the custom cache key the expected amount of times");
            Assert.That(policy.EnforceCallCount, Is.EqualTo(1), "Did not call enforce the expected amount of times");
        }
        public void Should_return_the_same_results()
        {
            // Arrange
            var context = TestDataFactory.CreateSecurityContext(false);
            var firstPolicy = new IgnorePolicy();
            var policyContainer = new PolicyContainer(TestDataFactory.ValidControllerName, TestDataFactory.ValidActionName, TestDataFactory.CreateValidPolicyAppender());
            policyContainer.SecurityConfigurationProvider = () => TestDataFactory.CreateValidSecurityConfiguration(configuration => configuration.Advanced.SetDefaultResultsCacheLifecycle(Cache.PerHttpRequest));
            policyContainer.AddPolicy(firstPolicy);

            // Act
            var results1 = policyContainer.EnforcePolicies(context);
            var results2 = policyContainer.EnforcePolicies(context);

            SecurityCache.ClearCache(Lifecycle.HybridHttpContext);;

            var results3 = policyContainer.EnforcePolicies(context);
            var results4 = policyContainer.EnforcePolicies(context);

            // Assert
            Assert.That(results1.Single(), Is.EqualTo(results2.Single()));
            Assert.That(results3.Single(), Is.EqualTo(results4.Single()));

            Assert.That(results1.Single(), Is.Not.EqualTo(results3.Single()), "Results should not be equal across requests.");
        }
Ejemplo n.º 17
0
        public void Should_return_unique_results()
        {
            // Arrange
            var context = TestDataFactory.CreateSecurityContext(false);
            context.Runtime.As<SecurityRuntime>().DefaultResultsCacheLifecycle = Cache.DoNotCache;
            var firstPolicy = new IgnorePolicy();
            var policyContainer = new PolicyContainer(TestDataFactory.ValidControllerName, TestDataFactory.ValidActionName, TestDataFactory.CreateValidPolicyAppender());
            policyContainer.AddPolicy(firstPolicy);

            // Act

            var results1 = policyContainer.EnforcePolicies(context);
            var results2 = policyContainer.EnforcePolicies(context);

            // Assert
            Assert.That(results1.Single(), Is.Not.EqualTo(results2.Single()));
        }
Ejemplo n.º 18
0
        public void Should_return_the_same_results()
        {
            // Arrange
            var context = TestDataFactory.CreateSecurityContext(false);
            context.Runtime.As<SecurityRuntime>().DefaultResultsCacheLifecycle = Cache.PerHttpSession;
            var firstPolicy = new IgnorePolicy();
            var policyContainer = new PolicyContainer(TestDataFactory.ValidControllerName, TestDataFactory.ValidActionName, TestDataFactory.CreateValidPolicyAppender());
            policyContainer.AddPolicy(firstPolicy);

            // Act
            var results1 = policyContainer.EnforcePolicies(context);
            var results2 = policyContainer.EnforcePolicies(context);

            SecurityCache.ClearCache(Lifecycle.HybridHttpSession); ;

            var results3 = policyContainer.EnforcePolicies(context);
            var results4 = policyContainer.EnforcePolicies(context);

            // Assert
            Assert.That(results1.Single(), Is.EqualTo(results2.Single()));
            Assert.That(results3.Single(), Is.EqualTo(results4.Single()));

            Assert.That(results1.Single(), Is.Not.EqualTo(results3.Single()), "Results should not be equal across sessions.");
        }
        public void Should_return_unique_results()
        {
            // Arrange
            var firstPolicy = new IgnorePolicy();
            var policyContainer = new PolicyContainer(TestDataFactory.ValidControllerName, TestDataFactory.ValidActionName, TestDataFactory.CreateValidPolicyAppender());
            policyContainer.SecurityConfigurationProvider = () => TestDataFactory.CreateValidSecurityConfiguration(configuration => configuration.Advanced.SetDefaultResultsCacheLifecycle(Cache.DoNotCache));
            policyContainer.AddPolicy(firstPolicy);

            // Act
            var context = TestDataFactory.CreateSecurityContext(false);
            var results1 = policyContainer.EnforcePolicies(context);
            var results2 = policyContainer.EnforcePolicies(context);

            // Assert
            Assert.That(results1.Single(), Is.Not.EqualTo(results2.Single()));
        }