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

Function TestLogoutRemovesAllUserCredentialRecords

internal/config/refreshable_test.go:328–370  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

326}
327
328func TestLogoutRemovesAllUserCredentialRecords(t *testing.T) {
329 // This populates every credential representation the write paths can produce for the user at once: a non-expiring
330 // token and a refreshable credential, in both the keyring and the config file. The non-expiring token also has a
331 // host active ("") slot, which refreshable credentials do not use. That combined state is not realistic - the
332 // write paths keep the storage forms mutually exclusive - but populating it lets the test prove that Logout leaves
333 // nothing behind for the user on the host.
334 authCfg := newTestAuthConfig(t)
335
336 credential := gh.Credential{Token: "access", RefreshToken: "refresh"}
337
338 // Non-expiring tokens in the keyring (per-user and host active slot).
339 require.NoError(t, keyring.Set(keyringServiceName("github.com"), "test-user", "keyring-user-token"))
340 require.NoError(t, keyring.Set(keyringServiceName("github.com"), "", "keyring-active-token"))
341
342 // Refreshable credential in the keyring (per-user slot only).
343 setRefreshableKeyring(t, "github.com", "test-user", credential)
344
345 // Non-expiring tokens in the config (per-user and host active slot).
346 authCfg.cfg.Set([]string{hostsKey, "github.com", usersKey, "test-user", oauthTokenKey}, "config-user-token")
347 authCfg.cfg.Set([]string{hostsKey, "github.com", oauthTokenKey}, "config-active-token")
348
349 // Refreshable credential in the config (per-user slot only).
350 setRefreshableConfigField(t, authCfg, "github.com", "test-user", "access_token", "access")
351 setRefreshableConfigField(t, authCfg, "github.com", "test-user", "refresh_token", "refresh")
352
353 authCfg.cfg.Set([]string{hostsKey, "github.com", userKey}, "test-user")
354
355 require.NoError(t, authCfg.Logout("github.com", "test-user"))
356
357 // Every non-expiring keyring record for the host, per-user and host active, is gone.
358 for _, user := range []string{"test-user", ""} {
359 _, err := keyring.Get(keyringServiceName("github.com"), user)
360 require.Error(t, err)
361 }
362 // The per-user refreshable keyring record is gone.
363 _, err := keyring.Get(refreshableServiceName("github.com"), "test-user")
364 require.Error(t, err)
365
366 // The whole host entry, and therefore every config-stored credential, is gone.
367 _, err = authCfg.cfg.Get([]string{hostsKey, "github.com"})
368 require.Error(t, err)
369 require.Empty(t, authCfg.UsersForHost("github.com"))
370}
371
372func TestLogoutAcquiresRefreshLock(t *testing.T) {
373 // Logout must run under the same lock the refresh path uses so it wins any race with an in-flight refresh. Pin

Callers

nothing calls this directly

Calls 13

SetFunction · 0.92
GetFunction · 0.92
newTestAuthConfigFunction · 0.85
setRefreshableKeyringFunction · 0.85
refreshableServiceNameFunction · 0.85
EmptyMethod · 0.80
keyringServiceNameFunction · 0.70
SetMethod · 0.65
LogoutMethod · 0.65
GetMethod · 0.65
UsersForHostMethod · 0.65

Tested by

no test coverage detected