mattermost/server/channels/api4/limits_test.go
Maria A Nunez 461db71178
Add single-channel guest tracking and reporting (#35451)
* Add single-channel guest tracking and reporting

- Add AnalyticsGetSingleChannelGuestCount store method to count guests in exactly one channel
- Exclude single-channel guests from active user seat count in GetServerLimits
- Add single-channel guest count to standard analytics response
- Add Single-channel Guests card to System Statistics page with overage warning
- Add Single-channel guests row to Edition and License page with overage styling
- Add dismissible admin-only banner when single-channel guest limit is exceeded
- Gate feature behind non-Entry SKU and guest accounts enabled checks
- Re-fetch server limits on config changes for reactive UI updates
- Fix label alignment in license details panel

Made-with: Cursor

* Refine single-channel guest tracking

- Remove license GuestAccounts feature check from shouldTrackSingleChannelGuests (only config matters)
- Re-add getServerLimits calls on page mount for fresh data
- Remove config-change reactivity code (componentDidUpdate, useEffect)
- Add server i18n translations for error strings
- Sync webapp i18n via extract
- Add inline comments for business logic
- Restore struct field comments in ServerLimits model
- Add Playwright E2E tests for single-channel guest feature
- Fix label alignment in license details panel

Made-with: Cursor

* Guests over limit fixes and PR feedback

* Fix linter issues and code quality improvements

- Use max() builtin to clamp adjusted user count instead of if-statement (modernize linter)
- Change banner type from ADVISOR to CRITICAL for proper red color styling

Made-with: Cursor

* Fix overage warnings incorrectly counting single-channel guests

Single-channel guests are free and should not trigger license seat
overage warnings. Update all overage checks to use
serverLimits.activeUserCount (seat-adjusted, excluding SCG) instead
of the raw total_users_count or TOTAL_USERS analytics stat.

- UserSeatAlertBanner on License page: use serverLimits.activeUserCount
- UserSeatAlertBanner on Site Statistics page: use serverLimits.activeUserCount
- ActivatedUserCard display and overage check: use serverLimits.activeUserCount
- OverageUsersBanner: use serverLimits.activeUserCount

Made-with: Cursor

* Use license.Users as fallback for singleChannelGuestLimit before limits load

This prevents the SingleChannelGuestsCard from showing a false overage
state before serverLimits has been fetched, while still rendering the
card immediately on page load.

Made-with: Cursor

* Fix invite modal overage banner incorrectly counting single-channel guests

Made-with: Cursor

* Fix invitation modal tests missing limits entity in mock state

Made-with: Cursor

* Fix tests

* Add E2E test for single-channel guest exceeded limit scenario

Made-with: Cursor

* Fix TypeScript errors in single channel guests E2E test

Made-with: Cursor

* Fix channel name validation error caused by unawaited async getRandomId()

Made-with: Cursor

* Add contextual tooltips to stat cards when guest accounts are enabled

Made-with: Cursor

* Code review feedback: query builder, readability, tooltips, and alignment fixes

Made-with: Cursor

* Fix license page tooltip alignment, width, and SaveLicense SCG exclusion

Made-with: Cursor

* Fix banner dismiss, license spacing, and add dismiss test

Made-with: Cursor

* Exclude DM/GM channels from single-channel guest count and fix E2E tests

Filter the AnalyticsGetSingleChannelGuestCount query to only count
memberships in public/private channels, excluding DMs and GMs. Update
store tests with DM-only, GM-only, and mixed membership cases. Fix E2E
overage test to mock the server limits API instead of skipping, and
correct banner locator to use data-testid.

Made-with: Cursor
2026-03-10 10:31:10 -04:00

215 lines
7.1 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package api4
import (
"context"
"testing"
"github.com/stretchr/testify/require"
"github.com/mattermost/mattermost/server/public/model"
)
func TestGetServerLimits(t *testing.T) {
mainHelper.Parallel(t)
t.Run("admin users can get full server limits", func(t *testing.T) {
th := Setup(t).InitBasic(t)
// Set up unlicensed server
th.App.Srv().SetLicense(nil)
// Test with system admin
serverLimits, resp, err := th.SystemAdminClient.GetServerLimits(context.Background())
require.NoError(t, err)
CheckOKStatus(t, resp)
// Should have full access to all limits data
require.Greater(t, serverLimits.ActiveUserCount, int64(0))
require.Equal(t, int64(200), serverLimits.MaxUsersLimit)
require.Equal(t, int64(250), serverLimits.MaxUsersHardLimit)
require.Equal(t, int64(0), serverLimits.PostHistoryLimit)
require.Equal(t, int64(0), serverLimits.LastAccessiblePostTime)
})
t.Run("non-admin users get limited data with licensed server", func(t *testing.T) {
th := Setup(t).InitBasic(t)
// Set up licensed server with user limits
userLimit := 100
extraUsers := 10
postHistoryLimit := int64(10000)
license := model.NewTestLicense("")
license.IsSeatCountEnforced = true
license.Features.Users = &userLimit
license.ExtraUsers = &extraUsers
license.Limits = &model.LicenseLimits{
PostHistory: postHistoryLimit,
}
th.App.Srv().SetLicense(license)
// Test with regular user
serverLimits, resp, err := th.Client.GetServerLimits(context.Background())
require.NoError(t, err)
CheckOKStatus(t, resp)
// Non-admin users should get zero for user count data (privacy)
require.Equal(t, int64(0), serverLimits.ActiveUserCount)
require.Equal(t, int64(0), serverLimits.MaxUsersLimit)
require.Equal(t, int64(0), serverLimits.MaxUsersHardLimit)
// But should get message history limits (needed for UI)
require.Equal(t, postHistoryLimit, serverLimits.PostHistoryLimit)
})
t.Run("admin users get full limts", func(t *testing.T) {
th := Setup(t).InitBasic(t)
// Set up licensed server with post history limits
userLimit := 100
postHistoryLimit := int64(10000)
license := model.NewTestLicense("")
license.IsSeatCountEnforced = true
license.Features.Users = &userLimit
license.Limits = &model.LicenseLimits{
PostHistory: postHistoryLimit,
}
th.App.Srv().SetLicense(license)
// Test with system admin
serverLimits, resp, err := th.SystemAdminClient.GetServerLimits(context.Background())
require.NoError(t, err)
CheckOKStatus(t, resp)
// Should have full access to all limits data
require.Greater(t, serverLimits.ActiveUserCount, int64(0))
require.Equal(t, int64(100), serverLimits.MaxUsersLimit)
require.Equal(t, int64(100), serverLimits.MaxUsersHardLimit)
// Should have post history limits
require.Equal(t, postHistoryLimit, serverLimits.PostHistoryLimit)
// LastAccessiblePostTime may be 0 if no posts exist in test database, which is expected
require.GreaterOrEqual(t, serverLimits.LastAccessiblePostTime, int64(0))
})
t.Run("non-admin users get post history limits when configured", func(t *testing.T) {
th := Setup(t).InitBasic(t)
// Set up licensed server with post history limits
userLimit := 100
postHistoryLimit := int64(10000)
license := model.NewTestLicense("")
license.IsSeatCountEnforced = true
license.Features.Users = &userLimit
license.Limits = &model.LicenseLimits{
PostHistory: postHistoryLimit,
}
th.App.Srv().SetLicense(license)
// Test with regular user
serverLimits, resp, err := th.Client.GetServerLimits(context.Background())
require.NoError(t, err)
CheckOKStatus(t, resp)
// Non-admin users should get zero for user count data (privacy)
require.Equal(t, int64(0), serverLimits.ActiveUserCount)
require.Equal(t, int64(0), serverLimits.MaxUsersLimit)
require.Equal(t, int64(0), serverLimits.MaxUsersHardLimit)
// But should get post history limits (needed for UI)
require.Equal(t, postHistoryLimit, serverLimits.PostHistoryLimit)
// LastAccessiblePostTime may be 0 if no posts exist in test database, which is expected
require.GreaterOrEqual(t, serverLimits.LastAccessiblePostTime, int64(0))
})
t.Run("zero post history limit shows no limits", func(t *testing.T) {
th := Setup(t).InitBasic(t)
// Set up licensed server with zero post history limit
userLimit := 100
postHistoryLimit := int64(0)
license := model.NewTestLicense("")
license.IsSeatCountEnforced = true
license.Features.Users = &userLimit
license.Limits = &model.LicenseLimits{
PostHistory: postHistoryLimit,
}
th.App.Srv().SetLicense(license)
// Test with both admin and regular user
clients := []*model.Client4{th.SystemAdminClient, th.Client}
for i, client := range clients {
serverLimits, resp, err := client.GetServerLimits(context.Background())
require.NoError(t, err, "Failed for client %d", i)
CheckOKStatus(t, resp)
// Should have no post history limits
require.Equal(t, int64(0), serverLimits.PostHistoryLimit)
require.Equal(t, int64(0), serverLimits.LastAccessiblePostTime)
}
})
t.Run("license with nil Limits shows no post history limits", func(t *testing.T) {
th := Setup(t).InitBasic(t)
// Set up licensed server with nil Limits
userLimit := 100
license := model.NewTestLicense("")
license.IsSeatCountEnforced = true
license.Features.Users = &userLimit
license.Limits = nil // Explicitly set to nil
th.App.Srv().SetLicense(license)
// Test with both admin and regular user
clients := []*model.Client4{th.SystemAdminClient, th.Client}
for i, client := range clients {
serverLimits, resp, err := client.GetServerLimits(context.Background())
require.NoError(t, err, "Failed for client %d", i)
CheckOKStatus(t, resp)
// Should have no post history limits
require.Equal(t, int64(0), serverLimits.PostHistoryLimit)
require.Equal(t, int64(0), serverLimits.LastAccessiblePostTime)
}
})
t.Run("non-admin users get zero single channel guest data", func(t *testing.T) {
th := Setup(t).InitBasic(t)
userLimit := 100
license := model.NewTestLicense("")
license.Features.Users = &userLimit
th.App.Srv().SetLicense(license)
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.GuestAccountsSettings.Enable = true })
serverLimits, resp, err := th.Client.GetServerLimits(context.Background())
require.NoError(t, err)
CheckOKStatus(t, resp)
require.Equal(t, int64(0), serverLimits.SingleChannelGuestCount)
require.Equal(t, int64(0), serverLimits.SingleChannelGuestLimit)
})
t.Run("admin users get single channel guest data with eligible license", func(t *testing.T) {
th := Setup(t).InitBasic(t)
userLimit := 100
license := model.NewTestLicense("")
license.Features.Users = &userLimit
th.App.Srv().SetLicense(license)
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.GuestAccountsSettings.Enable = true })
serverLimits, resp, err := th.SystemAdminClient.GetServerLimits(context.Background())
require.NoError(t, err)
CheckOKStatus(t, resp)
require.Equal(t, int64(userLimit), serverLimits.SingleChannelGuestLimit)
})
}