MCPcopy Index your code
hub / github.com/coder/coder / TestRefresher_CorrectTokenPerOrigin

Function TestRefresher_CorrectTokenPerOrigin

coderd/x/gitsync/gitsync_test.go:650–757  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

648}
649
650func TestRefresher_CorrectTokenPerOrigin(t *testing.T) {
651 t.Parallel()
652
653 var tokenCalls atomic.Int32
654 tokens := func(_ context.Context, _ uuid.UUID, origin string) (*string, error) {
655 tokenCalls.Add(1)
656 switch {
657 case strings.Contains(origin, "github.com"):
658 return ptr.Ref("gh-public-token"), nil
659 case strings.Contains(origin, "ghes.corp.com"):
660 return ptr.Ref("ghe-private-token"), nil
661 default:
662 return nil, fmt.Errorf("unexpected origin: %s", origin)
663 }
664 }
665
666 // Track which token each FetchPullRequestStatus call received,
667 // keyed by chat ID. We pass the chat ID through the PRRef.Number
668 // field (unique per request) so FetchPullRequestStatus can
669 // identify which row it's processing.
670 var mu sync.Mutex
671 tokensByPR := make(map[int]string)
672
673 mp := &mockProvider{
674 parsePullRequestURL: func(raw string) (gitprovider.PRRef, bool) {
675 // Extract a unique PR number from the URL to identify
676 // each row inside FetchPullRequestStatus.
677 var num int
678 switch {
679 case strings.HasSuffix(raw, "/pull/1"):
680 num = 1
681 case strings.HasSuffix(raw, "/pull/2"):
682 num = 2
683 case strings.HasSuffix(raw, "/pull/10"):
684 num = 10
685 default:
686 return gitprovider.PRRef{}, false
687 }
688 return gitprovider.PRRef{Owner: "org", Repo: "repo", Number: num}, true
689 },
690 fetchPullRequestStatus: func(_ context.Context, token string, ref gitprovider.PRRef) (*gitprovider.PRStatus, error) {
691 mu.Lock()
692 tokensByPR[ref.Number] = token
693 mu.Unlock()
694 return &gitprovider.PRStatus{State: gitprovider.PRStateOpen}, nil
695 },
696 }
697
698 providers := func(_ context.Context, _ string) gitprovider.Provider { return mp }
699
700 r := gitsync.NewRefresher(providers, tokens, slogtest.Make(t, nil), quartz.NewReal())
701
702 ownerID := uuid.New()
703
704 requests := []gitsync.RefreshRequest{
705 {
706 Row: database.ChatDiffStatus{
707 ChatID: uuid.New(),

Callers

nothing calls this directly

Calls 12

RefreshMethod · 0.95
RefFunction · 0.92
NewRefresherFunction · 0.92
AddMethod · 0.65
NewMethod · 0.65
ContainsMethod · 0.45
ErrorfMethod · 0.45
LockMethod · 0.45
UnlockMethod · 0.45
LenMethod · 0.45
EqualMethod · 0.45
LoadMethod · 0.45

Tested by

no test coverage detected