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

Function TestConfigWithTokenRefresher

pkg/cmd/factory/default_test.go:499–550  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

497}
498
499func TestConfigWithTokenRefresher(t *testing.T) {
500 t.Run("injects a refresher once across multiple config resolutions", func(t *testing.T) {
501 authCfg := &recordingAuthConfig{}
502 cfg := &ghmock.ConfigMock{AuthenticationFunc: func() gh.AuthConfig { return authCfg }}
503 plainCalls := 0
504 plainClientFunc := func() (*http.Client, error) {
505 plainCalls++
506 return &http.Client{}, nil
507 }
508
509 wrapped := configWithTokenRefresher(func() (gh.Config, error) { return cfg, nil }, plainClientFunc)
510
511 got, err := wrapped()
512 require.NoError(t, err)
513 require.Same(t, cfg, got)
514 require.Equal(t, 1, authCfg.setCalls)
515 require.NotNil(t, authCfg.lastRefresher)
516
517 // A second resolution reuses the already-injected refresher rather than building another.
518 _, err = wrapped()
519 require.NoError(t, err)
520 require.Equal(t, 1, authCfg.setCalls)
521 require.Equal(t, 1, plainCalls)
522 })
523
524 t.Run("skips injection when the plain client cannot be built", func(t *testing.T) {
525 authCfg := &recordingAuthConfig{}
526 cfg := &ghmock.ConfigMock{AuthenticationFunc: func() gh.AuthConfig { return authCfg }}
527 plainClientFunc := func() (*http.Client, error) { return nil, errors.New("no client") }
528
529 wrapped := configWithTokenRefresher(func() (gh.Config, error) { return cfg, nil }, plainClientFunc)
530
531 got, err := wrapped()
532 require.NoError(t, err)
533 require.Same(t, cfg, got)
534 require.Equal(t, 0, authCfg.setCalls)
535 })
536
537 t.Run("propagates a configuration error without injecting", func(t *testing.T) {
538 plainCalls := 0
539 plainClientFunc := func() (*http.Client, error) {
540 plainCalls++
541 return &http.Client{}, nil
542 }
543
544 wrapped := configWithTokenRefresher(func() (gh.Config, error) { return nil, errors.New("config boom") }, plainClientFunc)
545
546 _, err := wrapped()
547 require.EqualError(t, err, "config boom")
548 require.Equal(t, 0, plainCalls)
549 })
550}

Callers

nothing calls this directly

Calls 3

configWithTokenRefresherFunction · 0.85
EqualMethod · 0.80
RunMethod · 0.65

Tested by

no test coverage detected