enforce License.IsSeatCountEnforced if set (#31354)

* enforce License.IsSeatCountEnforced if set

If a license sets `IsSeatCountEnforced`, enforce the user limit therein
as a hard cap.

Fixes: https://mattermost.atlassian.net/browse/CLD-9260

* remove duplicate tests

* Improve user limit error messages and display

- Add separate error messages for licensed vs unlicensed servers
- Licensed servers: "Server exceeds maximum licensed users. ERROR_LICENSED_USERS_LIMITS"
- Unlicensed servers: "Server exceeds safe user limit. ERROR_SAFETY_LIMITS_EXCEEDED"
- Remove redundant "Contact administrator" text from activation errors shown to admins
- Fix system console to display actual server error messages instead of generic "Failed to activate user"

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* Add license nil check and test coverage

- Add license != nil check in GetServerLimits to prevent panic
- Add test case to verify graceful handling of license being set to nil
- Ensures fallback to hard-coded limits when license becomes nil

Co-authored-by: lieut-data <lieut-data@users.noreply.github.com>

* Fix user limits tests to expect license-specific error IDs

Update test expectations to use the new license-specific error IDs:
- app.user.update_active.license_user_limit.exceeded for licensed server user activation
- api.user.create_user.license_user_limits.exceeded for licensed server user creation

Also update frontend to show actual server error messages instead of generic ones in system console.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* Remove redundant license nil test

The test couldn't meaningfully verify nil license behavior since it relied on
hard-coded constants that can't be modified in the test.

Co-authored-by: lieut-data <lieut-data@users.noreply.github.com>

* Fix whitespace issue in limits_test.go

Remove unnecessary trailing newline to pass style checks.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* updated i18n

* s/ERROR_LICENSED_USERS_LIMITS/ERROR_LICENSED_USERS_LIMIT_EXCEEDED/, expand warning log

* Add 5% grace period for licensed user limits

- Add calculateGraceLimit() function with 5% or +1 minimum grace
- Apply grace period only to licensed servers with seat count enforcement
- Handle zero user licenses by returning zero grace limit
- Add comprehensive test coverage for grace period scenarios
- Unlicensed servers maintain existing hard-coded limits without grace

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* Fix TestCreateUserOrGuestSeatCountEnforcement to account for 5% grace period

The test was failing because it expected user creation to fail at exactly
the license limit, but the implementation now includes a 5% grace period
before enforcement kicks in.

Changes:
- Update test cases to create users up to the grace limit (6 for a 5-user license)
- Add comments explaining the grace period calculation
- Both regular user and guest user creation tests now properly validate
  enforcement at the grace limit rather than the base license limit

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* Fix TestUpdateActiveWithUserLimits to account for 5% grace period

Update test expectations to match the new grace period behavior:
- At base limit (100) but below grace limit (105): should succeed
- At grace limit (105): should fail
- Above grace limit (106): should fail

This aligns the tests with the license enforcement implementation
that includes a 5% grace period above the licensed user count.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

---------

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
Co-authored-by: lieut-data <lieut-data@users.noreply.github.com>
Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
Jesse Hallam
2025-06-13 17:12:05 -03:00
коммит произвёл GitHub
родитель f89326574f
Коммит 0082e3e94d
8 изменённых файлов: 978 добавлений и 204 удалений

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

@@ -21,7 +21,6 @@ import (
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/shared/request"
oauthgitlab "github.com/mattermost/mattermost/server/v8/channels/app/oauthproviders/gitlab"
"github.com/mattermost/mattermost/server/v8/channels/app/users"
"github.com/mattermost/mattermost/server/v8/channels/store"
storemocks "github.com/mattermost/mattermost/server/v8/channels/store/storetest/mocks"
"github.com/mattermost/mattermost/server/v8/channels/utils/testutils"
@@ -2292,161 +2291,3 @@ func TestGetUsersForReporting(t *testing.T) {
require.NotNil(t, userReports)
})
}
func TestCreateUserOrGuest(t *testing.T) {
mainHelper.Parallel(t)
t.Run("base case - you can create a user", func(t *testing.T) {
th := Setup(t)
defer th.TearDown()
user := &model.User{
Email: "TestCreateUserOrGuest@example.com",
Username: "username_123",
Nickname: "nn_username_123",
Password: "Password1",
EmailVerified: true,
}
createdUser, appErr := th.App.createUserOrGuest(th.Context, user, false)
require.Nil(t, appErr)
require.Equal(t, "username_123", createdUser.Username)
})
t.Run("cannot create user when user count has exceeded the permissible limit", func(t *testing.T) {
th := SetupWithStoreMock(t)
defer th.TearDown()
mockUserStore := storemocks.UserStore{}
mockUserStore.On("Count", mock.Anything).Return(int64(12000), nil)
mockStore := th.App.Srv().Store().(*storemocks.Store)
mockStore.On("User").Return(&mockUserStore)
user := &model.User{
Email: "TestCreateUserOrGuest@example.com",
Username: "username_123",
Nickname: "nn_username_123",
Password: "Password1",
EmailVerified: true,
}
createdUser, appErr := th.App.createUserOrGuest(th.Context, user, false)
require.NotNil(t, appErr)
require.Nil(t, createdUser)
})
t.Run("can create user when server is exactly on limit", func(t *testing.T) {
th := SetupWithStoreMock(t)
defer th.TearDown()
id := NewTestId()
userCreationMocks(t, th, id, 5000)
user := &model.User{
Email: "TestCreateUserOrGuest@example.com",
Username: "username_123",
Nickname: "nn_username_123",
Password: "Password1",
EmailVerified: true,
}
createdUser, appErr := th.App.createUserOrGuest(th.Context, user, false)
require.Nil(t, appErr)
require.Equal(t, "username_123", createdUser.Username)
})
t.Run("licensed server can create user when server is OVER limit", func(t *testing.T) {
th := SetupWithStoreMock(t)
defer th.TearDown()
id := NewTestId()
userCreationMocks(t, th, id, 20000)
user := &model.User{
Email: "TestCreateUserOrGuest@example.com",
Username: "username_123",
Nickname: "nn_username_123",
Password: "Password1",
EmailVerified: true,
}
th.App.Srv().SetLicense(model.NewTestLicense(""))
createdUser, appErr := th.App.createUserOrGuest(th.Context, user, false)
require.Nil(t, appErr)
require.Equal(t, "username_123", createdUser.Username)
})
t.Run("licensed server can create user when server is UNDER limit", func(t *testing.T) {
th := SetupWithStoreMock(t)
defer th.TearDown()
id := NewTestId()
userCreationMocks(t, th, id, 10)
user := &model.User{
Email: "TestCreateUserOrGuest@example.com",
Username: "username_123",
Nickname: "nn_username_123",
Password: "Password1",
EmailVerified: true,
}
th.App.Srv().SetLicense(model.NewTestLicense(""))
createdUser, appErr := th.App.createUserOrGuest(th.Context, user, false)
require.Nil(t, appErr)
require.Equal(t, "username_123", createdUser.Username)
})
}
func userCreationMocks(t *testing.T, th *TestHelper, userID string, activeUserCount int64) {
mockUserStore := storemocks.UserStore{}
mockUserStore.On("Count", mock.Anything).Return(activeUserCount, nil)
mockUserStore.On("IsEmpty", mock.Anything).Return(false, nil)
mockUserStore.On("VerifyEmail", mock.Anything, "TestCreateUserOrGuest@example.com").Return("", nil)
mockUserStore.On("InvalidateProfilesInChannelCacheByUser", mock.Anything).Return()
mockUserStore.On("InvalidateProfileCacheForUser", mock.Anything).Return()
mockUserStore.On("Save", mock.Anything, mock.Anything).Return(&model.User{
Id: userID,
Email: "TestCreateUserOrGuest@example.com",
Username: "username_123",
Nickname: "nn_username_123",
Password: "Password1",
EmailVerified: true,
}, nil)
mockUserStore.On("Get", mock.Anything, userID).Return(&model.User{
Id: userID,
Email: "TestCreateUserOrGuest@example.com",
Username: "username_123",
Nickname: "nn_username_123",
Password: "Password1",
EmailVerified: true,
}, nil)
mockGroupStore := storemocks.GroupStore{}
mockGroupStore.On("GetByName", "username_123", mock.Anything).Return(nil, nil)
mockChannelStore := storemocks.ChannelStore{}
mockChannelStore.On("InvalidateAllChannelMembersForUser", mock.Anything).Return()
mockPreferencesStore := storemocks.PreferenceStore{}
mockPreferencesStore.On("Save", mock.Anything).Return(nil)
mockProductNoticeStore := storemocks.ProductNoticesStore{}
mockProductNoticeStore.On("View", userID, mock.Anything).Return(nil)
mockStore := th.App.Srv().Store().(*storemocks.Store)
mockStore.On("User").Return(&mockUserStore)
mockStore.On("Group").Return(&mockGroupStore)
mockStore.On("Channel").Return(&mockChannelStore)
mockStore.On("Preference").Return(&mockPreferencesStore)
mockStore.On("ProductNotices").Return(&mockProductNoticeStore)
var err error
th.App.ch.srv.userService, err = users.New(users.ServiceConfig{
UserStore: &mockUserStore,
SessionStore: &storemocks.SessionStore{},
OAuthStore: &storemocks.OAuthStore{},
ConfigFn: th.App.ch.srv.platform.Config,
LicenseFn: th.App.ch.srv.License,
})
require.NoError(t, err)
}