@@ -18,7 +18,7 @@ public class KeychainTests
18
18
private static readonly SubstituteFactory SubstituteFactory = new SubstituteFactory ( ) ;
19
19
20
20
[ Test ]
21
- public void Should_Initialize_When_Cache_Does_Not_Exist ( )
21
+ public void ShouldInitializeWhenCacheDoesNotExist ( )
22
22
{
23
23
const string connectionsCachePath = @"c:\UserCachePath\" ;
24
24
@@ -52,7 +52,7 @@ public void Should_Initialize_When_Cache_Does_Not_Exist()
52
52
}
53
53
54
54
[ Test ]
55
- public void Should_Initialize_When_Cache_Invalid ( )
55
+ public void ShouldInitializeWhenCacheInvalid ( )
56
56
{
57
57
const string connectionsCachePath = @"c:\UserCachePath\" ;
58
58
const string connectionsCacheFile = @"c:\UserCachePath\connections.json" ;
@@ -92,7 +92,7 @@ public void Should_Initialize_When_Cache_Invalid()
92
92
}
93
93
94
94
[ Test ]
95
- public void Should_Initialize_When_Cache_Exists ( )
95
+ public void ShouldInitializeWhenCacheExists ( )
96
96
{
97
97
const string connectionsCachePath = @"c:\UserCachePath\" ;
98
98
const string connectionsCacheFile = @"c:\UserCachePath\connections.json" ;
@@ -134,7 +134,7 @@ public void Should_Initialize_When_Cache_Exists()
134
134
}
135
135
136
136
[ Test ]
137
- public void Should_Load_From_ConnectionManager ( )
137
+ public void ShouldLoadFromConnectionManager ( )
138
138
{
139
139
const string connectionsCachePath = @"c:\UserCachePath\" ;
140
140
const string connectionsCacheFile = @"c:\UserCachePath\connections.json" ;
@@ -194,7 +194,7 @@ public void Should_Load_From_ConnectionManager()
194
194
}
195
195
196
196
[ Test ]
197
- public void Should_Delete_From_Cache_When_Load_Returns_Null_From_ConnectionManager ( )
197
+ public void ShouldDeleteFromCacheWhenLoadReturnsNullFromConnectionManager ( )
198
198
{
199
199
const string connectionsCachePath = @"c:\UserCachePath\" ;
200
200
const string connectionsCacheFile = @"c:\UserCachePath\connections.json" ;
@@ -248,7 +248,82 @@ public void Should_Delete_From_Cache_When_Load_Returns_Null_From_ConnectionManag
248
248
}
249
249
250
250
[ Test ]
251
- public void Should_Connect_Set_Credentials_Token_And_Save ( )
251
+ public void ShouldDeleteFromCacheWhenLoadReturnsNullFromConnectionManagerDueToUserMismatch ( )
252
+ {
253
+ const string connectionsCachePath = @"c:\UserCachePath\" ;
254
+ const string connectionsCacheFile = @"c:\UserCachePath\connections.json" ;
255
+
256
+ const string cachedUsername = "SomeCachedUser" ;
257
+ const string credentialedUsername = "SomeCredentialedUser" ;
258
+
259
+ const string token = "SomeToken" ;
260
+
261
+ var hostUri = new UriString ( "https://github.com/" ) ;
262
+
263
+ var fileSystem = SubstituteFactory . CreateFileSystem ( new CreateFileSystemOptions
264
+ {
265
+ FilesThatExist = new List < string > { connectionsCacheFile } ,
266
+ FileContents = new Dictionary < string , IList < string > > {
267
+ { connectionsCacheFile , new List < string > { $@ "[{{""Host"":""https://github.com/"",""Username"":""{ cachedUsername } ""}}]"
268
+ } }
269
+ }
270
+ } ) ;
271
+
272
+ NPath . FileSystem = fileSystem ;
273
+
274
+ var environment = SubstituteFactory . CreateEnvironment ( ) ;
275
+ environment . UserCachePath . Returns ( info => connectionsCachePath . ToNPath ( ) ) ;
276
+ environment . FileSystem . Returns ( fileSystem ) ;
277
+
278
+ var credentialManager = Substitute . For < ICredentialManager > ( ) ;
279
+ credentialManager . Load ( hostUri ) . Returns ( info =>
280
+ {
281
+ var credential = Substitute . For < ICredential > ( ) ;
282
+ credential . Username . Returns ( credentialedUsername ) ;
283
+ credential . Token . Returns ( token ) ;
284
+ credential . Host . Returns ( hostUri ) ;
285
+ return TaskEx . FromResult ( credential ) ;
286
+ } ) ;
287
+
288
+ var keychain = new Keychain ( environment , credentialManager ) ;
289
+ keychain . Initialize ( ) ;
290
+
291
+ fileSystem . Received ( 1 ) . FileExists ( connectionsCacheFile ) ;
292
+ fileSystem . DidNotReceive ( ) . FileDelete ( Args . String ) ;
293
+ fileSystem . Received ( 1 ) . ReadAllText ( connectionsCacheFile ) ;
294
+ fileSystem . DidNotReceive ( ) . ReadAllLines ( Args . String ) ;
295
+ fileSystem . DidNotReceive ( ) . WriteAllText ( Args . String , Args . String ) ;
296
+ fileSystem . DidNotReceive ( ) . WriteAllLines ( Args . String , Arg . Any < string [ ] > ( ) ) ;
297
+
298
+ credentialManager . DidNotReceive ( ) . Load ( Args . UriString ) ;
299
+ credentialManager . DidNotReceive ( ) . HasCredentials ( ) ;
300
+ credentialManager . DidNotReceive ( ) . Delete ( Args . UriString ) ;
301
+ credentialManager . DidNotReceive ( ) . Save ( Arg . Any < ICredential > ( ) ) ;
302
+
303
+ fileSystem . ClearReceivedCalls ( ) ;
304
+
305
+ var uriString = keychain . Connections . FirstOrDefault ( ) ;
306
+ var keychainAdapter = keychain . Load ( uriString ) . Result ;
307
+ keychainAdapter . Credential . Should ( ) . BeNull ( ) ;
308
+
309
+ keychainAdapter . OctokitCredentials . AuthenticationType . Should ( ) . Be ( AuthenticationType . Anonymous ) ;
310
+ keychainAdapter . OctokitCredentials . Login . Should ( ) . BeNull ( ) ;
311
+ keychainAdapter . OctokitCredentials . Password . Should ( ) . BeNull ( ) ;
312
+
313
+ fileSystem . DidNotReceive ( ) . FileExists ( Args . String ) ;
314
+ fileSystem . DidNotReceive ( ) . ReadAllText ( Args . String ) ;
315
+ fileSystem . DidNotReceive ( ) . FileDelete ( Args . String ) ;
316
+ fileSystem . Received ( 1 ) . WriteAllText ( connectionsCacheFile , "[]" ) ;
317
+ fileSystem . DidNotReceive ( ) . WriteAllLines ( Args . String , Arg . Any < string [ ] > ( ) ) ;
318
+
319
+ credentialManager . Received ( 1 ) . Load ( hostUri ) ;
320
+ credentialManager . DidNotReceive ( ) . HasCredentials ( ) ;
321
+ credentialManager . DidNotReceive ( ) . Delete ( Args . UriString ) ;
322
+ credentialManager . DidNotReceive ( ) . Save ( Arg . Any < ICredential > ( ) ) ;
323
+ }
324
+
325
+ [ Test ]
326
+ public void ShouldConnectSetCredentialsTokenAndSave ( )
252
327
{
253
328
const string connectionsCachePath = @"c:\UserCachePath\" ;
254
329
const string connectionsCacheFile = @"c:\UserCachePath\connections.json" ;
@@ -334,7 +409,7 @@ public void Should_Connect_Set_Credentials_Token_And_Save()
334
409
}
335
410
336
411
[ Test ]
337
- public void Should_Connect_Set_Credentials_And_Clear ( )
412
+ public void ShouldConnectSetCredentialsAndClear ( )
338
413
{
339
414
const string connectionsCachePath = @"c:\UserCachePath\" ;
340
415
const string connectionsCacheFile = @"c:\UserCachePath\connections.json" ;
0 commit comments