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

Function TestConfigWithTokenRefresher

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

Source from the content-addressed store, hash-verified

558}
559
560func TestConfigWithTokenRefresher(t *testing.T) {
561 t.Run("injects a refresher once across multiple config resolutions", func(t *testing.T) {
562 authCfg := &recordingAuthConfig{}
563 cfg := &ghmock.ConfigMock{AuthenticationFunc: func() gh.AuthConfig { return authCfg }}
564 plainCalls := 0
565 plainClientFunc := func() (*http.Client, error) {
566 plainCalls++
567 return &http.Client{}, nil
568 }
569
570 wrapped := configWithTokenRefresher(func() (gh.Config, error) { return cfg, nil }, plainClientFunc)
571
572 got, err := wrapped()
573 require.NoError(t, err)
574 require.Same(t, cfg, got)
575 require.Equal(t, 1, authCfg.setCalls)
576 require.NotNil(t, authCfg.lastRefresher)
577
578 // A second resolution reuses the already-injected refresher rather than building another.
579 _, err = wrapped()
580 require.NoError(t, err)
581 require.Equal(t, 1, authCfg.setCalls)
582 require.Equal(t, 1, plainCalls)
583 })
584
585 t.Run("skips injection when the plain client cannot be built", func(t *testing.T) {
586 authCfg := &recordingAuthConfig{}
587 cfg := &ghmock.ConfigMock{AuthenticationFunc: func() gh.AuthConfig { return authCfg }}
588 plainClientFunc := func() (*http.Client, error) { return nil, errors.New("no client") }
589
590 wrapped := configWithTokenRefresher(func() (gh.Config, error) { return cfg, nil }, plainClientFunc)
591
592 got, err := wrapped()
593 require.NoError(t, err)
594 require.Same(t, cfg, got)
595 require.Equal(t, 0, authCfg.setCalls)
596 })
597
598 t.Run("propagates a configuration error without injecting", func(t *testing.T) {
599 plainCalls := 0
600 plainClientFunc := func() (*http.Client, error) {
601 plainCalls++
602 return &http.Client{}, nil
603 }
604
605 wrapped := configWithTokenRefresher(func() (gh.Config, error) { return nil, errors.New("config boom") }, plainClientFunc)
606
607 _, err := wrapped()
608 require.EqualError(t, err, "config boom")
609 require.Equal(t, 0, plainCalls)
610 })
611}

Callers

nothing calls this directly

Calls 3

configWithTokenRefresherFunction · 0.85
EqualMethod · 0.80
RunMethod · 0.65

Tested by

no test coverage detected