3
0

Improve error creation & handling by API

This commit is contained in:
Denis Arh
2020-11-02 14:25:08 +01:00
parent 8ba5e22d72
commit ecb7f52c31
160 changed files with 7931 additions and 11070 deletions

View File

@@ -98,6 +98,7 @@ func TestApplicationCreateForbidden(t *testing.T) {
h.apiInit().
Post("/application/").
Header("Accept", "application/json").
FormData("name", rs()).
Expect(t).
Status(http.StatusOK).
@@ -125,6 +126,7 @@ func TestApplicationUpdateForbidden(t *testing.T) {
h.apiInit().
Put(fmt.Sprintf("/application/%d", u.ID)).
Header("Accept", "application/json").
FormData("email", h.randEmail()).
Expect(t).
Status(http.StatusOK).
@@ -142,6 +144,7 @@ func TestApplicationUpdate(t *testing.T) {
h.apiInit().
Put(fmt.Sprintf("/application/%d", res.ID)).
Header("Accept", "application/json").
FormData("name", newName).
FormData("handle", newHandle).
Expect(t).
@@ -160,6 +163,7 @@ func TestApplicationDeleteForbidden(t *testing.T) {
h.apiInit().
Delete(fmt.Sprintf("/application/%d", u.ID)).
Header("Accept", "application/json").
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertError("not allowed to delete this application")).
@@ -174,6 +178,7 @@ func TestApplicationDelete(t *testing.T) {
h.apiInit().
Delete(fmt.Sprintf("/application/%d", res.ID)).
Header("Accept", "application/json").
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertNoErrors).

View File

@@ -4,7 +4,7 @@ import (
"context"
"errors"
"github.com/cortezaproject/corteza-server/app"
"github.com/cortezaproject/corteza-server/pkg/api"
"github.com/cortezaproject/corteza-server/pkg/api/server"
"github.com/cortezaproject/corteza-server/pkg/auth"
"github.com/cortezaproject/corteza-server/pkg/cli"
"github.com/cortezaproject/corteza-server/pkg/eventbus"
@@ -83,7 +83,7 @@ func InitTestApp() {
if r == nil {
r = chi.NewRouter()
r.Use(api.BaseMiddleware(logger.Default())...)
r.Use(server.BaseMiddleware(logger.Default())...)
helpers.BindAuthMiddleware(r)
rest.MountRoutes(r)
}
@@ -125,6 +125,7 @@ func (h helper) apiInit() *apitest.APITest {
New().
Handler(r).
Intercept(helpers.ReqHeaderAuthBearer(h.cUser))
}
func (h helper) mockPermissions(rules ...*rbac.Rule) {

View File

@@ -103,6 +103,7 @@ func TestRoleCreateForbidden(t *testing.T) {
h.apiInit().
Post("/roles/").
Header("Accept", "application/json").
FormData("name", rs()).
Expect(t).
Status(http.StatusOK).
@@ -117,6 +118,7 @@ func TestRoleCreateNotUnique(t *testing.T) {
role := h.repoMakeRole()
h.apiInit().
Post("/roles/").
Header("Accept", "application/json").
FormData("name", rs()).
FormData("handle", role.Handle).
Expect(t).
@@ -126,6 +128,7 @@ func TestRoleCreateNotUnique(t *testing.T) {
h.apiInit().
Post("/roles/").
Header("Accept", "application/json").
FormData("name", role.Name).
FormData("handle", "handle_"+rs()).
Expect(t).
@@ -155,6 +158,7 @@ func TestRoleUpdateForbidden(t *testing.T) {
h.apiInit().
Put(fmt.Sprintf("/roles/%d", u.ID)).
Header("Accept", "application/json").
FormData("email", h.randEmail()).
Expect(t).
Status(http.StatusOK).
@@ -191,6 +195,7 @@ func TestRoleDeleteForbidden(t *testing.T) {
h.apiInit().
Delete(fmt.Sprintf("/roles/%d", u.ID)).
Header("Accept", "application/json").
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertError("not allowed to delete this role")).

View File

@@ -39,6 +39,7 @@ func TestSettingsList_noPermissions(t *testing.T) {
h.apiInit().
Get("/settings/").
Header("Accept", "application/json").
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertError("not allowed to read settings")).
@@ -78,6 +79,7 @@ func TestSettingsUpdate_noPermissions(t *testing.T) {
h.apiInit().
Patch("/settings/").
Header("Accept", "application/json").
JSON(`{"values":[]}`).
Expect(t).
Status(http.StatusOK).
@@ -119,6 +121,7 @@ func TestSettingsGet_noPermissions(t *testing.T) {
h.apiInit().
Get("/settings/t_sys_k1.s1").
Header("Accept", "application/json").
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertError("not allowed to read settings")).

View File

@@ -128,8 +128,8 @@ func TestUserListWithPaging(t *testing.T) {
h.a.NotNil(aux.Response.Filter.NextPage)
h.apiInit().
Debug().
Get("/users/").
Header("Accept", "application/json").
Query("limit", "13").
Query("pageCursor", *aux.Response.Filter.NextPage).
Expect(t).
@@ -158,6 +158,7 @@ func TestUserList_filterForbidden(t *testing.T) {
h.apiInit().
Get("/users/").
Header("Accept", "application/json").
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertNoErrors).
@@ -290,6 +291,7 @@ func TestUserCreateForbidden(t *testing.T) {
h.apiInit().
Post("/users/").
Header("Accept", "application/json").
FormData("email", h.randEmail()).
Expect(t).
Status(http.StatusOK).
@@ -322,6 +324,7 @@ func TestUserUpdateForbidden(t *testing.T) {
h.apiInit().
Put(fmt.Sprintf("/users/%d", u.ID)).
Header("Accept", "application/json").
FormData("email", h.randEmail()).
Expect(t).
Status(http.StatusOK).
@@ -355,6 +358,7 @@ func TestUserDeleteForbidden(t *testing.T) {
h.apiInit().
Delete(fmt.Sprintf("/users/%d", u.ID)).
Header("Accept", "application/json").
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertError("not allowed to delete this user")).