Permalink
Browse files

Merge branch 'code-coverage-improvements' into develop

  • Loading branch information...
2 parents f38aa5a + 108810a commit 515936172c044c624059ecb4c57b3a04f7378824 Kristoffer Ahl committed Dec 4, 2012
View
3 .gitignore
@@ -27,4 +27,5 @@ _ReSharper*/
*.resharper
[Tt]est[Rr]esult*
NCover*.lic
-FluentSecurity.*.dotCover
+*.*.dotCover
+*.DotSettings
View
80 FluentSecurity.Specification/Configuration/PolicyContainerConfigurationWrapperSpec.cs
@@ -0,0 +1,80 @@
+using System;
+using FluentSecurity.Caching;
+using FluentSecurity.Configuration;
+using FluentSecurity.Policy;
+using Moq;
+using NUnit.Framework;
+
+namespace FluentSecurity.Specification.Configuration
+{
+ [TestFixture]
+ [Category("PolicyContainerConfigurationWrapperSpec")]
+ public class When_performing_actions_on_PolicyContainerConfigurationWrapper
+ {
+ private Mock<IPolicyContainerConfiguration> _inner;
+ private PolicyContainerConfigurationWrapper<IgnorePolicy> _wrapper;
+
+ [SetUp]
+ public void SetUp()
+ {
+ _inner = new Mock<IPolicyContainerConfiguration>();
+ _wrapper = new PolicyContainerConfigurationWrapper<IgnorePolicy>(_inner.Object);
+ }
+
+ [Test]
+ public void Should_add_policy_to_inner_configuration()
+ {
+ // Arrange
+ var expectedPolicy = new IgnorePolicy();
+
+ // Act
+ _wrapper.AddPolicy(expectedPolicy);
+ _wrapper.AddPolicy<IgnorePolicy>();
+
+ // Assert
+ _inner.Verify(x => x.AddPolicy(expectedPolicy), Times.Exactly(1));
+ _inner.Verify(x => x.AddPolicy<IgnorePolicy>(), Times.Exactly(1));
+ }
+
+ [Test]
+ public void Should_remove_policy_from_inner_configuration()
+ {
+ // Arrange
+ Func<IgnorePolicy, bool> expectedPredicate = p => true;
+
+ // Act
+ _wrapper.RemovePolicy(expectedPredicate);
+
+ // Assert
+ _inner.Verify(x => x.RemovePolicy(expectedPredicate), Times.Exactly(1));
+ }
+
+ [Test]
+ public void Should_set_cache_lifecycle_on_inner_configuration()
+ {
+ // Arrange
+ const Cache expectedLifecycle = Cache.PerHttpRequest;
+ const By expectedLevel = By.Controller;
+
+ // Act
+ _wrapper.Cache<IgnorePolicy>(expectedLifecycle);
+ _wrapper.Cache<IgnorePolicy>(expectedLifecycle, expectedLevel);
+
+ // Assert
+ _inner.Verify(x => x.Cache<IgnorePolicy>(expectedLifecycle), Times.Exactly(1));
+ _inner.Verify(x => x.Cache<IgnorePolicy>(expectedLifecycle, expectedLevel), Times.Exactly(1));
+ }
+
+ [Test]
+ public void Should_clear_cache_strategies_on_inner_configuration()
+ {
+ // Act
+ _wrapper.ClearCacheStrategies();
+ _wrapper.ClearCacheStrategyFor<IgnorePolicy>();
+
+ // Assert
+ _inner.Verify(x => x.ClearCacheStrategies(), Times.Exactly(1));
+ _inner.Verify(x => x.ClearCacheStrategyFor<IgnorePolicy>(), Times.Exactly(1));
+ }
+ }
+}
View
2 FluentSecurity.Specification/FluentSecurity.Specification.csproj
@@ -97,6 +97,7 @@
<Compile Include="AssemblyScannerBaseSpecification.cs" />
<Compile Include="ConfigurationExpressionSpec.cs" />
<Compile Include="Configuration\AdvancedConfigurationSpec.cs" />
+ <Compile Include="Configuration\PolicyContainerConfigurationWrapperSpec.cs" />
<Compile Include="Configuration\SecurityProfileSpec.cs" />
<Compile Include="Configuration\ViolationHandlerConfigurationSpec.cs" />
<Compile Include="Configuration\ViolationConfigurationSpec.cs" />
@@ -142,6 +143,7 @@
<Compile Include="Scanning\TypeScanners\ControllerTypeScannerSpec.cs" />
<Compile Include="Scanning\ProfileScannerSpec.cs" />
<Compile Include="SecurityModelSpec.cs" />
+ <Compile Include="SecurityRuntimeSpec.cs" />
<Compile Include="TestData\Controllers\BaseControllers.cs" />
<Compile Include="TestData\MockConvention.cs" />
<Compile Include="Policy\ViolationHandlers\PolicyViolationHandlerSelectorSpec.cs" />
View
15 FluentSecurity.Specification/Policy/LazySecurityPolicySpec.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using System.Linq;
using FluentSecurity.Policy;
using FluentSecurity.Policy.Contexts;
using FluentSecurity.Specification.Helpers;
@@ -84,6 +85,20 @@ public void Should_return_null_when_loading_policy_with_constructor_arguments()
// Assert
Assert.That(policy, Is.Null);
}
+
+ [Test]
+ public void Should_return_null_when_no_policy_is_returned_by_service_locator()
+ {
+ // Arrange
+ SecurityConfigurator.Configure(configuration => configuration.ResolveServicesUsing(t => Enumerable.Empty<object>()));
+ var lazySecurityPolicy = new LazySecurityPolicy<PolicyWithConstructorArguments>();
+
+ // Act
+ var policy = lazySecurityPolicy.Load();
+
+ // Assert
+ Assert.That(policy, Is.Null);
+ }
}
[TestFixture]
View
27 ...fication/Policy/ViolationHandlers/Conventions/PolicyViolationHandlerTypeConventionSpec.cs
@@ -11,10 +11,24 @@ namespace FluentSecurity.Specification.Policy.ViolationHandlers.Conventions
public class When_getting_a_PolicyViolationHandler_using_PolicyViolationHandlerTypeConvention
{
[Test]
- public void Should_return_null_when_derrived_convention_returns_null_for_GetHandlerTypeFor()
+ public void Should_return_null_when_derrived_convention_returns_null_from_GetHandlerTypeFor()
{
// Arrange
- var convention = new DerrivedPolicyViolationHandlerTypeConvention();
+ var convention = new DerrivedPolicyViolationHandlerTypeConvention(null);
+ var exception = TestDataFactory.CreateExceptionFor(new IgnorePolicy());
+
+ // Act
+ var handler = convention.GetHandlerFor(exception);
+
+ // Assert
+ Assert.That(handler, Is.Null);
+ }
+
+ [Test]
+ public void Should_return_null_when_derrived_convention_returns_type_not_assignable_to_IPolicyViolationHandler_from_GetHandlerTypeFor()
+ {
+ // Arrange
+ var convention = new DerrivedPolicyViolationHandlerTypeConvention(typeof(Exception));
var exception = TestDataFactory.CreateExceptionFor(new IgnorePolicy());
// Act
@@ -26,9 +40,16 @@ public void Should_return_null_when_derrived_convention_returns_null_for_GetHand
public class DerrivedPolicyViolationHandlerTypeConvention : PolicyViolationHandlerTypeConvention
{
+ public Type ReturnType { get; private set; }
+
+ public DerrivedPolicyViolationHandlerTypeConvention(Type returnType)
+ {
+ ReturnType = returnType;
+ }
+
public override Type GetHandlerTypeFor(PolicyViolationException exception)
{
- return null;
+ return ReturnType;
}
}
}
View
105 FluentSecurity.Specification/SecurityRuntimeSpec.cs
@@ -0,0 +1,105 @@
+using System;
+using FluentSecurity.Caching;
+using FluentSecurity.Configuration;
+using NUnit.Framework;
+
+namespace FluentSecurity.Specification
+{
+ public class When_creating_a_SecurityRuntime : SecurityRuntimeSpecification
+ {
+ [Test]
+ public void Should_not_ignore_missing_configuration()
+ {
+ Assert.That(Runtime.ShouldIgnoreMissingConfiguration, Is.False);
+ }
+
+ [Test]
+ public void Should_not_cache_results_by_default()
+ {
+ Assert.That(Runtime.DefaultResultsCacheLifecycle, Is.EqualTo(Cache.DoNotCache));
+ }
+
+ [Test]
+ public void Should_not_have_any_profiles()
+ {
+ Assert.That(Runtime.Profiles, Is.Empty);
+ }
+
+ [Test]
+ public void Should_not_have_any_policy_containers()
+ {
+ Assert.That(Runtime.PolicyContainers, Is.Empty);
+ }
+
+ [Test]
+ public void Should_not_have_any_conventions()
+ {
+ Assert.That(Runtime.Conventions, Is.Empty);
+ }
+ }
+
+ public class When_applying_ConventionConfiguration_to_SecurityRuntime : SecurityRuntimeSpecification
+ {
+ [Test]
+ public void Should_throw_when_null()
+ {
+ // Arrange
+ Action<ConventionConfiguration> configuration = null;
+
+ // Act & Assert
+ Assert.Throws<ArgumentNullException>(() => Runtime.ApplyConfiguration(configuration));
+ }
+ }
+
+ public class When_applying_ViolationConfiguration_to_SecurityRuntime : SecurityRuntimeSpecification
+ {
+ [Test]
+ public void Should_throw_when_null()
+ {
+ // Arrange
+ Action<ViolationConfiguration> configuration = null;
+
+ // Act & Assert
+ Assert.Throws<ArgumentNullException>(() => Runtime.ApplyConfiguration(configuration));
+ }
+ }
+
+ public class When_applying_a_SecurityProfile_to_SecurityRuntime : SecurityRuntimeSpecification
+ {
+ [Test]
+ public void Should_throw_when_null()
+ {
+ // Arrange
+ SecurityProfile configuration = null;
+
+ // Act & Assert
+ Assert.Throws<ArgumentNullException>(() => Runtime.ApplyConfiguration(configuration));
+ }
+ }
+
+ public class When_adding_a_PolicyContainer_to_SecurityRuntime : SecurityRuntimeSpecification
+ {
+ [Test]
+ public void Should_throw_when_null()
+ {
+ // Arrange
+ PolicyContainer policyContainer = null;
+
+ // Act & Assert
+ Assert.Throws<ArgumentNullException>(() => Runtime.AddPolicyContainer(policyContainer));
+ }
+ }
+
+ [TestFixture]
+ [Category("SecurityRuntimeSpec")]
+ public abstract class SecurityRuntimeSpecification
+ {
+ internal SecurityRuntime Runtime;
+
+ [SetUp]
+ public void SetUp()
+ {
+ Runtime = new SecurityRuntime();
+ }
+ }
+}
View
4 FluentSecurity/Policy/LazySecurityPolicy.cs
@@ -20,8 +20,8 @@ public ISecurityPolicy Load()
}
return PolicyType.HasEmptyConstructor()
- ? (ISecurityPolicy)Activator.CreateInstance<TSecurityPolicy>()
- : null;
+ ? (ISecurityPolicy)Activator.CreateInstance<TSecurityPolicy>()
+ : null;
}
public PolicyResult Enforce(ISecurityContext context)

0 comments on commit 5159361

Please sign in to comment.