package httpapi import ( "mime/multipart" "net/http/httptest" "strings" "testing" "github.com/vasyansk/imap-copier/internal/config" ) func TestImportCSVFailsOnBadEncKey(t *testing.T) { // EncKey wrong size => crypto.Encrypt errors => handler must NOT return success s := &Server{cfg: config.Config{EncKey: make([]byte, 16)}} body := &strings.Builder{} mw := multipart.NewWriter(body) fw, _ := mw.CreateFormFile("file", "a.csv") fw.Write([]byte("a@x,p1,a@y,p2\n")) mw.Close() req := httptest.NewRequest("POST", "/api/tasks/1/import", strings.NewReader(body.String())) req.Header.Set("Content-Type", mw.FormDataContentType()) req.SetPathValue("id", "1") rw := httptest.NewRecorder() s.handleImportCSV(rw, req) if rw.Code == 200 || rw.Code == 201 { t.Fatalf("import must fail on bad EncKey, got %d", rw.Code) } } func TestParseRunAccountIDs(t *testing.T) { // empty body => nil (run all) req := httptest.NewRequest("POST", "/api/tasks/1/run", strings.NewReader("")) ids, err := parseRunAccountIDs(req) if err != nil || ids != nil { t.Fatalf("empty body must yield nil ids, got %v err=%v", ids, err) } // explicit selection req = httptest.NewRequest("POST", "/api/tasks/1/run", strings.NewReader(`{"account_ids":[3,7]}`)) ids, err = parseRunAccountIDs(req) if err != nil || len(ids) != 2 || ids[0] != 3 || ids[1] != 7 { t.Fatalf("must parse account_ids, got %v err=%v", ids, err) } // malformed JSON => error req = httptest.NewRequest("POST", "/api/tasks/1/run", strings.NewReader(`{bad`)) if _, err := parseRunAccountIDs(req); err == nil { t.Fatal("malformed body must error") } }