1- namespace NServiceBus . Encryption . MessageProperty . AcceptanceTests
1+ namespace NServiceBus . Encryption . MessageProperty . AcceptanceTests ;
2+
3+ using System ;
4+ using System . Collections . Generic ;
5+ using System . Threading . Tasks ;
6+ using AcceptanceTesting ;
7+ using NUnit . Framework ;
8+
9+ public class When_using_Aes_with_custom : NServiceBusAcceptanceTest
210{
3- using System ;
4- using System . Collections . Generic ;
5- using System . Text ;
6- using System . Threading . Tasks ;
7- using AcceptanceTesting ;
8- using NUnit . Framework ;
9-
10- public class When_using_Aes_with_custom : NServiceBusAcceptanceTest
11+ [ Test ]
12+ public async Task Should_receive_decrypted_message ( )
1113 {
12- [ Test ]
13- public async Task Should_receive_decrypted_message ( )
14+ var messageToSend = new MessageWithSecretData
1415 {
15- var context = await Scenario . Define < Context > ( )
16- . WithEndpoint < Endpoint > ( b => b . When ( session => session . SendLocal ( new MessageWithSecretData
16+ Secret = "betcha can't guess my secret" ,
17+ SubProperty = new MySecretSubProperty { Secret = "My sub secret" } ,
18+ CreditCards =
19+ [
20+ new CreditCardDetails
21+ {
22+ ValidTo = DateTime . UtcNow . AddYears ( 1 ) ,
23+ Number = "312312312312312"
24+ } ,
25+ new CreditCardDetails
1726 {
18- Secret = "betcha can't guess my secret" ,
19- SubProperty = new MySecretSubProperty
20- {
21- Secret = "My sub secret"
22- } ,
23- CreditCards =
24- [
25- new CreditCardDetails
26- {
27- ValidTo = DateTime . UtcNow . AddYears ( 1 ) ,
28- Number = "312312312312312"
29- } ,
30- new CreditCardDetails
31- {
32- ValidTo = DateTime . UtcNow . AddYears ( 2 ) ,
33- Number = "543645546546456"
34- }
35- ]
36- } ) ) )
37- . Done ( c => c . GetTheMessage )
38- . Run ( ) ;
39-
40- Assert . AreEqual ( "betcha can't guess my secret" , context . Secret ) ;
41- Assert . AreEqual ( "My sub secret" , context . SubPropertySecret ) ;
42- CollectionAssert . AreEquivalent ( new List < string >
27+ ValidTo = DateTime . UtcNow . AddYears ( 2 ) ,
28+ Number = "543645546546456"
29+ }
30+ ]
31+ } ;
32+
33+ var context = await Scenario . Define < Context > ( )
34+ . WithEndpoint < Endpoint > ( b => b . When ( session => session . SendLocal ( messageToSend ) ) )
35+ . Done ( c => c . GetTheMessage )
36+ . Run ( ) ;
37+
38+ Assert . Multiple ( ( ) =>
39+ {
40+ Assert . That ( context . Secret , Is . EqualTo ( messageToSend . Secret . Value ) ) ;
41+ Assert . That ( context . SubPropertySecret , Is . EqualTo ( messageToSend . SubProperty . Secret . Value ) ) ;
42+ Assert . That ( context . CreditCards , Is . EquivalentTo ( new List < string > ( )
4343 {
4444 "312312312312312" ,
4545 "543645546546456"
46- } , context . CreditCards ) ;
47- }
46+ } ) ) ;
47+ } ) ;
48+ }
4849
49- public class Context : ScenarioContext
50- {
51- public bool GetTheMessage { get ; set ; }
50+ public class Context : ScenarioContext
51+ {
52+ public bool GetTheMessage { get ; set ; }
5253
53- public string Secret { get ; set ; }
54+ public string Secret { get ; set ; }
5455
55- public string SubPropertySecret { get ; set ; }
56+ public string SubPropertySecret { get ; set ; }
5657
57- public List < string > CreditCards { get ; set ; }
58- }
58+ public List < string > CreditCards { get ; set ; }
59+ }
5960
60- public class Endpoint : EndpointConfigurationBuilder
61+ public class Endpoint : EndpointConfigurationBuilder
62+ {
63+ public Endpoint ( )
6164 {
62- public Endpoint ( )
63- {
64- var keys = new Dictionary < string , byte [ ] >
65- {
66- { "1st" , Encoding . ASCII . GetBytes ( "gdDbqRpqdRbTs3mhdZh9qCaDaxJXl+e6" ) }
67- } ;
65+ var keys = new Dictionary < string , byte [ ] > { { "1st" , "gdDbqRpqdRbTs3mhdZh9qCaDaxJXl+e6"u8 . ToArray ( ) } } ;
6866
69- EndpointSetup < DefaultServer > ( builder => builder . EnableMessagePropertyEncryption ( new AesEncryptionService ( "1st" , keys ) ) ) ;
70- }
67+ EndpointSetup < DefaultServer > ( builder => builder . EnableMessagePropertyEncryption ( new AesEncryptionService ( "1st" , keys ) ) ) ;
68+ }
7169
72- public class Handler : IHandleMessages < MessageWithSecretData >
70+ public class Handler ( Context testContext ) : IHandleMessages < MessageWithSecretData >
71+ {
72+ public Task Handle ( MessageWithSecretData message , IMessageHandlerContext context )
7373 {
74- Context testContext ;
74+ testContext . Secret = message . Secret . Value ;
7575
76- public Handler ( Context testContext )
77- {
78- this . testContext = testContext ;
79- }
80-
81- public Task Handle ( MessageWithSecretData message , IMessageHandlerContext context )
82- {
83- testContext . Secret = message . Secret . Value ;
84-
85- testContext . SubPropertySecret = message . SubProperty . Secret . Value ;
76+ testContext . SubPropertySecret = message . SubProperty . Secret . Value ;
8677
87- testContext . CreditCards =
88- [
89- message . CreditCards [ 0 ] . Number . Value ,
90- message . CreditCards [ 1 ] . Number . Value
91- ] ;
78+ testContext . CreditCards =
79+ [
80+ message . CreditCards [ 0 ] . Number . Value ,
81+ message . CreditCards [ 1 ] . Number . Value
82+ ] ;
9283
93- testContext . GetTheMessage = true ;
84+ testContext . GetTheMessage = true ;
9485
95- return Task . FromResult ( 0 ) ;
96- }
86+ return Task . FromResult ( 0 ) ;
9787 }
9888 }
89+ }
9990
100- public class MessageWithSecretData : IMessage
101- {
102- public EncryptedString Secret { get ; set ; }
103- public MySecretSubProperty SubProperty { get ; set ; }
104- public List < CreditCardDetails > CreditCards { get ; set ; }
105- }
91+ public class MessageWithSecretData : IMessage
92+ {
93+ public EncryptedString Secret { get ; set ; }
94+ public MySecretSubProperty SubProperty { get ; set ; }
95+ public List < CreditCardDetails > CreditCards { get ; set ; }
96+ }
10697
107- public class CreditCardDetails
108- {
109- public DateTime ValidTo { get ; set ; }
110- public EncryptedString Number { get ; set ; }
111- }
98+ public class CreditCardDetails
99+ {
100+ public DateTime ValidTo { get ; set ; }
101+ public EncryptedString Number { get ; set ; }
102+ }
112103
113- public class MySecretSubProperty
114- {
115- public EncryptedString Secret { get ; set ; }
116- }
104+ public class MySecretSubProperty
105+ {
106+ public EncryptedString Secret { get ; set ; }
117107 }
118108}
0 commit comments