* TestPool * Store infra * Store tests updates * Bump maximum concurrent postgres connections * More infra * channels/jobs * channels/app * channels/api4 * Protect i18n from concurrent access * Replace some use of os.Setenv * Remove debug * Lint fixes * Fix more linting * Fix test * Remove use of Setenv in drafts tests * Fix flaky TestWebHubCloseConnOnDBFail * Fix merge * [MM-62408] Add CI job to generate test coverage (#30284) * Add CI job to generate test coverage * Remove use of Setenv in drafts tests * Fix flaky TestWebHubCloseConnOnDBFail * Fix more Setenv usage * Fix more potential flakyness * Remove parallelism from flaky test * Remove conflicting env var * Fix * Disable parallelism * Test atomic covermode * Disable parallelism * Enable parallelism * Add upload coverage step * Fix codecov.yml * Add codecov.yml * Remove redundant workspace field * Add Parallel() util methods and refactor * Fix formatting * More formatting fixes * Fix reporting
177 строки
5.9 KiB
Go
177 строки
5.9 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
package api4
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
)
|
|
|
|
func TestNotifyAdmin(t *testing.T) {
|
|
mainHelper.Parallel(t)
|
|
t.Run("error when notifying with empty data", func(t *testing.T) {
|
|
mainHelper.Parallel(t)
|
|
th := Setup(t).InitBasic()
|
|
defer th.TearDown()
|
|
|
|
statusCode, err := th.Client.NotifyAdmin(context.Background(), nil)
|
|
|
|
require.Error(t, err)
|
|
require.Equal(t, http.StatusBadRequest, statusCode)
|
|
})
|
|
|
|
t.Run("error when plan is unknown when notifying on upgrade", func(t *testing.T) {
|
|
mainHelper.Parallel(t)
|
|
th := Setup(t).InitBasic()
|
|
defer th.TearDown()
|
|
|
|
statusCode, err := th.Client.NotifyAdmin(context.Background(), &model.NotifyAdminToUpgradeRequest{
|
|
RequiredPlan: "Unknown plan",
|
|
RequiredFeature: model.PaidFeatureAllProfessionalfeatures,
|
|
})
|
|
|
|
require.Error(t, err)
|
|
require.Equal(t, "Unable to save notify data.", err.Error())
|
|
require.Equal(t, http.StatusInternalServerError, statusCode)
|
|
})
|
|
|
|
t.Run("error when plan is unknown when notifying to trial", func(t *testing.T) {
|
|
mainHelper.Parallel(t)
|
|
th := Setup(t).InitBasic()
|
|
defer th.TearDown()
|
|
|
|
statusCode, err := th.Client.NotifyAdmin(context.Background(), &model.NotifyAdminToUpgradeRequest{
|
|
RequiredPlan: "Unknown plan",
|
|
RequiredFeature: model.PaidFeatureAllProfessionalfeatures,
|
|
TrialNotification: true,
|
|
})
|
|
|
|
require.Error(t, err)
|
|
require.Equal(t, "Unable to save notify data.", err.Error())
|
|
require.Equal(t, http.StatusInternalServerError, statusCode)
|
|
})
|
|
|
|
t.Run("error when feature is unknown when notifying on upgrade", func(t *testing.T) {
|
|
mainHelper.Parallel(t)
|
|
th := Setup(t).InitBasic()
|
|
defer th.TearDown()
|
|
|
|
statusCode, err := th.Client.NotifyAdmin(context.Background(), &model.NotifyAdminToUpgradeRequest{
|
|
RequiredPlan: model.LicenseShortSkuProfessional,
|
|
RequiredFeature: "Unknown feature",
|
|
})
|
|
|
|
require.Error(t, err)
|
|
require.Equal(t, "Unable to save notify data.", err.Error())
|
|
require.Equal(t, http.StatusInternalServerError, statusCode)
|
|
})
|
|
|
|
t.Run("error when feature is unknown when notifying to trial", func(t *testing.T) {
|
|
mainHelper.Parallel(t)
|
|
th := Setup(t).InitBasic()
|
|
defer th.TearDown()
|
|
|
|
statusCode, err := th.Client.NotifyAdmin(context.Background(), &model.NotifyAdminToUpgradeRequest{
|
|
RequiredPlan: model.LicenseShortSkuProfessional,
|
|
RequiredFeature: "Unknown feature",
|
|
TrialNotification: true,
|
|
})
|
|
|
|
require.Error(t, err)
|
|
require.Equal(t, "Unable to save notify data.", err.Error())
|
|
require.Equal(t, http.StatusInternalServerError, statusCode)
|
|
})
|
|
|
|
t.Run("error when user tries to notify again on same feature within the cool off period", func(t *testing.T) {
|
|
mainHelper.Parallel(t)
|
|
th := Setup(t).InitBasic()
|
|
defer th.TearDown()
|
|
|
|
statusCode, err := th.Client.NotifyAdmin(context.Background(), &model.NotifyAdminToUpgradeRequest{
|
|
RequiredPlan: model.LicenseShortSkuProfessional,
|
|
RequiredFeature: model.PaidFeatureAllProfessionalfeatures,
|
|
})
|
|
require.NoError(t, err)
|
|
require.Equal(t, http.StatusOK, statusCode)
|
|
|
|
// second attempt to notify for all professional features
|
|
statusCode, err = th.Client.NotifyAdmin(context.Background(), &model.NotifyAdminToUpgradeRequest{
|
|
RequiredPlan: model.LicenseShortSkuProfessional,
|
|
RequiredFeature: model.PaidFeatureAllProfessionalfeatures,
|
|
})
|
|
require.Error(t, err)
|
|
|
|
require.Equal(t, "Already notified admin", err.Error())
|
|
require.Equal(t, http.StatusForbidden, statusCode)
|
|
})
|
|
|
|
t.Run("successfully save upgrade notification", func(t *testing.T) {
|
|
mainHelper.Parallel(t)
|
|
th := Setup(t).InitBasic()
|
|
defer th.TearDown()
|
|
|
|
statusCode, err := th.Client.NotifyAdmin(context.Background(), &model.NotifyAdminToUpgradeRequest{
|
|
RequiredPlan: model.LicenseShortSkuProfessional,
|
|
RequiredFeature: model.PaidFeatureAllProfessionalfeatures,
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
require.Equal(t, http.StatusOK, statusCode)
|
|
})
|
|
}
|
|
|
|
func TestTriggerNotifyAdmin(t *testing.T) {
|
|
mainHelper.Parallel(t)
|
|
t.Run("error when EnableAPITriggerAdminNotifications is not true", func(t *testing.T) {
|
|
mainHelper.Parallel(t)
|
|
th := Setup(t).InitBasic()
|
|
defer th.TearDown()
|
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableAPITriggerAdminNotifications = false })
|
|
|
|
statusCode, err := th.SystemAdminClient.TriggerNotifyAdmin(context.Background(), &model.NotifyAdminToUpgradeRequest{})
|
|
|
|
require.Error(t, err)
|
|
require.Equal(t, "Internal error during cloud api request.", err.Error())
|
|
require.Equal(t, http.StatusForbidden, statusCode)
|
|
})
|
|
|
|
t.Run("error when non admins try to trigger notifications", func(t *testing.T) {
|
|
mainHelper.Parallel(t)
|
|
th := Setup(t).InitBasic()
|
|
defer th.TearDown()
|
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableAPITriggerAdminNotifications = true })
|
|
|
|
statusCode, err := th.Client.TriggerNotifyAdmin(context.Background(), &model.NotifyAdminToUpgradeRequest{})
|
|
|
|
require.Error(t, err)
|
|
require.Equal(t, "You do not have the appropriate permissions.", err.Error())
|
|
require.Equal(t, http.StatusForbidden, statusCode)
|
|
})
|
|
|
|
t.Run("happy path", func(t *testing.T) {
|
|
mainHelper.Parallel(t)
|
|
th := Setup(t)
|
|
defer th.TearDown()
|
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableAPITriggerAdminNotifications = true })
|
|
|
|
statusCode, err := th.Client.NotifyAdmin(context.Background(), &model.NotifyAdminToUpgradeRequest{
|
|
RequiredPlan: model.LicenseShortSkuProfessional,
|
|
RequiredFeature: model.PaidFeatureAllProfessionalfeatures,
|
|
})
|
|
require.NoError(t, err)
|
|
require.Equal(t, http.StatusOK, statusCode)
|
|
|
|
statusCode, err = th.SystemAdminClient.TriggerNotifyAdmin(context.Background(), &model.NotifyAdminToUpgradeRequest{})
|
|
require.NoError(t, err)
|
|
require.Equal(t, http.StatusOK, statusCode)
|
|
})
|
|
}
|