@@ -769,6 +769,62 @@ func TestTokenWorksRightAfterMigration(t *testing.T) {
769
769
require .Equal (t , oauthTokenKey , source )
770
770
}
771
771
772
+ func TestTokenPrioritizesActiveUserToken (t * testing.T ) {
773
+ // Given a keyring where the active slot contains the token from a previous user
774
+ authCfg := newTestAuthConfig (t )
775
+ require .NoError (t , keyring .Set (keyringServiceName ("github.com" ), "" , "test-token" ))
776
+ require .NoError (t , keyring .Set (keyringServiceName ("github.com" ), "test-user1" , "test-token" ))
777
+ require .NoError (t , keyring .Set (keyringServiceName ("github.com" ), "test-user2" , "test-token2" ))
778
+
779
+ // When no active user is set
780
+ authCfg .cfg .Remove ([]string {hostsKey , "github.com" , userKey })
781
+
782
+ // And get the token from the auth config
783
+ token , source := authCfg .ActiveToken ("github.com" )
784
+
785
+ // Then it returns the token from the keyring active slot
786
+ require .Equal (t , "keyring" , source )
787
+ require .Equal (t , "test-token" , token )
788
+
789
+ // When we set the active user to test-user1
790
+ authCfg .cfg .Set ([]string {hostsKey , "github.com" , userKey }, "test-user1" )
791
+
792
+ // And get the token from the auth config
793
+ token , source = authCfg .ActiveToken ("github.com" )
794
+
795
+ // Then it returns the token from the active user entry in the keyring
796
+ require .Equal (t , "keyring" , source )
797
+ require .Equal (t , "test-token" , token )
798
+
799
+ // When we set the active user to test-user2
800
+ authCfg .cfg .Set ([]string {hostsKey , "github.com" , userKey }, "test-user2" )
801
+
802
+ // And get the token from the auth config
803
+ token , source = authCfg .ActiveToken ("github.com" )
804
+
805
+ // Then it returns the token from the active user entry in the keyring
806
+ require .Equal (t , "keyring" , source )
807
+ require .Equal (t , "test-token2" , token )
808
+ }
809
+
810
+ func TestTokenWithActiveUserNotInKeyringFallsBackToBlank (t * testing.T ) {
811
+ // Given a keyring that contains a token for a host
812
+ authCfg := newTestAuthConfig (t )
813
+ require .NoError (t , keyring .Set (keyringServiceName ("github.com" ), "" , "test-token" ))
814
+ require .NoError (t , keyring .Set (keyringServiceName ("github.com" ), "test-user1" , "test-token1" ))
815
+ require .NoError (t , keyring .Set (keyringServiceName ("github.com" ), "test-user2" , "test-token2" ))
816
+
817
+ // When we set the active user to test-user3
818
+ authCfg .cfg .Set ([]string {hostsKey , "github.com" , userKey }, "test-user3" )
819
+
820
+ // And get the token from the auth config
821
+ token , source := authCfg .ActiveToken ("github.com" )
822
+
823
+ // Then it returns successfully with the fallback token
824
+ require .Equal (t , "keyring" , source )
825
+ require .Equal (t , "test-token" , token )
826
+ }
827
+
772
828
func TestLogoutRightAfterMigrationRemovesHost (t * testing.T ) {
773
829
// Given we have logged in before migration
774
830
authCfg := newTestAuthConfig (t )
0 commit comments