dramaling-vocab-learning/系統上線前測試代碼清理計劃.md

109 lines
3.5 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 系統上線前測試代碼清理計劃
## 🎯 目標
清理所有開發測試代碼,確保系統安全上線,特別是解決圖片生成功能的用戶認證問題。
## 🚨 **高優先級 - 必須修復**
### 1. 固定測試用戶 ID 問題 ⭐ **最重要**
**影響**:🔴 **阻止圖片生成功能運行**
**位置**
- `Controllers/BaseController.cs:79`
- `Controllers/ImageGenerationController.cs:160`
**問題**
```csharp
// 當前代碼
return Guid.Parse("00000000-0000-0000-0000-000000000001");
// 應該修復為
var userIdClaim = User.FindFirst(ClaimTypes.NameIdentifier)?.Value
?? User.FindFirst("sub")?.Value;
return Guid.Parse(userIdClaim);
```
**後果**
- 圖片生成使用錯誤的用戶 ID
- 外鍵約束失敗,功能完全無法使用
### 2. 開發環境認證繞過
**影響**:🟠 **安全風險**
**位置**
- `Controllers/FlashcardsController.cs:16` - `[AllowAnonymous] // 暫時開放以測試`
- `Controllers/ImageGenerationController.cs:10` - `[AllowAnonymous] // 暫時移除認證要求`
- `Controllers/AIController.cs:10` - `[AllowAnonymous]`
**修復**:移除 `[AllowAnonymous]` 或改為 `[Authorize]`
### 3. 開發專用 JWT Secret
**影響**:🟠 **安全風險**
**位置**
- `Program.cs:93`
- `Extensions/ServiceCollectionExtensions.cs:174`
- `Controllers/AuthController.cs:181`
**問題**
```csharp
?? "dev-secret-minimum-32-characters-long-for-jwt-signing-in-development-mode-only"
```
**修復**:確保生產環境使用正確的環境變數
## 🟡 **中優先級 - 建議修復**
### 4. 開發專用配置
**位置**
- `Program.cs:49` - `options.ApiKey = "test-key"`
- `Models/Configuration/GeminiOptionsValidator.cs:15` - 允許 `"test-key"`
### 5. CORS 開發設定
**位置**
- `Program.cs:115` - localhost CORS 設定
- `Program.cs:185` - 開發環境寬鬆 CORS
### 6. 其他開發標記
**位置**
- `Controllers/ImageGenerationController.cs:134` - `TODO: 實現分頁查詢邏輯`
- `Controllers/ImageGenerationController.cs:162` - `TODO: 恢復真實認證後改回`
## ✅ **可保留 - 正確的生產實務**
### 7. 環境判斷邏輯
- `if (builder.Environment.IsDevelopment())` - 正確的條件判斷
- 環境變數讀取邏輯
- 配置驗證邏輯
### 8. 測試相關檔案
- `frontend/public/test-register.html` - 不影響生產環境
- 各種文檔中的測試範例
- 測試套件依賴
## 🔧 **關鍵發現**
### 圖片生成失敗的根本原因
1. **用戶認證問題**:使用固定測試 ID `00000000-0000-0000-0000-000000000001`
2. **外鍵約束失敗**:嘗試為不存在的用戶創建圖片生成請求
3. **API keys 正常**:所有必要的 API keys 都已正確配置
### Google Cloud Storage 狀態
-**認證正常**gcloud auth 已設置
-**配置正確**:專案和儲存桶設定完整
-**服務初始化**GoogleCloudImageStorageService 正常運行
-**儲存就緒**:圖片將正確儲存到 `gs://dramaling-images/examples/`
## 🚀 **修復順序建議**
1. **立即修復**:用戶 ID hardcoding - 解決圖片生成問題
2. **安全加強**:移除 AllowAnonymous 標記
3. **配置清理**:確保生產環境配置安全
4. **測試驗證**:完整功能測試
## 💡 **完成修復後的預期結果**
- ✅ 圖片生成功能完全正常
- ✅ 圖片正確儲存到 Google Cloud Storage
- ✅ 用戶認證安全保護
- ✅ 系統準備好安全上線