V2 : flag serveur has_content (migration 000014, lazy healing, FileDto)
This commit is contained in:
@@ -7,6 +7,8 @@ import (
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -748,6 +750,74 @@ func TestFilesContentStreaming(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilesHasContentLazy(t *testing.T) {
|
||||
r, store, repo := setup(t)
|
||||
ownerDevice := repository.NewID()
|
||||
ownerToken, owner := registerAndLogin(t, r, repo, testUserUsername(ownerDevice, "hc"), "hascontent-password", ownerDevice)
|
||||
|
||||
getHasContent := func(fileID, label string) bool {
|
||||
t.Helper()
|
||||
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID, ownerToken, nil, "")
|
||||
env := expectOK(t, rec, label)
|
||||
var dto struct {
|
||||
HasContent bool `json:"hasContent"`
|
||||
}
|
||||
if err := json.Unmarshal(env.Data, &dto); err != nil {
|
||||
t.Fatalf("%s: unmarshal: %v", label, err)
|
||||
}
|
||||
return dto.HasContent
|
||||
}
|
||||
|
||||
// Upload avec octets → TRUE ; metadata-only → FALSE.
|
||||
fileID := repository.NewID()
|
||||
if err := repo.Resources.InsertFile(owner, fileID, "note.txt", "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert file: %v", err)
|
||||
}
|
||||
if getHasContent(fileID, "hascontent-bare") {
|
||||
t.Errorf("metadata-only : hasContent attendu false")
|
||||
}
|
||||
rec := uploadMultipartToResource(t, r, ownerToken, fileID, "note.txt", []byte("hello"))
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("upload ciblé: attendu 200, got %d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if !getHasContent(fileID, "hascontent-uploaded") {
|
||||
t.Errorf("après upload : hasContent attendu true")
|
||||
}
|
||||
|
||||
// Suppression manuelle sous UPLOAD_DIR puis GET content → 404 + heal FALSE.
|
||||
matches, _ := filepath.Glob(filepath.Join(store.UploadDir, owner, fileID+".*"))
|
||||
if len(matches) == 0 {
|
||||
t.Fatalf("fichier physique introuvable sous %s", store.UploadDir)
|
||||
}
|
||||
if err := os.Remove(matches[0]); err != nil {
|
||||
t.Fatalf("remove physique: %v", err)
|
||||
}
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/content", ownerToken, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "hascontent-missing-bytes")
|
||||
if getHasContent(fileID, "hascontent-healed-down") {
|
||||
t.Errorf("octets disparus : hasContent attendu false après heal")
|
||||
}
|
||||
|
||||
// Octets replacés à la main sur une ligne FALSE → GET content 200 + heal TRUE.
|
||||
bareID := repository.NewID()
|
||||
if err := repo.Resources.InsertFile(owner, bareID, "manuel.txt", "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert bare: %v", err)
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Join(store.UploadDir, owner), 0o700); err != nil {
|
||||
t.Fatalf("mkdir: %v", err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(store.UploadDir, owner, bareID+".txt"), []byte("hi"), 0o600); err != nil {
|
||||
t.Fatalf("write: %v", err)
|
||||
}
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+bareID+"/content", ownerToken, nil, "")
|
||||
if rec.Code != http.StatusOK || rec.Body.String() != "hi" {
|
||||
t.Fatalf("content-heal-up: attendu 200 hi, got %d %q", rec.Code, rec.Body.String())
|
||||
}
|
||||
if !getHasContent(bareID, "hascontent-healed-up") {
|
||||
t.Errorf("octets constatés : hasContent attendu true après heal")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilesSharesLinksListing(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
ownerDevice := repository.NewID()
|
||||
|
||||
Reference in New Issue
Block a user