fix(backend): harden final MVP invariants

This commit is contained in:
2026-07-22 01:24:41 +08:00
parent 251b212e06
commit 78d4845688
26 changed files with 977 additions and 175 deletions

View File

@@ -11,6 +11,7 @@ import (
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"github.com/gin-gonic/gin"
@@ -43,14 +44,17 @@ func TestFileRegistrarSavesBeforeCreatingIdentitySourceDTO(t *testing.T) {
var source models.SenlinAgentSource
require.NoError(t, database.Where("project_id = ?", project.ID).First(&source).Error)
require.FileExists(t, filepath.Join(storageRoot, filepath.FromSlash(source.FilePath)))
require.Contains(t, filepath.ToSlash(source.FilePath), "projects/"+project.Identity+"/")
require.NotContains(t, filepath.ToSlash(source.FilePath), fmt.Sprintf("projects/%d/", project.ID))
var payload map[string]any
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &payload))
require.Equal(t, source.Identity, payload["id"])
require.Equal(t, project.Identity, payload["projectId"])
require.Equal(t, filepath.ToSlash(source.FilePath), payload["filePath"])
require.NotContains(t, payload["filePath"], storageRoot)
require.Equal(t, filepath.Base(source.FilePath), payload["storageKey"])
require.NotContains(t, payload, "filePath")
require.NotContains(t, payload, "AbsolutePath")
require.NotContains(t, rec.Body.String(), storageRoot)
require.NotContains(t, rec.Body.String(), fmt.Sprintf("projects/%d/", project.ID))
}
func TestFileRegistrarChecksOwnershipBeforeWritingFile(t *testing.T) {
@@ -139,7 +143,12 @@ func TestFileRegistrarLogsCleanupFailureWithoutLeakingStorageRoot(t *testing.T)
tx.AddError(errors.New("forced source create failure"))
}
}))
service.remove = func(path string) error { return fmt.Errorf("cleanup blocked for %s", path) }
service.remove = func(path string) error {
if strings.HasPrefix(filepath.Base(path), ".upload-") {
return os.Remove(path)
}
return fmt.Errorf("cleanup blocked for %s", path)
}
var serverLog bytes.Buffer
previousLogOutput := log.Writer()
log.SetOutput(&serverLog)