2022-09-02 06:52:48 -04:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
|
|
|
// See LICENSE.txt for license information.
|
|
|
|
|
package api4
|
|
|
|
|
|
|
|
|
|
import (
|
2023-06-06 17:29:29 -04:00
|
|
|
"context"
|
2022-09-02 06:52:48 -04:00
|
|
|
"net/http"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-03-22 17:22:27 -04:00
|
|
|
|
2023-06-11 01:24:35 -04:00
|
|
|
"github.com/mattermost/mattermost/server/public/model"
|
2022-09-02 06:52:48 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestNotifyAdmin(t *testing.T) {
|
2025-05-30 07:58:26 -04:00
|
|
|
mainHelper.Parallel(t)
|
2023-10-05 09:55:59 -04:00
|
|
|
t.Run("error when notifying with empty data", func(t *testing.T) {
|
2025-05-30 07:58:26 -04:00
|
|
|
mainHelper.Parallel(t)
|
2025-11-12 07:00:51 -05:00
|
|
|
th := Setup(t).InitBasic(t)
|
2023-10-05 09:55:59 -04:00
|
|
|
|
|
|
|
|
statusCode, err := th.Client.NotifyAdmin(context.Background(), nil)
|
|
|
|
|
|
|
|
|
|
require.Error(t, err)
|
|
|
|
|
require.Equal(t, http.StatusBadRequest, statusCode)
|
|
|
|
|
})
|
|
|
|
|
|
2022-09-02 06:52:48 -04:00
|
|
|
t.Run("error when plan is unknown when notifying on upgrade", func(t *testing.T) {
|
2025-05-30 07:58:26 -04:00
|
|
|
mainHelper.Parallel(t)
|
2025-11-12 07:00:51 -05:00
|
|
|
th := Setup(t).InitBasic(t)
|
2022-09-02 06:52:48 -04:00
|
|
|
|
2023-06-06 17:29:29 -04:00
|
|
|
statusCode, err := th.Client.NotifyAdmin(context.Background(), &model.NotifyAdminToUpgradeRequest{
|
2022-09-02 06:52:48 -04:00
|
|
|
RequiredPlan: "Unknown plan",
|
|
|
|
|
RequiredFeature: model.PaidFeatureAllProfessionalfeatures,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
require.Error(t, err)
|
2023-12-11 04:27:51 -05:00
|
|
|
require.Equal(t, "Unable to save notify data.", err.Error())
|
2022-09-02 06:52:48 -04:00
|
|
|
require.Equal(t, http.StatusInternalServerError, statusCode)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("error when plan is unknown when notifying to trial", func(t *testing.T) {
|
2025-05-30 07:58:26 -04:00
|
|
|
mainHelper.Parallel(t)
|
2025-11-12 07:00:51 -05:00
|
|
|
th := Setup(t).InitBasic(t)
|
2022-09-02 06:52:48 -04:00
|
|
|
|
2023-06-06 17:29:29 -04:00
|
|
|
statusCode, err := th.Client.NotifyAdmin(context.Background(), &model.NotifyAdminToUpgradeRequest{
|
2022-09-02 06:52:48 -04:00
|
|
|
RequiredPlan: "Unknown plan",
|
|
|
|
|
RequiredFeature: model.PaidFeatureAllProfessionalfeatures,
|
|
|
|
|
TrialNotification: true,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
require.Error(t, err)
|
2023-12-11 04:27:51 -05:00
|
|
|
require.Equal(t, "Unable to save notify data.", err.Error())
|
2022-09-02 06:52:48 -04:00
|
|
|
require.Equal(t, http.StatusInternalServerError, statusCode)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("error when feature is unknown when notifying on upgrade", func(t *testing.T) {
|
2025-05-30 07:58:26 -04:00
|
|
|
mainHelper.Parallel(t)
|
2025-11-12 07:00:51 -05:00
|
|
|
th := Setup(t).InitBasic(t)
|
2022-09-02 06:52:48 -04:00
|
|
|
|
2023-06-06 17:29:29 -04:00
|
|
|
statusCode, err := th.Client.NotifyAdmin(context.Background(), &model.NotifyAdminToUpgradeRequest{
|
2022-09-02 06:52:48 -04:00
|
|
|
RequiredPlan: model.LicenseShortSkuProfessional,
|
|
|
|
|
RequiredFeature: "Unknown feature",
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
require.Error(t, err)
|
2023-12-11 04:27:51 -05:00
|
|
|
require.Equal(t, "Unable to save notify data.", err.Error())
|
2022-09-02 06:52:48 -04:00
|
|
|
require.Equal(t, http.StatusInternalServerError, statusCode)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("error when feature is unknown when notifying to trial", func(t *testing.T) {
|
2025-05-30 07:58:26 -04:00
|
|
|
mainHelper.Parallel(t)
|
2025-11-12 07:00:51 -05:00
|
|
|
th := Setup(t).InitBasic(t)
|
2022-09-02 06:52:48 -04:00
|
|
|
|
2023-06-06 17:29:29 -04:00
|
|
|
statusCode, err := th.Client.NotifyAdmin(context.Background(), &model.NotifyAdminToUpgradeRequest{
|
2022-09-02 06:52:48 -04:00
|
|
|
RequiredPlan: model.LicenseShortSkuProfessional,
|
|
|
|
|
RequiredFeature: "Unknown feature",
|
|
|
|
|
TrialNotification: true,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
require.Error(t, err)
|
2023-12-11 04:27:51 -05:00
|
|
|
require.Equal(t, "Unable to save notify data.", err.Error())
|
2022-09-02 06:52:48 -04:00
|
|
|
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) {
|
2025-05-30 07:58:26 -04:00
|
|
|
mainHelper.Parallel(t)
|
2025-11-12 07:00:51 -05:00
|
|
|
th := Setup(t).InitBasic(t)
|
2022-09-02 06:52:48 -04:00
|
|
|
|
2023-06-06 17:29:29 -04:00
|
|
|
statusCode, err := th.Client.NotifyAdmin(context.Background(), &model.NotifyAdminToUpgradeRequest{
|
2022-09-02 06:52:48 -04:00
|
|
|
RequiredPlan: model.LicenseShortSkuProfessional,
|
|
|
|
|
RequiredFeature: model.PaidFeatureAllProfessionalfeatures,
|
|
|
|
|
})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.Equal(t, http.StatusOK, statusCode)
|
|
|
|
|
|
|
|
|
|
// second attempt to notify for all professional features
|
2023-06-06 17:29:29 -04:00
|
|
|
statusCode, err = th.Client.NotifyAdmin(context.Background(), &model.NotifyAdminToUpgradeRequest{
|
2022-09-02 06:52:48 -04:00
|
|
|
RequiredPlan: model.LicenseShortSkuProfessional,
|
|
|
|
|
RequiredFeature: model.PaidFeatureAllProfessionalfeatures,
|
|
|
|
|
})
|
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
2023-12-11 04:27:51 -05:00
|
|
|
require.Equal(t, "Already notified admin", err.Error())
|
2022-09-02 06:52:48 -04:00
|
|
|
require.Equal(t, http.StatusForbidden, statusCode)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("successfully save upgrade notification", func(t *testing.T) {
|
2025-05-30 07:58:26 -04:00
|
|
|
mainHelper.Parallel(t)
|
2025-11-12 07:00:51 -05:00
|
|
|
th := Setup(t).InitBasic(t)
|
2022-09-02 06:52:48 -04:00
|
|
|
|
2023-06-06 17:29:29 -04:00
|
|
|
statusCode, err := th.Client.NotifyAdmin(context.Background(), &model.NotifyAdminToUpgradeRequest{
|
2022-09-02 06:52:48 -04:00
|
|
|
RequiredPlan: model.LicenseShortSkuProfessional,
|
|
|
|
|
RequiredFeature: model.PaidFeatureAllProfessionalfeatures,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.Equal(t, http.StatusOK, statusCode)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestTriggerNotifyAdmin(t *testing.T) {
|
2025-05-30 07:58:26 -04:00
|
|
|
mainHelper.Parallel(t)
|
2022-09-02 06:52:48 -04:00
|
|
|
t.Run("error when EnableAPITriggerAdminNotifications is not true", func(t *testing.T) {
|
2025-05-30 07:58:26 -04:00
|
|
|
mainHelper.Parallel(t)
|
2025-11-12 07:00:51 -05:00
|
|
|
th := Setup(t).InitBasic(t)
|
2022-09-02 06:52:48 -04:00
|
|
|
|
|
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableAPITriggerAdminNotifications = false })
|
|
|
|
|
|
2023-06-06 17:29:29 -04:00
|
|
|
statusCode, err := th.SystemAdminClient.TriggerNotifyAdmin(context.Background(), &model.NotifyAdminToUpgradeRequest{})
|
2022-09-02 06:52:48 -04:00
|
|
|
|
|
|
|
|
require.Error(t, err)
|
2023-12-11 04:27:51 -05:00
|
|
|
require.Equal(t, "Internal error during cloud api request.", err.Error())
|
2022-09-02 06:52:48 -04:00
|
|
|
require.Equal(t, http.StatusForbidden, statusCode)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("error when non admins try to trigger notifications", func(t *testing.T) {
|
2025-05-30 07:58:26 -04:00
|
|
|
mainHelper.Parallel(t)
|
2025-11-12 07:00:51 -05:00
|
|
|
th := Setup(t).InitBasic(t)
|
2022-09-02 06:52:48 -04:00
|
|
|
|
|
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableAPITriggerAdminNotifications = true })
|
|
|
|
|
|
2023-06-06 17:29:29 -04:00
|
|
|
statusCode, err := th.Client.TriggerNotifyAdmin(context.Background(), &model.NotifyAdminToUpgradeRequest{})
|
2022-09-02 06:52:48 -04:00
|
|
|
|
|
|
|
|
require.Error(t, err)
|
2023-12-11 04:27:51 -05:00
|
|
|
require.Equal(t, "You do not have the appropriate permissions.", err.Error())
|
2022-09-02 06:52:48 -04:00
|
|
|
require.Equal(t, http.StatusForbidden, statusCode)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("happy path", func(t *testing.T) {
|
2025-05-30 07:58:26 -04:00
|
|
|
mainHelper.Parallel(t)
|
2022-09-02 06:52:48 -04:00
|
|
|
th := Setup(t)
|
|
|
|
|
|
|
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableAPITriggerAdminNotifications = true })
|
|
|
|
|
|
2023-06-06 17:29:29 -04:00
|
|
|
statusCode, err := th.Client.NotifyAdmin(context.Background(), &model.NotifyAdminToUpgradeRequest{
|
2022-09-02 06:52:48 -04:00
|
|
|
RequiredPlan: model.LicenseShortSkuProfessional,
|
|
|
|
|
RequiredFeature: model.PaidFeatureAllProfessionalfeatures,
|
|
|
|
|
})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.Equal(t, http.StatusOK, statusCode)
|
|
|
|
|
|
2023-06-06 17:29:29 -04:00
|
|
|
statusCode, err = th.SystemAdminClient.TriggerNotifyAdmin(context.Background(), &model.NotifyAdminToUpgradeRequest{})
|
2022-09-02 06:52:48 -04:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.Equal(t, http.StatusOK, statusCode)
|
|
|
|
|
})
|
|
|
|
|
}
|