|
| 1 | +package management |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/gin-gonic/gin" |
| 13 | + "github.com/router-for-me/CLIProxyAPI/v7/internal/config" |
| 14 | +) |
| 15 | + |
| 16 | +func TestPostOAuthCallbackCreatesMissingAuthDir(t *testing.T) { |
| 17 | + gin.SetMode(gin.TestMode) |
| 18 | + |
| 19 | + authDir := filepath.Join(t.TempDir(), "missing-auth") |
| 20 | + state := "test-antigravity-state" |
| 21 | + RegisterOAuthSession(state, "antigravity") |
| 22 | + defer CompleteOAuthSession(state) |
| 23 | + |
| 24 | + h := NewHandlerWithoutConfigFilePath(&config.Config{AuthDir: authDir}, nil) |
| 25 | + router := gin.New() |
| 26 | + router.POST("/v0/management/oauth-callback", h.PostOAuthCallback) |
| 27 | + |
| 28 | + body := `{"provider":"antigravity","redirect_url":"http://localhost:59788/oauth-callback?state=test-antigravity-state&code=test-code"}` |
| 29 | + req := httptest.NewRequest(http.MethodPost, "/v0/management/oauth-callback", strings.NewReader(body)) |
| 30 | + req.Header.Set("Content-Type", "application/json") |
| 31 | + w := httptest.NewRecorder() |
| 32 | + |
| 33 | + router.ServeHTTP(w, req) |
| 34 | + |
| 35 | + if w.Code != http.StatusOK { |
| 36 | + t.Fatalf("expected status %d, got %d with body %s", http.StatusOK, w.Code, w.Body.String()) |
| 37 | + } |
| 38 | + |
| 39 | + callbackPath := filepath.Join(authDir, ".oauth-antigravity-"+state+".oauth") |
| 40 | + data, errRead := os.ReadFile(callbackPath) |
| 41 | + if errRead != nil { |
| 42 | + t.Fatalf("expected callback file to be written: %v", errRead) |
| 43 | + } |
| 44 | + |
| 45 | + var payload oauthCallbackFilePayload |
| 46 | + if errUnmarshal := json.Unmarshal(data, &payload); errUnmarshal != nil { |
| 47 | + t.Fatalf("failed to decode callback payload: %v", errUnmarshal) |
| 48 | + } |
| 49 | + if payload.State != state || payload.Code != "test-code" || payload.Error != "" { |
| 50 | + t.Fatalf("unexpected callback payload: %+v", payload) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func TestWriteOAuthCallbackFileForPendingSessionCreatesMissingAuthDirForCallbackProviders(t *testing.T) { |
| 55 | + providers := []string{"anthropic", "codex", "gemini", "antigravity", "xai"} |
| 56 | + for _, provider := range providers { |
| 57 | + t.Run(provider, func(t *testing.T) { |
| 58 | + authDir := filepath.Join(t.TempDir(), "missing-auth") |
| 59 | + state := provider + "-state" |
| 60 | + RegisterOAuthSession(state, provider) |
| 61 | + defer CompleteOAuthSession(state) |
| 62 | + |
| 63 | + path, errWrite := WriteOAuthCallbackFileForPendingSession(authDir, provider, state, "code-"+provider, "") |
| 64 | + if errWrite != nil { |
| 65 | + t.Fatalf("expected callback file write to succeed: %v", errWrite) |
| 66 | + } |
| 67 | + |
| 68 | + data, errRead := os.ReadFile(path) |
| 69 | + if errRead != nil { |
| 70 | + t.Fatalf("expected callback file to be written: %v", errRead) |
| 71 | + } |
| 72 | + |
| 73 | + var payload oauthCallbackFilePayload |
| 74 | + if errUnmarshal := json.Unmarshal(data, &payload); errUnmarshal != nil { |
| 75 | + t.Fatalf("failed to decode callback payload: %v", errUnmarshal) |
| 76 | + } |
| 77 | + if payload.State != state || payload.Code != "code-"+provider || payload.Error != "" { |
| 78 | + t.Fatalf("unexpected callback payload: %+v", payload) |
| 79 | + } |
| 80 | + }) |
| 81 | + } |
| 82 | +} |
0 commit comments