fix backend audit findings (auth, upload, sync, data, ocr, contract)
CI / lint-backend (push) Failing after 1m39s
CI / lint-mobile (push) Canceled after 11m8s
CI / test-backend (push) Canceled after 0s
CI / test-mobile (push) Canceled after 0s
CI / build-backend (push) Canceled after 0s
CI / docker (push) Canceled after 0s
CI / lint-backend (push) Failing after 1m39s
CI / lint-mobile (push) Canceled after 11m8s
CI / test-backend (push) Canceled after 0s
CI / test-mobile (push) Canceled after 0s
CI / build-backend (push) Canceled after 0s
CI / docker (push) Canceled after 0s
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
@@ -66,7 +65,7 @@ func TestOperationsRecordRejectsInvalidOperationID(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestOperationsRecordNormalizesInvalidPayload(t *testing.T) {
|
||||
func TestOperationsRecordRejectsInvalidPayload(t *testing.T) {
|
||||
o, devices := newTestOperations(t)
|
||||
deviceID := NewID()
|
||||
if err := devices.Upsert(deviceID); err != nil {
|
||||
@@ -74,20 +73,128 @@ func TestOperationsRecordNormalizesInvalidPayload(t *testing.T) {
|
||||
}
|
||||
|
||||
opID := NewID()
|
||||
// Un payload non-JSON ne doit pas faire échouer la trace (jsonBytes → {}).
|
||||
if err := o.Record(deviceID, opID, "create_resource", "resource", nil, NewID(), []byte(`not-json`)); err != nil {
|
||||
t.Fatalf("record: %v", err)
|
||||
// Un payload non-JSON est refusé (pas de normalisation silencieuse vers {}) :
|
||||
// l'appelant (service) doit rejeter en amont en INVALID_REQUEST.
|
||||
if err := o.Record(deviceID, opID, "create_resource", "resource", nil, NewID(), []byte(`not-json`)); err == nil {
|
||||
t.Error("payload non-JSON : erreur attendue, got nil")
|
||||
}
|
||||
if applied, _ := o.Applied(deviceID, opID); applied {
|
||||
t.Error("record refusé : l'op ne doit pas être tracée")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOperationsRecordStoresPayloadHash(t *testing.T) {
|
||||
o, devices := newTestOperations(t)
|
||||
deviceID := NewID()
|
||||
if err := devices.Upsert(deviceID); err != nil {
|
||||
t.Fatalf("register device: %v", err)
|
||||
}
|
||||
|
||||
opID := NewID()
|
||||
payload := []byte(`{"name":"x"}`)
|
||||
if err := o.Record(deviceID, opID, "create_resource", "resource", nil, NewID(), payload); err != nil {
|
||||
t.Fatalf("record: %v", err)
|
||||
}
|
||||
stored, err := o.Get(deviceID, opID)
|
||||
if err != nil {
|
||||
t.Fatalf("get: %v", err)
|
||||
}
|
||||
if stored.Status != OpStatusApplied {
|
||||
t.Errorf("status = %q, attendu %q", stored.Status, OpStatusApplied)
|
||||
}
|
||||
want := CanonicalPayloadHash(payload)
|
||||
if stored.PayloadHash != want {
|
||||
t.Errorf("payload_hash = %q, attendu %q", stored.PayloadHash, want)
|
||||
}
|
||||
if len(stored.PayloadHash) != 64 {
|
||||
t.Errorf("payload_hash attendu 64-hex, got %q", stored.PayloadHash)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOperationsClaimAndMarkApplied(t *testing.T) {
|
||||
o, devices := newTestOperations(t)
|
||||
deviceID := NewID()
|
||||
if err := devices.Upsert(deviceID); err != nil {
|
||||
t.Fatalf("register device: %v", err)
|
||||
}
|
||||
|
||||
var stored string
|
||||
if err := o.DB.QueryRow(`SELECT payload::text FROM operations WHERE device_id = $1 AND operation_id = $2`, deviceID, opID).Scan(&stored); err != nil {
|
||||
t.Fatalf("read payload: %v", err)
|
||||
opID := NewID()
|
||||
payload := []byte(`{"name":"x"}`)
|
||||
// Premier claim → pris.
|
||||
claimed, err := o.Claim(deviceID, opID, "create_resource", "resource", nil, NewID(), payload)
|
||||
if err != nil || !claimed {
|
||||
t.Fatalf("claim 1: claimed=%v err=%v", claimed, err)
|
||||
}
|
||||
// La trace brouillon est en status failed (pas encore confirmée).
|
||||
stored, err := o.Get(deviceID, opID)
|
||||
if err != nil {
|
||||
t.Fatalf("get: %v", err)
|
||||
}
|
||||
if stored.Status != OpStatusFailed {
|
||||
t.Errorf("status après claim = %q, attendu %q", stored.Status, OpStatusFailed)
|
||||
}
|
||||
// Second claim (batch concurrent) → perdu, chemin rejeu.
|
||||
claimed, err = o.Claim(deviceID, opID, "create_resource", "resource", nil, NewID(), payload)
|
||||
if err != nil || claimed {
|
||||
t.Fatalf("claim 2: claimed=%v err=%v", claimed, err)
|
||||
}
|
||||
// Promotion après apply réussi.
|
||||
if err := o.MarkApplied(deviceID, opID); err != nil {
|
||||
t.Fatalf("mark applied: %v", err)
|
||||
}
|
||||
stored, err = o.Get(deviceID, opID)
|
||||
if err != nil {
|
||||
t.Fatalf("get 2: %v", err)
|
||||
}
|
||||
if stored.Status != OpStatusApplied {
|
||||
t.Errorf("status après promote = %q, attendu %q", stored.Status, OpStatusApplied)
|
||||
}
|
||||
// MarkApplied idempotent (re-promote sans effet).
|
||||
if err := o.MarkApplied(deviceID, opID); err != nil {
|
||||
t.Errorf("re-promote: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoredOpMatchesReplay(t *testing.T) {
|
||||
o, devices := newTestOperations(t)
|
||||
deviceID := NewID()
|
||||
if err := devices.Upsert(deviceID); err != nil {
|
||||
t.Fatalf("register device: %v", err)
|
||||
}
|
||||
|
||||
opID := NewID()
|
||||
resID := NewID()
|
||||
payload := []byte(`{"name":"x"}`)
|
||||
if err := o.Record(deviceID, opID, "create_resource", "resource", nil, resID, payload); err != nil {
|
||||
t.Fatalf("record: %v", err)
|
||||
}
|
||||
stored, err := o.Get(deviceID, opID)
|
||||
if err != nil {
|
||||
t.Fatalf("get: %v", err)
|
||||
}
|
||||
// Identique (espaces neutres via hash canonique) → match.
|
||||
if err := stored.MatchesReplay("create_resource", resID, []byte("{ \"name\" : \"x\" }")); err != nil {
|
||||
t.Errorf("rejeu identique: %v", err)
|
||||
}
|
||||
// Payload muté → ErrPayloadMismatch.
|
||||
if err := stored.MatchesReplay("create_resource", resID, []byte(`{"name":"y"}`)); !errors.Is(err, ErrPayloadMismatch) {
|
||||
t.Errorf("payload muté : attendu ErrPayloadMismatch, got %v", err)
|
||||
}
|
||||
// Type muté → ErrPayloadMismatch.
|
||||
if err := stored.MatchesReplay("delete_resource", resID, payload); !errors.Is(err, ErrPayloadMismatch) {
|
||||
t.Errorf("type muté : attendu ErrPayloadMismatch, got %v", err)
|
||||
}
|
||||
// Ressource mutée → ErrPayloadMismatch.
|
||||
if err := stored.MatchesReplay("create_resource", NewID(), payload); !errors.Is(err, ErrPayloadMismatch) {
|
||||
t.Errorf("ressource mutée : attendu ErrPayloadMismatch, got %v", err)
|
||||
}
|
||||
var parsed any
|
||||
if err := json.Unmarshal([]byte(stored), &parsed); err != nil {
|
||||
t.Fatalf("payload stocké invalide: %q", stored)
|
||||
// Ligne héritée sans payload_hash (pré-000013) : hash recalculé depuis
|
||||
// le payload stocké → match quand identique.
|
||||
stored.PayloadHash = ""
|
||||
if err := stored.MatchesReplay("create_resource", resID, payload); err != nil {
|
||||
t.Errorf("ligne héritée identique: %v", err)
|
||||
}
|
||||
if stored != "{}" {
|
||||
t.Errorf("payload normalisé attendu {}, got %q", stored)
|
||||
if err := stored.MatchesReplay("create_resource", resID, []byte(`{"name":"z"}`)); !errors.Is(err, ErrPayloadMismatch) {
|
||||
t.Errorf("ligne héritée mutée : attendu ErrPayloadMismatch, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user