Skip to content
This repository has been archived by the owner on Dec 9, 2022. It is now read-only.

Commit

Permalink
- Moved scanning of event listeners to configure method.
Browse files Browse the repository at this point in the history
- Added property to SecurityDoctor for disabling scanning on configure.
- Refactored SecurityDoctor to make it more testable.
- Added property for event listener scanner setup for allowing filtering of assemblies to scan.
- Exposed the current security doctor instance through SecurityDoctor.Current.
  • Loading branch information
Kristoffer Ahl committed Dec 10, 2013
1 parent b8fc666 commit 1a176ff
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 42 deletions.
155 changes: 130 additions & 25 deletions FluentSecurity.Specification/Diagnostics/SecurityDoctorSpecs.cs
Expand Up @@ -9,69 +9,150 @@ namespace FluentSecurity.Specification.Diagnostics
{
[TestFixture]
[Category("SecurityDoctorSpecs")]
public class When_SecurityDoctor_is_created
public class When_creating_a_security_doctor
{
private SecurityDoctor _doctor;

[SetUp]
public void SetUp()
{
SecurityDoctor.Reset();
// Act
_doctor = new SecurityDoctor();
}

[Test]
public void Should_have_no_event_listeners()
{
// Assert
Assert.That(_doctor.Listeners, Is.Null);
}

[Test]
public void Should_have_ScanForEventListenersOnConfigure_set_to_true()
{
// Assert
Assert.That(_doctor.ScanForEventListenersOnConfigure, Is.True);
}

[Test]
public void Should_have_ScannedForEventListeners_set_to_false()
{
// Assert
Assert.That(_doctor.ScannedForEventListeners, Is.False);
}
}

[TestFixture]
[Category("SecurityDoctorSpecs")]
public class When_registering_event_listeners
{
[Test]
public void Should_register_anonymous_event_listener()
{
// Arrange
var doctor = new SecurityDoctor();
Action<ISecurityEvent> eventListener = e => { };

// Act
doctor.RegisterListener(eventListener);

// Assert
Assert.That(doctor.Listeners.Cast<AnonymousSecurityEventListener>().Single().EventListener, Is.EqualTo(eventListener));
}

[Test]
public void Should_not_have_event_listeners_registered()
public void Should_register_multiple_anonymous_event_listener()
{
Assert.That(SecurityDoctor.Listeners, Is.Null);
// Arrange
var doctor = new SecurityDoctor();
Action<ISecurityEvent> eventListener1 = e => { };
Action<ISecurityEvent> eventListener2 = e => { };

// Act
doctor.RegisterListener(eventListener1);
doctor.RegisterListener(eventListener2);

// Assert
Assert.That(doctor.Listeners.Count(), Is.EqualTo(2));
Assert.That(doctor.Listeners.Cast<AnonymousSecurityEventListener>().First().EventListener, Is.EqualTo(eventListener1));
Assert.That(doctor.Listeners.Cast<AnonymousSecurityEventListener>().Last().EventListener, Is.EqualTo(eventListener2));
}

[Test]
public void Should_register_event_listener()
{
// Arrange
Action<ISecurityEvent> eventListener = e => {};

var doctor = new SecurityDoctor();
ISecurityEventListener eventListener = new TestSecurityEventListener();

// Act
SecurityDoctor.Register(eventListener);
doctor.RegisterListener(eventListener);

// Assert
Assert.That(SecurityDoctor.Listeners.Cast<AnonymousSecurityEventListener>().Single().EventListener, Is.EqualTo(eventListener));
Assert.That(doctor.Listeners.Single(), Is.EqualTo(eventListener));
}

[Test]
public void Should_register_multiple_event_listener()
{
// Arrange
var doctor = new SecurityDoctor();
ISecurityEventListener eventListener1 = new TestSecurityEventListener();
ISecurityEventListener eventListener2 = new AnonymousSecurityEventListener(e => { });

// Act
doctor.RegisterListener(eventListener1);
doctor.RegisterListener(eventListener2);

// Assert
Assert.That(doctor.Listeners.Count(), Is.EqualTo(2));
Assert.That(doctor.Listeners.First(), Is.EqualTo(eventListener1));
Assert.That(doctor.Listeners.Last(), Is.EqualTo(eventListener2));
}

[Test]
public void Should_register_anonymous_event_listener_using_the_static_method()
{
// Arrange
SecurityDoctor.Reset();
Action<ISecurityEvent> eventListener1 = e => {};
Action<ISecurityEvent> eventListener2 = e => {};
Action<ISecurityEvent> eventListener = e => { };

// Act
SecurityDoctor.Register(eventListener1);
SecurityDoctor.Register(eventListener2);
SecurityDoctor.Register(eventListener);

// Assert
Assert.That(SecurityDoctor.Listeners.Count(), Is.EqualTo(2));
Assert.That(SecurityDoctor.Listeners.Cast<AnonymousSecurityEventListener>().First().EventListener, Is.EqualTo(eventListener1));
Assert.That(SecurityDoctor.Listeners.Cast<AnonymousSecurityEventListener>().Last().EventListener, Is.EqualTo(eventListener2));
Assert.That(SecurityDoctor.Current.Listeners.Cast<AnonymousSecurityEventListener>().Single().EventListener, Is.EqualTo(eventListener));
}

[Test]
public void Should_register_event_listener_using_the_static_method()
{
// Arrange
SecurityDoctor.Reset();
ISecurityEventListener eventListener = new TestSecurityEventListener();

// Act
SecurityDoctor.Register(eventListener);

// Assert
Assert.That(SecurityDoctor.Current.Listeners.Single(), Is.EqualTo(eventListener));
}
}

[TestFixture]
[Category("SecurityDoctorSpecs")]
public class When_event_listeners_are_registered
public class When_resetting_security_doctor
{
[Test]
public void Should_reset_event_listeners()
public void Should_create_a_new_instance_of_SecurityDoctor()
{
// Arrange
SecurityDoctor.Register(e => {});
SecurityDoctor.Register(e => {});
SecurityDoctor.Register(e => {});
var securityDoctor = SecurityDoctor.Current;

// Act
SecurityDoctor.Reset();

// Assert
Assert.That(SecurityDoctor.Listeners, Is.Null);
Assert.That(SecurityDoctor.Current, Is.Not.EqualTo(securityDoctor));
}
}

Expand All @@ -83,13 +164,37 @@ public class When_scanning_for_event_listeners
public void Should_find_a_single_event_listener()
{
// Arrange
SecurityDoctor.Reset();
var doctor = new SecurityDoctor();

// Act
doctor.ScanForEventListeners();

// Assert
Assert.That(doctor.Listeners.Single(), Is.TypeOf<TestSecurityEventListener>());
}
}

