package handlers_test import ( "net/http" "testing" "github.com/kazier/backend/repository" ) // TestExportEndpoint : POST /export exige un mot de passe 8+ (400 sinon) et // streame un zip (200 + application/zip). func TestExportEndpoint(t *testing.T) { r, _, repo := setup(t) device := repository.NewID() token, _ := registerAndLogin(t, r, repo, testUserUsername(device, "ex"), "export-test-password", device) for _, body := range []string{`{}`, `{"password":"court"}`, `not-json`} { rec, _ := doRequest(t, r, http.MethodPost, "/api/v1/export", token, []byte(body), "application/json") expectError(t, rec, http.StatusBadRequest, "INVALID_REQUEST", "export-400:"+body) } rec, _ := doRequest(t, r, http.MethodPost, "/api/v1/export", token, []byte(`{"password":"motdepasse-8"}`), "application/json") if rec.Code != http.StatusOK { t.Fatalf("export: attendu 200, got %d %s", rec.Code, rec.Body.String()) } if ct := rec.Header().Get("Content-Type"); ct != "application/zip" { t.Fatalf("content-type = %q", ct) } if cd := rec.Header().Get("Content-Disposition"); cd == "" { t.Fatal("content-disposition manquant") } if rec.Body.Len() == 0 { t.Fatal("zip vide") } if rec.Body.Bytes()[0] != 'P' || rec.Body.Bytes()[1] != 'K' { t.Fatal("pas une archive zip (signature PK manquante)") } }