fix(api): enforce project identifier uniqueness

This commit is contained in:
2026-07-21 14:41:30 +08:00
parent 5341b44cc5
commit 14a140985c
6 changed files with 89 additions and 31 deletions

View File

@@ -142,6 +142,38 @@ func TestUpdateProject(t *testing.T) {
require.Equal(t, "更新后的项目说明", updated.Description)
})
t.Run("clears optional metadata while preserving omitted name and identifier", func(t *testing.T) {
router, project, _ := newProjectsHandlerTestRouter(t)
require.NoError(t, models.DBService.Model(project).Updates(map[string]any{
"icon": "tree",
"background": "#165DFF",
"description": "原项目说明",
}).Error)
originalName := project.Name
originalIdentifier := project.Identifier
body, err := json.Marshal(gin.H{
"icon": "",
"background": "",
"description": "",
})
require.NoError(t, err)
req := httptest.NewRequest(http.MethodPatch, "/api/v1/projects/"+project.Identity, bytes.NewReader(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer test-token")
rec := httptest.NewRecorder()
router.ServeHTTP(rec, req)
require.Equal(t, http.StatusNoContent, rec.Code)
var updated models.SenlinAgentProject
require.NoError(t, models.DBService.First(&updated, project.ID).Error)
require.Equal(t, originalName, updated.Name)
require.Equal(t, originalIdentifier, updated.Identifier)
require.Empty(t, updated.Icon)
require.Empty(t, updated.Background)
require.Empty(t, updated.Description)
})
t.Run("returns not found when the identity is not owned by the current user", func(t *testing.T) {
router, _, _ := newProjectsHandlerTestRouter(t)
other, err := NewService().CreateProject(2, "Other", "")