streaming d'un fichier

This commit is contained in:
venture
2026-09-25 15:34:28 +02:00
parent 9e8b01b135
commit b869f70922
23 changed files with 856 additions and 1 deletions
+61
View File
@@ -686,3 +686,64 @@ func TestSearchQTooLong(t *testing.T) {
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/files/search?q="+string(long), token, nil, "")
expectError(t, rec, http.StatusBadRequest, "INVALID_REQUEST", "search-q-too-long")
}
func TestFilesContentStreaming(t *testing.T) {
r, _, repo := setup(t)
ownerDevice := repository.NewID()
ownerToken, owner := registerAndLogin(t, r, repo, testUserUsername(ownerDevice, "ct"), "content-test-password", ownerDevice)
granteeDevice := repository.NewID()
granteeToken, grantee := registerAndLogin(t, r, repo, testUserUsername(granteeDevice, "cg"), "content-test-password-b", granteeDevice)
outsiderDevice := repository.NewID()
outsiderToken, _ := registerAndLogin(t, r, repo, testUserUsername(outsiderDevice, "ch"), "content-test-password-c", outsiderDevice)
// Ressource metadata-only (outbox) puis upload ciblé des octets (≤10o, cf. setup).
fileID := repository.NewID()
if err := repo.Resources.InsertFile(owner, fileID, "note.txt", "", 0, nil, nil); err != nil {
t.Fatalf("insert file: %v", err)
}
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())
}
// Owner : octets + Content-Type.
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/content", ownerToken, nil, "")
if rec.Code != http.StatusOK {
t.Fatalf("content-owner: attendu 200, got %d body=%s", rec.Code, rec.Body.String())
}
if rec.Body.String() != "hello" {
t.Errorf("content-owner = %q, attendu %q", rec.Body.String(), "hello")
}
if ct := rec.Header().Get("Content-Type"); ct == "" {
t.Errorf("content-owner: Content-Type manquant")
}
// Grantée viewer : mêmes octets (résolution dir owner, pas requester).
if err := repo.Shares.Upsert(fileID, grantee, "viewer", false, nil, owner); err != nil {
t.Fatalf("share: %v", err)
}
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/content", granteeToken, nil, "")
if rec.Code != http.StatusOK || rec.Body.String() != "hello" {
t.Errorf("content-grantee: attendu 200 hello, got %d %q", rec.Code, rec.Body.String())
}
// Outsider : 404 (pas d'énumération).
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/content", outsiderToken, nil, "")
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "content-outsider")
// Metadata-only sans octets : 404 (jamais uploadé).
bareID := repository.NewID()
if err := repo.Resources.InsertFile(owner, bareID, "vide.txt", "", 0, nil, nil); err != nil {
t.Fatalf("insert bare: %v", err)
}
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+bareID+"/content", ownerToken, nil, "")
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "content-no-bytes")
// ID invalide → 404 ; sans token → 401.
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/not-hex/content", ownerToken, nil, "")
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "content-invalid-id")
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/content", "", nil, "")
if rec.Code != http.StatusUnauthorized {
t.Errorf("content-sans-token: attendu 401, got %d", rec.Code)
}
}