|
| 1 | +package management |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "net/url" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/gin-gonic/gin" |
| 14 | + "github.com/router-for-me/CLIProxyAPI/v6/internal/config" |
| 15 | + coreauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth" |
| 16 | +) |
| 17 | + |
| 18 | +func TestDeleteAuthFile_UsesAuthPathFromManager(t *testing.T) { |
| 19 | + t.Setenv("MANAGEMENT_PASSWORD", "") |
| 20 | + gin.SetMode(gin.TestMode) |
| 21 | + |
| 22 | + tempDir := t.TempDir() |
| 23 | + authDir := filepath.Join(tempDir, "auth") |
| 24 | + externalDir := filepath.Join(tempDir, "external") |
| 25 | + if errMkdirAuth := os.MkdirAll(authDir, 0o700); errMkdirAuth != nil { |
| 26 | + t.Fatalf("failed to create auth dir: %v", errMkdirAuth) |
| 27 | + } |
| 28 | + if errMkdirExternal := os.MkdirAll(externalDir, 0o700); errMkdirExternal != nil { |
| 29 | + t.Fatalf("failed to create external dir: %v", errMkdirExternal) |
| 30 | + } |
| 31 | + |
| 32 | + fileName := "codex-user@example.com-plus.json" |
| 33 | + shadowPath := filepath.Join(authDir, fileName) |
| 34 | + realPath := filepath.Join(externalDir, fileName) |
| 35 | + if errWriteShadow := os.WriteFile(shadowPath, []byte(`{"type":"codex","email":"shadow@example.com"}`), 0o600); errWriteShadow != nil { |
| 36 | + t.Fatalf("failed to write shadow file: %v", errWriteShadow) |
| 37 | + } |
| 38 | + if errWriteReal := os.WriteFile(realPath, []byte(`{"type":"codex","email":"real@example.com"}`), 0o600); errWriteReal != nil { |
| 39 | + t.Fatalf("failed to write real file: %v", errWriteReal) |
| 40 | + } |
| 41 | + |
| 42 | + manager := coreauth.NewManager(nil, nil, nil) |
| 43 | + record := &coreauth.Auth{ |
| 44 | + ID: "legacy/" + fileName, |
| 45 | + FileName: fileName, |
| 46 | + Provider: "codex", |
| 47 | + Status: coreauth.StatusError, |
| 48 | + Unavailable: true, |
| 49 | + Attributes: map[string]string{ |
| 50 | + "path": realPath, |
| 51 | + }, |
| 52 | + Metadata: map[string]any{ |
| 53 | + "type": "codex", |
| 54 | + "email": "real@example.com", |
| 55 | + }, |
| 56 | + } |
| 57 | + if _, errRegister := manager.Register(context.Background(), record); errRegister != nil { |
| 58 | + t.Fatalf("failed to register auth record: %v", errRegister) |
| 59 | + } |
| 60 | + |
| 61 | + h := NewHandlerWithoutConfigFilePath(&config.Config{AuthDir: authDir}, manager) |
| 62 | + h.tokenStore = &memoryAuthStore{} |
| 63 | + |
| 64 | + deleteRec := httptest.NewRecorder() |
| 65 | + deleteCtx, _ := gin.CreateTestContext(deleteRec) |
| 66 | + deleteReq := httptest.NewRequest(http.MethodDelete, "/v0/management/auth-files?name="+url.QueryEscape(fileName), nil) |
| 67 | + deleteCtx.Request = deleteReq |
| 68 | + h.DeleteAuthFile(deleteCtx) |
| 69 | + |
| 70 | + if deleteRec.Code != http.StatusOK { |
| 71 | + t.Fatalf("expected delete status %d, got %d with body %s", http.StatusOK, deleteRec.Code, deleteRec.Body.String()) |
| 72 | + } |
| 73 | + if _, errStatReal := os.Stat(realPath); !os.IsNotExist(errStatReal) { |
| 74 | + t.Fatalf("expected managed auth file to be removed, stat err: %v", errStatReal) |
| 75 | + } |
| 76 | + if _, errStatShadow := os.Stat(shadowPath); errStatShadow != nil { |
| 77 | + t.Fatalf("expected shadow auth file to remain, stat err: %v", errStatShadow) |
| 78 | + } |
| 79 | + |
| 80 | + listRec := httptest.NewRecorder() |
| 81 | + listCtx, _ := gin.CreateTestContext(listRec) |
| 82 | + listReq := httptest.NewRequest(http.MethodGet, "/v0/management/auth-files", nil) |
| 83 | + listCtx.Request = listReq |
| 84 | + h.ListAuthFiles(listCtx) |
| 85 | + |
| 86 | + if listRec.Code != http.StatusOK { |
| 87 | + t.Fatalf("expected list status %d, got %d with body %s", http.StatusOK, listRec.Code, listRec.Body.String()) |
| 88 | + } |
| 89 | + var listPayload map[string]any |
| 90 | + if errUnmarshal := json.Unmarshal(listRec.Body.Bytes(), &listPayload); errUnmarshal != nil { |
| 91 | + t.Fatalf("failed to decode list payload: %v", errUnmarshal) |
| 92 | + } |
| 93 | + filesRaw, ok := listPayload["files"].([]any) |
| 94 | + if !ok { |
| 95 | + t.Fatalf("expected files array, payload: %#v", listPayload) |
| 96 | + } |
| 97 | + if len(filesRaw) != 0 { |
| 98 | + t.Fatalf("expected removed auth to be hidden from list, got %d entries", len(filesRaw)) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func TestDeleteAuthFile_FallbackToAuthDirPath(t *testing.T) { |
| 103 | + t.Setenv("MANAGEMENT_PASSWORD", "") |
| 104 | + gin.SetMode(gin.TestMode) |
| 105 | + |
| 106 | + authDir := t.TempDir() |
| 107 | + fileName := "fallback-user.json" |
| 108 | + filePath := filepath.Join(authDir, fileName) |
| 109 | + if errWrite := os.WriteFile(filePath, []byte(`{"type":"codex"}`), 0o600); errWrite != nil { |
| 110 | + t.Fatalf("failed to write auth file: %v", errWrite) |
| 111 | + } |
| 112 | + |
| 113 | + manager := coreauth.NewManager(nil, nil, nil) |
| 114 | + h := NewHandlerWithoutConfigFilePath(&config.Config{AuthDir: authDir}, manager) |
| 115 | + h.tokenStore = &memoryAuthStore{} |
| 116 | + |
| 117 | + deleteRec := httptest.NewRecorder() |
| 118 | + deleteCtx, _ := gin.CreateTestContext(deleteRec) |
| 119 | + deleteReq := httptest.NewRequest(http.MethodDelete, "/v0/management/auth-files?name="+url.QueryEscape(fileName), nil) |
| 120 | + deleteCtx.Request = deleteReq |
| 121 | + h.DeleteAuthFile(deleteCtx) |
| 122 | + |
| 123 | + if deleteRec.Code != http.StatusOK { |
| 124 | + t.Fatalf("expected delete status %d, got %d with body %s", http.StatusOK, deleteRec.Code, deleteRec.Body.String()) |
| 125 | + } |
| 126 | + if _, errStat := os.Stat(filePath); !os.IsNotExist(errStat) { |
| 127 | + t.Fatalf("expected auth file to be removed from auth dir, stat err: %v", errStat) |
| 128 | + } |
| 129 | +} |
0 commit comments