Skip to content

Commit

Permalink
resolve correct IAuthorizeSubscriptions
Browse files Browse the repository at this point in the history
fixes #3213
  • Loading branch information
SimonCropp committed Dec 14, 2015
1 parent 0f19136 commit f19702b
Show file tree
Hide file tree
Showing 6 changed files with 427 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
namespace NServiceBus.AcceptanceTests.Msmq
{
using System.Collections.Generic;
using NServiceBus.AcceptanceTesting;
using NServiceBus.AcceptanceTests.EndpointTemplates;
using NServiceBus.AcceptanceTests.ScenarioDescriptors;
using NServiceBus.Features;
using NServiceBus.MessageMutator;
using NServiceBus.Persistence.InMemory;
using NUnit.Framework;

public class When_publishing_with_authorizer : NServiceBusAcceptanceTest
{
[Test]
public void Should_only_deliver_to_authorized()
{
Scenario.Define<TestContext>()
.WithEndpoint<Publisher>(b =>
b.When(c => c.Subscriber1Subscribed && c.Subscriber2Subscribed, (bus, c) => bus.Publish(new MyEvent()))
)
.WithEndpoint<Subscriber1>(b => b.When(bus =>
{
bus.Subscribe<MyEvent>();
}))
.WithEndpoint<Subscriber2>(b => b.When(bus =>
{
bus.Subscribe<MyEvent>();
}))
.Done(c =>
c.Subscriber1GotTheEvent &&
c.DeclinedSubscriber2)
.Repeat(r => r.For(Transports.Msmq))
.Should(c =>
{
Assert.True(c.Subscriber1GotTheEvent);
Assert.False(c.Subscriber2GotTheEvent);
})
.Run();
}

public class TestContext : ScenarioContext
{
public bool Subscriber1GotTheEvent { get; set; }
public bool Subscriber2GotTheEvent { get; set; }
public bool Subscriber1Subscribed { get; set; }
public bool Subscriber2Subscribed { get; set; }
public bool DeclinedSubscriber2 { get; set; }
}

class Publisher : EndpointConfigurationBuilder
{
public Publisher()
{
EndpointSetup<DefaultServer>(b =>
{
// InMemoryPersistence.UseAsDefault();
Configure.Features.Disable<AutoSubscribe>();
Configure.Component<MyTransportMessageMutator>(DependencyLifecycle.InstancePerCall);
});
}

class MyTransportMessageMutator : IMutateIncomingTransportMessages
{
TestContext context;

public MyTransportMessageMutator(TestContext context)
{
this.context = context;
}

public void MutateIncoming(TransportMessage transportMessage)
{
if (transportMessage.MessageIntent == MessageIntentEnum.Subscribe)
{
var originatingEndpoint = transportMessage.Headers[Headers.OriginatingEndpoint];
if (originatingEndpoint.Contains("Subscriber1"))
{
context.Subscriber1Subscribed = true;
}
if (originatingEndpoint.Contains("Subscriber2"))
{
context.Subscriber2Subscribed = true;
}
}
}
}

public class SubscriptionAuthorizer : IAuthorizeSubscriptions
{
TestContext context;

public SubscriptionAuthorizer(TestContext context)
{
this.context = context;
}

public bool AuthorizeSubscribe(string messageType, string clientEndpoint, IDictionary<string, string> headers)
{
var isFromSubscriber1 = headers[Headers.OriginatingEndpoint]
.Contains("Subscriber1");
if (!isFromSubscriber1)
{
context.DeclinedSubscriber2 = true;
}
return isFromSubscriber1;
}

public bool AuthorizeUnsubscribe(string messageType, string clientEndpoint, IDictionary<string, string> headers)
{
return true;
}
}
}

public class Subscriber1 : EndpointConfigurationBuilder
{
public Subscriber1()
{
EndpointSetup<DefaultServer>(c =>
{
InMemoryPersistence.UseAsDefault();
Configure.Features.Disable<AutoSubscribe>();
})
.AddMapping<MyEvent>(typeof(Publisher));
}

public class MyEventHandler : IHandleMessages<MyEvent>
{
TestContext context;

public MyEventHandler(TestContext context)
{
this.context = context;
}

public void Handle(MyEvent message)
{
context.Subscriber1GotTheEvent = true;
}
}


}

public class Subscriber2 : EndpointConfigurationBuilder
{
public Subscriber2()
{
EndpointSetup<DefaultServer>(c =>
{
InMemoryPersistence.UseAsDefault();
Configure.Features.Disable<AutoSubscribe>();
})
.AddMapping<MyEvent>(typeof(Publisher));
}

public class MyEventHandler : IHandleMessages<MyEvent>
{
TestContext context;

public MyEventHandler(TestContext context)
{
this.context = context;
}

public void Handle(MyEvent messageThatIsEnlisted)
{
context.Subscriber2GotTheEvent = true;
}
}
}

public class MyEvent : IEvent
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
namespace NServiceBus.AcceptanceTests.Msmq
{
using System.Collections.Generic;
using NServiceBus.AcceptanceTesting;
using NServiceBus.AcceptanceTests.EndpointTemplates;
using NServiceBus.AcceptanceTests.ScenarioDescriptors;
using NServiceBus.Features;
using NServiceBus.MessageMutator;
using NServiceBus.Persistence.InMemory;
using NUnit.Framework;

public class When_unsubscribing_with_authorizer : NServiceBusAcceptanceTest
{

[Test]
public void Should_ignore_unsubscribe()
{
Scenario.Define<TestContext>()
.WithEndpoint<Publisher>(b =>
b.When(c => c.Subscribed, (bus, c) =>
{
bus.Publish(new MyEvent());
}).When(c => c.SubscriberEventCount == 1, (bus, c) =>
{
bus.Publish(new MyEvent());
})
)
.WithEndpoint<Subscriber>(b => b.When(c => c.PublisherStarted, bus =>
{
bus.Subscribe<MyEvent>();
}))
.Done(c =>
c.SubscriberEventCount == 2 &&
c.DeclinedUnSubscribe)
.Repeat(r => r.For(Transports.Msmq))
.Run();
}

public class TestContext : ScenarioContext
{
public int SubscriberEventCount { get; set; }
public bool UnsubscribeAttempted { get; set; }
public bool DeclinedUnSubscribe { get; set; }
public bool Subscribed { get; set; }
public bool PublisherStarted { get; set; }
}

class Publisher : EndpointConfigurationBuilder
{
public Publisher()
{
EndpointSetup<DefaultServer>(b =>
{
InMemoryPersistence.UseAsDefault();
Configure.Features.Disable<AutoSubscribe>();
Configure.Component<MyTransportMessageMutator>(DependencyLifecycle.InstancePerCall);
});
}
class MyTransportMessageMutator : IMutateIncomingTransportMessages
{
TestContext context;

public MyTransportMessageMutator(TestContext context)
{
this.context = context;
}

public void MutateIncoming(TransportMessage transportMessage)
{
if (transportMessage.MessageIntent == MessageIntentEnum.Subscribe)
{
var originatingEndpoint = transportMessage.Headers[Headers.OriginatingEndpoint];
if (originatingEndpoint.Contains("Subscriber"))
{
context.Subscribed = true;
}
}
}
}

public class CaptureStarted : IWantToRunWhenBusStartsAndStops
{
TestContext context;

public CaptureStarted(TestContext context)
{
this.context = context;
}

public void Start()
{
context.PublisherStarted = true;
}

public void Stop()
{
}
}

public class SubscriptionAuthorizer : IAuthorizeSubscriptions
{
TestContext context;

public SubscriptionAuthorizer(TestContext context)
{
this.context = context;
}

public bool AuthorizeSubscribe(string messageType, string clientEndpoint, IDictionary<string, string> headers)
{
return true;
}

public bool AuthorizeUnsubscribe(string messageType, string clientEndpoint, IDictionary<string, string> headers)
{
context.DeclinedUnSubscribe = true;
return false;
}
}
}

public class Subscriber : EndpointConfigurationBuilder
{
public Subscriber()
{
EndpointSetup<DefaultServer>(c =>
{
InMemoryPersistence.UseAsDefault();
Configure.Features.Disable<AutoSubscribe>();
})
.AddMapping<MyEvent>(typeof(Publisher));
}

public class MyEventHandler : IHandleMessages<MyEvent>
{
IBus bus;
TestContext context;

public MyEventHandler(IBus bus, TestContext context)
{
this.bus = bus;
this.context = context;
}

public void Handle(MyEvent message)
{
context.SubscriberEventCount++;
bus.Unsubscribe<MyEvent>();
}
}
}

public class MyEvent : IEvent
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@
</ItemGroup>
<ItemGroup>
<Compile Include="BasicMessaging\When_starting_a_send_only.cs" />
<Compile Include="Msmq\When_publishing_with_authorizer.cs" />
<Compile Include="Msmq\When_unsubscribing_with_authorizer.cs" />
<Compile Include="Msmq\When_TimeToBeReceivedOnForwardedMessages_set_and_transactional_Msmq.cs" />
<Compile Include="Msmq\When_Audit_OverrideTimeToBeReceived_set_and_transactional_Msmq.cs" />
<Compile Include="Msmq\When_TimeToBeReceived_set_and_DTC_Msmq.cs" />
Expand Down
1 change: 1 addition & 0 deletions src/NServiceBus.Core.Tests/NServiceBus.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
<Compile Include="AttributeTests.cs" />
<Compile Include="Encryption\RijndaelEncryptionServiceConfigValidationsTests.cs" />
<Compile Include="Msmq\TimeToBeRceivedOverrideCheckerTest.cs" />
<Compile Include="UnicastBusAuthorizationTests.cs" />
<Compile Include="Utils\ExceptionHeaderHelperTests.cs" />
<Compile Include="Hosting\When_creating_host_information_from_environment.cs" />
<Compile Include="SetLoggingLibraryTests.cs" />
Expand Down
Loading

0 comments on commit f19702b

Please sign in to comment.