[MM-44475] Team Unarchive: Do not allow to unarchive if workspace has reached the limit of teams (#20281)

* Prevent cloud limited installations from restoring teams when at or above the teams limit

* Code clean up

* fix i18n

* Actually fix i18n

* updates for govet

* Update model/client4.go

Co-authored-by: Vishal <vish9812@gmail.com>

* [MM-44397] Restrict team creation based on subscription limits (#20282)

* restrict team creation based on limits

* Update api4/team.go

Co-authored-by: Maria A Nunez <maria.nunez@mattermost.com>

* Fix tests

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Co-authored-by: Maria A Nunez <maria.nunez@mattermost.com>

* Add additional field to teamsusage

* Fix

* remove useless test

* Fix error for team creation

* Remove apostrophe

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Co-authored-by: Vishal <vish9812@gmail.com>
Co-authored-by: Maria A Nunez <maria.nunez@mattermost.com>
Этот коммит содержится в:
Nick Misasi
2022-05-31 15:06:54 -04:00
коммит произвёл GitHub
родитель 287edba57a
Коммит cffe921e62
10 изменённых файлов: 299 добавлений и 16 удалений

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

@@ -9,6 +9,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"
"strings"
"testing"
@@ -17,7 +18,9 @@ import (
"github.com/stretchr/testify/require"
"github.com/mattermost/mattermost-server/v6/app"
"github.com/mattermost/mattermost-server/v6/einterfaces/mocks"
"github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost-server/v6/plugin/plugintest/mock"
"github.com/mattermost/mattermost-server/v6/shared/i18n"
"github.com/mattermost/mattermost-server/v6/shared/mail"
"github.com/mattermost/mattermost-server/v6/utils/testutils"
@@ -67,27 +70,78 @@ func TestCreateTeam(t *testing.T) {
assert.Equal(t, *rteam.GroupConstrained, *groupConstrainedTeam.GroupConstrained, "GroupConstrained flags do not match")
})
th.Client.Logout()
t.Run("unauthenticated receives 403", func(t *testing.T) {
th.Client.Logout()
team := &model.Team{Name: GenerateTestUsername(), DisplayName: "Some Team", Type: model.TeamOpen}
_, resp, err := th.Client.CreateTeam(team)
require.Error(t, err)
CheckUnauthorizedStatus(t, resp)
team := &model.Team{Name: GenerateTestUsername(), DisplayName: "Some Team", Type: model.TeamOpen}
_, resp, err := th.Client.CreateTeam(team)
require.Error(t, err)
CheckUnauthorizedStatus(t, resp)
th.LoginBasic()
th.LoginBasic()
// Check the appropriate permissions are enforced.
defaultRolePermissions := th.SaveDefaultRolePermissions()
defer func() {
th.RestoreDefaultRolePermissions(defaultRolePermissions)
}()
// Check the appropriate permissions are enforced.
defaultRolePermissions := th.SaveDefaultRolePermissions()
defer func() {
th.RestoreDefaultRolePermissions(defaultRolePermissions)
}()
th.RemovePermissionFromRole(model.PermissionCreateTeam.Id, model.SystemUserRoleId)
th.AddPermissionToRole(model.PermissionCreateTeam.Id, model.SystemAdminRoleId)
th.RemovePermissionFromRole(model.PermissionCreateTeam.Id, model.SystemUserRoleId)
th.AddPermissionToRole(model.PermissionCreateTeam.Id, model.SystemAdminRoleId)
_, resp, err = th.Client.CreateTeam(team)
require.Error(t, err)
CheckForbiddenStatus(t, resp)
_, resp, err = th.Client.CreateTeam(team)
require.Error(t, err)
CheckForbiddenStatus(t, resp)
})
t.Run("cloud limit reached returns 400", func(t *testing.T) {
os.Setenv("MM_FEATUREFLAGS_CLOUDFREE", "true")
defer os.Unsetenv("MM_FEATUREFLAGS_CLOUDFREE")
th.App.ReloadConfig()
th.App.Srv().SetLicense(model.NewTestLicense("cloud"))
cloud := &mocks.CloudInterface{}
cloudImpl := th.App.Srv().Cloud
defer func() {
th.App.Srv().Cloud = cloudImpl
}()
th.App.Srv().Cloud = cloud
cloud.Mock.On("GetCloudLimits", mock.Anything).Return(&model.ProductLimits{
Teams: &model.TeamsLimits{
Active: model.NewInt(1),
},
}, nil).Once()
team := &model.Team{Name: GenerateTestUsername(), DisplayName: "Some Team", Type: model.TeamOpen}
_, resp, err := th.Client.CreateTeam(team)
require.Error(t, err)
CheckBadRequestStatus(t, resp)
})
t.Run("cloud below limit returns 200", func(t *testing.T) {
os.Setenv("MM_FEATUREFLAGS_CLOUDFREE", "true")
defer os.Unsetenv("MM_FEATUREFLAGS_CLOUDFREE")
th.App.ReloadConfig()
defer th.App.ReloadConfig()
th.App.Srv().SetLicense(model.NewTestLicense("cloud"))
cloud := &mocks.CloudInterface{}
cloudImpl := th.App.Srv().Cloud
defer func() {
th.App.Srv().Cloud = cloudImpl
}()
th.App.Srv().Cloud = cloud
cloud.Mock.On("GetCloudLimits", mock.Anything).Return(&model.ProductLimits{
Teams: &model.TeamsLimits{
Active: model.NewInt(200),
},
}, nil).Once()
team := &model.Team{Name: GenerateTestUsername(), DisplayName: "Some Team", Type: model.TeamOpen}
_, resp, err := th.Client.CreateTeam(team)
require.NoError(t, err)
CheckCreatedStatus(t, resp)
})
}
func TestCreateTeamSanitization(t *testing.T) {
@@ -578,6 +632,56 @@ func TestRestoreTeam(t *testing.T) {
require.NoError(t, err)
CheckOKStatus(t, resp)
})
t.Run("cloud limit reached returns 400", func(t *testing.T) {
// Create an archived team to be restored later
team := createTeam(t, true, model.TeamOpen)
os.Setenv("MM_FEATUREFLAGS_CLOUDFREE", "true")
defer os.Unsetenv("MM_FEATUREFLAGS_CLOUDFREE")
th.App.ReloadConfig()
th.App.Srv().SetLicense(model.NewTestLicense("cloud"))
cloud := &mocks.CloudInterface{}
cloudImpl := th.App.Srv().Cloud
defer func() {
th.App.Srv().Cloud = cloudImpl
}()
th.App.Srv().Cloud = cloud
cloud.Mock.On("GetCloudLimits", mock.Anything).Return(&model.ProductLimits{
Teams: &model.TeamsLimits{
Active: model.NewInt(1),
},
}, nil).Once()
_, resp, err := client.RestoreTeam(team.Id)
require.Error(t, err)
CheckBadRequestStatus(t, resp)
})
t.Run("cloud below limit returns 200", func(t *testing.T) {
os.Setenv("MM_FEATUREFLAGS_CLOUDFREE", "true")
defer os.Unsetenv("MM_FEATUREFLAGS_CLOUDFREE")
th.App.ReloadConfig()
th.App.Srv().SetLicense(model.NewTestLicense("cloud"))
cloud := &mocks.CloudInterface{}
cloudImpl := th.App.Srv().Cloud
defer func() {
th.App.Srv().Cloud = cloudImpl
}()
th.App.Srv().Cloud = cloud
cloud.Mock.On("GetCloudLimits", mock.Anything).Return(&model.ProductLimits{
Teams: &model.TeamsLimits{
Active: model.NewInt(200),
},
}, nil).Twice()
team := createTeam(t, true, model.TeamOpen)
_, resp, err := client.RestoreTeam(team.Id)
require.NoError(t, err)
CheckOKStatus(t, resp)
})
}
func TestPatchTeamSanitization(t *testing.T) {