MCPcopy Create free account
hub / github.com/cli/cli / TestTokenPrioritizesActiveUserToken

Function TestTokenPrioritizesActiveUserToken

internal/config/auth_config_test.go:790–829  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

788}
789
790func TestTokenPrioritizesActiveUserToken(t *testing.T) {
791 // Given a keyring where the active slot contains the token from a previous user
792 authCfg := newTestAuthConfig(t)
793 require.NoError(t, keyring.Set(keyringServiceName("github.com"), "", "test-token"))
794 require.NoError(t, keyring.Set(keyringServiceName("github.com"), "test-user1", "test-token"))
795 require.NoError(t, keyring.Set(keyringServiceName("github.com"), "test-user2", "test-token2"))
796
797 // When no active user is set
798 authCfg.cfg.Remove([]string{hostsKey, "github.com", userKey})
799
800 // And get the token from the auth config
801 cred := authCfg.ActiveToken("github.com")
802 token, source := cred.Token, cred.Source
803
804 // Then it returns the token from the keyring active slot
805 require.Equal(t, "keyring", source)
806 require.Equal(t, "test-token", token)
807
808 // When we set the active user to test-user1
809 authCfg.cfg.Set([]string{hostsKey, "github.com", userKey}, "test-user1")
810
811 // And get the token from the auth config
812 cred = authCfg.ActiveToken("github.com")
813 token, source = cred.Token, cred.Source
814
815 // Then it returns the token from the active user entry in the keyring
816 require.Equal(t, "keyring", source)
817 require.Equal(t, "test-token", token)
818
819 // When we set the active user to test-user2
820 authCfg.cfg.Set([]string{hostsKey, "github.com", userKey}, "test-user2")
821
822 // And get the token from the auth config
823 cred = authCfg.ActiveToken("github.com")
824 token, source = cred.Token, cred.Source
825
826 // Then it returns the token from the active user entry in the keyring
827 require.Equal(t, "keyring", source)
828 require.Equal(t, "test-token2", token)
829}
830
831// TestActiveTokenPrefersPerUserConfigTokenOverUnkeyedKeyring pins an intentional behavior change introduced with the
832// refreshable-credential groundwork. ActiveToken now resolves the active user through TokenForUser, which consults the

Callers

nothing calls this directly

Calls 7

SetFunction · 0.92
newTestAuthConfigFunction · 0.85
EqualMethod · 0.80
keyringServiceNameFunction · 0.70
RemoveMethod · 0.65
ActiveTokenMethod · 0.65
SetMethod · 0.65

Tested by

no test coverage detected