Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
Server CI Nightly Race / Build Environment (push) Failing after 1s
151 lines
6.2 KiB
Go
151 lines
6.2 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package app
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
)
|
|
|
|
func TestAccessTokenExpiryBucket(t *testing.T) {
|
|
now := int64(1_000_000_000_000)
|
|
|
|
testCases := []struct {
|
|
name string
|
|
expiresAt int64
|
|
expected int
|
|
}{
|
|
{"already expired", now - model.DayInMilliseconds, 0},
|
|
{"expires now", now, 0},
|
|
{"12 hours left -> 1-day bucket", now + 12*60*60*1000, 1},
|
|
{"exactly 1 day left -> 1-day bucket", now + 1*model.DayInMilliseconds, 1},
|
|
{"2 days left -> 3-day bucket", now + 2*model.DayInMilliseconds, 3},
|
|
{"exactly 3 days left -> 3-day bucket", now + 3*model.DayInMilliseconds, 3},
|
|
{"5 days left -> 7-day bucket", now + 5*model.DayInMilliseconds, 7},
|
|
{"exactly 7 days left -> 7-day bucket", now + 7*model.DayInMilliseconds, 7},
|
|
{"8 days left -> outside cascade", now + 8*model.DayInMilliseconds, 0},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
require.Equal(t, tc.expected, accessTokenExpiryBucket(tc.expiresAt, now))
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestNotifyExpiringAccessTokens(t *testing.T) {
|
|
setup := func(t *testing.T) *TestHelper {
|
|
th := Setup(t).InitBasic(t)
|
|
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableUserAccessTokens = true })
|
|
return th
|
|
}
|
|
|
|
systemBotDMMessages := func(t *testing.T, th *TestHelper, userID string) []string {
|
|
systemBot, appErr := th.App.GetSystemBot(th.Context)
|
|
require.Nil(t, appErr)
|
|
channel, appErr := th.App.GetOrCreateDirectChannel(th.Context, userID, systemBot.UserId)
|
|
require.Nil(t, appErr)
|
|
posts, appErr := th.App.GetPosts(th.Context, channel.Id, 0, 50)
|
|
require.Nil(t, appErr)
|
|
var messages []string
|
|
for _, p := range posts.Posts {
|
|
if p.UserId == systemBot.UserId {
|
|
messages = append(messages, p.Message)
|
|
}
|
|
}
|
|
return messages
|
|
}
|
|
|
|
t.Run("disabled when EnableUserAccessTokens is off", func(t *testing.T) {
|
|
th := setup(t)
|
|
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableUserAccessTokens = false })
|
|
|
|
token := &model.UserAccessToken{Token: model.NewId(), UserId: th.BasicUser.Id, Description: "t", ExpiresAt: model.GetMillis() + 5*model.DayInMilliseconds}
|
|
_, err := th.App.Srv().Store().UserAccessToken().Save(token)
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, th.App.NotifyExpiringAccessTokens())
|
|
require.Empty(t, systemBotDMMessages(t, th, th.BasicUser.Id))
|
|
})
|
|
|
|
t.Run("warns once and dedups on re-run", func(t *testing.T) {
|
|
th := setup(t)
|
|
|
|
token := &model.UserAccessToken{Token: model.NewId(), UserId: th.BasicUser.Id, Description: "deploy bot", ExpiresAt: model.GetMillis() + 5*model.DayInMilliseconds}
|
|
_, err := th.App.Srv().Store().UserAccessToken().Save(token)
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, th.App.NotifyExpiringAccessTokens())
|
|
messages := systemBotDMMessages(t, th, th.BasicUser.Id)
|
|
require.Len(t, messages, 1, "owner should get exactly one warning")
|
|
require.Contains(t, messages[0], "deploy bot", "warning should name the token")
|
|
require.Contains(t, messages[0], "7 days or less", "5-day token is in the 7-day bucket")
|
|
require.Contains(t, messages[0], "regenerate", "warning should tell the owner to regenerate the token")
|
|
require.NotContains(t, messages[0], "day(s)", "message must not use the awkward day(s) plural")
|
|
|
|
stored, err := th.App.Srv().Store().UserAccessToken().Get(token.Id)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, stored.LastNotifiedAt, "last-notified time should be recorded")
|
|
|
|
// A second run in the same bucket must not send another DM.
|
|
require.NoError(t, th.App.NotifyExpiringAccessTokens())
|
|
require.Len(t, systemBotDMMessages(t, th, th.BasicUser.Id), 1, "re-run in the same bucket must not duplicate")
|
|
})
|
|
|
|
t.Run("token already inside window sends only the current bucket, not a catch-up burst", func(t *testing.T) {
|
|
th := setup(t)
|
|
|
|
// Token surfaces with only 2 days left (e.g. created short-lived, or owner
|
|
// just reactivated). It must get a single warning at the 3-day bucket, never
|
|
// the already-passed 7-day one.
|
|
token := &model.UserAccessToken{Token: model.NewId(), UserId: th.BasicUser.Id, Description: "short lived", ExpiresAt: model.GetMillis() + 2*model.DayInMilliseconds}
|
|
_, err := th.App.Srv().Store().UserAccessToken().Save(token)
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, th.App.NotifyExpiringAccessTokens())
|
|
messages := systemBotDMMessages(t, th, th.BasicUser.Id)
|
|
require.Len(t, messages, 1, "exactly one warning, no 7+3 burst")
|
|
require.Contains(t, messages[0], "3 days or less", "2-day token is in the 3-day bucket")
|
|
|
|
stored, err := th.App.Srv().Store().UserAccessToken().Get(token.Id)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, stored.LastNotifiedAt, "last-notified time should be recorded")
|
|
})
|
|
|
|
t.Run("sub-day token uses the 24-hour wording", func(t *testing.T) {
|
|
th := setup(t)
|
|
|
|
token := &model.UserAccessToken{Token: model.NewId(), UserId: th.BasicUser.Id, Description: "almost gone", ExpiresAt: model.GetMillis() + 12*60*60*1000}
|
|
_, err := th.App.Srv().Store().UserAccessToken().Save(token)
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, th.App.NotifyExpiringAccessTokens())
|
|
messages := systemBotDMMessages(t, th, th.BasicUser.Id)
|
|
require.Len(t, messages, 1)
|
|
require.Contains(t, messages[0], "within 24 hours", "sub-day token uses the dedicated 24-hour message")
|
|
require.NotContains(t, messages[0], "day", "the 24-hour message must not mention days")
|
|
})
|
|
|
|
t.Run("bot-owned token is not notified", func(t *testing.T) {
|
|
th := setup(t)
|
|
|
|
bot, appErr := th.App.CreateBot(th.Context, &model.Bot{Username: "expiring_bot", OwnerId: th.BasicUser.Id})
|
|
require.Nil(t, appErr)
|
|
|
|
token := &model.UserAccessToken{Token: model.NewId(), UserId: bot.UserId, Description: "bot token", ExpiresAt: model.GetMillis() + 5*model.DayInMilliseconds}
|
|
_, err := th.App.Srv().Store().UserAccessToken().Save(token)
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, th.App.NotifyExpiringAccessTokens())
|
|
require.Empty(t, systemBotDMMessages(t, th, bot.UserId), "bot accounts must not be warned")
|
|
|
|
stored, err := th.App.Srv().Store().UserAccessToken().Get(token.Id)
|
|
require.NoError(t, err)
|
|
require.Nil(t, stored.LastNotifiedAt, "bot token marker must stay unset")
|
|
})
|
|
}
|