[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
@@ -17,8 +17,8 @@ import (
|
||||
func TestCreateOAuthApp(t *testing.T) {
|
||||
th := Setup(t)
|
||||
defer th.TearDown()
|
||||
Client := th.Client
|
||||
AdminClient := th.SystemAdminClient
|
||||
client := th.Client
|
||||
adminClient := th.SystemAdminClient
|
||||
|
||||
defaultRolePermissions := th.SaveDefaultRolePermissions()
|
||||
enableOAuthServiceProvider := th.App.Config().ServiceSettings.EnableOAuthServiceProvider
|
||||
@@ -34,8 +34,8 @@ func TestCreateOAuthApp(t *testing.T) {
|
||||
|
||||
oapp := &model.OAuthApp{Name: GenerateTestAppName(), Homepage: "https://nowhere.com", Description: "test", CallbackUrls: []string{"https://nowhere.com"}, IsTrusted: true}
|
||||
|
||||
rapp, resp := AdminClient.CreateOAuthApp(oapp)
|
||||
CheckNoError(t, resp)
|
||||
rapp, resp, err := adminClient.CreateOAuthApp(oapp)
|
||||
require.NoError(t, err)
|
||||
CheckCreatedStatus(t, resp)
|
||||
assert.Equal(t, oapp.Name, rapp.Name, "names did not match")
|
||||
assert.Equal(t, oapp.IsTrusted, rapp.IsTrusted, "trusted did no match")
|
||||
@@ -43,40 +43,44 @@ func TestCreateOAuthApp(t *testing.T) {
|
||||
// Revoke permission from regular users.
|
||||
th.RemovePermissionFromRole(model.PermissionManageOAuth.Id, model.SystemUserRoleId)
|
||||
|
||||
_, resp = Client.CreateOAuthApp(oapp)
|
||||
_, resp, err = client.CreateOAuthApp(oapp)
|
||||
require.Error(t, err)
|
||||
CheckForbiddenStatus(t, resp)
|
||||
// Grant permission to regular users.
|
||||
th.AddPermissionToRole(model.PermissionManageOAuth.Id, model.SystemUserRoleId)
|
||||
|
||||
rapp, resp = Client.CreateOAuthApp(oapp)
|
||||
CheckNoError(t, resp)
|
||||
rapp, resp, err = client.CreateOAuthApp(oapp)
|
||||
require.NoError(t, err)
|
||||
CheckCreatedStatus(t, resp)
|
||||
|
||||
assert.False(t, rapp.IsTrusted, "trusted should be false - created by non admin")
|
||||
|
||||
oapp.Name = ""
|
||||
_, resp = AdminClient.CreateOAuthApp(oapp)
|
||||
_, resp, err = adminClient.CreateOAuthApp(oapp)
|
||||
require.Error(t, err)
|
||||
CheckBadRequestStatus(t, resp)
|
||||
|
||||
r, err := Client.DoApiPost("/oauth/apps", "garbage")
|
||||
require.NotNil(t, err, "expected error from garbage post")
|
||||
r, err := client.DoApiPost("/oauth/apps", "garbage")
|
||||
require.Error(t, err, "expected error from garbage post")
|
||||
assert.Equal(t, http.StatusBadRequest, r.StatusCode)
|
||||
|
||||
Client.Logout()
|
||||
_, resp = Client.CreateOAuthApp(oapp)
|
||||
client.Logout()
|
||||
_, resp, err = client.CreateOAuthApp(oapp)
|
||||
require.Error(t, err)
|
||||
CheckUnauthorizedStatus(t, resp)
|
||||
|
||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableOAuthServiceProvider = false })
|
||||
oapp.Name = GenerateTestAppName()
|
||||
_, resp = AdminClient.CreateOAuthApp(oapp)
|
||||
_, resp, err = adminClient.CreateOAuthApp(oapp)
|
||||
require.Error(t, err)
|
||||
CheckNotImplementedStatus(t, resp)
|
||||
}
|
||||
|
||||
func TestUpdateOAuthApp(t *testing.T) {
|
||||
th := Setup(t).InitBasic()
|
||||
defer th.TearDown()
|
||||
Client := th.Client
|
||||
AdminClient := th.SystemAdminClient
|
||||
client := th.Client
|
||||
adminClient := th.SystemAdminClient
|
||||
|
||||
defaultRolePermissions := th.SaveDefaultRolePermissions()
|
||||
enableOAuthServiceProvider := th.App.Config().ServiceSettings.EnableOAuthServiceProvider
|
||||
@@ -98,7 +102,7 @@ func TestUpdateOAuthApp(t *testing.T) {
|
||||
CallbackUrls: []string{"https://callback.com"},
|
||||
}
|
||||
|
||||
oapp, _ = AdminClient.CreateOAuthApp(oapp)
|
||||
oapp, _, _ = adminClient.CreateOAuthApp(oapp)
|
||||
|
||||
oapp.Name = "oapp_update"
|
||||
oapp.IsTrusted = true
|
||||
@@ -107,8 +111,8 @@ func TestUpdateOAuthApp(t *testing.T) {
|
||||
oapp.Description = "test_update"
|
||||
oapp.CallbackUrls = []string{"https://callback_update.com", "https://another_callback.com"}
|
||||
|
||||
updatedApp, resp := AdminClient.UpdateOAuthApp(oapp)
|
||||
CheckNoError(t, resp)
|
||||
updatedApp, _, err := adminClient.UpdateOAuthApp(oapp)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, oapp.Id, updatedApp.Id, "Id should have not updated")
|
||||
assert.Equal(t, oapp.CreatorId, updatedApp.CreatorId, "CreatorId should have not updated")
|
||||
assert.Equal(t, oapp.CreateAt, updatedApp.CreateAt, "CreateAt should have not updated")
|
||||
@@ -128,7 +132,8 @@ func TestUpdateOAuthApp(t *testing.T) {
|
||||
|
||||
th.LoginBasic2()
|
||||
updatedApp.CreatorId = th.BasicUser2.Id
|
||||
_, resp = Client.UpdateOAuthApp(oapp)
|
||||
_, resp, err := client.UpdateOAuthApp(oapp)
|
||||
require.Error(t, err)
|
||||
CheckForbiddenStatus(t, resp)
|
||||
|
||||
th.LoginBasic()
|
||||
@@ -136,24 +141,29 @@ func TestUpdateOAuthApp(t *testing.T) {
|
||||
// Revoke permission from regular users.
|
||||
th.RemovePermissionFromRole(model.PermissionManageOAuth.Id, model.SystemUserRoleId)
|
||||
|
||||
_, resp = Client.UpdateOAuthApp(oapp)
|
||||
_, resp, err = client.UpdateOAuthApp(oapp)
|
||||
require.Error(t, err)
|
||||
CheckForbiddenStatus(t, resp)
|
||||
|
||||
oapp.Id = "zhk9d1ggatrqz236c7h87im7bc"
|
||||
_, resp = AdminClient.UpdateOAuthApp(oapp)
|
||||
_, resp, err = adminClient.UpdateOAuthApp(oapp)
|
||||
require.Error(t, err)
|
||||
CheckNotFoundStatus(t, resp)
|
||||
|
||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableOAuthServiceProvider = false })
|
||||
|
||||
_, resp = AdminClient.UpdateOAuthApp(oapp)
|
||||
_, resp, err = adminClient.UpdateOAuthApp(oapp)
|
||||
require.Error(t, err)
|
||||
CheckNotImplementedStatus(t, resp)
|
||||
|
||||
Client.Logout()
|
||||
_, resp = Client.UpdateOAuthApp(oapp)
|
||||
client.Logout()
|
||||
_, resp, err = client.UpdateOAuthApp(oapp)
|
||||
require.Error(t, err)
|
||||
CheckUnauthorizedStatus(t, resp)
|
||||
|
||||
oapp.Id = "junk"
|
||||
_, resp = AdminClient.UpdateOAuthApp(oapp)
|
||||
_, resp, err = adminClient.UpdateOAuthApp(oapp)
|
||||
require.Error(t, err)
|
||||
CheckBadRequestStatus(t, resp)
|
||||
|
||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableOAuthServiceProvider = true })
|
||||
@@ -169,30 +179,30 @@ func TestUpdateOAuthApp(t *testing.T) {
|
||||
CallbackUrls: []string{"https://callback.com"},
|
||||
}
|
||||
|
||||
userOapp, resp = Client.CreateOAuthApp(userOapp)
|
||||
CheckNoError(t, resp)
|
||||
userOapp, _, err = client.CreateOAuthApp(userOapp)
|
||||
require.NoError(t, err)
|
||||
|
||||
userOapp.IsTrusted = true
|
||||
userOapp, resp = Client.UpdateOAuthApp(userOapp)
|
||||
CheckNoError(t, resp)
|
||||
userOapp, _, err = client.UpdateOAuthApp(userOapp)
|
||||
require.NoError(t, err)
|
||||
assert.False(t, userOapp.IsTrusted)
|
||||
|
||||
userOapp.IsTrusted = true
|
||||
userOapp, resp = AdminClient.UpdateOAuthApp(userOapp)
|
||||
CheckNoError(t, resp)
|
||||
userOapp, _, err = adminClient.UpdateOAuthApp(userOapp)
|
||||
require.NoError(t, err)
|
||||
assert.True(t, userOapp.IsTrusted)
|
||||
|
||||
userOapp.IsTrusted = false
|
||||
userOapp, resp = Client.UpdateOAuthApp(userOapp)
|
||||
CheckNoError(t, resp)
|
||||
userOapp, _, err = client.UpdateOAuthApp(userOapp)
|
||||
require.NoError(t, err)
|
||||
assert.True(t, userOapp.IsTrusted)
|
||||
}
|
||||
|
||||
func TestGetOAuthApps(t *testing.T) {
|
||||
th := Setup(t)
|
||||
defer th.TearDown()
|
||||
Client := th.Client
|
||||
AdminClient := th.SystemAdminClient
|
||||
client := th.Client
|
||||
adminClient := th.SystemAdminClient
|
||||
|
||||
defaultRolePermissions := th.SaveDefaultRolePermissions()
|
||||
enableOAuthServiceProvider := th.App.Config().ServiceSettings.EnableOAuthServiceProvider
|
||||
@@ -207,15 +217,15 @@ func TestGetOAuthApps(t *testing.T) {
|
||||
|
||||
oapp := &model.OAuthApp{Name: GenerateTestAppName(), Homepage: "https://nowhere.com", Description: "test", CallbackUrls: []string{"https://nowhere.com"}}
|
||||
|
||||
rapp, resp := AdminClient.CreateOAuthApp(oapp)
|
||||
CheckNoError(t, resp)
|
||||
rapp, _, err := adminClient.CreateOAuthApp(oapp)
|
||||
require.NoError(t, err)
|
||||
|
||||
oapp.Name = GenerateTestAppName()
|
||||
rapp2, resp := Client.CreateOAuthApp(oapp)
|
||||
CheckNoError(t, resp)
|
||||
rapp2, _, err := client.CreateOAuthApp(oapp)
|
||||
require.NoError(t, err)
|
||||
|
||||
apps, resp := AdminClient.GetOAuthApps(0, 1000)
|
||||
CheckNoError(t, resp)
|
||||
apps, _, err := adminClient.GetOAuthApps(0, 1000)
|
||||
require.NoError(t, err)
|
||||
|
||||
found1 := false
|
||||
found2 := false
|
||||
@@ -230,35 +240,38 @@ func TestGetOAuthApps(t *testing.T) {
|
||||
assert.Truef(t, found1, "missing oauth app %v", rapp.Id)
|
||||
assert.Truef(t, found2, "missing oauth app %v", rapp2.Id)
|
||||
|
||||
apps, resp = AdminClient.GetOAuthApps(1, 1)
|
||||
CheckNoError(t, resp)
|
||||
apps, _, err = adminClient.GetOAuthApps(1, 1)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 1, len(apps), "paging failed")
|
||||
|
||||
apps, resp = Client.GetOAuthApps(0, 1000)
|
||||
CheckNoError(t, resp)
|
||||
apps, _, err = client.GetOAuthApps(0, 1000)
|
||||
require.NoError(t, err)
|
||||
require.True(t, len(apps) == 1 || apps[0].Id == rapp2.Id, "wrong apps returned")
|
||||
|
||||
// Revoke permission from regular users.
|
||||
th.RemovePermissionFromRole(model.PermissionManageOAuth.Id, model.SystemUserRoleId)
|
||||
|
||||
_, resp = Client.GetOAuthApps(0, 1000)
|
||||
_, resp, err := client.GetOAuthApps(0, 1000)
|
||||
require.Error(t, err)
|
||||
CheckForbiddenStatus(t, resp)
|
||||
|
||||
Client.Logout()
|
||||
client.Logout()
|
||||
|
||||
_, resp = Client.GetOAuthApps(0, 1000)
|
||||
_, resp, err = client.GetOAuthApps(0, 1000)
|
||||
require.Error(t, err)
|
||||
CheckUnauthorizedStatus(t, resp)
|
||||
|
||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableOAuthServiceProvider = false })
|
||||
_, resp = AdminClient.GetOAuthApps(0, 1000)
|
||||
_, resp, err = adminClient.GetOAuthApps(0, 1000)
|
||||
require.Error(t, err)
|
||||
CheckNotImplementedStatus(t, resp)
|
||||
}
|
||||
|
||||
func TestGetOAuthApp(t *testing.T) {
|
||||
th := Setup(t)
|
||||
defer th.TearDown()
|
||||
Client := th.Client
|
||||
AdminClient := th.SystemAdminClient
|
||||
client := th.Client
|
||||
adminClient := th.SystemAdminClient
|
||||
|
||||
defaultRolePermissions := th.SaveDefaultRolePermissions()
|
||||
enableOAuthServiceProvider := th.App.Config().ServiceSettings.EnableOAuthServiceProvider
|
||||
@@ -273,56 +286,62 @@ func TestGetOAuthApp(t *testing.T) {
|
||||
|
||||
oapp := &model.OAuthApp{Name: GenerateTestAppName(), Homepage: "https://nowhere.com", Description: "test", CallbackUrls: []string{"https://nowhere.com"}}
|
||||
|
||||
rapp, resp := AdminClient.CreateOAuthApp(oapp)
|
||||
CheckNoError(t, resp)
|
||||
rapp, _, err := adminClient.CreateOAuthApp(oapp)
|
||||
require.NoError(t, err)
|
||||
|
||||
oapp.Name = GenerateTestAppName()
|
||||
rapp2, resp := Client.CreateOAuthApp(oapp)
|
||||
CheckNoError(t, resp)
|
||||
rapp2, _, err := client.CreateOAuthApp(oapp)
|
||||
require.NoError(t, err)
|
||||
|
||||
rrapp, resp := AdminClient.GetOAuthApp(rapp.Id)
|
||||
CheckNoError(t, resp)
|
||||
rrapp, _, err := adminClient.GetOAuthApp(rapp.Id)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, rapp.Id, rrapp.Id, "wrong app")
|
||||
assert.NotEqual(t, "", rrapp.ClientSecret, "should not be sanitized")
|
||||
|
||||
rrapp2, resp := AdminClient.GetOAuthApp(rapp2.Id)
|
||||
CheckNoError(t, resp)
|
||||
rrapp2, _, err := adminClient.GetOAuthApp(rapp2.Id)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, rapp2.Id, rrapp2.Id, "wrong app")
|
||||
assert.NotEqual(t, "", rrapp2.ClientSecret, "should not be sanitized")
|
||||
|
||||
_, resp = Client.GetOAuthApp(rapp2.Id)
|
||||
CheckNoError(t, resp)
|
||||
_, _, err = client.GetOAuthApp(rapp2.Id)
|
||||
require.NoError(t, err)
|
||||
|
||||
_, resp = Client.GetOAuthApp(rapp.Id)
|
||||
_, resp, err := client.GetOAuthApp(rapp.Id)
|
||||
require.Error(t, err)
|
||||
CheckForbiddenStatus(t, resp)
|
||||
|
||||
// Revoke permission from regular users.
|
||||
th.RemovePermissionFromRole(model.PermissionManageOAuth.Id, model.SystemUserRoleId)
|
||||
|
||||
_, resp = Client.GetOAuthApp(rapp2.Id)
|
||||
_, resp, err = client.GetOAuthApp(rapp2.Id)
|
||||
require.Error(t, err)
|
||||
CheckForbiddenStatus(t, resp)
|
||||
|
||||
Client.Logout()
|
||||
client.Logout()
|
||||
|
||||
_, resp = Client.GetOAuthApp(rapp2.Id)
|
||||
_, resp, err = client.GetOAuthApp(rapp2.Id)
|
||||
require.Error(t, err)
|
||||
CheckUnauthorizedStatus(t, resp)
|
||||
|
||||
_, resp = AdminClient.GetOAuthApp("junk")
|
||||
_, resp, err = adminClient.GetOAuthApp("junk")
|
||||
require.Error(t, err)
|
||||
CheckBadRequestStatus(t, resp)
|
||||
|
||||
_, resp = AdminClient.GetOAuthApp(model.NewId())
|
||||
_, resp, err = adminClient.GetOAuthApp(model.NewId())
|
||||
require.Error(t, err)
|
||||
CheckNotFoundStatus(t, resp)
|
||||
|
||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableOAuthServiceProvider = false })
|
||||
_, resp = AdminClient.GetOAuthApp(rapp.Id)
|
||||
_, resp, err = adminClient.GetOAuthApp(rapp.Id)
|
||||
require.Error(t, err)
|
||||
CheckNotImplementedStatus(t, resp)
|
||||
}
|
||||
|
||||
func TestGetOAuthAppInfo(t *testing.T) {
|
||||
th := Setup(t)
|
||||
defer th.TearDown()
|
||||
Client := th.Client
|
||||
AdminClient := th.SystemAdminClient
|
||||
client := th.Client
|
||||
adminClient := th.SystemAdminClient
|
||||
|
||||
defaultRolePermissions := th.SaveDefaultRolePermissions()
|
||||
enableOAuthServiceProvider := th.App.Config().ServiceSettings.EnableOAuthServiceProvider
|
||||
@@ -337,56 +356,60 @@ func TestGetOAuthAppInfo(t *testing.T) {
|
||||
|
||||
oapp := &model.OAuthApp{Name: GenerateTestAppName(), Homepage: "https://nowhere.com", Description: "test", CallbackUrls: []string{"https://nowhere.com"}}
|
||||
|
||||
rapp, resp := AdminClient.CreateOAuthApp(oapp)
|
||||
CheckNoError(t, resp)
|
||||
rapp, _, err := adminClient.CreateOAuthApp(oapp)
|
||||
require.NoError(t, err)
|
||||
|
||||
oapp.Name = GenerateTestAppName()
|
||||
rapp2, resp := Client.CreateOAuthApp(oapp)
|
||||
CheckNoError(t, resp)
|
||||
rapp2, _, err := client.CreateOAuthApp(oapp)
|
||||
require.NoError(t, err)
|
||||
|
||||
rrapp, resp := AdminClient.GetOAuthAppInfo(rapp.Id)
|
||||
CheckNoError(t, resp)
|
||||
rrapp, _, err := adminClient.GetOAuthAppInfo(rapp.Id)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, rapp.Id, rrapp.Id, "wrong app")
|
||||
assert.Equal(t, "", rrapp.ClientSecret, "should be sanitized")
|
||||
|
||||
rrapp2, resp := AdminClient.GetOAuthAppInfo(rapp2.Id)
|
||||
CheckNoError(t, resp)
|
||||
rrapp2, _, err := adminClient.GetOAuthAppInfo(rapp2.Id)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, rapp2.Id, rrapp2.Id, "wrong app")
|
||||
assert.Equal(t, "", rrapp2.ClientSecret, "should be sanitized")
|
||||
|
||||
_, resp = Client.GetOAuthAppInfo(rapp2.Id)
|
||||
CheckNoError(t, resp)
|
||||
_, _, err = client.GetOAuthAppInfo(rapp2.Id)
|
||||
require.NoError(t, err)
|
||||
|
||||
_, resp = Client.GetOAuthAppInfo(rapp.Id)
|
||||
CheckNoError(t, resp)
|
||||
_, _, err = client.GetOAuthAppInfo(rapp.Id)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Revoke permission from regular users.
|
||||
th.RemovePermissionFromRole(model.PermissionManageOAuth.Id, model.SystemUserRoleId)
|
||||
|
||||
_, resp = Client.GetOAuthAppInfo(rapp2.Id)
|
||||
CheckNoError(t, resp)
|
||||
_, _, err = client.GetOAuthAppInfo(rapp2.Id)
|
||||
require.NoError(t, err)
|
||||
|
||||
Client.Logout()
|
||||
client.Logout()
|
||||
|
||||
_, resp = Client.GetOAuthAppInfo(rapp2.Id)
|
||||
_, resp, err := client.GetOAuthAppInfo(rapp2.Id)
|
||||
require.Error(t, err)
|
||||
CheckUnauthorizedStatus(t, resp)
|
||||
|
||||
_, resp = AdminClient.GetOAuthAppInfo("junk")
|
||||
_, resp, err = adminClient.GetOAuthAppInfo("junk")
|
||||
require.Error(t, err)
|
||||
CheckBadRequestStatus(t, resp)
|
||||
|
||||
_, resp = AdminClient.GetOAuthAppInfo(model.NewId())
|
||||
_, resp, err = adminClient.GetOAuthAppInfo(model.NewId())
|
||||
require.Error(t, err)
|
||||
CheckNotFoundStatus(t, resp)
|
||||
|
||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableOAuthServiceProvider = false })
|
||||
_, resp = AdminClient.GetOAuthAppInfo(rapp.Id)
|
||||
_, resp, err = adminClient.GetOAuthAppInfo(rapp.Id)
|
||||
require.Error(t, err)
|
||||
CheckNotImplementedStatus(t, resp)
|
||||
}
|
||||
|
||||
func TestDeleteOAuthApp(t *testing.T) {
|
||||
th := Setup(t)
|
||||
defer th.TearDown()
|
||||
Client := th.Client
|
||||
AdminClient := th.SystemAdminClient
|
||||
client := th.Client
|
||||
adminClient := th.SystemAdminClient
|
||||
|
||||
defaultRolePermissions := th.SaveDefaultRolePermissions()
|
||||
enableOAuthServiceProvider := th.App.Config().ServiceSettings.EnableOAuthServiceProvider
|
||||
@@ -401,59 +424,64 @@ func TestDeleteOAuthApp(t *testing.T) {
|
||||
|
||||
oapp := &model.OAuthApp{Name: GenerateTestAppName(), Homepage: "https://nowhere.com", Description: "test", CallbackUrls: []string{"https://nowhere.com"}}
|
||||
|
||||
rapp, resp := AdminClient.CreateOAuthApp(oapp)
|
||||
CheckNoError(t, resp)
|
||||
rapp, _, err := adminClient.CreateOAuthApp(oapp)
|
||||
require.NoError(t, err)
|
||||
|
||||
oapp.Name = GenerateTestAppName()
|
||||
rapp2, resp := Client.CreateOAuthApp(oapp)
|
||||
CheckNoError(t, resp)
|
||||
rapp2, _, err := client.CreateOAuthApp(oapp)
|
||||
require.NoError(t, err)
|
||||
|
||||
pass, resp := AdminClient.DeleteOAuthApp(rapp.Id)
|
||||
CheckNoError(t, resp)
|
||||
assert.True(t, pass, "should have passed")
|
||||
_, err = adminClient.DeleteOAuthApp(rapp.Id)
|
||||
require.NoError(t, err)
|
||||
|
||||
_, resp = AdminClient.DeleteOAuthApp(rapp2.Id)
|
||||
CheckNoError(t, resp)
|
||||
_, err = adminClient.DeleteOAuthApp(rapp2.Id)
|
||||
require.NoError(t, err)
|
||||
|
||||
rapp, resp = AdminClient.CreateOAuthApp(oapp)
|
||||
CheckNoError(t, resp)
|
||||
rapp, _, err = adminClient.CreateOAuthApp(oapp)
|
||||
require.NoError(t, err)
|
||||
|
||||
oapp.Name = GenerateTestAppName()
|
||||
rapp2, resp = Client.CreateOAuthApp(oapp)
|
||||
CheckNoError(t, resp)
|
||||
rapp2, _, err = client.CreateOAuthApp(oapp)
|
||||
require.NoError(t, err)
|
||||
|
||||
_, resp = Client.DeleteOAuthApp(rapp.Id)
|
||||
resp, err := client.DeleteOAuthApp(rapp.Id)
|
||||
require.Error(t, err)
|
||||
CheckForbiddenStatus(t, resp)
|
||||
|
||||
_, resp = Client.DeleteOAuthApp(rapp2.Id)
|
||||
CheckNoError(t, resp)
|
||||
_, err = client.DeleteOAuthApp(rapp2.Id)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Revoke permission from regular users.
|
||||
th.RemovePermissionFromRole(model.PermissionManageOAuth.Id, model.SystemUserRoleId)
|
||||
|
||||
_, resp = Client.DeleteOAuthApp(rapp.Id)
|
||||
resp, err = client.DeleteOAuthApp(rapp.Id)
|
||||
require.Error(t, err)
|
||||
CheckForbiddenStatus(t, resp)
|
||||
|
||||
Client.Logout()
|
||||
_, resp = Client.DeleteOAuthApp(rapp.Id)
|
||||
client.Logout()
|
||||
resp, err = client.DeleteOAuthApp(rapp.Id)
|
||||
require.Error(t, err)
|
||||
CheckUnauthorizedStatus(t, resp)
|
||||
|
||||
_, resp = AdminClient.DeleteOAuthApp("junk")
|
||||
resp, err = adminClient.DeleteOAuthApp("junk")
|
||||
require.Error(t, err)
|
||||
CheckBadRequestStatus(t, resp)
|
||||
|
||||
_, resp = AdminClient.DeleteOAuthApp(model.NewId())
|
||||
resp, err = adminClient.DeleteOAuthApp(model.NewId())
|
||||
require.Error(t, err)
|
||||
CheckNotFoundStatus(t, resp)
|
||||
|
||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableOAuthServiceProvider = false })
|
||||
_, resp = AdminClient.DeleteOAuthApp(rapp.Id)
|
||||
resp, err = adminClient.DeleteOAuthApp(rapp.Id)
|
||||
require.Error(t, err)
|
||||
CheckNotImplementedStatus(t, resp)
|
||||
}
|
||||
|
||||
func TestRegenerateOAuthAppSecret(t *testing.T) {
|
||||
th := Setup(t)
|
||||
defer th.TearDown()
|
||||
Client := th.Client
|
||||
AdminClient := th.SystemAdminClient
|
||||
client := th.Client
|
||||
adminClient := th.SystemAdminClient
|
||||
|
||||
defaultRolePermissions := th.SaveDefaultRolePermissions()
|
||||
enableOAuthServiceProvider := th.App.Config().ServiceSettings.EnableOAuthServiceProvider
|
||||
@@ -468,60 +496,66 @@ func TestRegenerateOAuthAppSecret(t *testing.T) {
|
||||
|
||||
oapp := &model.OAuthApp{Name: GenerateTestAppName(), Homepage: "https://nowhere.com", Description: "test", CallbackUrls: []string{"https://nowhere.com"}}
|
||||
|
||||
rapp, resp := AdminClient.CreateOAuthApp(oapp)
|
||||
CheckNoError(t, resp)
|
||||
rapp, _, err := adminClient.CreateOAuthApp(oapp)
|
||||
require.NoError(t, err)
|
||||
|
||||
oapp.Name = GenerateTestAppName()
|
||||
rapp2, resp := Client.CreateOAuthApp(oapp)
|
||||
CheckNoError(t, resp)
|
||||
rapp2, _, err := client.CreateOAuthApp(oapp)
|
||||
require.NoError(t, err)
|
||||
|
||||
rrapp, resp := AdminClient.RegenerateOAuthAppSecret(rapp.Id)
|
||||
CheckNoError(t, resp)
|
||||
rrapp, _, err := adminClient.RegenerateOAuthAppSecret(rapp.Id)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, rrapp.Id, rapp.Id, "wrong app")
|
||||
assert.NotEqual(t, rapp.ClientSecret, rrapp.ClientSecret, "secret didn't change")
|
||||
|
||||
_, resp = AdminClient.RegenerateOAuthAppSecret(rapp2.Id)
|
||||
CheckNoError(t, resp)
|
||||
_, _, err = adminClient.RegenerateOAuthAppSecret(rapp2.Id)
|
||||
require.NoError(t, err)
|
||||
|
||||
rapp, resp = AdminClient.CreateOAuthApp(oapp)
|
||||
CheckNoError(t, resp)
|
||||
rapp, _, err = adminClient.CreateOAuthApp(oapp)
|
||||
require.NoError(t, err)
|
||||
|
||||
oapp.Name = GenerateTestAppName()
|
||||
rapp2, resp = Client.CreateOAuthApp(oapp)
|
||||
CheckNoError(t, resp)
|
||||
rapp2, _, err = client.CreateOAuthApp(oapp)
|
||||
require.NoError(t, err)
|
||||
|
||||
_, resp = Client.RegenerateOAuthAppSecret(rapp.Id)
|
||||
_, resp, err := client.RegenerateOAuthAppSecret(rapp.Id)
|
||||
require.Error(t, err)
|
||||
CheckForbiddenStatus(t, resp)
|
||||
|
||||
_, resp = Client.RegenerateOAuthAppSecret(rapp2.Id)
|
||||
CheckNoError(t, resp)
|
||||
_, _, err = client.RegenerateOAuthAppSecret(rapp2.Id)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Revoke permission from regular users.
|
||||
th.RemovePermissionFromRole(model.PermissionManageOAuth.Id, model.SystemUserRoleId)
|
||||
|
||||
_, resp = Client.RegenerateOAuthAppSecret(rapp.Id)
|
||||
_, resp, err = client.RegenerateOAuthAppSecret(rapp.Id)
|
||||
require.Error(t, err)
|
||||
CheckForbiddenStatus(t, resp)
|
||||
|
||||
Client.Logout()
|
||||
_, resp = Client.RegenerateOAuthAppSecret(rapp.Id)
|
||||
client.Logout()
|
||||
_, resp, err = client.RegenerateOAuthAppSecret(rapp.Id)
|
||||
require.Error(t, err)
|
||||
CheckUnauthorizedStatus(t, resp)
|
||||
|
||||
_, resp = AdminClient.RegenerateOAuthAppSecret("junk")
|
||||
_, resp, err = adminClient.RegenerateOAuthAppSecret("junk")
|
||||
require.Error(t, err)
|
||||
CheckBadRequestStatus(t, resp)
|
||||
|
||||
_, resp = AdminClient.RegenerateOAuthAppSecret(model.NewId())
|
||||
_, resp, err = adminClient.RegenerateOAuthAppSecret(model.NewId())
|
||||
require.Error(t, err)
|
||||
CheckNotFoundStatus(t, resp)
|
||||
|
||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableOAuthServiceProvider = false })
|
||||
_, resp = AdminClient.RegenerateOAuthAppSecret(rapp.Id)
|
||||
_, resp, err = adminClient.RegenerateOAuthAppSecret(rapp.Id)
|
||||
require.Error(t, err)
|
||||
CheckNotImplementedStatus(t, resp)
|
||||
}
|
||||
|
||||
func TestGetAuthorizedOAuthAppsForUser(t *testing.T) {
|
||||
th := Setup(t).InitBasic()
|
||||
defer th.TearDown()
|
||||
Client := th.Client
|
||||
AdminClient := th.SystemAdminClient
|
||||
client := th.Client
|
||||
adminClient := th.SystemAdminClient
|
||||
|
||||
enableOAuth := th.App.Config().ServiceSettings.EnableOAuthServiceProvider
|
||||
defer func() {
|
||||
@@ -531,8 +565,8 @@ func TestGetAuthorizedOAuthAppsForUser(t *testing.T) {
|
||||
|
||||
oapp := &model.OAuthApp{Name: GenerateTestAppName(), Homepage: "https://nowhere.com", Description: "test", CallbackUrls: []string{"https://nowhere.com"}}
|
||||
|
||||
rapp, resp := AdminClient.CreateOAuthApp(oapp)
|
||||
CheckNoError(t, resp)
|
||||
rapp, _, err := adminClient.CreateOAuthApp(oapp)
|
||||
require.NoError(t, err)
|
||||
|
||||
authRequest := &model.AuthorizeRequest{
|
||||
ResponseType: model.AuthCodeResponseType,
|
||||
@@ -542,11 +576,11 @@ func TestGetAuthorizedOAuthAppsForUser(t *testing.T) {
|
||||
State: "123",
|
||||
}
|
||||
|
||||
_, resp = Client.AuthorizeOAuthApp(authRequest)
|
||||
CheckNoError(t, resp)
|
||||
_, _, err = client.AuthorizeOAuthApp(authRequest)
|
||||
require.NoError(t, err)
|
||||
|
||||
apps, resp := Client.GetAuthorizedOAuthAppsForUser(th.BasicUser.Id, 0, 1000)
|
||||
CheckNoError(t, resp)
|
||||
apps, _, err := client.GetAuthorizedOAuthAppsForUser(th.BasicUser.Id, 0, 1000)
|
||||
require.NoError(t, err)
|
||||
|
||||
found := false
|
||||
for _, a := range apps {
|
||||
@@ -557,18 +591,21 @@ func TestGetAuthorizedOAuthAppsForUser(t *testing.T) {
|
||||
}
|
||||
require.True(t, found, "missing app")
|
||||
|
||||
_, resp = Client.GetAuthorizedOAuthAppsForUser(th.BasicUser2.Id, 0, 1000)
|
||||
_, resp, err := client.GetAuthorizedOAuthAppsForUser(th.BasicUser2.Id, 0, 1000)
|
||||
require.Error(t, err)
|
||||
CheckForbiddenStatus(t, resp)
|
||||
|
||||
_, resp = Client.GetAuthorizedOAuthAppsForUser("junk", 0, 1000)
|
||||
_, resp, err = client.GetAuthorizedOAuthAppsForUser("junk", 0, 1000)
|
||||
require.Error(t, err)
|
||||
CheckBadRequestStatus(t, resp)
|
||||
|
||||
Client.Logout()
|
||||
_, resp = Client.GetAuthorizedOAuthAppsForUser(th.BasicUser.Id, 0, 1000)
|
||||
client.Logout()
|
||||
_, resp, err = client.GetAuthorizedOAuthAppsForUser(th.BasicUser.Id, 0, 1000)
|
||||
require.Error(t, err)
|
||||
CheckUnauthorizedStatus(t, resp)
|
||||
|
||||
_, resp = AdminClient.GetAuthorizedOAuthAppsForUser(th.BasicUser.Id, 0, 1000)
|
||||
CheckNoError(t, resp)
|
||||
_, _, err = adminClient.GetAuthorizedOAuthAppsForUser(th.BasicUser.Id, 0, 1000)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func closeBody(r *http.Response) {
|
||||
@@ -577,3 +614,13 @@ func closeBody(r *http.Response) {
|
||||
r.Body.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func TestNilAuthorizeOAuthApp(t *testing.T) {
|
||||
th := Setup(t).InitBasic()
|
||||
defer th.TearDown()
|
||||
client := th.Client
|
||||
|
||||
_, _, err := client.AuthorizeOAuthApp(nil)
|
||||
require.Error(t, err)
|
||||
CheckErrorID(t, err, "api.context.invalid_body_param.app_error")
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user