[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
Этот коммит содержится в:
Ben Schumacher
2021-08-13 13:12:16 +02:00
коммит произвёл GitHub
родитель 96593580ae
Коммит a8ca5c423f
60 изменённых файлов: 9790 добавлений и 8706 удалений

Просмотреть файл

@@ -23,14 +23,15 @@ import (
func TestGetConfig(t *testing.T) {
th := Setup(t)
defer th.TearDown()
Client := th.Client
client := th.Client
_, resp := Client.GetConfig()
_, resp, err := client.GetConfig()
require.Error(t, err)
CheckForbiddenStatus(t, resp)
th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) {
cfg, resp := client.GetConfig()
CheckNoError(t, resp)
cfg, _, err := client.GetConfig()
require.NoError(t, err)
require.NotEqual(t, "", cfg.TeamSettings.SiteName)
@@ -77,8 +78,8 @@ func TestGetConfigWithAccessTag(t *testing.T) {
th.AddPermissionToRole(model.PermissionSysconsoleReadEnvironmentRateLimiting.Id, model.SystemUserRoleId)
defer th.RemovePermissionFromRole(model.PermissionSysconsoleReadEnvironmentRateLimiting.Id, model.SystemUserRoleId)
cfg, resp := th.Client.GetConfig()
CheckNoError(t, resp)
cfg, _, err := th.Client.GetConfig()
require.NoError(t, err)
t.Run("Cannot read value without permission", func(t *testing.T) {
assert.Nil(t, cfg.SupportSettings.SupportEmail)
@@ -98,7 +99,7 @@ func TestGetConfigAnyFlagsAccess(t *testing.T) {
defer th.TearDown()
th.Client.Login(th.BasicUser.Username, th.BasicUser.Password)
_, resp := th.Client.GetConfig()
_, resp, _ := th.Client.GetConfig()
t.Run("Check permissions error with no sysconsole read permission", func(t *testing.T) {
CheckForbiddenStatus(t, resp)
@@ -108,8 +109,8 @@ func TestGetConfigAnyFlagsAccess(t *testing.T) {
th.AddPermissionToRole(model.PermissionSysconsoleReadEnvironmentRateLimiting.Id, model.SystemUserRoleId)
defer th.RemovePermissionFromRole(model.PermissionSysconsoleReadEnvironmentRateLimiting.Id, model.SystemUserRoleId)
cfg, resp := th.Client.GetConfig()
CheckNoError(t, resp)
cfg, _, err := th.Client.GetConfig()
require.NoError(t, err)
t.Run("Can read value with permission", func(t *testing.T) {
assert.NotNil(t, cfg.FeatureFlags)
})
@@ -118,59 +119,59 @@ func TestGetConfigAnyFlagsAccess(t *testing.T) {
func TestReloadConfig(t *testing.T) {
th := Setup(t)
defer th.TearDown()
Client := th.Client
client := th.Client
t.Run("as system user", func(t *testing.T) {
ok, resp := Client.ReloadConfig()
resp, err := client.ReloadConfig()
require.Error(t, err)
CheckForbiddenStatus(t, resp)
require.False(t, ok, "should not Reload the config due no permission.")
})
t.Run("as system admin", func(t *testing.T) {
ok, resp := th.SystemAdminClient.ReloadConfig()
CheckNoError(t, resp)
require.True(t, ok, "should Reload the config")
_, err := th.SystemAdminClient.ReloadConfig()
require.NoError(t, err)
})
t.Run("as restricted system admin", func(t *testing.T) {
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ExperimentalSettings.RestrictSystemAdmin = true })
ok, resp := Client.ReloadConfig()
resp, err := client.ReloadConfig()
require.Error(t, err)
CheckForbiddenStatus(t, resp)
require.False(t, ok, "should not Reload the config due no permission.")
})
}
func TestUpdateConfig(t *testing.T) {
th := Setup(t)
defer th.TearDown()
Client := th.Client
client := th.Client
cfg, resp := th.SystemAdminClient.GetConfig()
CheckNoError(t, resp)
cfg, _, err := th.SystemAdminClient.GetConfig()
require.NoError(t, err)
_, resp = Client.UpdateConfig(cfg)
_, resp, err := client.UpdateConfig(cfg)
require.Error(t, err)
CheckForbiddenStatus(t, resp)
th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) {
SiteName := th.App.Config().TeamSettings.SiteName
*cfg.TeamSettings.SiteName = "MyFancyName"
cfg, resp = client.UpdateConfig(cfg)
CheckNoError(t, resp)
cfg, _, err = client.UpdateConfig(cfg)
require.NoError(t, err)
require.Equal(t, "MyFancyName", *cfg.TeamSettings.SiteName, "It should update the SiteName")
//Revert the change
cfg.TeamSettings.SiteName = SiteName
cfg, resp = client.UpdateConfig(cfg)
CheckNoError(t, resp)
cfg, _, err = client.UpdateConfig(cfg)
require.NoError(t, err)
require.Equal(t, SiteName, cfg.TeamSettings.SiteName, "It should update the SiteName")
t.Run("Should set defaults for missing fields", func(t *testing.T) {
_, appErr := th.SystemAdminClient.DoApiPut(th.SystemAdminClient.GetConfigRoute(), "{}")
require.Nil(t, appErr)
_, err = th.SystemAdminClient.DoApiPut(th.SystemAdminClient.GetConfigRoute(), "{}")
require.NoError(t, err)
})
t.Run("Should fail with validation error if invalid config setting is passed", func(t *testing.T) {
@@ -178,23 +179,24 @@ func TestUpdateConfig(t *testing.T) {
badcfg := cfg.Clone()
badcfg.PasswordSettings.MinimumLength = model.NewInt(4)
badcfg.PasswordSettings.MinimumLength = model.NewInt(4)
_, resp = client.UpdateConfig(badcfg)
_, resp, err = client.UpdateConfig(badcfg)
require.Error(t, err)
CheckBadRequestStatus(t, resp)
CheckErrorMessage(t, resp, "model.config.is_valid.password_length.app_error")
CheckErrorID(t, err, "model.config.is_valid.password_length.app_error")
})
t.Run("Should not be able to modify PluginSettings.EnableUploads", func(t *testing.T) {
oldEnableUploads := *th.App.Config().PluginSettings.EnableUploads
*cfg.PluginSettings.EnableUploads = !oldEnableUploads
cfg, resp = client.UpdateConfig(cfg)
CheckNoError(t, resp)
cfg, _, err = client.UpdateConfig(cfg)
require.NoError(t, err)
assert.Equal(t, oldEnableUploads, *cfg.PluginSettings.EnableUploads)
assert.Equal(t, oldEnableUploads, *th.App.Config().PluginSettings.EnableUploads)
cfg.PluginSettings.EnableUploads = nil
cfg, resp = client.UpdateConfig(cfg)
CheckNoError(t, resp)
cfg, _, err = client.UpdateConfig(cfg)
require.NoError(t, err)
assert.Equal(t, oldEnableUploads, *cfg.PluginSettings.EnableUploads)
assert.Equal(t, oldEnableUploads, *th.App.Config().PluginSettings.EnableUploads)
})
@@ -203,14 +205,14 @@ func TestUpdateConfig(t *testing.T) {
oldPublicKeys := th.App.Config().PluginSettings.SignaturePublicKeyFiles
cfg.PluginSettings.SignaturePublicKeyFiles = append(cfg.PluginSettings.SignaturePublicKeyFiles, "new_signature")
cfg, resp = client.UpdateConfig(cfg)
CheckNoError(t, resp)
cfg, _, err = client.UpdateConfig(cfg)
require.NoError(t, err)
assert.Equal(t, oldPublicKeys, cfg.PluginSettings.SignaturePublicKeyFiles)
assert.Equal(t, oldPublicKeys, th.App.Config().PluginSettings.SignaturePublicKeyFiles)
cfg.PluginSettings.SignaturePublicKeyFiles = nil
cfg, resp = client.UpdateConfig(cfg)
CheckNoError(t, resp)
cfg, _, err = client.UpdateConfig(cfg)
require.NoError(t, err)
assert.Equal(t, oldPublicKeys, cfg.PluginSettings.SignaturePublicKeyFiles)
assert.Equal(t, oldPublicKeys, th.App.Config().PluginSettings.SignaturePublicKeyFiles)
})
@@ -224,18 +226,19 @@ func TestUpdateConfig(t *testing.T) {
cfg.ServiceSettings.SiteURL = &nonEmptyURL
// Set the SiteURL
cfg, resp = th.SystemAdminClient.UpdateConfig(cfg)
CheckNoError(t, resp)
cfg, _, err = th.SystemAdminClient.UpdateConfig(cfg)
require.NoError(t, err)
require.Equal(t, nonEmptyURL, *cfg.ServiceSettings.SiteURL)
// Check that the Site URL can't be cleared
cfg.ServiceSettings.SiteURL = sToP("")
cfg, resp = th.SystemAdminClient.UpdateConfig(cfg)
cfg, resp, err = th.SystemAdminClient.UpdateConfig(cfg)
require.Error(t, err)
CheckBadRequestStatus(t, resp)
CheckErrorMessage(t, resp, "api.config.update_config.clear_siteurl.app_error")
CheckErrorID(t, err, "api.config.update_config.clear_siteurl.app_error")
// Check that the Site URL wasn't cleared
cfg, resp = th.SystemAdminClient.GetConfig()
CheckNoError(t, resp)
cfg, _, err = th.SystemAdminClient.GetConfig()
require.NoError(t, err)
require.Equal(t, nonEmptyURL, *cfg.ServiceSettings.SiteURL)
})
}
@@ -247,15 +250,15 @@ func TestGetConfigWithoutManageSystemPermission(t *testing.T) {
t.Run("any sysconsole read permission provides config read access", func(t *testing.T) {
// forbidden by default
_, resp := th.Client.GetConfig()
_, resp, err := th.Client.GetConfig()
require.Error(t, err)
CheckForbiddenStatus(t, resp)
// add any sysconsole read permission
th.AddPermissionToRole(model.SysconsoleReadPermissions[0].Id, model.SystemUserRoleId)
_, resp = th.Client.GetConfig()
_, _, err = th.Client.GetConfig()
// should be readable now
CheckNoError(t, resp)
require.NoError(t, err)
})
}
@@ -270,18 +273,18 @@ func TestUpdateConfigWithoutManageSystemPermission(t *testing.T) {
t.Run("sysconsole read permission does not provides config write access", func(t *testing.T) {
// should be readable because has a sysconsole read permission
cfg, resp := th.Client.GetConfig()
CheckNoError(t, resp)
_, resp = th.Client.UpdateConfig(cfg)
cfg, _, err := th.Client.GetConfig()
require.NoError(t, err)
_, resp, err := th.Client.UpdateConfig(cfg)
require.Error(t, err)
CheckForbiddenStatus(t, resp)
})
t.Run("the wrong write permission does not grant access", func(t *testing.T) {
// should be readable because has a sysconsole read permission
cfg, resp := th.SystemAdminClient.GetConfig()
CheckNoError(t, resp)
cfg, _, err := th.SystemAdminClient.GetConfig()
require.NoError(t, err)
originalValue := *cfg.ServiceSettings.AllowCorsFrom
@@ -292,19 +295,19 @@ func TestUpdateConfigWithoutManageSystemPermission(t *testing.T) {
// try update a config value allowed by sysconsole WRITE integrations
mockVal := model.NewId()
cfg.ServiceSettings.AllowCorsFrom = &mockVal
_, resp = th.Client.UpdateConfig(cfg)
CheckNoError(t, resp)
_, _, err = th.Client.UpdateConfig(cfg)
require.NoError(t, err)
// ensure the config setting was not updated
cfg, resp = th.SystemAdminClient.GetConfig()
CheckNoError(t, resp)
cfg, _, err = th.SystemAdminClient.GetConfig()
require.NoError(t, err)
assert.Equal(t, *cfg.ServiceSettings.AllowCorsFrom, originalValue)
})
t.Run("config value is writeable by specific system console permission", func(t *testing.T) {
// should be readable because has a sysconsole read permission
cfg, resp := th.SystemAdminClient.GetConfig()
CheckNoError(t, resp)
cfg, _, err := th.SystemAdminClient.GetConfig()
require.NoError(t, err)
th.AddPermissionToRole(model.PermissionSysconsoleWriteIntegrationsCors.Id, model.SystemUserRoleId)
defer th.RemovePermissionFromRole(model.PermissionSysconsoleWriteIntegrationsCors.Id, model.SystemUserRoleId)
@@ -314,12 +317,12 @@ func TestUpdateConfigWithoutManageSystemPermission(t *testing.T) {
// try update a config value allowed by sysconsole WRITE integrations
mockVal := model.NewId()
cfg.ServiceSettings.AllowCorsFrom = &mockVal
_, resp = th.Client.UpdateConfig(cfg)
CheckNoError(t, resp)
_, _, err = th.Client.UpdateConfig(cfg)
require.NoError(t, err)
// ensure the config setting was updated
cfg, resp = th.Client.GetConfig()
CheckNoError(t, resp)
cfg, _, err = th.Client.GetConfig()
require.NoError(t, err)
assert.Equal(t, *cfg.ServiceSettings.AllowCorsFrom, mockVal)
})
}
@@ -342,23 +345,23 @@ func TestUpdateConfigMessageExportSpecialHandling(t *testing.T) {
})
// Turn it on, timestamp should be updated.
cfg, resp := th.SystemAdminClient.GetConfig()
CheckNoError(t, resp)
cfg, _, err := th.SystemAdminClient.GetConfig()
require.NoError(t, err)
*cfg.MessageExportSettings.EnableExport = true
_, resp = th.SystemAdminClient.UpdateConfig(cfg)
CheckNoError(t, resp)
_, _, err = th.SystemAdminClient.UpdateConfig(cfg)
require.NoError(t, err)
assert.True(t, *th.App.Config().MessageExportSettings.EnableExport)
assert.NotEqual(t, int64(0), *th.App.Config().MessageExportSettings.ExportFromTimestamp)
// Turn it off, timestamp should be cleared.
cfg, resp = th.SystemAdminClient.GetConfig()
CheckNoError(t, resp)
cfg, _, err = th.SystemAdminClient.GetConfig()
require.NoError(t, err)
*cfg.MessageExportSettings.EnableExport = false
_, resp = th.SystemAdminClient.UpdateConfig(cfg)
CheckNoError(t, resp)
_, _, err = th.SystemAdminClient.UpdateConfig(cfg)
require.NoError(t, err)
assert.False(t, *th.App.Config().MessageExportSettings.EnableExport)
assert.Equal(t, int64(0), *th.App.Config().MessageExportSettings.ExportFromTimestamp)
@@ -370,23 +373,23 @@ func TestUpdateConfigMessageExportSpecialHandling(t *testing.T) {
})
// Turn it on, timestamp should *not* be updated.
cfg, resp = th.SystemAdminClient.GetConfig()
CheckNoError(t, resp)
cfg, _, err = th.SystemAdminClient.GetConfig()
require.NoError(t, err)
*cfg.MessageExportSettings.EnableExport = true
_, resp = th.SystemAdminClient.UpdateConfig(cfg)
CheckNoError(t, resp)
_, _, err = th.SystemAdminClient.UpdateConfig(cfg)
require.NoError(t, err)
assert.True(t, *th.App.Config().MessageExportSettings.EnableExport)
assert.Equal(t, int64(12345), *th.App.Config().MessageExportSettings.ExportFromTimestamp)
// Turn it off, timestamp should be cleared.
cfg, resp = th.SystemAdminClient.GetConfig()
CheckNoError(t, resp)
cfg, _, err = th.SystemAdminClient.GetConfig()
require.NoError(t, err)
*cfg.MessageExportSettings.EnableExport = false
_, resp = th.SystemAdminClient.UpdateConfig(cfg)
CheckNoError(t, resp)
_, _, err = th.SystemAdminClient.UpdateConfig(cfg)
require.NoError(t, err)
assert.False(t, *th.App.Config().MessageExportSettings.EnableExport)
assert.Equal(t, int64(0), *th.App.Config().MessageExportSettings.ExportFromTimestamp)
@@ -398,35 +401,35 @@ func TestUpdateConfigRestrictSystemAdmin(t *testing.T) {
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ExperimentalSettings.RestrictSystemAdmin = true })
t.Run("Restrict flag should be honored for sysadmin", func(t *testing.T) {
originalCfg, resp := th.SystemAdminClient.GetConfig()
CheckNoError(t, resp)
originalCfg, _, err := th.SystemAdminClient.GetConfig()
require.NoError(t, err)
cfg := originalCfg.Clone()
*cfg.TeamSettings.SiteName = "MyFancyName" // Allowed
*cfg.ServiceSettings.SiteURL = "http://example.com" // Ignored
returnedCfg, resp := th.SystemAdminClient.UpdateConfig(cfg)
CheckNoError(t, resp)
returnedCfg, _, err := th.SystemAdminClient.UpdateConfig(cfg)
require.NoError(t, err)
require.Equal(t, "MyFancyName", *returnedCfg.TeamSettings.SiteName)
require.Equal(t, *originalCfg.ServiceSettings.SiteURL, *returnedCfg.ServiceSettings.SiteURL)
actualCfg, resp := th.SystemAdminClient.GetConfig()
CheckNoError(t, resp)
actualCfg, _, err := th.SystemAdminClient.GetConfig()
require.NoError(t, err)
require.Equal(t, returnedCfg, actualCfg)
})
t.Run("Restrict flag should be ignored by local mode", func(t *testing.T) {
originalCfg, resp := th.LocalClient.GetConfig()
CheckNoError(t, resp)
originalCfg, _, err := th.LocalClient.GetConfig()
require.NoError(t, err)
cfg := originalCfg.Clone()
*cfg.TeamSettings.SiteName = "MyFancyName" // Allowed
*cfg.ServiceSettings.SiteURL = "http://example.com" // Ignored
returnedCfg, resp := th.LocalClient.UpdateConfig(cfg)
CheckNoError(t, resp)
returnedCfg, _, err := th.LocalClient.UpdateConfig(cfg)
require.NoError(t, err)
require.Equal(t, "MyFancyName", *returnedCfg.TeamSettings.SiteName)
require.Equal(t, "http://example.com", *returnedCfg.ServiceSettings.SiteURL)
@@ -452,13 +455,13 @@ func TestUpdateConfigDiffInAuditRecord(t *testing.T) {
th := SetupWithServerOptions(t, options)
defer th.TearDown()
cfg, resp := th.SystemAdminClient.GetConfig()
CheckNoError(t, resp)
cfg, _, err := th.SystemAdminClient.GetConfig()
require.NoError(t, err)
timeoutVal := *cfg.ServiceSettings.ReadTimeout
cfg.ServiceSettings.ReadTimeout = model.NewInt(timeoutVal + 1)
cfg, resp = th.SystemAdminClient.UpdateConfig(cfg)
CheckNoError(t, resp)
cfg, _, err = th.SystemAdminClient.UpdateConfig(cfg)
require.NoError(t, err)
defer th.App.UpdateConfig(func(cfg *model.Config) {
cfg.ServiceSettings.ReadTimeout = model.NewInt(timeoutVal)
})
@@ -490,8 +493,8 @@ func TestGetEnvironmentConfig(t *testing.T) {
t.Run("as system admin", func(t *testing.T) {
SystemAdminClient := th.SystemAdminClient
envConfig, resp := SystemAdminClient.GetEnvironmentConfig()
CheckNoError(t, resp)
envConfig, _, err := SystemAdminClient.GetEnvironmentConfig()
require.NoError(t, err)
serviceSettings, ok := envConfig["ServiceSettings"]
require.True(t, ok, "should've returned ServiceSettings")
@@ -521,23 +524,24 @@ func TestGetEnvironmentConfig(t *testing.T) {
TeamAdminClient := th.CreateClient()
th.LoginTeamAdminWithClient(TeamAdminClient)
envConfig, resp := TeamAdminClient.GetEnvironmentConfig()
CheckNoError(t, resp)
envConfig, _, err := TeamAdminClient.GetEnvironmentConfig()
require.NoError(t, err)
require.Empty(t, envConfig)
})
t.Run("as regular user", func(t *testing.T) {
Client := th.Client
client := th.Client
envConfig, resp := Client.GetEnvironmentConfig()
CheckNoError(t, resp)
envConfig, _, err := client.GetEnvironmentConfig()
require.NoError(t, err)
require.Empty(t, envConfig)
})
t.Run("as not-regular user", func(t *testing.T) {
Client := th.CreateClient()
client := th.CreateClient()
_, resp := Client.GetEnvironmentConfig()
_, resp, err := client.GetEnvironmentConfig()
require.Error(t, err)
CheckUnauthorizedStatus(t, resp)
})
}
@@ -554,10 +558,10 @@ func TestGetOldClientConfig(t *testing.T) {
*cfg.ServiceSettings.GoogleDeveloperKey = testKey
})
Client := th.Client
client := th.Client
config, resp := Client.GetOldClientConfig("")
CheckNoError(t, resp)
config, _, err := client.GetOldClientConfig("")
require.NoError(t, err)
require.NotEmpty(t, config["Version"], "config not returned correctly")
require.Equal(t, testKey, config["GoogleDeveloperKey"])
@@ -568,29 +572,29 @@ func TestGetOldClientConfig(t *testing.T) {
*cfg.ServiceSettings.GoogleDeveloperKey = testKey
})
Client := th.CreateClient()
client := th.CreateClient()
config, resp := Client.GetOldClientConfig("")
CheckNoError(t, resp)
config, _, err := client.GetOldClientConfig("")
require.NoError(t, err)
require.NotEmpty(t, config["Version"], "config not returned correctly")
require.Empty(t, config["GoogleDeveloperKey"], "config should be missing developer key")
})
t.Run("missing format", func(t *testing.T) {
Client := th.Client
client := th.Client
_, err := Client.DoApiGet("/config/client", "")
require.NotNil(t, err)
require.Equal(t, http.StatusNotImplemented, err.StatusCode)
resp, err := client.DoApiGet("/config/client", "")
require.Error(t, err)
require.Equal(t, http.StatusNotImplemented, resp.StatusCode)
})
t.Run("invalid format", func(t *testing.T) {
Client := th.Client
client := th.Client
_, err := Client.DoApiGet("/config/client?format=junk", "")
require.NotNil(t, err)
require.Equal(t, http.StatusBadRequest, err.StatusCode)
resp, err := client.DoApiGet("/config/client?format=junk", "")
require.Error(t, err)
require.Equal(t, http.StatusBadRequest, resp.StatusCode)
})
}
@@ -599,12 +603,14 @@ func TestPatchConfig(t *testing.T) {
defer th.TearDown()
t.Run("config is missing", func(t *testing.T) {
_, response := th.Client.PatchConfig(nil)
_, response, err := th.Client.PatchConfig(nil)
require.Error(t, err)
CheckBadRequestStatus(t, response)
})
t.Run("user is not system admin", func(t *testing.T) {
_, response := th.Client.PatchConfig(&model.Config{})
_, response, err := th.Client.PatchConfig(&model.Config{})
require.Error(t, err)
CheckForbiddenStatus(t, response)
})
@@ -615,7 +621,7 @@ func TestPatchConfig(t *testing.T) {
ConsoleLevel: model.NewString("INFO"),
}}
updatedConfig, _ := th.SystemAdminClient.PatchConfig(&config)
updatedConfig, _, _ := th.SystemAdminClient.PatchConfig(&config)
assert.Equal(t, "DEBUG", *updatedConfig.LogSettings.ConsoleLevel)
})
@@ -627,13 +633,13 @@ func TestPatchConfig(t *testing.T) {
ConsoleLevel: model.NewString("INFO"),
}}
oldConfig, _ := th.LocalClient.GetConfig()
updatedConfig, _ := th.LocalClient.PatchConfig(&config)
oldConfig, _, _ := th.LocalClient.GetConfig()
updatedConfig, _, _ := th.LocalClient.PatchConfig(&config)
assert.Equal(t, "INFO", *updatedConfig.LogSettings.ConsoleLevel)
// reset the config
_, resp := th.LocalClient.UpdateConfig(oldConfig)
CheckNoError(t, resp)
_, _, err := th.LocalClient.UpdateConfig(oldConfig)
require.NoError(t, err)
})
th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) {
@@ -642,18 +648,19 @@ func TestPatchConfig(t *testing.T) {
MinimumLength: model.NewInt(4),
}}
_, response := client.PatchConfig(&config)
_, response, err := client.PatchConfig(&config)
assert.Equal(t, http.StatusBadRequest, response.StatusCode)
assert.NotNil(t, response.Error)
assert.Equal(t, "model.config.is_valid.password_length.app_error", response.Error.Id)
assert.Error(t, err)
CheckErrorID(t, err, "model.config.is_valid.password_length.app_error")
})
t.Run("should patch the config", func(t *testing.T) {
*th.App.Config().ExperimentalSettings.RestrictSystemAdmin = false
th.App.UpdateConfig(func(cfg *model.Config) { cfg.TeamSettings.ExperimentalDefaultChannels = []string{"some-channel"} })
oldConfig, _ := client.GetConfig()
oldConfig, _, err := client.GetConfig()
require.NoError(t, err)
assert.False(t, *oldConfig.PasswordSettings.Lowercase)
assert.NotEqual(t, 15, *oldConfig.PasswordSettings.MinimumLength)
@@ -676,9 +683,11 @@ func TestPatchConfig(t *testing.T) {
},
}
_, response := client.PatchConfig(&config)
_, response, err := client.PatchConfig(&config)
require.NoError(t, err)
updatedConfig, _ := client.GetConfig()
updatedConfig, _, err := client.GetConfig()
require.NoError(t, err)
assert.True(t, *updatedConfig.PasswordSettings.Lowercase)
assert.Equal(t, "INFO", *updatedConfig.LogSettings.ConsoleLevel)
assert.Equal(t, []string{"another-channel"}, updatedConfig.TeamSettings.ExperimentalDefaultChannels)
@@ -686,8 +695,8 @@ func TestPatchConfig(t *testing.T) {
assert.Equal(t, "no-cache, no-store, must-revalidate", response.Header.Get("Cache-Control"))
// reset the config
_, resp := client.UpdateConfig(oldConfig)
CheckNoError(t, resp)
_, _, err = client.UpdateConfig(oldConfig)
require.NoError(t, err)
})
t.Run("should sanitize config", func(t *testing.T) {
@@ -695,7 +704,8 @@ func TestPatchConfig(t *testing.T) {
Symbol: model.NewBool(true),
}}
updatedConfig, _ := client.PatchConfig(&config)
updatedConfig, _, err := client.PatchConfig(&config)
require.NoError(t, err)
assert.Equal(t, model.FakeSetting, *updatedConfig.SqlSettings.DataSource)
})
@@ -705,19 +715,21 @@ func TestPatchConfig(t *testing.T) {
EnableUploads: model.NewBool(true),
}}
updatedConfig, resp := client.PatchConfig(&config)
updatedConfig, resp, err := client.PatchConfig(&config)
if client == th.LocalClient {
require.NoError(t, err)
CheckOKStatus(t, resp)
assert.Equal(t, true, *updatedConfig.PluginSettings.EnableUploads)
} else {
require.Error(t, err)
CheckForbiddenStatus(t, resp)
}
})
})
t.Run("System Admin should not be able to clear Site URL", func(t *testing.T) {
cfg, resp := th.SystemAdminClient.GetConfig()
CheckNoError(t, resp)
cfg, _, err := th.SystemAdminClient.GetConfig()
require.NoError(t, err)
siteURL := cfg.ServiceSettings.SiteURL
defer th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.SiteURL = siteURL })
@@ -728,8 +740,8 @@ func TestPatchConfig(t *testing.T) {
SiteURL: model.NewString(nonEmptyURL),
},
}
updatedConfig, resp := th.SystemAdminClient.PatchConfig(&config)
CheckNoError(t, resp)
updatedConfig, _, err := th.SystemAdminClient.PatchConfig(&config)
require.NoError(t, err)
require.Equal(t, nonEmptyURL, *updatedConfig.ServiceSettings.SiteURL)
// Check that the Site URL can't be cleared
@@ -738,18 +750,19 @@ func TestPatchConfig(t *testing.T) {
SiteURL: model.NewString(""),
},
}
_, resp = th.SystemAdminClient.PatchConfig(&config)
_, resp, err := th.SystemAdminClient.PatchConfig(&config)
require.Error(t, err)
CheckBadRequestStatus(t, resp)
CheckErrorMessage(t, resp, "api.config.update_config.clear_siteurl.app_error")
CheckErrorID(t, err, "api.config.update_config.clear_siteurl.app_error")
// Check that the Site URL wasn't cleared
cfg, resp = th.SystemAdminClient.GetConfig()
CheckNoError(t, resp)
cfg, _, err = th.SystemAdminClient.GetConfig()
require.NoError(t, err)
require.Equal(t, nonEmptyURL, *cfg.ServiceSettings.SiteURL)
// Check that sending an empty config returns no error.
_, resp = th.SystemAdminClient.PatchConfig(&model.Config{})
CheckNoError(t, resp)
_, _, err = th.SystemAdminClient.PatchConfig(&model.Config{})
require.NoError(t, err)
})
}
@@ -758,7 +771,8 @@ func TestMigrateConfig(t *testing.T) {
defer th.TearDown()
t.Run("user is not system admin", func(t *testing.T) {
_, response := th.Client.MigrateConfig("from", "to")
response, err := th.Client.MigrateConfig("from", "to")
require.Error(t, err)
CheckForbiddenStatus(t, response)
})
@@ -771,7 +785,7 @@ func TestMigrateConfig(t *testing.T) {
require.NoError(t, err)
defer f.RemoveFile("to.json")
_, response := client.MigrateConfig("from.json", "to.json")
CheckNoError(t, response)
_, err = client.MigrateConfig("from.json", "to.json")
require.NoError(t, err)
})
}