-
Notifications
You must be signed in to change notification settings - Fork 647
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
resolve correct IAuthorizeSubscriptions
fixes #3213
- Loading branch information
1 parent
0f19136
commit f19702b
Showing
6 changed files
with
427 additions
and
4 deletions.
There are no files selected for viewing
177 changes: 177 additions & 0 deletions
177
src/NServiceBus.AcceptanceTests/Msmq/When_publishing_with_authorizer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} | ||
} | ||
} |
157 changes: 157 additions & 0 deletions
157
src/NServiceBus.AcceptanceTests/Msmq/When_unsubscribing_with_authorizer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.