Repository: cli/cli
File: internal/config/auth_config_test.go (Go)

Code before:
	require.Equal(t, "ghe.io", defaultHost)
	require.Equal(t, hostsKey, source)
}

func TestTokenWorksRightAfterMigration(t *testing.T) {
	// Given we have logged in before migration
	authCfg := newTestAuthConfig(t)
	_, err := preMigrationLogin(authCfg, "github.com", "test-user", "test-token", "ssh", false)
	require.NoError(t, err)

	// When we migrate
	var m migration.MultiAccount
	c := cfg{authCfg.cfg}
	require.NoError(t, c.Migrate(m))

	// Then we can still get the token correctly
	token, source := authCfg.ActiveToken("github.com")
	require.Equal(t, "test-token", token)
	require.Equal(t, oauthTokenKey, source)
}

func TestTokenPrioritizesActiveUserToken(t *testing.T) {
	// Given a keyring that contains a token for a host
	authCfg := newTestAuthConfig(t)
	require.NoError(t, keyring.Set(keyringServiceName("github.com"), "", "test-token"))
	require.NoError(t, keyring.Set(keyringServiceName("github.com"), "test-user1", "test-token1"))
	require.NoError(t, keyring.Set(keyringServiceName("github.com"), "test-user2", "test-token2"))

	// When we get the token from the auth config
	token, source := authCfg.ActiveToken("github.com")

	// Then it returns successfully with the correct token
	require.Equal(t, "keyring", source)
	require.Equal(t, "test-token", token)

	// When we set the active user to test-user1
	authCfg.cfg.Set([]string{hostsKey, "github.com", userKey}, "test-user1")

	// And get the token from the auth config
	token, source = authCfg.ActiveToken("github.com")

	// Then it returns successfully with the correct token
	require.Equal(t, "keyring", source)
	require.Equal(t, "test-token1", token)

	// When we set the active user to test-user2
	authCfg.cfg.Set([]string{hostsKey, "github.com", userKey}, "test-user2")

	// And get the token from the auth config
	token, source = authCfg.ActiveToken("github.com")


Review comment (suggestion):
This isn't _really_ representative of the most common case. In nearly all cases the token in the empty username keyring entry will match one of the username keyring entries. The previous test is much more likely to be the case.

Code after:
	require.Equal(t, "ghe.io", defaultHost)
	require.Equal(t, hostsKey, source)
}

func TestTokenWorksRightAfterMigration(t *testing.T) {
	// Given we have logged in before migration
	authCfg := newTestAuthConfig(t)
	_, err := preMigrationLogin(authCfg, "github.com", "test-user", "test-token", "ssh", false)
	require.NoError(t, err)

	// When we migrate
	var m migration.MultiAccount
	c := cfg{authCfg.cfg}
	require.NoError(t, c.Migrate(m))

	// Then we can still get the token correctly
	token, source := authCfg.ActiveToken("github.com")
	require.Equal(t, "test-token", token)
	require.Equal(t, oauthTokenKey, source)
}

func TestTokenPrioritizesActiveUserToken(t *testing.T) {
	// Given a keyring where the active slot contains the token from a previous user
	authCfg := newTestAuthConfig(t)
	require.NoError(t, keyring.Set(keyringServiceName("github.com"), "", "test-token"))
	require.NoError(t, keyring.Set(keyringServiceName("github.com"), "test-user1", "test-token"))
	require.NoError(t, keyring.Set(keyringServiceName("github.com"), "test-user2", "test-token2"))

	// When no active user is set
	authCfg.cfg.Remove([]string{hostsKey, "github.com", userKey})

	// And get the token from the auth config
	token, source := authCfg.ActiveToken("github.com")

	// Then it returns the token from the keyring active slot
	require.Equal(t, "keyring", source)
	require.Equal(t, "test-token", token)

	// When we set the active user to test-user1
	authCfg.cfg.Set([]string{hostsKey, "github.com", userKey}, "test-user1")

	// And get the token from the auth config
	token, source = authCfg.ActiveToken("github.com")

	// Then it returns the token from the active user entry in the keyring
	require.Equal(t, "keyring", source)
	require.Equal(t, "test-token", token)

	// When we set the active user to test-user2
	authCfg.cfg.Set([]string{hostsKey, "github.com", userKey}, "test-user2")