Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
Michael Kochell
2022-05-25 12:36:08 -04:00
коммит произвёл GitHub
родитель 54a88fb532
Коммит ea5bfe3fd9
22 изменённых файлов: 634 добавлений и 22 удалений

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

@@ -152,6 +152,11 @@ func updateConfig(c *Context, w http.ResponseWriter, r *http.Request) {
*cfg.PluginSettings.MarketplaceURL = *appCfg.PluginSettings.MarketplaceURL
}
if err := c.App.CheckFreemiumLimitsForConfigSave(appCfg, cfg); err != nil {
c.Err = err
return
}
// There are some settings that cannot be changed in a cloud env
if c.App.Channels().License() != nil && *c.App.Channels().License().Features.Cloud {
// Both of them cannot be nil since cfg.SetDefaults is called earlier for cfg,
@@ -286,6 +291,11 @@ func patchConfig(c *Context, w http.ResponseWriter, r *http.Request) {
}
}
if err := c.App.CheckFreemiumLimitsForConfigSave(appCfg, cfg); err != nil {
c.Err = err
return
}
// There are some settings that cannot be changed in a cloud env
if c.App.Channels().License() != nil && *c.App.Channels().License().Features.Cloud {
if cfg.ComplianceSettings.Directory != nil && *appCfg.ComplianceSettings.Directory != *cfg.ComplianceSettings.Directory {

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

@@ -17,7 +17,9 @@ import (
"github.com/mattermost/mattermost-server/v6/app"
"github.com/mattermost/mattermost-server/v6/config"
"github.com/mattermost/mattermost-server/v6/einterfaces/mocks"
"github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost-server/v6/plugin/plugintest/mock"
)
func TestGetConfig(t *testing.T) {
@@ -247,6 +249,60 @@ func TestUpdateConfig(t *testing.T) {
assert.Equal(t, newURL, *cfg2.PluginSettings.MarketplaceURL)
})
t.Run("Should not be able to save config if the new config exceeds Freemium limits", func(t *testing.T) {
os.Setenv("MM_FEATUREFLAGS_CLOUDFREE", "true")
defer os.Unsetenv("MM_FEATUREFLAGS_CLOUDFREE")
th.App.ReloadConfig()
cloud := &mocks.CloudInterface{}
cloudImpl := th.App.Srv().Cloud
defer func() {
th.App.Srv().Cloud = cloudImpl
}()
th.App.Srv().Cloud = cloud
cloud.Mock.On("GetCloudLimits", mock.Anything).Return(&model.ProductLimits{
Integrations: &model.IntegrationsLimits{
Enabled: model.NewInt(0),
},
}, nil).Once()
// Exceed freemium limit. Should throw error.
cfg1 := th.App.Config().Clone()
cfg1.PluginSettings.PluginStates["new-plugin"] = &model.PluginState{Enable: true}
_, _, err1 := th.SystemAdminClient.UpdateConfig(cfg1)
require.Error(t, err1)
// No attempt to enable a plugin. Should not throw error.
cfg1 = th.App.Config().Clone()
cfg1.PluginSettings.PluginStates["new-plugin"] = &model.PluginState{Enable: false}
_, _, err1 = th.SystemAdminClient.UpdateConfig(cfg1)
require.NoError(t, err1)
cloud.Mock.On("GetCloudLimits", mock.Anything).Return(&model.ProductLimits{
Integrations: &model.IntegrationsLimits{
Enabled: model.NewInt(1),
},
}, nil).Twice()
// Exceed freemium limit while enabling more than one plugin. Should throw error.
cfg1 = th.App.Config().Clone()
cfg1.PluginSettings.PluginStates["new-plugin"] = &model.PluginState{Enable: true}
cfg1.PluginSettings.PluginStates["new-plugin2"] = &model.PluginState{Enable: true}
_, _, err1 = th.SystemAdminClient.PatchConfig(cfg1)
require.Error(t, err1)
// Match freemium limit. Should not throw error.
cfg1 = th.App.Config().Clone()
cfg1.PluginSettings.PluginStates["new-plugin"] = &model.PluginState{Enable: true}
_, _, err1 = th.SystemAdminClient.UpdateConfig(cfg1)
require.NoError(t, err1)
// Save same config with same plugin enabled. Should not throw error.
_, _, err1 = th.SystemAdminClient.UpdateConfig(cfg1)
require.NoError(t, err1)
})
t.Run("Should not be able to modify ComplianceSettings.Directory in cloud", func(t *testing.T) {
th.App.Srv().SetLicense(model.NewTestLicense("cloud"))
defer th.App.Srv().RemoveLicense()
@@ -796,6 +852,60 @@ func TestPatchConfig(t *testing.T) {
assert.Equal(t, newURL, *cfg.PluginSettings.MarketplaceURL)
})
t.Run("Should not be able to save config if the new config exceeds Freemium limits", func(t *testing.T) {
os.Setenv("MM_FEATUREFLAGS_CLOUDFREE", "true")
defer os.Unsetenv("MM_FEATUREFLAGS_CLOUDFREE")
th.App.ReloadConfig()
cloud := &mocks.CloudInterface{}
cloudImpl := th.App.Srv().Cloud
defer func() {
th.App.Srv().Cloud = cloudImpl
}()
th.App.Srv().Cloud = cloud
cloud.Mock.On("GetCloudLimits", mock.Anything).Return(&model.ProductLimits{
Integrations: &model.IntegrationsLimits{
Enabled: model.NewInt(0),
},
}, nil).Once()
// Exceed freemium limit. Should throw error.
cfg1 := th.App.Config().Clone()
cfg1.PluginSettings.PluginStates["new-plugin"] = &model.PluginState{Enable: true}
_, _, err1 := th.SystemAdminClient.PatchConfig(cfg1)
require.Error(t, err1)
// No attempt to enable a plugin. Should not throw error.
cfg1 = th.App.Config().Clone()
cfg1.PluginSettings.PluginStates["new-plugin"] = &model.PluginState{Enable: false}
_, _, err1 = th.SystemAdminClient.PatchConfig(cfg1)
require.NoError(t, err1)
cloud.Mock.On("GetCloudLimits", mock.Anything).Return(&model.ProductLimits{
Integrations: &model.IntegrationsLimits{
Enabled: model.NewInt(1),
},
}, nil).Twice()
// Exceed freemium limit while enabling more than one plugin. Should throw error.
cfg1 = th.App.Config().Clone()
cfg1.PluginSettings.PluginStates["new-plugin"] = &model.PluginState{Enable: true}
cfg1.PluginSettings.PluginStates["new-plugin2"] = &model.PluginState{Enable: true}
_, _, err1 = th.SystemAdminClient.PatchConfig(cfg1)
require.Error(t, err1)
// Match freemium limit. Should not throw error.
cfg1 = th.App.Config().Clone()
cfg1.PluginSettings.PluginStates["new-plugin"] = &model.PluginState{Enable: true}
_, _, err1 = th.SystemAdminClient.PatchConfig(cfg1)
require.NoError(t, err1)
// Save same config with same plugin enabled. Should not throw error.
_, _, err1 = th.SystemAdminClient.PatchConfig(cfg1)
require.NoError(t, err1)
})
t.Run("System Admin should not be able to clear Site URL", func(t *testing.T) {
cfg, _, err := th.SystemAdminClient.GetConfig()
require.NoError(t, err)

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

@@ -13,6 +13,9 @@ import (
func (api *API) InitUsage() {
// GET /api/v4/usage/posts
api.BaseRoutes.Usage.Handle("/posts", api.APISessionRequired(getPostsUsage)).Methods("GET")
// GET /api/v4/usage/integrations
api.BaseRoutes.Usage.Handle("/integrations", api.APISessionRequired(getIntegrationsUsage)).Methods("GET")
}
func getPostsUsage(c *Context, w http.ResponseWriter, r *http.Request) {
@@ -30,3 +33,30 @@ func getPostsUsage(c *Context, w http.ResponseWriter, r *http.Request) {
w.Write(json)
}
func getIntegrationsUsage(c *Context, w http.ResponseWriter, r *http.Request) {
if !*c.App.Config().PluginSettings.Enable {
json, err := json.Marshal(&model.IntegrationsUsage{})
if err != nil {
c.Err = model.NewAppError("Api4.getIntegrationsUsage", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)
return
}
w.Write(json)
return
}
usage, appErr := c.App.GetIntegrationsUsage()
if appErr != nil {
c.Err = appErr
return
}
json, err := json.Marshal(usage)
if err != nil {
c.Err = model.NewAppError("Api4.getIntegrationsUsage", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)
return
}
w.Write(json)
}

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

@@ -37,3 +37,28 @@ func TestGetPostsUsage(t *testing.T) {
assert.Equal(t, int64(10), usage.Count)
})
}
func TestGetIntegrationsUsage(t *testing.T) {
t.Run("unauthenticated users can not access", func(t *testing.T) {
th := Setup(t)
defer th.TearDown()
th.Client.Logout()
usage, r, err := th.Client.GetIntegrationsUsage()
assert.Error(t, err)
assert.Nil(t, usage)
assert.Equal(t, http.StatusUnauthorized, r.StatusCode)
})
t.Run("good request returns response", func(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
usage, r, err := th.Client.GetIntegrationsUsage()
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, r.StatusCode)
assert.NotNil(t, usage)
assert.Equal(t, 0, usage.Enabled)
})
}