[TestFixture]
[Category("SecurityDoctorSpecs")]
public class When_setting_eventlistener_scanner_setup
{
[Test]
public void Should_scan_using_the_specified_scanner()
{
// Arrange
var doctor = new SecurityDoctor();

var calledScannerSetup = false;
doctor.EventListenerScannerSetup = scanner =>
{
calledScannerSetup = true;
};

// Act
SecurityDoctor.ScanForEventListeners();
doctor.ScanForEventListeners();

// Assert
Assert.That(SecurityDoctor.Listeners.Single(), Is.TypeOf<TestSecurityEventListener>());
Assert.That(calledScannerSetup, Is.True);
}
}
}
34 changes: 34 additions & 0 deletions FluentSecurity.Specification/SecurityConfiguratorSpec.cs
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using FluentSecurity.Diagnostics;
using FluentSecurity.Policy;
using FluentSecurity.Specification.Helpers;
using FluentSecurity.Specification.TestData;
Expand Down Expand Up @@ -180,4 +181,37 @@ public void Should_have_1_policycontainer()
Assert.That(SecurityConfiguration.Current.PolicyContainers.First().ActionName, Is.EqualTo("Index"));
}
}

[TestFixture]
[Category("SecurityConfiguratorSpec")]
public class When_setting_eventlistener_scan_property
{
[Test]
public void Should_scan_for_eventlistners()
{
// Arrange
SecurityDoctor.Reset();
SecurityDoctor.Current.ScanForEventListenersOnConfigure = true;

// Act
SecurityConfigurator.Configure(configuration => {});

// Assert
Assert.That(SecurityDoctor.Current.ScannedForEventListeners, Is.True);
}

[Test]
public void Should_not_scan_for_eventlistners()
{
// Arrange
SecurityDoctor.Reset();
SecurityDoctor.Current.ScanForEventListenersOnConfigure = false;

// Act
SecurityConfigurator.Configure(configuration => {});

// Assert
Assert.That(SecurityDoctor.Current.ScannedForEventListeners, Is.False);
}
}
}
4 changes: 2 additions & 2 deletions FluentSecurity/Diagnostics/Publish.cs
Expand Up @@ -38,7 +38,7 @@ public static TResult ConfigurationEvent<TResult>(Func<TResult> action, Func<TRe

private static void PublishEvent<TEvent>(Func<TEvent> eventBuilder) where TEvent : ISecurityEvent
{
var listeners = SecurityDoctor.Listeners;
var listeners = SecurityDoctor.Current.Listeners;
if (listeners == null) return;

var @event = eventBuilder.Invoke();
Expand All @@ -49,7 +49,7 @@ public static TResult ConfigurationEvent<TResult>(Func<TResult> action, Func<TRe

private static TResult PublishEventWithTiming<TEvent, TResult>(Func<TResult> action, Func<TResult, TEvent> eventBuilder) where TEvent : SecurityEvent
{
var listeners = SecurityDoctor.Listeners;
var listeners = SecurityDoctor.Current.Listeners;
if (listeners == null) return action.Invoke();

var stopwatch = new Stopwatch();
Expand Down
57 changes: 43 additions & 14 deletions FluentSecurity/Diagnostics/SecurityDoctor.cs
Expand Up @@ -5,43 +5,72 @@

namespace FluentSecurity.Diagnostics
{
public static class SecurityDoctor
public class SecurityDoctor
{
public static SecurityDoctor Current { get; private set; }

static SecurityDoctor()
{
Reset();
}

public static void ScanForEventListeners()
public static void Register(Action<ISecurityEvent> eventListener)
{
Current.RegisterListener(eventListener);
}

public static void Register(ISecurityEventListener eventListener)
{
Current.RegisterListener(eventListener);
}

public static void Reset()
{
Current = new SecurityDoctor();
}

public bool ScannedForEventListeners { get; private set; }
public bool ScanForEventListenersOnConfigure { get; set; }
public Action<AssemblyScanner> EventListenerScannerSetup { get; set; }
internal IList<ISecurityEventListener> Listeners { get; private set; }

public SecurityDoctor()
{
Listeners = null;
ScanForEventListenersOnConfigure = true;
ScannedForEventListeners = false;
}

public void ScanForEventListeners()
{
var assemblyScanner = new AssemblyScanner();
assemblyScanner.AssembliesFromApplicationBaseDirectory();

if (EventListenerScannerSetup == null)
assemblyScanner.AssembliesFromApplicationBaseDirectory();
else
EventListenerScannerSetup.Invoke(assemblyScanner);

assemblyScanner.With<SecurityEventListenerScanner>();
var eventListeners = assemblyScanner.Scan();

foreach (var eventListenerType in eventListeners)
{
var eventListener = (ISecurityEventListener) Activator.CreateInstance(eventListenerType);
Register(eventListener);
RegisterListener(eventListener);
}
}

internal static IList<ISecurityEventListener> Listeners { get; private set; }
ScannedForEventListeners = true;
}

public static void Register(Action<ISecurityEvent> eventListener)
public void RegisterListener(Action<ISecurityEvent> eventListener)
{
Register(new AnonymousSecurityEventListener(eventListener));
RegisterListener(new AnonymousSecurityEventListener(eventListener));
}

public static void Register(ISecurityEventListener eventListener)
public void RegisterListener(ISecurityEventListener eventListener)
{
if (Listeners == null) Listeners = new List<ISecurityEventListener>();
Listeners.Add(eventListener);
}

public static void Reset()
{
Listeners = null;
}
}
}
5 changes: 4 additions & 1 deletion FluentSecurity/SecurityConfigurator.cs
Expand Up @@ -13,7 +13,7 @@ public static class SecurityConfigurator
static SecurityConfigurator()
{
CorrelationId = Guid.NewGuid();
SecurityDoctor.ScanForEventListeners();
SecurityDoctor.Reset();
}

public static ISecurityConfiguration Configure(Action<ConfigurationExpression> configurationExpression)
Expand All @@ -23,6 +23,9 @@ public static ISecurityConfiguration Configure(Action<ConfigurationExpression> c

Reset();

if (SecurityDoctor.Current.ScanForEventListenersOnConfigure)
SecurityDoctor.Current.ScanForEventListeners();

Publish.ConfigurationEvent(() => "Configuring FluentSecurity.");

lock (LockObject)
Expand Down

0 comments on commit 1a176ff

Please sign in to comment.