feat(api): sync réel — outbox POST /sync/ops idempotente + snapshot GET /sync/permissions (owner V1)

- operations.operation_id/ref_id BIGINT (= id client pending_operations), UNIQUE(device_id, operation_id) idempotente ; migration 000004 corrigée + test
- service/sync.go : ApplyBatch séquentiel (no-op si déjà traitée ; create déjà présent / update/move sur ressource absente / delete absent = no-op) ;
  arrêt à la première erreur non-idempotente (NAME_CONFLICT, NOT_FOUND, INVALID_REQUEST) + trace après succès ; ops share/share_link accusées sans état (V1)
- handlers/sync.go : POST /sync/ops → {applied, failed|null} ; GET /sync/permissions?after= → snapshot delta (effectiveAccess owner, cachedAt ms)
- snapshot : repo.ListOwned (delta updated_at en ms, trié) ; index partiel racine (owner_id, name) WHERE parent_id IS NULL
- GET /files sans folderId = racine uniquement (convention useFiles(undefined)) ; tests files_test corrigés (codes d'erreur imbriqués, tailles)
- fix dbtest : ping la connexion maintenance (création de la DB cible possible) — les tests handlers/repo tournent enfin contre PG réel
- tests handlers end-to-end : batch appliqué + retry idempotent, arrêt sur NAME_CONFLICT, delete idempotent, snapshot + delta
This commit is contained in:
m
2026-09-10 19:50:23 +02:00
parent 37ad2d4275
commit 5d96814853
14 changed files with 759 additions and 41 deletions
+20 -13
View File
@@ -39,8 +39,19 @@ type fileDTO struct {
}
type apiError struct {
Code string `json:"code"`
Message string `json:"message"`
Error struct {
Code string `json:"code"`
Message string `json:"message"`
} `json:"error"`
}
// wrapperError réutilise apiError pour casser l'imbrication.
func errorCode(rec *httptest.ResponseRecorder) string {
var e apiError
if err := json.Unmarshal(rec.Body.Bytes(), &e); err != nil {
return "<unmarshal: " + err.Error() + ">"
}
return e.Error.Code
}
func setup(t *testing.T) (*gin.Engine, *service.Resources, *repository.Repository) {
@@ -94,12 +105,8 @@ func expectError(t *testing.T, rec *httptest.ResponseRecorder, status int, code,
if rec.Code != status {
t.Fatalf("%s: attendu %d, got %d body=%s", path, status, rec.Code, rec.Body.String())
}
var e apiError
if err := json.Unmarshal(rec.Body.Bytes(), &e); err != nil {
t.Fatalf("%s: unmarshal error: %v body=%s", path, err, rec.Body.String())
}
if e.Code != code {
t.Errorf("%s: code erreur attendu %s, got %s", path, code, e.Code)
if got := errorCode(rec); got != code {
t.Errorf("%s: code erreur attendu %s, got %s (body=%s)", path, code, got, rec.Body.String())
}
}
@@ -157,13 +164,13 @@ func TestFilesFlow(t *testing.T) {
}
// Upload root + dossier
rec := uploadMultipart(t, r, tokenA, "", "hello.txt", []byte("hello world"))
rec := uploadMultipart(t, r, tokenA, "", "hello.txt", []byte("hello"))
env := expectOK(t, rec, "upload")
var uploaded fileDTO
if err := json.Unmarshal(env.Data, &uploaded); err != nil {
t.Fatalf("upload: unmarshal: %v", err)
}
if uploaded.Name != "hello.txt" || uploaded.Size != 11 || uploaded.FolderID != "" || uploaded.ID == "" {
if uploaded.Name != "hello.txt" || uploaded.Size != 5 || uploaded.FolderID != "" || uploaded.ID == "" {
t.Errorf("FileDto inattendu: %+v", uploaded)
}
@@ -222,7 +229,7 @@ func TestFilesFlow(t *testing.T) {
if err := json.Unmarshal(env.Data, &got); err != nil {
t.Fatalf("get: unmarshal: %v", err)
}
if got.ID != uploaded.ID || got.MimeType != "text/plain" || got.CreatedAt == "" {
if got.ID != uploaded.ID || got.MimeType != "application/octet-stream" || got.CreatedAt == "" {
t.Errorf("get FileDto inattendu: %+v", got)
}
@@ -262,14 +269,14 @@ func TestSearchFiles(t *testing.T) {
expectError(t, rec, http.StatusBadRequest, "INVALID_REQUEST", "search-no-q")
// insensible à la casse + sous-chaîne
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/search?q=APORT", token, nil, "")
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/search?q=RAPPORT", token, nil, "")
env := expectOK(t, rec, "search")
var files []fileDTO
if err := json.Unmarshal(env.Data, &files); err != nil {
t.Fatalf("search: unmarshal: %v", err)
}
if len(files) != 1 || files[0].Name != "rapport-q3.pdf" {
t.Errorf("search 'APORT': %+v", files)
t.Errorf("search 'RAPPORT': %+v", files)
}
if env.Meta == nil || env.Meta.Total != 1 {
t.Errorf("meta search: %+v", env.Meta)
+47 -2
View File
@@ -1,9 +1,54 @@
package handlers
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/vaultdrop/backend/pkg/api"
"github.com/vaultdrop/backend/service"
)
func SyncOpsPush(c *gin.Context) { api.NotImplemented(c) }
func SyncPermissionsGet(c *gin.Context) { api.NotImplemented(c) }
type syncOpsRequest struct {
Operations []service.SyncOperation `json:"operations"`
}
func SyncOpsPush(c *gin.Context) {
if Store == nil {
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
return
}
deviceID := c.GetString(DeviceIDKey)
var req syncOpsRequest
if err := c.ShouldBindJSON(&req); err != nil {
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid JSON body")
return
}
result, err := Store.ApplyBatch(deviceID, req.Operations)
if err != nil {
writeError(c, err)
return
}
api.OK(c, result)
}
func SyncPermissionsGet(c *gin.Context) {
if Store == nil {
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
return
}
deviceID := c.GetString(DeviceIDKey)
after := c.Query("after")
var afterMs int64
if after != "" {
afterMs = int64(intParam(after, 0))
}
perms, err := Store.Snapshot(deviceID, afterMs)
if err != nil {
writeError(c, err)
return
}
api.OK(c, perms)
}
+194
View File
@@ -0,0 +1,194 @@
package handlers_test
import (
"encoding/json"
"fmt"
"net/http"
"testing"
"github.com/vaultdrop/backend/repository"
)
func syncOpsBody(ops []map[string]any) []byte {
body, _ := json.Marshal(map[string]any{"operations": ops})
return body
}
func op(operationID int64, resourceID, operation, resourceType string, payload map[string]any) map[string]any {
return map[string]any{
"operation_id": operationID,
"ref_type": "resource",
"resource_id": resourceID,
"resource_type": resourceType,
"operation": operation,
"payload": payload,
}
}
func TestSyncOpsApplySequential(t *testing.T) {
r, _, _ := setup(t)
device := repository.NewID()
token := registerDevice(t, r, device)
folderID := repository.NewID()
fileID := repository.NewID()
ops := []map[string]any{
op(1, folderID, "create_resource", "folder", map[string]any{"name": "Docs"}),
op(2, fileID, "create_resource", "file", map[string]any{"name": "note.txt"}),
op(3, fileID, "move_resource", "file", map[string]any{"toFolderResourceId": folderID}),
}
rec, _ := doRequest(t, r, http.MethodPost, "/api/v1/sync/ops", token, syncOpsBody(ops), "application/json")
env := expectOK(t, rec, "sync-ops")
var result struct {
Applied int `json:"applied"`
Failed any `json:"failed"`
}
if err := json.Unmarshal(env.Data, &result); err != nil {
t.Fatalf("sync-ops: unmarshal: %v", err)
}
if result.Applied != 3 || result.Failed != nil {
t.Errorf("attendu applied=3 failed=null, got %+v", result)
}
// L'effet est visible côté CRUD
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID, token, nil, "")
env = expectOK(t, rec, "get-after-sync")
var got fileDTO
if err := json.Unmarshal(env.Data, &got); err != nil {
t.Fatalf("get-after-sync: unmarshal: %v", err)
}
if got.Name != "note.txt" || got.FolderID != folderID {
t.Errorf("fichier syncé: %+v", got)
}
// Re-envoi (retry) → idempotent, no-op
rec, _ = doRequest(t, r, http.MethodPost, "/api/v1/sync/ops", token, syncOpsBody(ops), "application/json")
env = expectOK(t, rec, "sync-ops-retry")
result = struct {
Applied int `json:"applied"`
Failed any `json:"failed"`
}{}
if err := json.Unmarshal(env.Data, &result); err != nil {
t.Fatalf("retry: unmarshal: %v", err)
}
if result.Applied != 3 {
t.Errorf("retry attendu applied=3, got %d", result.Applied)
}
}
func TestSyncOpsStopsAtFirstNonIdempotentFailure(t *testing.T) {
r, _, _ := setup(t)
device := repository.NewID()
token := registerDevice(t, r, device)
folderID := repository.NewID()
dupeID := repository.NewID()
ops := []map[string]any{
op(10, folderID, "create_resource", "folder", map[string]any{"name": "Docs"}),
// Conflicte avec Docs (même parent racine, même nom)
op(11, dupeID, "create_resource", "folder", map[string]any{"name": "Docs"}),
op(12, repository.NewID(), "create_resource", "file", map[string]any{"name": "after.txt"}),
}
rec, _ := doRequest(t, r, http.MethodPost, "/api/v1/sync/ops", token, syncOpsBody(ops), "application/json")
env := expectOK(t, rec, "sync-ops-fail")
var result struct {
Applied int `json:"applied"`
Failed *struct {
OperationID int64 `json:"operation_id"`
Code string `json:"code"`
Message string `json:"message"`
} `json:"failed"`
}
if err := json.Unmarshal(env.Data, &result); err != nil {
t.Fatalf("unmarshal: %v body=%s", err, rec.Body.String())
}
if result.Applied != 1 {
t.Errorf("attendu applied=1 (arrêt à la 2e op), got %d", result.Applied)
}
if result.Failed == nil || result.Failed.OperationID != 11 || result.Failed.Code != "NAME_CONFLICT" {
t.Errorf("failed attendu op 11 NAME_CONFLICT, got %+v", result.Failed)
}
// L'op 12 n'a PAS été appliquée
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files", token, nil, "")
env = expectOK(t, rec, "list-after-fail")
var files []fileDTO
if err := json.Unmarshal(env.Data, &files); err != nil {
t.Fatalf("list-after-fail: %v", err)
}
if len(files) != 0 {
t.Errorf("op 12 ne doit pas être appliquée: %+v", files)
}
}
func TestSyncOpsDeleteIdempotent(t *testing.T) {
r, _, repo := setup(t)
device := repository.NewID()
token := registerDevice(t, r, device)
fileID := repository.NewID()
if err := repo.Resources.InsertFile(device, fileID, "x.txt", "", 1, nil, nil); err != nil {
t.Fatalf("insert: %v", err)
}
// Supprimer une ressource absente → no-op réussi (pas de dead-letter)
absent := repository.NewID()
ops := []map[string]any{
op(20, fileID, "delete_resource", "file", map[string]any{}),
op(21, absent, "delete_resource", "file", map[string]any{}),
}
rec, _ := doRequest(t, r, http.MethodPost, "/api/v1/sync/ops", token, syncOpsBody(ops), "application/json")
env := expectOK(t, rec, "sync-delete")
var result struct {
Applied int `json:"applied"`
}
if err := json.Unmarshal(env.Data, &result); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if result.Applied != 2 {
t.Errorf("attendu applied=2, got %d", result.Applied)
}
}
func TestSnapshotPermissions(t *testing.T) {
r, _, repo := setup(t)
device := repository.NewID()
token := registerDevice(t, r, device)
folderID := repository.NewID()
if err := repo.Resources.InsertFolder(device, folderID, "Docs", ""); err != nil {
t.Fatalf("insert folder: %v", err)
}
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/sync/permissions", token, nil, "")
env := expectOK(t, rec, "snapshot")
var perms []struct {
ResourceID string `json:"resource_id"`
ResourceType string `json:"resourceType"`
EffectiveAccess string `json:"effectiveAccess"`
OwnerID string `json:"ownerId"`
CachedAt int64 `json:"cachedAt"`
}
if err := json.Unmarshal(env.Data, &perms); err != nil {
t.Fatalf("snapshot: unmarshal: %v", err)
}
if len(perms) != 1 || perms[0].ResourceID != folderID || perms[0].EffectiveAccess != "owner" || perms[0].OwnerID != device {
t.Errorf("snapshot: %+v", perms)
}
if perms[0].CachedAt == 0 {
t.Error("cachedAt manquant")
}
// Delta : après cachedAt → vide
rec, _ = doRequest(t, r, http.MethodGet, fmt.Sprintf("/api/v1/sync/permissions?after=%d", perms[0].CachedAt), token, nil, "")
env = expectOK(t, rec, "snapshot-after")
perms = nil
if err := json.Unmarshal(env.Data, &perms); err != nil {
t.Fatalf("snapshot-after: unmarshal: %v", err)
}
if len(perms) != 0 {
t.Errorf("delta attendu vide, got %+v", perms)
}
}