@@ -617,29 +617,48 @@ describe("StateMigrationService", () => {
617617 secureStorage . store . set ( `${ userId } _oktaToken` , "okta-token" ) ;
618618 } ) ;
619619
620- it ( "calls migrateKeytarPassword for each old {userId}_* key" , async ( ) => {
620+ it ( "does not call migrateKeytarPassword for keys that are already readable" , async ( ) => {
621+ // beforeEach seeds ldapPassword, entraIdKey, entraKey, oktaToken directly so
622+ // secureStorageService.get returns a value — migrateKeytarPassword should be skipped
623+ // for those keys.
621624 await svc . migrate ( ) ;
622625
623626 const calledKeys = passwords . migrateKeytarPassword . mock . calls . map (
624627 ( c : [ string , string ] ) => c [ 1 ] ,
625628 ) ;
626- expect ( calledKeys ) . toEqual (
627- expect . arrayContaining ( [
628- `${ userId } _ldapPassword` ,
629- `${ userId } _entraIdKey` ,
630- `${ userId } _entraKey` ,
631- `${ userId } _oktaToken` ,
632- ] ) ,
629+ expect ( calledKeys ) . not . toContain ( `${ userId } _ldapPassword` ) ;
630+ expect ( calledKeys ) . not . toContain ( `${ userId } _entraIdKey` ) ;
631+ expect ( calledKeys ) . not . toContain ( `${ userId } _oktaToken` ) ;
632+ } ) ;
633+
634+ it ( "calls migrateKeytarPassword when old key is unreadable (UTF-8 keytar blob)" , async ( ) => {
635+ // Remove the readable value to simulate a key that exists in the credential store
636+ // but cannot be decoded by desktop_core (UTF-8 blob).
637+ secureStorage . store . delete ( `${ userId } _ldapPassword` ) ;
638+
639+ await svc . migrate ( ) ;
640+
641+ const calledKeys = passwords . migrateKeytarPassword . mock . calls . map (
642+ ( c : [ string , string ] ) => c [ 1 ] ,
633643 ) ;
644+ expect ( calledKeys ) . toContain ( `${ userId } _ldapPassword` ) ;
634645 } ) ;
635646
636- it ( "copies old keys to flat keys via secureStorageService " , async ( ) => {
647+ it ( "copies old keys to flat keys when the flat key is absent " , async ( ) => {
637648 await svc . migrate ( ) ;
638649
639650 expect ( secureStorage . store . get ( SecureStorageKeys . ldap ) ) . toBe ( "ldap-pass" ) ;
640651 expect ( secureStorage . store . get ( SecureStorageKeys . okta ) ) . toBe ( "okta-token" ) ;
641652 } ) ;
642653
654+ it ( "does not overwrite a flat key that already has a current value" , async ( ) => {
655+ secureStorage . store . set ( SecureStorageKeys . ldap , "current-ldap-pass" ) ;
656+
657+ await svc . migrate ( ) ;
658+
659+ expect ( secureStorage . store . get ( SecureStorageKeys . ldap ) ) . toBe ( "current-ldap-pass" ) ;
660+ } ) ;
661+
643662 it ( "prefers _entraIdKey over _entraKey and keeps both old keys intact" , async ( ) => {
644663 await svc . migrate ( ) ;
645664
0 commit comments