[MM-29646] Add check for IsPaidTier, and don't send emails over limit for guest invites (#15958)

* Add check for ispaidtier to email invites

* Add same check for guest emails

* Update api4/team.go

Co-authored-by: Mario de Frutos Dieguez <mario@defrutos.org>

* Update api4/team.go

Co-authored-by: Mario de Frutos Dieguez <mario@defrutos.org>

* Check for an error and return if there is one

* Fix tests

* Remove test to move to enterprise repo

Co-authored-by: Mario de Frutos Dieguez <mario@defrutos.org>
Этот коммит содержится в:
Nick Misasi
2020-10-14 19:44:44 -04:00
коммит произвёл GitHub
родитель 07948ae7bc
Коммит e27a75fba6
2 изменённых файлов: 33 добавлений и 66 удалений

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

@@ -1200,8 +1200,15 @@ func inviteUsersToTeam(c *Context, w http.ResponseWriter, r *http.Request) {
if graceful {
cloudUserLimit := *c.App.Config().ExperimentalSettings.CloudUserLimit
var invitesOverLimit []*model.EmailInviteWithError
if cloudUserLimit > 0 && c.IsSystemAdmin() {
emailList, invitesOverLimit, _ = c.App.GetErrorListForEmailsOverLimit(emailList, cloudUserLimit)
if c.App.Srv().License() != nil && *c.App.Srv().License().Features.Cloud && cloudUserLimit > 0 && c.IsSystemAdmin() {
subscription, subErr := c.App.Cloud().GetSubscription()
if subErr != nil {
c.Err = subErr
return
}
if subscription == nil || subscription.IsPaidTier != "true" {
emailList, invitesOverLimit, _ = c.App.GetErrorListForEmailsOverLimit(emailList, cloudUserLimit)
}
}
var invitesWithError []*model.EmailInviteWithError
var err *model.AppError
@@ -1279,7 +1286,30 @@ func inviteGuestsToChannels(c *Context, w http.ResponseWriter, r *http.Request)
auditRec.AddMeta("channels", guestsInvite.Channels)
if graceful {
invitesWithError, err := c.App.InviteGuestsToChannelsGracefully(c.Params.TeamId, guestsInvite, c.App.Session().UserId)
cloudUserLimit := *c.App.Config().ExperimentalSettings.CloudUserLimit
var invitesOverLimit []*model.EmailInviteWithError
if c.App.Srv().License() != nil && *c.App.Srv().License().Features.Cloud && cloudUserLimit > 0 && c.IsSystemAdmin() {
subscription, subErr := c.App.Cloud().GetSubscription()
if subErr != nil {
c.Err = subErr
return
}
if subscription == nil || subscription.IsPaidTier != "true" {
guestsInvite.Emails, invitesOverLimit, _ = c.App.GetErrorListForEmailsOverLimit(guestsInvite.Emails, cloudUserLimit)
}
}
var invitesWithError []*model.EmailInviteWithError
var err *model.AppError
if guestsInvite.Emails != nil {
invitesWithError, err = c.App.InviteGuestsToChannelsGracefully(c.Params.TeamId, guestsInvite, c.App.Session().UserId)
}
if len(invitesOverLimit) > 0 {
invitesWithError = append(invitesWithError, invitesOverLimit...)
}
if err != nil {
errList := make([]string, 0, len(invitesWithError))
for _, inv := range invitesWithError {

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

@@ -2726,69 +2726,6 @@ func TestImportTeam(t *testing.T) {
})
}
func TestInviteUsersToTeamWithUserLimit(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
email1 := th.GenerateTestEmail()
email2 := th.GenerateTestEmail()
email3 := th.GenerateTestEmail()
th.App.UpdateConfig(func(cfg *model.Config) {
*cfg.ServiceSettings.EnableEmailInvitations = true
*cfg.ExperimentalSettings.CloudUserLimit = 2
})
t.Run("System admin, invite when at limit should fail", func(t *testing.T) {
invitesWithErrors, resp := th.SystemAdminClient.InviteUsersToTeamGracefully(th.BasicTeam.Id, []string{email1, email2})
CheckNoError(t, resp)
require.Len(t, invitesWithErrors, 2)
require.NotNil(t, invitesWithErrors[0].Error)
assert.Equal(t, invitesWithErrors[0].Error.Message, "You've reached the free tier user limit")
require.NotNil(t, invitesWithErrors[1].Error)
assert.Equal(t, invitesWithErrors[1].Error.Message, "You've reached the free tier user limit")
})
t.Run("Regular user, invite when at limit should succeed", func(t *testing.T) {
invitesWithErrors, resp := th.Client.InviteUsersToTeamGracefully(th.BasicTeam.Id, []string{email3})
CheckNoError(t, resp)
require.Len(t, invitesWithErrors, 1)
assert.Nil(t, invitesWithErrors[0].Error)
})
th.App.UpdateConfig(func(cfg *model.Config) {
*cfg.ExperimentalSettings.CloudUserLimit = 5
})
t.Run("With one remaining user inviting more than one user as admin invites only one user", func(t *testing.T) {
invitesWithErrors, resp := th.SystemAdminClient.InviteUsersToTeamGracefully(th.BasicTeam.Id, []string{email1, email2})
CheckNoError(t, resp)
require.Len(t, invitesWithErrors, 2)
require.Nil(t, invitesWithErrors[0].Error)
require.NotNil(t, invitesWithErrors[1].Error)
assert.Equal(t, invitesWithErrors[1].Error.Message, "You've reached the free tier user limit")
})
t.Run("With one remaining user inviting more than one user as a regular user sends all invites", func(t *testing.T) {
invitesWithErrors, resp := th.Client.InviteUsersToTeamGracefully(th.BasicTeam.Id, []string{email1, email2})
CheckNoError(t, resp)
require.Len(t, invitesWithErrors, 2)
assert.Nil(t, invitesWithErrors[0].Error)
assert.Nil(t, invitesWithErrors[1].Error)
})
th.App.UpdateConfig(func(cfg *model.Config) {
*cfg.ExperimentalSettings.CloudUserLimit = 100
})
t.Run("Invited user count is well below limit", func(t *testing.T) {
invitesWithErrors, resp := th.SystemAdminClient.InviteUsersToTeamGracefully(th.BasicTeam.Id, []string{email1, email2})
CheckNoError(t, resp)
require.Len(t, invitesWithErrors, 2)
require.Nil(t, invitesWithErrors[0].Error)
})
}
func TestInviteUsersToTeam(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()