[MM-37557] Move error out of client4 response (#18101)
* Return an error seperately from Response * Remove BuildErrorResponse * Drop Response.Error from model/client4.go * Migrate require.Nil checks * Migrate require.NotNil checks * More manual fixes * Move error check out of CheckOKStatus and CheckCreatedStatus * Move error check out of CheckForbiddenStatus * Move error check out of CheckUnauthorizedStatus * Move error check out of CheckNotFoundStatus * Move error check out of CheckBadRequestStatus * Move error check out of CheckNotImplementedStatus and CheckRequestEntityTooLargeStatus * Move error check out of CheckInternalErrorStatus * Move error check out of CheckServiceUnavailableStatus * Remove error check from checkHTTPStatus * Remove remaining references to Response.Error * Check previously unchecked errors * Manually fix compile and linter errors * Return error in CreateWebSocket methods * Return error instead of *AppError in DoApi methods * Manually fix bad replacments * Conistently return Response and error * Use err instead of seperate bool return value to indicate success * Reduce ussage of model.AppError in web/oauth_test.go * Remove client4.Must * Check error in buf.ReadFrom * Fix failing tests
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
96593580ae
Коммит
a8ca5c423f
@@ -32,8 +32,8 @@ func TestGetRole(t *testing.T) {
|
||||
defer th.App.Srv().Store.Job().Delete(role.Id)
|
||||
|
||||
th.TestForAllClients(t, func(t *testing.T, client *model.Client4) {
|
||||
received, resp := client.GetRole(role.Id)
|
||||
CheckNoError(t, resp)
|
||||
received, _, err := client.GetRole(role.Id)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, received.Id, role.Id)
|
||||
assert.Equal(t, received.Name, role.Name)
|
||||
@@ -44,10 +44,12 @@ func TestGetRole(t *testing.T) {
|
||||
})
|
||||
|
||||
th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) {
|
||||
_, resp := client.GetRole("1234")
|
||||
_, resp, err := client.GetRole("1234")
|
||||
require.Error(t, err)
|
||||
CheckBadRequestStatus(t, resp)
|
||||
|
||||
_, resp = client.GetRole(model.NewId())
|
||||
_, resp, err = client.GetRole(model.NewId())
|
||||
require.Error(t, err)
|
||||
CheckNotFoundStatus(t, resp)
|
||||
})
|
||||
}
|
||||
@@ -69,8 +71,8 @@ func TestGetRoleByName(t *testing.T) {
|
||||
defer th.App.Srv().Store.Job().Delete(role.Id)
|
||||
|
||||
th.TestForAllClients(t, func(t *testing.T, client *model.Client4) {
|
||||
received, resp := client.GetRoleByName(role.Name)
|
||||
CheckNoError(t, resp)
|
||||
received, _, err := client.GetRoleByName(role.Name)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, received.Id, role.Id)
|
||||
assert.Equal(t, received.Name, role.Name)
|
||||
@@ -81,10 +83,12 @@ func TestGetRoleByName(t *testing.T) {
|
||||
})
|
||||
|
||||
th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) {
|
||||
_, resp := client.GetRoleByName(strings.Repeat("abcdefghij", 10))
|
||||
_, resp, err := client.GetRoleByName(strings.Repeat("abcdefghij", 10))
|
||||
require.Error(t, err)
|
||||
CheckBadRequestStatus(t, resp)
|
||||
|
||||
_, resp = client.GetRoleByName(model.NewId())
|
||||
_, resp, err = client.GetRoleByName(model.NewId())
|
||||
require.Error(t, err)
|
||||
CheckNotFoundStatus(t, resp)
|
||||
})
|
||||
}
|
||||
@@ -129,32 +133,34 @@ func TestGetRolesByNames(t *testing.T) {
|
||||
|
||||
th.TestForAllClients(t, func(t *testing.T, client *model.Client4) {
|
||||
// Check all three roles can be found.
|
||||
received, resp := client.GetRolesByNames([]string{role1.Name, role2.Name, role3.Name})
|
||||
CheckNoError(t, resp)
|
||||
received, _, err := client.GetRolesByNames([]string{role1.Name, role2.Name, role3.Name})
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Contains(t, received, role1)
|
||||
assert.Contains(t, received, role2)
|
||||
assert.Contains(t, received, role3)
|
||||
|
||||
// Check a list of non-existent roles.
|
||||
_, resp = client.GetRolesByNames([]string{model.NewId(), model.NewId()})
|
||||
CheckNoError(t, resp)
|
||||
_, _, err = client.GetRolesByNames([]string{model.NewId(), model.NewId()})
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) {
|
||||
// Empty list should error.
|
||||
_, resp := client.GetRolesByNames([]string{})
|
||||
_, resp, err := client.GetRolesByNames([]string{})
|
||||
require.Error(t, err)
|
||||
CheckBadRequestStatus(t, resp)
|
||||
})
|
||||
|
||||
th.TestForAllClients(t, func(t *testing.T, client *model.Client4) {
|
||||
// Invalid role name should error.
|
||||
_, resp := client.GetRolesByNames([]string{model.NewId(), model.NewId(), "!!!!!!"})
|
||||
_, resp, err := client.GetRolesByNames([]string{model.NewId(), model.NewId(), "!!!!!!"})
|
||||
require.Error(t, err)
|
||||
CheckBadRequestStatus(t, resp)
|
||||
|
||||
// Empty/whitespace rolenames should be ignored.
|
||||
_, resp = client.GetRolesByNames([]string{model.NewId(), model.NewId(), "", " "})
|
||||
CheckNoError(t, resp)
|
||||
_, _, err = client.GetRolesByNames([]string{model.NewId(), model.NewId(), "", " "})
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
}
|
||||
@@ -171,8 +177,8 @@ func TestPatchRole(t *testing.T) {
|
||||
SchemeManaged: true,
|
||||
}
|
||||
|
||||
role, err := th.App.Srv().Store.Role().Save(role)
|
||||
assert.NoError(t, err)
|
||||
role, err2 := th.App.Srv().Store.Role().Save(role)
|
||||
assert.NoError(t, err2)
|
||||
defer th.App.Srv().Store.Job().Delete(role.Id)
|
||||
|
||||
patch := &model.RolePatch{
|
||||
@@ -186,7 +192,8 @@ func TestPatchRole(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
defer th.App.Srv().Store.Job().Delete(adminRole.Id)
|
||||
|
||||
_, resp := client.PatchRole(adminRole.Id, patch)
|
||||
_, resp, err := client.PatchRole(adminRole.Id, patch)
|
||||
require.Error(t, err)
|
||||
CheckNotImplementedStatus(t, resp)
|
||||
|
||||
// Cannot give other roles read / write to system roles or manage roles because only system admin can do these actions
|
||||
@@ -198,27 +205,30 @@ func TestPatchRole(t *testing.T) {
|
||||
Permissions: &[]string{model.PermissionSysconsoleWriteUserManagementSystemRoles.Id},
|
||||
}
|
||||
|
||||
_, resp = client.PatchRole(systemManager.Id, patchWriteSystemRoles)
|
||||
_, resp, err = client.PatchRole(systemManager.Id, patchWriteSystemRoles)
|
||||
require.Error(t, err)
|
||||
CheckNotImplementedStatus(t, resp)
|
||||
|
||||
patchReadSystemRoles := &model.RolePatch{
|
||||
Permissions: &[]string{model.PermissionSysconsoleReadUserManagementSystemRoles.Id},
|
||||
}
|
||||
|
||||
_, resp = client.PatchRole(systemManager.Id, patchReadSystemRoles)
|
||||
_, resp, err = client.PatchRole(systemManager.Id, patchReadSystemRoles)
|
||||
require.Error(t, err)
|
||||
CheckNotImplementedStatus(t, resp)
|
||||
|
||||
patchManageRoles := &model.RolePatch{
|
||||
Permissions: &[]string{model.PermissionManageRoles.Id},
|
||||
}
|
||||
|
||||
_, resp = client.PatchRole(systemManager.Id, patchManageRoles)
|
||||
_, resp, err = client.PatchRole(systemManager.Id, patchManageRoles)
|
||||
require.Error(t, err)
|
||||
CheckNotImplementedStatus(t, resp)
|
||||
})
|
||||
|
||||
th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) {
|
||||
received, resp := client.PatchRole(role.Id, patch)
|
||||
CheckNoError(t, resp)
|
||||
received, _, err := client.PatchRole(role.Id, patch)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, received.Id, role.Id)
|
||||
assert.Equal(t, received.Name, role.Name)
|
||||
@@ -230,17 +240,20 @@ func TestPatchRole(t *testing.T) {
|
||||
assert.Equal(t, received.SchemeManaged, role.SchemeManaged)
|
||||
|
||||
// Check a no-op patch succeeds.
|
||||
_, resp = client.PatchRole(role.Id, patch)
|
||||
CheckNoError(t, resp)
|
||||
_, _, err = client.PatchRole(role.Id, patch)
|
||||
require.NoError(t, err)
|
||||
|
||||
_, resp = client.PatchRole("junk", patch)
|
||||
_, resp, err := client.PatchRole("junk", patch)
|
||||
require.Error(t, err)
|
||||
CheckBadRequestStatus(t, resp)
|
||||
})
|
||||
|
||||
_, resp := th.Client.PatchRole(model.NewId(), patch)
|
||||
_, resp, err := th.Client.PatchRole(model.NewId(), patch)
|
||||
require.Error(t, err)
|
||||
CheckNotFoundStatus(t, resp)
|
||||
|
||||
_, resp = th.Client.PatchRole(role.Id, patch)
|
||||
_, resp, err = th.Client.PatchRole(role.Id, patch)
|
||||
require.Error(t, err)
|
||||
CheckForbiddenStatus(t, resp)
|
||||
|
||||
patch = &model.RolePatch{
|
||||
@@ -248,8 +261,8 @@ func TestPatchRole(t *testing.T) {
|
||||
}
|
||||
|
||||
th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) {
|
||||
received, resp := client.PatchRole(role.Id, patch)
|
||||
CheckNoError(t, resp)
|
||||
received, _, err := client.PatchRole(role.Id, patch)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, received.Id, role.Id)
|
||||
assert.Equal(t, received.Name, role.Name)
|
||||
@@ -267,7 +280,8 @@ func TestPatchRole(t *testing.T) {
|
||||
|
||||
guestRole, err := th.App.Srv().Store.Role().GetByName(context.Background(), "system_guest")
|
||||
require.NoError(t, err)
|
||||
received, resp = client.PatchRole(guestRole.Id, patch)
|
||||
received, resp, err = client.PatchRole(guestRole.Id, patch)
|
||||
require.Error(t, err)
|
||||
CheckNotImplementedStatus(t, resp)
|
||||
})
|
||||
|
||||
@@ -277,8 +291,8 @@ func TestPatchRole(t *testing.T) {
|
||||
th.App.Srv().SetLicense(license)
|
||||
guestRole, err := th.App.Srv().Store.Role().GetByName(context.Background(), "system_guest")
|
||||
require.NoError(t, err)
|
||||
_, resp = client.PatchRole(guestRole.Id, patch)
|
||||
CheckNoError(t, resp)
|
||||
_, _, err = client.PatchRole(guestRole.Id, patch)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user