add kerio format support
This commit is contained in:
+12
-1
@@ -14,6 +14,17 @@ import (
|
||||
"github.com/vasyansk/imap-copier/internal/store"
|
||||
)
|
||||
|
||||
// parseImportRows picks the CSV dialect from the "format" form field. A Kerio
|
||||
// Connect export holds one login/password pair and no domain, so the operator
|
||||
// supplies the domain alongside the file; anything else is the plain 4-column
|
||||
// src/dst format.
|
||||
func parseImportRows(r *http.Request, file io.Reader) ([]csvimport.Row, error) {
|
||||
if r.FormValue("format") == "kerio" {
|
||||
return csvimport.ParseKerio(file, r.FormValue("domain"))
|
||||
}
|
||||
return csvimport.Parse(file)
|
||||
}
|
||||
|
||||
func (s *Server) handleImportCSV(w http.ResponseWriter, r *http.Request) {
|
||||
taskID, err := pathID(r, "id")
|
||||
if err != nil {
|
||||
@@ -26,7 +37,7 @@ func (s *Server) handleImportCSV(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
rows, err := csvimport.Parse(file)
|
||||
rows, err := parseImportRows(r, file)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -27,6 +29,60 @@ func TestImportCSVFailsOnBadEncKey(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// importReq builds a multipart import request with the given CSV payload and
|
||||
// extra form fields, then returns it with the uploaded file ready to read.
|
||||
func importReq(t *testing.T, csv string, fields map[string]string) (*http.Request, io.Reader) {
|
||||
t.Helper()
|
||||
body := &strings.Builder{}
|
||||
mw := multipart.NewWriter(body)
|
||||
for k, v := range fields {
|
||||
_ = mw.WriteField(k, v)
|
||||
}
|
||||
fw, _ := mw.CreateFormFile("file", "a.csv")
|
||||
fw.Write([]byte(csv))
|
||||
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")
|
||||
file, _, err := req.FormFile("file")
|
||||
if err != nil {
|
||||
t.Fatalf("form file: %v", err)
|
||||
}
|
||||
return req, file
|
||||
}
|
||||
|
||||
const kerioCSV = "Name;FullName;Description;Enable;DataSource\n" +
|
||||
"info;;InfoPass11;Yes;Internal\n"
|
||||
|
||||
func TestParseImportRowsUsesKerioParserWithDomain(t *testing.T) {
|
||||
req, file := importReq(t, kerioCSV, map[string]string{"format": "kerio", "domain": "example.test"})
|
||||
rows, err := parseImportRows(req, file)
|
||||
if err != nil {
|
||||
t.Fatalf("parse: %v", err)
|
||||
}
|
||||
if len(rows) != 1 || rows[0].SrcLogin != "info@example.test" || rows[0].DstLogin != "info@example.test" {
|
||||
t.Fatalf("kerio rows must carry the supplied domain on both sides, got %+v", rows)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseImportRowsKerioRequiresDomain(t *testing.T) {
|
||||
req, file := importReq(t, kerioCSV, map[string]string{"format": "kerio"})
|
||||
if _, err := parseImportRows(req, file); err == nil {
|
||||
t.Fatal("kerio import without a domain must error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseImportRowsDefaultsToPlainFormat(t *testing.T) {
|
||||
req, file := importReq(t, "a@x,p1,a@y,p2\n", nil)
|
||||
rows, err := parseImportRows(req, file)
|
||||
if err != nil {
|
||||
t.Fatalf("parse: %v", err)
|
||||
}
|
||||
if len(rows) != 1 || rows[0].SrcLogin != "a@x" || rows[0].DstPass != "p2" {
|
||||
t.Fatalf("no format field must keep the 4-column parser, got %+v", rows)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseRunAccountIDs(t *testing.T) {
|
||||
// empty body => nil (run all)
|
||||
req := httptest.NewRequest("POST", "/api/tasks/1/run", strings.NewReader(""))
|
||||
|
||||
Reference in New Issue
Block a user