[MM-43649] - Ability for end users to notify admin to upgrade workspace (#20338)
* [MM-43649] - Ability for end users to notify admin to upgrade workspace * add test case * move code to app layer * feedback impl * feedback impl-1 * feedback impl * simplify logic * fix typo and lint * fix app layers * Use a single critical section for users notified admin (#20488) * Use a single critical section for users notifying admin * change persistence mechanism * add more tests * change bot message * fix translations * update logic to notify once within cooloff period Co-authored-by: Mattermod <mattermod@users.noreply.github.com> Co-authored-by: Nathaniel Allred <neallred@protonmail.com>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
dad8eab777
Коммит
1d9fa3c333
@@ -48,6 +48,25 @@ func (api *API) InitCloud() {
|
||||
|
||||
// POST /api/v4/cloud/webhook
|
||||
api.BaseRoutes.Cloud.Handle("/webhook", api.CloudAPIKeyRequired(handleCWSWebhook)).Methods("POST")
|
||||
|
||||
api.BaseRoutes.Cloud.Handle("/notify-admin-to-upgrade", api.APISessionRequired(handleNotifyAdminToUpgrade)).Methods("POST")
|
||||
}
|
||||
|
||||
func handleNotifyAdminToUpgrade(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
var notifyAdminRequest *model.NotifyAdminToUpgradeRequest
|
||||
err := json.NewDecoder(r.Body).Decode(¬ifyAdminRequest)
|
||||
if err != nil {
|
||||
c.SetInvalidParam("notifyAdminRequest")
|
||||
return
|
||||
}
|
||||
|
||||
appErr := c.App.NotifySystemAdminsToUpgrade(c.AppContext, notifyAdminRequest.CurrentTeamId)
|
||||
if appErr != nil {
|
||||
c.Err = appErr
|
||||
return
|
||||
}
|
||||
|
||||
ReturnStatusOK(w)
|
||||
}
|
||||
|
||||
func getSubscription(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
@@ -5,9 +5,12 @@ package api4
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/require"
|
||||
@@ -295,6 +298,108 @@ func Test_requestTrial(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestNotifyAdminToUpgrade(t *testing.T) {
|
||||
t.Run("user can only notify admin once in cool off period", func(t *testing.T) {
|
||||
th := Setup(t).InitBasic().InitLogin()
|
||||
defer th.TearDown()
|
||||
|
||||
statusCode := th.Client.NotifyAdmin(&model.NotifyAdminToUpgradeRequest{
|
||||
CurrentTeamId: th.BasicTeam.Id,
|
||||
})
|
||||
|
||||
bot, appErr := th.App.GetSystemBot()
|
||||
require.Nil(t, appErr)
|
||||
|
||||
// message sending is async, wait time for it
|
||||
var channel *model.Channel
|
||||
var err error
|
||||
var 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(model.GetPostsOptions{ChannelId: channel.Id, Page: 0, PerPage: 1}, false, map[string]bool{})
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, len(postList.Order), 1)
|
||||
|
||||
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, fmt.Sprintf("A member of %s has notified you to upgrade this workspace.", th.BasicTeam.Name), post.Message)
|
||||
|
||||
require.Equal(t, http.StatusOK, statusCode)
|
||||
|
||||
// second time trying to call notify endpoint by same user is forbidden
|
||||
statusCode = th.Client.NotifyAdmin(&model.NotifyAdminToUpgradeRequest{
|
||||
CurrentTeamId: th.BasicTeam.Id,
|
||||
})
|
||||
require.Equal(t, http.StatusForbidden, statusCode)
|
||||
})
|
||||
|
||||
t.Run("user can only notify admin after cool off period", func(t *testing.T) {
|
||||
th := Setup(t).InitBasic().InitLogin()
|
||||
defer th.TearDown()
|
||||
|
||||
os.Setenv("MM_CLOUD_NOTIFY_ADMIN_COOL_OFF_DAYS", "0.00003472222222") // set to 3 seconds
|
||||
defer os.Unsetenv("MM_CLOUD_NOTIFY_ADMIN_COOL_OFF_DAYS")
|
||||
|
||||
statusCode := th.Client.NotifyAdmin(&model.NotifyAdminToUpgradeRequest{
|
||||
CurrentTeamId: th.BasicTeam.Id,
|
||||
})
|
||||
|
||||
bot, appErr := th.App.GetSystemBot()
|
||||
require.Nil(t, appErr)
|
||||
|
||||
channel, err := th.App.Srv().Store.Channel().GetByName("", model.GetDMNameFromIds(bot.UserId, th.SystemAdminUser.Id), false)
|
||||
require.NoError(t, err)
|
||||
|
||||
postList, err := th.App.Srv().Store.Post().GetPosts(model.GetPostsOptions{ChannelId: channel.Id, Page: 0, PerPage: 1}, false, map[string]bool{})
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, len(postList.Order), 1)
|
||||
|
||||
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, fmt.Sprintf("A member of %s has notified you to upgrade this workspace.", th.BasicTeam.Name), post.Message)
|
||||
|
||||
require.Equal(t, http.StatusOK, statusCode)
|
||||
|
||||
time.Sleep(5 * time.Second)
|
||||
|
||||
// second time trying to call notify endpoint by same user is NOT forbidden because it is after cool off period set to 3 seconds
|
||||
statusCode = th.Client.NotifyAdmin(&model.NotifyAdminToUpgradeRequest{
|
||||
CurrentTeamId: th.BasicTeam.Id,
|
||||
})
|
||||
|
||||
require.Equal(t, http.StatusOK, statusCode)
|
||||
})
|
||||
|
||||
t.Run("can cloud/model.Notify", func(t *testing.T) {
|
||||
|
||||
os.Setenv("MM_CLOUD_NOTIFY_ADMIN_COOL_OFF_DAYS", "10") // set to 10 days
|
||||
canNotify := model.CanNotify(model.GetMillis())
|
||||
require.Equal(t, false, canNotify)
|
||||
|
||||
os.Setenv("MM_CLOUD_NOTIFY_ADMIN_COOL_OFF_DAYS", "0.00003472222222") // set to 3 seconds
|
||||
canNotify = model.CanNotify(model.GetMillis())
|
||||
time.Sleep(5 * time.Second)
|
||||
require.Equal(t, false, canNotify)
|
||||
os.Unsetenv("MM_CLOUD_NOTIFY_ADMIN_COOL_OFF_DAYS")
|
||||
})
|
||||
}
|
||||
func Test_validateBusinessEmail(t *testing.T) {
|
||||
t.Run("Initial request has invalid email", func(t *testing.T) {
|
||||
th := Setup(t).InitBasic()
|
||||
|
||||
Ссылка в новой задаче
Block a user