Files
imap-copier/internal/httpapi/run_test.go
T

29 lines
834 B
Go

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)
}
}