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

This commit is contained in:
m
2026-09-21 19:59:52 +02:00
parent df7dc76405
commit a6b7f2002b
60 changed files with 3414 additions and 412 deletions
+90
View File
@@ -67,6 +67,10 @@ func TestOcrJobsFail(t *testing.T) {
if err := repo.OcrJobs.Create(jobID, deviceID, fileID); err != nil {
t.Fatalf("create: %v", err)
}
// Fail n'est valide que depuis `processing` (transition gardée).
if err := repo.OcrJobs.TouchProcessing(deviceID, jobID); err != nil {
t.Fatalf("touch processing: %v", err)
}
if err := repo.OcrJobs.Fail(deviceID, jobID, "tesseract exploded"); err != nil {
t.Fatalf("fail: %v", err)
}
@@ -76,6 +80,71 @@ func TestOcrJobsFail(t *testing.T) {
}
}
func TestOcrJobsGuardedTransitions(t *testing.T) {
repo, _, deviceID, fileID := seedOcrJobFixture(t)
newJob := func(t *testing.T) string {
t.Helper()
jobID := NewID()
if err := repo.OcrJobs.Create(jobID, deviceID, fileID); err != nil {
t.Fatalf("create: %v", err)
}
return jobID
}
// Fail depuis `queued` → conflit, le job reste queued.
queued := newJob(t)
if err := repo.OcrJobs.Fail(deviceID, queued, "boom"); !errors.Is(err, ErrJobStateConflict) {
t.Errorf("fail depuis queued : attendu ErrJobStateConflict, got %v", err)
}
if row, _ := repo.OcrJobs.Get(deviceID, queued); row.Status != "queued" {
t.Errorf("fail depuis queued : status = %q, attendu queued", row.Status)
}
// Complete depuis `queued` → conflit.
if err := repo.OcrJobs.Complete(deviceID, queued, "x"); !errors.Is(err, ErrJobStateConflict) {
t.Errorf("complete depuis queued : attendu ErrJobStateConflict, got %v", err)
}
// Double TouchProcessing → conflit.
if err := repo.OcrJobs.TouchProcessing(deviceID, queued); err != nil {
t.Fatalf("touch processing: %v", err)
}
if err := repo.OcrJobs.TouchProcessing(deviceID, queued); !errors.Is(err, ErrJobStateConflict) {
t.Errorf("double touch : attendu ErrJobStateConflict, got %v", err)
}
// Double Complete → le premier gagne, le second est un conflit.
if err := repo.OcrJobs.Complete(deviceID, queued, "FIRST"); err != nil {
t.Fatalf("complete: %v", err)
}
if err := repo.OcrJobs.Complete(deviceID, queued, "SECOND"); !errors.Is(err, ErrJobStateConflict) {
t.Errorf("double complete : attendu ErrJobStateConflict, got %v", err)
}
if row, _ := repo.OcrJobs.Get(deviceID, queued); row.Text == nil || *row.Text != "FIRST" {
t.Errorf("double complete : texte = %+v, attendu FIRST", row.Text)
}
// Fail après Complete → conflit, le statut `done` est collant.
if err := repo.OcrJobs.Fail(deviceID, queued, "late"); !errors.Is(err, ErrJobStateConflict) {
t.Errorf("fail après done : attendu ErrJobStateConflict, got %v", err)
}
if row, _ := repo.OcrJobs.Get(deviceID, queued); row.Status != "done" {
t.Errorf("fail après done : status = %q, attendu done", row.Status)
}
// Complete après Fail → conflit.
failed := newJob(t)
if err := repo.OcrJobs.TouchProcessing(deviceID, failed); err != nil {
t.Fatalf("touch processing: %v", err)
}
if err := repo.OcrJobs.Fail(deviceID, failed, "boom"); err != nil {
t.Fatalf("fail: %v", err)
}
if err := repo.OcrJobs.Complete(deviceID, failed, "late"); !errors.Is(err, ErrJobStateConflict) {
t.Errorf("complete après failed : attendu ErrJobStateConflict, got %v", err)
}
}
func TestOcrJobsGetScopedByDevice(t *testing.T) {
repo, _, deviceID, fileID := seedOcrJobFixture(t)
jobID := NewID()
@@ -179,3 +248,24 @@ func TestOcrJobsResetStaleProcessing(t *testing.T) {
t.Errorf("status après reset = %q, attendu queued", row.Status)
}
}
func TestOcrJobsResetStaleProcessingNullStartedAt(t *testing.T) {
repo, _, deviceID, fileID := seedOcrJobFixture(t)
jobID := NewID()
if err := repo.OcrJobs.Create(jobID, deviceID, fileID); err != nil {
t.Fatalf("create: %v", err)
}
// Job passé en `processing` sans `started_at` (anomalie de claim) → stale.
if _, err := repo.OcrJobs.DB.Exec(
`UPDATE ocr_jobs SET status = 'processing', started_at = NULL WHERE job_id = $1`, jobID); err != nil {
t.Fatalf("forcer processing sans started_at: %v", err)
}
if n, err := repo.OcrJobs.ResetStaleProcessing(time.Minute); err != nil {
t.Fatalf("reset: %v", err)
} else if n != 1 {
t.Errorf("reset started_at NULL : attendu 1, got %d", n)
}
if row, _ := repo.OcrJobs.Get(deviceID, jobID); row.Status != "queued" {
t.Errorf("status après reset = %q, attendu queued", row.Status)
}
}