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

Function TestTokenPrioritizesActiveUserToken

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

Source from the content-addressed store, hash-verified

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