Files
upstream-mattermost/server/channels/app/notify_admin_test.go
upstream-mirror 240db4e2a7
Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Nightly Race / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
snapshot of https://github.com/mattermost/mattermost.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 00:29:55 +05:00

252 lines
8.6 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package app
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/mattermost/mattermost/server/public/model"
)
func Test_SendNotifyAdminPosts(t *testing.T) {
mainHelper.Parallel(t)
t.Run("no error sending non trial upgrade post when no notifications are available", func(t *testing.T) {
th := Setup(t).InitBasic(t)
th.App.Srv().SetLicense(model.NewTestLicense("cloud"))
err := th.App.SendNotifyAdminPosts(th.Context, "", "", false)
require.Nil(t, err)
})
t.Run("no error sending trial upgrade post when no notifications are available", func(t *testing.T) {
th := Setup(t).InitBasic(t)
th.App.Srv().SetLicense(model.NewTestLicense("cloud"))
err := th.App.SendNotifyAdminPosts(th.Context, "", "", true)
require.Nil(t, err)
})
t.Run("successfully send upgrade notification", func(t *testing.T) {
th := Setup(t).InitBasic(t)
th.App.Srv().SetLicense(model.NewTestLicense("cloud"))
// some notifications
_, appErr := th.App.SaveAdminNotifyData(&model.NotifyAdminData{
UserId: th.BasicUser.Id,
RequiredPlan: model.LicenseShortSkuProfessional,
RequiredFeature: model.PaidFeatureGuestAccounts,
})
require.Nil(t, appErr)
_, appErr = th.App.SaveAdminNotifyData(&model.NotifyAdminData{
UserId: th.BasicUser2.Id,
RequiredPlan: model.LicenseShortSkuProfessional,
RequiredFeature: model.PaidFeatureGuestAccounts,
})
require.Nil(t, appErr)
appErr = th.App.SendNotifyAdminPosts(th.Context, "test", "", false)
require.Nil(t, appErr)
bot, appErr := th.App.GetSystemBot(th.Context)
require.Nil(t, appErr)
// message sending is async, wait time for it
var channel *model.Channel
var err error
timeout := 5 * time.Second
begin := time.Now()
for {
if time.Since(begin) > timeout {
break
}
channel, err = th.App.Srv().Store().Channel().GetByName("", model.GetDMNameFromIds(bot.UserId, th.SystemAdminUser.Id), false)
if err == nil && channel != nil {
break
}
time.Sleep(100 * time.Millisecond)
}
require.NoError(t, err, "Expected message to have been sent within %d seconds", timeout)
postList, err := th.App.Srv().Store().Post().GetPosts(th.Context, model.GetPostsOptions{ChannelId: channel.Id, Page: 0, PerPage: 1}, false, map[string]bool{})
require.NoError(t, err)
post := postList.Posts[postList.Order[0]]
require.Equal(t, fmt.Sprintf("%sup_notification", model.PostCustomTypePrefix), post.Type)
require.Equal(t, bot.UserId, post.UserId)
require.Equal(t, "2 members of the test workspace have requested a workspace upgrade for: ", post.Message)
})
t.Run("successfully send trial upgrade notification", func(t *testing.T) {
th := Setup(t).InitBasic(t)
th.App.Srv().SetLicense(model.NewTestLicense("cloud"))
// some notifications
_, appErr := th.App.SaveAdminNotifyData(&model.NotifyAdminData{
UserId: th.BasicUser.Id,
RequiredPlan: model.LicenseShortSkuProfessional,
RequiredFeature: model.PaidFeatureAllProfessionalfeatures,
Trial: true,
})
require.Nil(t, appErr)
appErr = th.App.SendNotifyAdminPosts(th.Context, "test", "", true)
require.Nil(t, appErr)
bot, appErr := th.App.GetSystemBot(th.Context)
require.Nil(t, appErr)
// message sending is async, wait time for it
var channel *model.Channel
var err error
timeout := 5 * time.Second
begin := time.Now()
for {
if time.Since(begin) > timeout {
break
}
channel, err = th.App.Srv().Store().Channel().GetByName("", model.GetDMNameFromIds(bot.UserId, th.SystemAdminUser.Id), false)
if err == nil && channel != nil {
break
}
time.Sleep(100 * time.Millisecond)
}
require.NoError(t, err, "Expected message to have been sent within %d seconds", timeout)
postList, err := th.App.Srv().Store().Post().GetPosts(th.Context, model.GetPostsOptions{ChannelId: channel.Id, Page: 0, PerPage: 1}, false, map[string]bool{})
require.NoError(t, err)
post := postList.Posts[postList.Order[0]]
require.Equal(t, fmt.Sprintf("%sup_notification", model.PostCustomTypePrefix), post.Type)
require.Equal(t, bot.UserId, post.UserId)
require.Equal(t, "1 member of the test workspace has requested starting the Enterprise trial for access to: ", post.Message)
})
t.Run("error when trying to send upgrade post before end of cool off period", func(t *testing.T) {
th := Setup(t).InitBasic(t)
th.App.Srv().SetLicense(model.NewTestLicense("cloud"))
// some notifications
_, appErr := th.App.SaveAdminNotifyData(&model.NotifyAdminData{
UserId: th.BasicUser.Id,
RequiredPlan: model.LicenseShortSkuProfessional,
RequiredFeature: model.PaidFeatureAllProfessionalfeatures,
})
require.Nil(t, appErr)
appErr = th.App.SendNotifyAdminPosts(th.Context, "", "", false)
require.Nil(t, appErr)
// add some more notifications while in cool off
_, appErr = th.App.SaveAdminNotifyData(&model.NotifyAdminData{
UserId: th.BasicUser.Id,
RequiredPlan: model.LicenseShortSkuProfessional,
RequiredFeature: model.PaidFeatureCustomUsergroups,
})
require.Nil(t, appErr)
// second time trying to notify is forbidden
appErr = th.App.SendNotifyAdminPosts(th.Context, "", "", false)
require.NotNil(t, appErr)
require.Equal(t, appErr.Error(), "SendNotifyAdminPosts: Unable to send notification post., Cannot notify yet")
})
t.Run("can send upgrade post at the end of cool off period", func(t *testing.T) {
th := Setup(t).InitBasic(t)
th.App.Srv().SetLicense(model.NewTestLicense("cloud"))
th.App.Srv().SetNotifyAdminCoolOffDaysOverride("0.00003472222222") // set to 3 seconds
t.Cleanup(func() { th.App.Srv().SetNotifyAdminCoolOffDaysOverride("") })
// some notifications
_, appErr := th.App.SaveAdminNotifyData(&model.NotifyAdminData{
UserId: th.BasicUser.Id,
RequiredPlan: model.LicenseShortSkuProfessional,
RequiredFeature: model.PaidFeatureAllProfessionalfeatures,
})
require.Nil(t, appErr)
appErr = th.App.SendNotifyAdminPosts(th.Context, "", "", false)
require.Nil(t, appErr)
// add some more notifications while in cool off
_, appErr = th.App.SaveAdminNotifyData(&model.NotifyAdminData{
UserId: th.BasicUser.Id,
RequiredPlan: model.LicenseShortSkuProfessional,
RequiredFeature: model.PaidFeatureCustomUsergroups,
})
require.Nil(t, appErr)
time.Sleep(5 * time.Second)
// no error sending second time
appErr = th.App.SendNotifyAdminPosts(th.Context, "", "", false)
require.Nil(t, appErr)
})
t.Run("can filter notifications when plan changes within cool off period", func(t *testing.T) {
th := Setup(t).InitBasic(t)
th.App.Srv().SetLicense(model.NewTestLicense("cloud"))
// some notifications
_, appErr := th.App.SaveAdminNotifyData(&model.NotifyAdminData{
UserId: th.BasicUser.Id,
RequiredPlan: model.LicenseShortSkuProfessional,
RequiredFeature: model.PaidFeatureAllProfessionalfeatures,
Trial: false,
})
require.Nil(t, appErr)
_, appErr = th.App.SaveAdminNotifyData(&model.NotifyAdminData{
UserId: th.BasicUser2.Id,
RequiredPlan: model.LicenseShortSkuEnterprise,
RequiredFeature: model.PaidFeatureAllEnterprisefeatures,
Trial: false,
})
require.Nil(t, appErr)
appErr = th.App.SendNotifyAdminPosts(th.Context, "test", model.LicenseShortSkuProfessional, false) // try and send notification but workspace currentSKU has since changed to cloud-professional
require.Nil(t, appErr)
bot, appErr := th.App.GetSystemBot(th.Context)
require.Nil(t, appErr)
// message sending is async, wait time for it
var channel *model.Channel
var err error
timeout := 5 * time.Second
begin := time.Now()
for {
if time.Since(begin) > timeout {
break
}
channel, err = th.App.Srv().Store().Channel().GetByName("", model.GetDMNameFromIds(bot.UserId, th.SystemAdminUser.Id), false)
if err == nil && channel != nil {
break
}
time.Sleep(100 * time.Millisecond)
}
require.NoError(t, err, "Expected message to have been sent within %d seconds", timeout)
postList, err := th.App.Srv().Store().Post().GetPosts(th.Context, model.GetPostsOptions{ChannelId: channel.Id, Page: 0, PerPage: 1}, false, map[string]bool{})
require.NoError(t, err)
post := postList.Posts[postList.Order[0]]
require.Equal(t, fmt.Sprintf("%sup_notification", model.PostCustomTypePrefix), post.Type)
require.Equal(t, bot.UserId, post.UserId)
require.Equal(t, "1 member of the test workspace has requested a workspace upgrade for: ", post.Message) // expect only one member's notification even though 2 were added
})
}