@@ -24,35 +24,35 @@ public class AddTrustedClientViewModelTests
2424 private Mock < IApplicationSettingsRepository > _appSettings = null ! ;
2525 private Mock < IPublicKeysTruster > _truster = null ! ;
2626 private Mock < ILogger < AddTrustedClientViewModel > > _logger = null ! ;
27-
27+
2828 private PublicKeyCheckData CreateCheckData ( )
2929 {
3030 var issuer = new PublicKeyInfo
3131 {
3232 ClientId = "OtherClient" ,
3333 PublicKey = Encoding . UTF8 . GetBytes ( "OTHER_PUBLIC_KEY" )
3434 } ;
35-
35+
3636 return new PublicKeyCheckData
3737 {
3838 IssuerPublicKeyInfo = issuer ,
3939 } ;
4040 }
41-
41+
4242 [ SetUp ]
4343 public void SetUp ( )
4444 {
4545 _publicKeysManager = new Mock < IPublicKeysManager > ( ) ;
4646 _appSettings = new Mock < IApplicationSettingsRepository > ( ) ;
4747 _truster = new Mock < IPublicKeysTruster > ( ) ;
4848 _logger = new Mock < ILogger < AddTrustedClientViewModel > > ( ) ;
49-
49+
5050 _appSettings . Setup ( a => a . GetCurrentApplicationSettings ( ) )
5151 . Returns ( new ApplicationSettings { ClientId = "MyClient" } ) ;
52-
52+
5353 _publicKeysManager . Setup ( m => m . GetMyPublicKeyInfo ( ) )
5454 . Returns ( new PublicKeyInfo { ClientId = "MyClient" , PublicKey = Encoding . UTF8 . GetBytes ( "MY_PUBLIC_KEY" ) } ) ;
55-
55+
5656 _publicKeysManager . Setup ( m => m . BuildTrustedPublicKey ( It . IsAny < PublicKeyCheckData > ( ) ) )
5757 . Returns ( ( PublicKeyCheckData p ) => new TrustedPublicKey
5858 {
@@ -61,177 +61,173 @@ public void SetUp()
6161 SafetyKey = new string ( '0' , 64 )
6262 } ) ;
6363 }
64-
64+
6565 private TrustDataParameters CreateTrustParams ( out PeerTrustProcessData peer , bool otherFinished , bool success )
6666 {
6767 peer = new PeerTrustProcessData ( "OtherClient" ) ;
6868 if ( otherFinished )
6969 {
7070 peer . SetOtherPartyChecked ( success ) ;
7171 }
72-
72+
7373 return new TrustDataParameters ( 0 , 2 , false , "S1" , peer ) ;
7474 }
75-
75+
76+ private TestableAddTrustedClientViewModel CreateVm ( PublicKeyCheckData check , TrustDataParameters trustParams )
77+ {
78+ return new TestableAddTrustedClientViewModel ( check , trustParams , _publicKeysManager . Object , _appSettings . Object ,
79+ _truster . Object , _logger . Object , null ! ) ;
80+ }
81+
7682 [ Test ]
7783 public async Task ValidateClient_Success_Should_Trust_And_Close ( )
7884 {
7985 var check = CreateCheckData ( ) ;
8086 var trustParams = CreateTrustParams ( out var peer , true , true ) ;
81-
82- var vm = new AddTrustedClientViewModel ( check , trustParams , _publicKeysManager . Object , _appSettings . Object ,
83- _truster . Object , _logger . Object , null ! )
84- {
85- Container = new FlyoutContainerViewModel { CanCloseCurrentFlyout = false }
86- } ;
87-
87+
88+ var vm = CreateVm ( check , trustParams ) ;
89+ vm . Container = new FlyoutContainerViewModel { CanCloseCurrentFlyout = false } ;
90+
8891 _truster . Setup ( t => t . OnPublicKeyValidationFinished ( It . IsAny < PublicKeyCheckData > ( ) , trustParams , true ) )
8992 . Returns ( async ( ) =>
9093 {
9194 peer . SetMyPartyChecked ( true ) ;
9295 await Task . CompletedTask ;
9396 } ) ;
94-
97+
9598 bool closeRequested = false ;
9699 vm . CloseFlyoutRequested += ( _ , _ ) => closeRequested = true ;
97-
100+
98101 await vm . ValidateClientCommand . Execute ( ) ;
99-
102+
100103 _publicKeysManager . Verify ( m => m . Trust ( It . IsAny < TrustedPublicKey > ( ) ) , Times . Once ) ;
101- vm . ShowSuccess . Should ( ) . BeFalse ( ) ; // after delay, it returns to false
104+ vm . ShowSuccess . Should ( ) . BeFalse ( ) ;
102105 vm . ShowError . Should ( ) . BeFalse ( ) ;
103106 vm . Container . CanCloseCurrentFlyout . Should ( ) . BeTrue ( ) ;
104107 closeRequested . Should ( ) . BeTrue ( ) ;
105108 }
106-
109+
107110 [ Test ]
108111 public async Task ValidateClient_Failure_Should_ShowError_And_Close ( )
109112 {
110113 var check = CreateCheckData ( ) ;
111114 var trustParams = CreateTrustParams ( out var peer , true , false ) ;
112-
113- var vm = new AddTrustedClientViewModel ( check , trustParams , _publicKeysManager . Object , _appSettings . Object ,
114- _truster . Object , _logger . Object , null ! )
115- {
116- Container = new FlyoutContainerViewModel { CanCloseCurrentFlyout = false }
117- } ;
118-
115+
116+ var vm = CreateVm ( check , trustParams ) ;
117+ vm . Container = new FlyoutContainerViewModel { CanCloseCurrentFlyout = false } ;
118+
119119 _truster . Setup ( t => t . OnPublicKeyValidationFinished ( It . IsAny < PublicKeyCheckData > ( ) , trustParams , true ) )
120120 . Returns ( async ( ) =>
121121 {
122122 peer . SetMyPartyChecked ( true ) ;
123123 await Task . CompletedTask ;
124124 } ) ;
125-
125+
126126 bool closeRequested = false ;
127127 vm . CloseFlyoutRequested += ( _ , _ ) => closeRequested = true ;
128-
128+
129129 await vm . ValidateClientCommand . Execute ( ) ;
130-
130+
131131 _publicKeysManager . Verify ( m => m . Trust ( It . IsAny < TrustedPublicKey > ( ) ) , Times . Never ) ;
132132 vm . ShowError . Should ( ) . BeFalse ( ) ;
133133 vm . Container . CanCloseCurrentFlyout . Should ( ) . BeTrue ( ) ;
134134 closeRequested . Should ( ) . BeTrue ( ) ;
135135 }
136-
136+
137137 [ Test ]
138138 public async Task RejectClient_Should_Call_Truster_Cancel_And_Close ( )
139139 {
140140 var check = CreateCheckData ( ) ;
141141 var trustParams = CreateTrustParams ( out _ , true , false ) ;
142-
143- var vm = new AddTrustedClientViewModel ( check , trustParams , _publicKeysManager . Object , _appSettings . Object ,
144- _truster . Object , _logger . Object , null ! )
145- {
146- Container = new FlyoutContainerViewModel { CanCloseCurrentFlyout = false }
147- } ;
148-
142+
143+ var vm = CreateVm ( check , trustParams ) ;
144+ vm . Container = new FlyoutContainerViewModel { CanCloseCurrentFlyout = false } ;
145+
149146 bool closeRequested = false ;
150147 vm . CloseFlyoutRequested += ( _ , _ ) => closeRequested = true ;
151-
148+
152149 await vm . RejectClientCommand . Execute ( ) ;
153-
150+
154151 _truster . Verify ( t => t . OnPublicKeyValidationFinished ( It . IsAny < PublicKeyCheckData > ( ) , trustParams , false ) , Times . Once ) ;
155152 _truster . Verify ( t => t . OnPublicKeyValidationCanceled ( It . IsAny < PublicKeyCheckData > ( ) , trustParams ) , Times . Once ) ;
156153 vm . Container . CanCloseCurrentFlyout . Should ( ) . BeTrue ( ) ;
157154 closeRequested . Should ( ) . BeTrue ( ) ;
158155 }
159-
156+
160157 [ Test ]
161158 public void EmptyConstructor_Should_Work_Fine ( )
162159 {
163160 var vm = new AddTrustedClientViewModel ( ) ;
164-
161+
165162 vm . Should ( ) . NotBeNull ( ) ;
166163 }
167-
164+
168165 [ Test ]
169166 public void OnDisplayed_Should_Disable_Flyout_Closing ( )
170167 {
171168 var check = CreateCheckData ( ) ;
172169 var trustParams = CreateTrustParams ( out _ , true , true ) ;
173-
174- var vm = new AddTrustedClientViewModel ( check , trustParams , _publicKeysManager . Object , _appSettings . Object ,
175- _truster . Object , _logger . Object , null ! )
176- {
177- Container = new FlyoutContainerViewModel { CanCloseCurrentFlyout = true }
178- } ;
179-
180- // Act
170+
171+ var vm = CreateVm ( check , trustParams ) ;
172+ vm . Container = new FlyoutContainerViewModel { CanCloseCurrentFlyout = true } ;
173+
181174 vm . OnDisplayed ( ) ;
182-
183- // Assert
175+
184176 vm . Container . CanCloseCurrentFlyout . Should ( ) . BeFalse ( ) ;
185177 }
186-
178+
187179 [ Test ]
188180 public void WhenActivated_Toggles_CanExecute_While_Command_IsExecuting ( )
189181 {
190182 var check = CreateCheckData ( ) ;
191183 var trustParams = CreateTrustParams ( out _ , true , true ) ;
192-
193- var vm = new AddTrustedClientViewModel ( check , trustParams , _publicKeysManager . Object , _appSettings . Object ,
194- _truster . Object , _logger . Object , null ! )
195- {
196- Container = new FlyoutContainerViewModel { CanCloseCurrentFlyout = false }
197- } ;
198-
199- // Arrange a long-running Cancel to keep IsExecuting = true
184+
185+ var vm = CreateVm ( check , trustParams ) ;
186+ vm . Container = new FlyoutContainerViewModel { CanCloseCurrentFlyout = false } ;
187+
200188 var tcs = new TaskCompletionSource ( ) ;
201189 _truster . Setup ( t => t . OnPublicKeyValidationCanceled ( It . IsAny < PublicKeyCheckData > ( ) , trustParams ) )
202190 . Returns ( tcs . Task ) ;
203-
204- // Activate to wire up WhenActivated subscriptions
191+
205192 vm . Activator . Activate ( ) ;
206-
193+
207194 bool canExecuteCopy = true ;
208195 using var sub = vm . CopyToClipboardCommand . CanExecute . Subscribe ( v => canExecuteCopy = v ) ;
209-
210- // Initially true
196+
211197 canExecuteCopy . Should ( ) . BeTrue ( ) ;
212-
213- // Start Cancel (this flips canRun to false while executing)
198+
214199 vm . CancelCommand . Execute ( ) . Subscribe ( ) ;
215-
216- // Wait until CanExecute becomes false
200+
217201 var sw = Stopwatch . StartNew ( ) ;
218202 while ( canExecuteCopy && sw . ElapsedMilliseconds < 1000 )
219203 {
220204 Thread . Sleep ( 10 ) ;
221205 }
222-
206+
223207 canExecuteCopy . Should ( ) . BeFalse ( ) ;
224-
225- // Complete the cancel to release IsExecuting
208+
226209 tcs . SetResult ( ) ;
227-
228- // Wait until CanExecute becomes true again
210+
229211 sw . Restart ( ) ;
230212 while ( ! canExecuteCopy && sw . ElapsedMilliseconds < 1000 )
231213 {
232214 Thread . Sleep ( 10 ) ;
233215 }
234-
216+
235217 canExecuteCopy . Should ( ) . BeTrue ( ) ;
236218 }
219+
220+ private class TestableAddTrustedClientViewModel : AddTrustedClientViewModel
221+ {
222+ public TestableAddTrustedClientViewModel ( PublicKeyCheckData ? publicKeyCheckData ,
223+ TrustDataParameters trustDataParameters , IPublicKeysManager publicKeysManager ,
224+ IApplicationSettingsRepository applicationSettingsManager , IPublicKeysTruster publicKeysTruster ,
225+ ILogger < AddTrustedClientViewModel > logger , Views . MainWindow mainWindow )
226+ : base ( publicKeyCheckData , trustDataParameters , publicKeysManager , applicationSettingsManager ,
227+ publicKeysTruster , logger , mainWindow )
228+ {
229+ }
230+
231+ protected override Task DelayAsync ( TimeSpan delay ) => Task . CompletedTask ;
232+ }
237233}
0 commit comments