MM-41969: Sends notifications of group mentions without LDAPGroups license feature. (#19598)

* MM-41969: Sends notifications of group mentions without LDAPGroups license feature.

* MM-41969: Improves tests.

* MM-41969: Removes redundant test.

* MM-41969: Test that group mention is not sent without license.

* MM-41969: Fix for incorrect test requirement.

* MM-41969: Change back to require.Nil.

* MM-41969: Fix lint.
Этот коммит содержится в:
mkraft
2022-02-18 20:01:05 -05:00
коммит произвёл GitHub
родитель d4378a6b42
Коммит e96daef1e1
2 изменённых файлов: 64 добавлений и 6 удалений

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

@@ -1041,7 +1041,7 @@ func (a *App) allowChannelMentions(post *model.Post, numProfiles int) bool {
// allowGroupMentions returns whether or not the group mentions are allowed for the given post.
func (a *App) allowGroupMentions(post *model.Post) bool {
if license := a.Srv().License(); license == nil || !*license.Features.LDAPGroups {
if license := a.Srv().License(); license == nil || (license.SkuShortName != model.LicenseShortSkuProfessional && license.SkuShortName != model.LicenseShortSkuEnterprise) {
return false
}

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

@@ -16,6 +16,20 @@ import (
"github.com/mattermost/mattermost-server/v6/utils"
)
func getLicWithSkuShortName(skuShortName string) *model.License {
return &model.License{
Features: &model.Features{},
Customer: &model.Customer{
Name: "TestName",
Email: "test@example.com",
},
SkuName: "SKU NAME",
SkuShortName: skuShortName,
StartsAt: model.GetMillis() - 1000,
ExpiresAt: model.GetMillis() + 100000,
}
}
func TestSendNotifications(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
@@ -36,6 +50,37 @@ func TestSendNotifications(t *testing.T) {
require.NotNil(t, mentions)
require.True(t, utils.StringInSlice(th.BasicUser2.Id, mentions), "mentions", mentions)
t.Run("license is required for group mention", func(t *testing.T) {
group := th.CreateGroup()
group.AllowReference = true
group, updateErr := th.App.UpdateGroup(group)
require.Nil(t, updateErr)
_, upsertErr := th.App.UpsertGroupMember(group.Id, th.BasicUser2.Id)
require.Nil(t, upsertErr)
groupMentionPost := &model.Post{
UserId: th.BasicUser.Id,
ChannelId: th.BasicChannel.Id,
Message: fmt.Sprintf("hello @%s group", *group.Name),
CreateAt: model.GetMillis() - 10000,
}
groupMentionPost, createPostErr := th.App.CreatePost(th.Context, groupMentionPost, th.BasicChannel, false, true)
require.Nil(t, createPostErr)
mentions, err = th.App.SendNotifications(groupMentionPost, th.BasicTeam, th.BasicChannel, th.BasicUser, nil, true)
require.NoError(t, err)
require.NotNil(t, mentions)
require.Len(t, mentions, 0)
th.App.Srv().SetLicense(getLicWithSkuShortName(model.LicenseShortSkuProfessional))
mentions, err = th.App.SendNotifications(groupMentionPost, th.BasicTeam, th.BasicChannel, th.BasicUser, nil, true)
require.NoError(t, err)
require.NotNil(t, mentions)
require.Len(t, mentions, 1)
})
dm, appErr := th.App.GetOrCreateDirectChannel(th.Context, th.BasicUser.Id, th.BasicUser2.Id)
require.Nil(t, appErr)
@@ -1048,12 +1093,25 @@ func TestAllowGroupMentions(t *testing.T) {
post := &model.Post{ChannelId: th.BasicChannel.Id, UserId: th.BasicUser.Id}
t.Run("should return false without ldap groups license", func(t *testing.T) {
allowGroupMentions := th.App.allowGroupMentions(post)
assert.False(t, allowGroupMentions)
})
t.Run("should return false without the correct license sku short name", func(t *testing.T) {
tests := map[string]struct {
license *model.License
want bool
}{
"no license": {nil, false},
"license with wrong SKU short name": {getLicWithSkuShortName("foobar"), false},
"'professional' license": {getLicWithSkuShortName(model.LicenseShortSkuProfessional), true},
"'enterprise' license": {getLicWithSkuShortName(model.LicenseShortSkuEnterprise), true},
}
th.App.Srv().SetLicense(model.NewTestLicense("ldap_groups"))
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
th.App.Srv().SetLicense(tc.license)
got := th.App.allowGroupMentions(post)
assert.Equal(t, tc.want, got)
})
}
})
t.Run("should return true for a regular post with few channel members", func(t *testing.T) {
allowGroupMentions := th.App.allowGroupMentions(post)