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");
        }
        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");
        }