public void Should_clear_all_cache_strategies_for_policy()
        {
            var policyContainer = new PolicyContainer("Controller", "Action", TestDataFactory.CreateValidPolicyAppender());

            policyContainer.Cache <RequireRolePolicy>(Cache.PerHttpRequest);
            policyContainer.Cache <RequireAllRolesPolicy>(Cache.PerHttpRequest);

            // Act
            policyContainer.ClearCacheStrategyFor <RequireRolePolicy>();

            // Assert
            Assert.That(policyContainer.CacheStrategies.Single().PolicyType, Is.EqualTo(typeof(RequireAllRolesPolicy)));
        }
        public void Should_add_policyresult_cache_strategies_for_each_policy_type()
        {
            // Arrange
            const string expectedControllerName = "Controller5";
            const string expectedActionName     = "Action5";
            var          policyContainer        = new PolicyContainer(expectedControllerName, expectedActionName, TestDataFactory.CreateValidPolicyAppender());

            // Act
            policyContainer
            .Cache <RequireAllRolesPolicy>(Cache.PerHttpRequest)
            .Cache <RequireRolePolicy>(Cache.PerHttpSession);

            // Assert
            Assert.That(policyContainer.CacheStrategies.Count, Is.EqualTo(2));

            var strategy1 = policyContainer.CacheStrategies.First();

            Assert.That(strategy1.ControllerName, Is.EqualTo(expectedControllerName));
            Assert.That(strategy1.ActionName, Is.EqualTo(expectedActionName));
            Assert.That(strategy1.PolicyType, Is.EqualTo(typeof(RequireAllRolesPolicy)));
            Assert.That(strategy1.CacheLifecycle, Is.EqualTo(Cache.PerHttpRequest));
            Assert.That(strategy1.CacheLevel, Is.EqualTo(By.ControllerAction));

            var strategy2 = policyContainer.CacheStrategies.Last();

            Assert.That(strategy2.ControllerName, Is.EqualTo(expectedControllerName));
            Assert.That(strategy2.ActionName, Is.EqualTo(expectedActionName));
            Assert.That(strategy2.PolicyType, Is.EqualTo(typeof(RequireRolePolicy)));
            Assert.That(strategy2.CacheLifecycle, Is.EqualTo(Cache.PerHttpSession));
            Assert.That(strategy2.CacheLevel, Is.EqualTo(By.ControllerAction));
        }
        public void Should_clear_all_cache_strategies()
        {
            var policyContainer = new PolicyContainer("Controller", "Action", TestDataFactory.CreateValidPolicyAppender());

            policyContainer.Cache <RequireRolePolicy>(Cache.PerHttpRequest);

            // Act
            policyContainer.ClearCacheStrategies();

            // Assert
            Assert.That(policyContainer.CacheStrategies.Any(), Is.False);
        }
        public void Should_add_policyresult_cache_strategy_for_RequireRolePolicy_with_level_set_to_ControllerAction()
        {
            const By expectedLevel   = By.ControllerAction;
            var      policyContainer = new PolicyContainer("Controller", "Action", TestDataFactory.CreateValidPolicyAppender());

            // Act
            policyContainer.Cache <RequireRolePolicy>(Cache.PerHttpRequest, expectedLevel);

            // Assert
            var policyResultCacheStrategy = policyContainer.CacheStrategies.Single();

            Assert.That(policyResultCacheStrategy.CacheLevel, Is.EqualTo(expectedLevel));
        }
        public void Should_add_policyresult_cache_strategy_for_DenyAnonymousAccessPolicy_with_lifecycle_set_to_PerHttpRequest()
        {
            const Cache  expectedLifecycle      = Cache.PerHttpRequest;
            const string expectedControllerName = "Controller4";
            const string expectedActionName     = "Action4";

            var policyContainer = new PolicyContainer(expectedControllerName, expectedActionName, TestDataFactory.CreateValidPolicyAppender());

            // Act
            policyContainer.Cache <DenyAnonymousAccessPolicy>(expectedLifecycle);

            // Assert
            var policyResultCacheStrategy = policyContainer.CacheStrategies.Single();

            Assert.That(policyResultCacheStrategy.ControllerName, Is.EqualTo(expectedControllerName));
            Assert.That(policyResultCacheStrategy.ActionName, Is.EqualTo(expectedActionName));
            Assert.That(policyResultCacheStrategy.PolicyType, Is.EqualTo(typeof(DenyAnonymousAccessPolicy)));
            Assert.That(policyResultCacheStrategy.CacheLifecycle, Is.EqualTo(expectedLifecycle));
            Assert.That(policyResultCacheStrategy.CacheLevel, Is.EqualTo(By.ControllerAction));
        }
        public void Should_add_policyresult_cache_strategy_for_RequireRolePolicy_with_lifecycle_set_to_DoNotCache()
        {
            const Cache  expectedLifecycle      = Cache.DoNotCache;
            const string expectedControllerName = "Controller1";
            const string expectedActionName     = "Action1";

            var policyContainer = new PolicyContainer(expectedControllerName, expectedActionName, TestDataFactory.CreateValidPolicyAppender());

            // Act
            policyContainer.Cache <RequireRolePolicy>(expectedLifecycle);

            // Assert
            var policyResultCacheStrategy = policyContainer.CacheStrategies.Single();

            Assert.That(policyResultCacheStrategy.ControllerName, Is.EqualTo(expectedControllerName));
            Assert.That(policyResultCacheStrategy.ActionName, Is.EqualTo(expectedActionName));
            Assert.That(policyResultCacheStrategy.PolicyType, Is.EqualTo(typeof(RequireRolePolicy)));
            Assert.That(policyResultCacheStrategy.CacheLifecycle, Is.EqualTo(expectedLifecycle));
            Assert.That(policyResultCacheStrategy.CacheLevel, Is.EqualTo(By.ControllerAction));
        }
        public void Should_update_existing_policyresult_cache_strategies()
        {
            // Arrange
            const Cache  expectedLifecycle      = Cache.PerHttpSession;
            const string expectedControllerName = "Controller6";
            const string expectedActionName     = "Action6";
            var          policyContainer        = new PolicyContainer(expectedControllerName, expectedActionName, TestDataFactory.CreateValidPolicyAppender());

            // Act
            policyContainer
            .Cache <RequireAllRolesPolicy>(Cache.PerHttpRequest)
            .Cache <RequireAllRolesPolicy>(Cache.PerHttpSession);

            // Assert
            var policyResultCacheStrategy = policyContainer.CacheStrategies.Single();

            Assert.That(policyResultCacheStrategy.ControllerName, Is.EqualTo(expectedControllerName));
            Assert.That(policyResultCacheStrategy.ActionName, Is.EqualTo(expectedActionName));
            Assert.That(policyResultCacheStrategy.PolicyType, Is.EqualTo(typeof(RequireAllRolesPolicy)));
            Assert.That(policyResultCacheStrategy.CacheLifecycle, Is.EqualTo(expectedLifecycle));
            Assert.That(policyResultCacheStrategy.CacheLevel, Is.EqualTo(By.ControllerAction));
        }
        public void Should_clear_all_cache_strategies_for_policy()
        {
            var policyContainer = new PolicyContainer("Controller", "Action", TestDataFactory.CreateValidPolicyAppender());
            policyContainer.Cache<RequireAnyRolePolicy>(Cache.PerHttpRequest);
            policyContainer.Cache<RequireAllRolesPolicy>(Cache.PerHttpRequest);

            // Act
            policyContainer.ClearCacheStrategyFor<RequireAnyRolePolicy>();

            // Assert
            Assert.That(policyContainer.CacheStrategies.Single().PolicyType, Is.EqualTo(typeof(RequireAllRolesPolicy)));
        }
        public void Should_clear_all_cache_strategies()
        {
            var policyContainer = new PolicyContainer("Controller", "Action", TestDataFactory.CreateValidPolicyAppender());
            policyContainer.Cache<RequireAnyRolePolicy>(Cache.PerHttpRequest);

            // Act
            policyContainer.ClearCacheStrategies();

            // Assert
            Assert.That(policyContainer.CacheStrategies.Any(), Is.False);
        }
        public void Should_update_existing_policyresult_cache_strategies()
        {
            // Arrange
            const Cache expectedLifecycle = Cache.PerHttpSession;
            const string expectedControllerName = "Controller6";
            const string expectedActionName = "Action6";
            var policyContainer = new PolicyContainer(expectedControllerName, expectedActionName, TestDataFactory.CreateValidPolicyAppender());

            // Act
            policyContainer
                .Cache<RequireAllRolesPolicy>(Cache.PerHttpRequest, By.Controller)
                .Cache<RequireAllRolesPolicy>(Cache.PerHttpSession, By.Policy);

            // Assert
            var policyResultCacheStrategy = policyContainer.CacheStrategies.Single();
            Assert.That(policyResultCacheStrategy.ControllerName, Is.EqualTo(expectedControllerName));
            Assert.That(policyResultCacheStrategy.ActionName, Is.EqualTo(expectedActionName));
            Assert.That(policyResultCacheStrategy.PolicyType, Is.EqualTo(typeof(RequireAllRolesPolicy)));
            Assert.That(policyResultCacheStrategy.CacheLifecycle, Is.EqualTo(expectedLifecycle));
            Assert.That(policyResultCacheStrategy.CacheLevel, Is.EqualTo(By.Policy));
        }
        public void Should_add_policyresult_cache_strategy_for_RequireAnyRolePolicy_with_level_set_to_Policy()
        {
            const By expectedLevel = By.Policy;
            var policyContainer = new PolicyContainer("Controller", "Action", TestDataFactory.CreateValidPolicyAppender());

            // Act
            policyContainer.Cache<RequireAnyRolePolicy>(Cache.PerHttpRequest, expectedLevel);

            // Assert
            var policyResultCacheStrategy = policyContainer.CacheStrategies.Single();
            Assert.That(policyResultCacheStrategy.CacheLevel, Is.EqualTo(expectedLevel));
        }
        public void Should_add_policyresult_cache_strategy_for_RequireAnyRolePolicy_with_lifecycle_set_to_PerHttpSession()
        {
            const Cache expectedLifecycle = Cache.PerHttpSession;
            const string expectedControllerName = "Controller3";
            const string expectedActionName = "Action3";

            var policyContainer = new PolicyContainer(expectedControllerName, expectedActionName, TestDataFactory.CreateValidPolicyAppender());

            // Act
            policyContainer.Cache<RequireAnyRolePolicy>(expectedLifecycle);

            // Assert
            var policyResultCacheStrategy = policyContainer.CacheStrategies.Single();
            Assert.That(policyResultCacheStrategy.ControllerName, Is.EqualTo(expectedControllerName));
            Assert.That(policyResultCacheStrategy.ActionName, Is.EqualTo(expectedActionName));
            Assert.That(policyResultCacheStrategy.PolicyType, Is.EqualTo(typeof(RequireAnyRolePolicy)));
            Assert.That(policyResultCacheStrategy.CacheLifecycle, Is.EqualTo(expectedLifecycle));
            Assert.That(policyResultCacheStrategy.CacheLevel, Is.EqualTo(By.ControllerAction));
        }
        public void Should_add_policyresult_cache_strategies_for_each_policy_type()
        {
            // Arrange
            const string expectedControllerName = "Controller5";
            const string expectedActionName = "Action5";
            var policyContainer = new PolicyContainer(expectedControllerName, expectedActionName, TestDataFactory.CreateValidPolicyAppender());

            // Act
            policyContainer
                .Cache<RequireAllRolesPolicy>(Cache.PerHttpRequest)
                .Cache<RequireAnyRolePolicy>(Cache.PerHttpSession);

            // Assert
            Assert.That(policyContainer.CacheStrategies.Count, Is.EqualTo(2));

            var strategy1 = policyContainer.CacheStrategies.First();
            Assert.That(strategy1.ControllerName, Is.EqualTo(expectedControllerName));
            Assert.That(strategy1.ActionName, Is.EqualTo(expectedActionName));
            Assert.That(strategy1.PolicyType, Is.EqualTo(typeof(RequireAllRolesPolicy)));
            Assert.That(strategy1.CacheLifecycle, Is.EqualTo(Cache.PerHttpRequest));
            Assert.That(strategy1.CacheLevel, Is.EqualTo(By.ControllerAction));

            var strategy2 = policyContainer.CacheStrategies.Last();
            Assert.That(strategy2.ControllerName, Is.EqualTo(expectedControllerName));
            Assert.That(strategy2.ActionName, Is.EqualTo(expectedActionName));
            Assert.That(strategy2.PolicyType, Is.EqualTo(typeof(RequireAnyRolePolicy)));
            Assert.That(strategy2.CacheLifecycle, Is.EqualTo(Cache.PerHttpSession));
            Assert.That(strategy2.CacheLevel, Is.EqualTo(By.ControllerAction));
        }