Add intg. test for record exporting, more strict param checking
This commit is contained in:
@@ -304,7 +304,6 @@ func (ctrl *Record) Export(ctx context.Context, r *request.RecordExport) (interf
|
||||
|
||||
contentType string
|
||||
)
|
||||
|
||||
// Access control.
|
||||
if _, err = ctrl.module.With(ctx).FindByID(r.NamespaceID, r.ModuleID); err != nil {
|
||||
return nil, err
|
||||
@@ -317,6 +316,10 @@ func (ctrl *Record) Export(ctx context.Context, r *request.RecordExport) (interf
|
||||
return func(w http.ResponseWriter, req *http.Request) {
|
||||
ff := encoder.MakeFields(r.Fields...)
|
||||
|
||||
if len(ff) == 0 {
|
||||
http.Error(w, "no record value fields provided", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
switch strings.ToLower(r.Ext) {
|
||||
case "json", "jsonl", "ldjson", "ndjson":
|
||||
contentType = "application/jsonl"
|
||||
|
||||
@@ -3,6 +3,7 @@ package compose
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -197,3 +198,25 @@ func TestRecordDelete(t *testing.T) {
|
||||
_, err := h.repoRecord().FindByID(module.NamespaceID, record.ID)
|
||||
h.a.Error(err, "compose.repository.RecordNotFound")
|
||||
}
|
||||
|
||||
func TestRecordExport(t *testing.T) {
|
||||
h := newHelper(t)
|
||||
|
||||
module := h.repoMakeRecordModuleWithFields("record export module")
|
||||
for i := 0; i < 10; i++ {
|
||||
h.repoMakeRecord(module, &types.RecordValue{Name: "name", Value: fmt.Sprintf("d%d", i)})
|
||||
}
|
||||
|
||||
// we'll not use standard asserts (AssertNoErrors) here,
|
||||
// because we're not returning JSON errors.
|
||||
r := h.apiInit().
|
||||
Get(fmt.Sprintf("/namespace/%d/module/%d/record/export.csv", module.NamespaceID, module.ID)).
|
||||
Query("fields", "name").
|
||||
Expect(t).
|
||||
Status(http.StatusOK).
|
||||
End()
|
||||
|
||||
b, err := ioutil.ReadAll(r.Response.Body)
|
||||
h.a.NoError(err)
|
||||
h.a.Equal("name\nd0\nd1\nd2\nd3\nd4\nd5\nd6\nd7\nd8\nd9\n", string(b))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user