From 46186f84f06ec8cbf803178062851b717cc0b8d8 Mon Sep 17 00:00:00 2001 From: Gani Georgiev Date: Mon, 14 Apr 2025 09:29:25 +0300 Subject: [PATCH] [#6718] fixed collections import error response --- apis/collection_import.go | 2 +- apis/collection_import_test.go | 67 ++++++++++++++++++++++++++++++++++ tools/types/geo_point_test.go | 1 + 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/apis/collection_import.go b/apis/collection_import.go index 040270c1..b77a9ba6 100644 --- a/apis/collection_import.go +++ b/apis/collection_import.go @@ -34,7 +34,7 @@ func collectionsImport(e *core.RequestEvent) error { // validation failure var validationErrors validation.Errors - if errors.As(err, &validationErrors) { + if errors.As(importErr, &validationErrors) { return e.BadRequestError("Failed to import collections.", validationErrors) } diff --git a/apis/collection_import_test.go b/apis/collection_import_test.go index cfa5902a..f20b0772 100644 --- a/apis/collection_import_test.go +++ b/apis/collection_import_test.go @@ -1,6 +1,7 @@ package apis_test import ( + "errors" "net/http" "strings" "testing" @@ -88,6 +89,7 @@ func TestCollectionsImport(t *testing.T) { `import2`, `fields`, }, + NotExpectedContent: []string{"Raw error:"}, ExpectedEvents: map[string]int{ "*": 0, "OnCollectionsImportRequest": 1, @@ -109,6 +111,71 @@ func TestCollectionsImport(t *testing.T) { } }, }, + { + Name: "authorized as superuser + non-validator failure", + Method: http.MethodPut, + URL: "/api/collections/import", + Body: strings.NewReader(`{ + "collections":[ + { + "name": "import1", + "fields": [ + { + "id": "koih1lqx", + "name": "test", + "type": "text" + } + ] + }, + { + "name": "import2", + "fields": [ + { + "id": "koih1lqx", + "name": "test", + "type": "text" + } + ], + "indexes": [ + "create index idx_test on import2 (test)" + ] + } + ] + }`), + Headers: map[string]string{ + "Authorization": "eyJhbGciOiJIUzI1NiJ9.eyJpZCI6InN5d2JoZWNuaDQ2cmhtMCIsInR5cGUiOiJhdXRoIiwiY29sbGVjdGlvbklkIjoicGJjXzMxNDI2MzU4MjMiLCJleHAiOjI1MjQ2MDQ0NjEsInJlZnJlc2hhYmxlIjp0cnVlfQ.UXgO3j-0BumcugrFjbd7j0M4MQvbrLggLlcu_YNGjoY", + }, + ExpectedStatus: 400, + ExpectedContent: []string{ + `"data":{`, + `"collections":{"code":"validation_collections_import_failure"`, + `Raw error:`, + `custom_error`, + }, + ExpectedEvents: map[string]int{ + "*": 0, + "OnCollectionsImportRequest": 1, + "OnCollectionCreate": 1, + "OnCollectionAfterCreateError": 1, + "OnModelCreate": 1, + "OnModelAfterCreateError": 1, + }, + BeforeTestFunc: func(t testing.TB, app *tests.TestApp, e *core.ServeEvent) { + app.OnCollectionCreate().BindFunc(func(e *core.CollectionEvent) error { + return errors.New("custom_error") + }) + }, + AfterTestFunc: func(t testing.TB, app *tests.TestApp, res *http.Response) { + collections := []*core.Collection{} + if err := app.CollectionQuery().All(&collections); err != nil { + t.Fatal(err) + } + expected := totalCollections + if len(collections) != expected { + t.Fatalf("Expected %d collections, got %d", expected, len(collections)) + } + }, + }, { Name: "authorized as superuser + successful collections create", Method: http.MethodPut, diff --git a/tools/types/geo_point_test.go b/tools/types/geo_point_test.go index 9b269e76..5ee3990e 100644 --- a/tools/types/geo_point_test.go +++ b/tools/types/geo_point_test.go @@ -54,6 +54,7 @@ func TestGeoPointScan(t *testing.T) { {`{}`, false, `{"lon":1,"lat":2}`}, {`[]`, true, `{"lon":1,"lat":2}`}, {0, true, `{"lon":1,"lat":2}`}, + {`{"lon":"1.23","lat":"4.56"}`, true, `{"lon":1,"lat":2}`}, {`{"lon":1.23,"lat":4.56}`, false, `{"lon":1.23,"lat":4.56}`}, {[]byte(`{"lon":1.23,"lat":4.56}`), false, `{"lon":1.23,"lat":4.56}`}, {types.JSONRaw(`{"lon":1.23,"lat":4.56}`), false, `{"lon":1.23,"lat":4.56}`},