@@ -247,6 +247,81 @@ public void Should_Delete_From_Cache_When_Load_Returns_Null_From_ConnectionManag
247247 credentialManager . DidNotReceive ( ) . Save ( Arg . Any < ICredential > ( ) ) ;
248248 }
249249
250+ [ Test ]
251+ public void Should_Delete_From_Cache_When_Load_Returns_Null_From_ConnectionManager_Due_To_User_Mismatch ( )
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+
250325 [ Test ]
251326 public void Should_Connect_Set_Credentials_Token_And_Save ( )
252327 {
0 commit comments