|
| 1 | +package management |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "path/filepath" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/gin-gonic/gin" |
| 13 | + "github.com/router-for-me/CLIProxyAPI/v7/internal/auth/codex" |
| 14 | + "github.com/router-for-me/CLIProxyAPI/v7/internal/config" |
| 15 | +) |
| 16 | + |
| 17 | +type fakeCodexOAuthService struct{} |
| 18 | + |
| 19 | +func (f *fakeCodexOAuthService) GenerateAuthURL(state string, pkceCodes *codex.PKCECodes) (string, error) { |
| 20 | + return "https://auth.example.test/oauth?state=" + state, nil |
| 21 | +} |
| 22 | + |
| 23 | +func (f *fakeCodexOAuthService) ExchangeCodeForTokens(ctx context.Context, code string, pkceCodes *codex.PKCECodes) (*codex.CodexAuthBundle, error) { |
| 24 | + now := time.Now() |
| 25 | + return &codex.CodexAuthBundle{ |
| 26 | + TokenData: codex.CodexTokenData{ |
| 27 | + IDToken: "invalid-test-id-token", |
| 28 | + AccessToken: "access-" + code, |
| 29 | + RefreshToken: "refresh-" + code, |
| 30 | + Email: "codex-" + code + "@example.test", |
| 31 | + Expire: now.Add(time.Hour).Format(time.RFC3339), |
| 32 | + }, |
| 33 | + LastRefresh: now.Format(time.RFC3339), |
| 34 | + }, nil |
| 35 | +} |
| 36 | + |
| 37 | +func (f *fakeCodexOAuthService) CreateTokenStorage(bundle *codex.CodexAuthBundle) *codex.CodexTokenStorage { |
| 38 | + return &codex.CodexTokenStorage{ |
| 39 | + IDToken: bundle.TokenData.IDToken, |
| 40 | + AccessToken: bundle.TokenData.AccessToken, |
| 41 | + RefreshToken: bundle.TokenData.RefreshToken, |
| 42 | + AccountID: bundle.TokenData.AccountID, |
| 43 | + LastRefresh: bundle.LastRefresh, |
| 44 | + Email: bundle.TokenData.Email, |
| 45 | + Expire: bundle.TokenData.Expire, |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func TestRequestCodexTokenCompletionKeepsConcurrentSessionPending(t *testing.T) { |
| 50 | + originalNewCodexOAuthService := newCodexOAuthService |
| 51 | + newCodexOAuthService = func(cfg *config.Config) codexOAuthService { |
| 52 | + return &fakeCodexOAuthService{} |
| 53 | + } |
| 54 | + defer func() { |
| 55 | + newCodexOAuthService = originalNewCodexOAuthService |
| 56 | + }() |
| 57 | + |
| 58 | + authDir := filepath.Join(t.TempDir(), "auths") |
| 59 | + handler := NewHandlerWithoutConfigFilePath(&config.Config{AuthDir: authDir}, nil) |
| 60 | + router := gin.New() |
| 61 | + router.GET("/codex-auth-url", handler.RequestCodexToken) |
| 62 | + |
| 63 | + firstState := requestCodexTokenState(t, router) |
| 64 | + secondState := requestCodexTokenState(t, router) |
| 65 | + defer CompleteOAuthSession(firstState) |
| 66 | + defer CompleteOAuthSession(secondState) |
| 67 | + |
| 68 | + if _, errWrite := WriteOAuthCallbackFileForPendingSession(authDir, "codex", firstState, "first-code", ""); errWrite != nil { |
| 69 | + t.Fatalf("write first callback file: %v", errWrite) |
| 70 | + } |
| 71 | + |
| 72 | + waitForOAuthSessionDone(t, firstState) |
| 73 | + if !IsOAuthSessionPending(secondState, "codex") { |
| 74 | + t.Fatalf("expected concurrent codex session %s to remain pending after %s completed", secondState, firstState) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func requestCodexTokenState(t *testing.T, router http.Handler) string { |
| 79 | + t.Helper() |
| 80 | + |
| 81 | + req := httptest.NewRequest(http.MethodGet, "/codex-auth-url", nil) |
| 82 | + w := httptest.NewRecorder() |
| 83 | + router.ServeHTTP(w, req) |
| 84 | + if w.Code != http.StatusOK { |
| 85 | + t.Fatalf("expected status %d, got %d with body %s", http.StatusOK, w.Code, w.Body.String()) |
| 86 | + } |
| 87 | + |
| 88 | + var payload struct { |
| 89 | + State string `json:"state"` |
| 90 | + } |
| 91 | + if errDecode := json.Unmarshal(w.Body.Bytes(), &payload); errDecode != nil { |
| 92 | + t.Fatalf("decode codex auth URL response: %v", errDecode) |
| 93 | + } |
| 94 | + if payload.State == "" { |
| 95 | + t.Fatalf("expected codex auth URL response to include state") |
| 96 | + } |
| 97 | + return payload.State |
| 98 | +} |
| 99 | + |
| 100 | +func waitForOAuthSessionDone(t *testing.T, state string) { |
| 101 | + t.Helper() |
| 102 | + |
| 103 | + deadline := time.Now().Add(3 * time.Second) |
| 104 | + for time.Now().Before(deadline) { |
| 105 | + if !IsOAuthSessionPending(state, "codex") { |
| 106 | + return |
| 107 | + } |
| 108 | + time.Sleep(20 * time.Millisecond) |
| 109 | + } |
| 110 | + t.Fatalf("timed out waiting for codex session %s to complete", state) |
| 111 | +} |
0 commit comments