snapshot of https://github.com/mattermost/mattermost.git (master, 2026-09-16; blobs > 131072B omitted)
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

This commit is contained in:
upstream-mirror
2026-09-16 00:29:55 +05:00
commit 240db4e2a7
15309 changed files with 2173406 additions and 0 deletions

View File

@@ -0,0 +1,233 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package api4
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/shared/mlog"
)
// deliveryEventCapture routes the test server's audit logger to a temp file bound to the
// audit-delivery level so the REST handlers' delivery records can be asserted end to end.
type deliveryEventCapture struct {
t *testing.T
th *TestHelper
path string
}
func startDeliveryEventCapture(t *testing.T, th *TestHelper) *deliveryEventCapture {
t.Helper()
path := filepath.Join(t.TempDir(), "delivery-audit.log")
cfg := mlog.LoggerConfiguration{
"_testDelivery": mlog.TargetCfg{
Type: "file",
Format: "json",
Levels: []mlog.Level{mlog.LvlAuditDelivery},
Options: json.RawMessage(fmt.Sprintf(`{"filename":%q}`, path)),
},
}
require.NoError(t, th.App.Srv().Audit.Configure(cfg))
return &deliveryEventCapture{t: t, th: th, path: path}
}
// postIDs returns the post IDs recorded so far for the given mechanism.
func (c *deliveryEventCapture) postIDs(mechanism string) []string {
c.t.Helper()
require.NoError(c.t, c.th.App.Srv().Audit.Flush())
data, err := os.ReadFile(c.path)
if errors.Is(err, os.ErrNotExist) {
return nil
}
require.NoError(c.t, err)
var out []string
for _, line := range splitJSONLines(data) {
var rec map[string]any
require.NoError(c.t, json.Unmarshal(line, &rec))
if rec[model.AuditKeyEventName] != model.AuditEventPostDelivered {
continue
}
meta, ok := rec[model.AuditKeyMeta].(map[string]any)
require.True(c.t, ok)
if meta[model.PostDeliveryKeyMechanism] != mechanism {
continue
}
postID, _ := meta[model.PostDeliveryKeyPostID].(string)
out = append(out, postID)
}
return out
}
func splitJSONLines(data []byte) [][]byte {
var out [][]byte
start := 0
for i := range data {
if data[i] != '\n' {
continue
}
if i > start {
out = append(out, data[start:i])
}
start = i + 1
}
if start < len(data) {
out = append(out, data[start:])
}
return out
}
// enableDeliveryTrackingToggle turns the admin toggle on for all eligible channels. The
// feature flag cannot be set this way — FeatureFlags writes through UpdateConfig are discarded
// by the config store — so callers that need the flag must use SetupConfig.
func enableDeliveryTrackingToggle(th *TestHelper) {
th.App.UpdateConfig(func(cfg *model.Config) {
cfg.DeliveryTrackingSettings.Enable = model.NewPointer(true)
cfg.DeliveryTrackingSettings.EnableForAllChannels = model.NewPointer(true)
})
}
// setupDeliveryEvents returns a helper with the admin toggle on but the feature flag off, for
// asserting that the flag alone gates emission.
func setupDeliveryEvents(t *testing.T) *TestHelper {
t.Helper()
th := Setup(t).InitBasic(t)
enableDeliveryTrackingToggle(th)
return th
}
func TestPostDeliveryEventsForRESTReads(t *testing.T) {
th := SetupConfig(t, func(cfg *model.Config) {
cfg.FeatureFlags.PostDeliveryTracking = true
}).InitBasic(t)
enableDeliveryTrackingToggle(th)
// BasicUser2 reads posts authored by BasicUser, so the self-delivery skip never applies.
post, _, err := th.Client.CreatePost(context.Background(), &model.Post{
ChannelId: th.BasicChannel.Id,
Message: "tracked message",
})
require.NoError(t, err)
th.LoginBasic2(t)
client := th.Client
t.Run("getPostsForChannel", func(t *testing.T) {
capture := startDeliveryEventCapture(t, th)
_, _, err := client.GetPostsForChannel(context.Background(), th.BasicChannel.Id, 0, 60, "", false, false)
require.NoError(t, err)
require.Contains(t, capture.postIDs(model.DeliveryMechanismProduct), post.Id)
})
t.Run("getPost", func(t *testing.T) {
capture := startDeliveryEventCapture(t, th)
_, _, err := client.GetPost(context.Background(), post.Id, "")
require.NoError(t, err)
require.Equal(t, []string{post.Id}, capture.postIDs(model.DeliveryMechanismProduct))
})
t.Run("getPost with a matching etag records nothing", func(t *testing.T) {
fetched, resp, err := client.GetPost(context.Background(), post.Id, "")
require.NoError(t, err)
require.NotNil(t, fetched)
etag := resp.Etag
require.NotEmpty(t, etag)
capture := startDeliveryEventCapture(t, th)
_, _, err = client.GetPost(context.Background(), post.Id, etag)
require.NoError(t, err)
require.Empty(t, capture.postIDs(model.DeliveryMechanismProduct))
})
t.Run("getPostThread", func(t *testing.T) {
capture := startDeliveryEventCapture(t, th)
_, _, err := client.GetPostThread(context.Background(), post.Id, "", false)
require.NoError(t, err)
require.Contains(t, capture.postIDs(model.DeliveryMechanismProduct), post.Id)
})
t.Run("getPostsByIds", func(t *testing.T) {
capture := startDeliveryEventCapture(t, th)
_, _, err := client.GetPostsByIds(context.Background(), []string{post.Id})
require.NoError(t, err)
require.Equal(t, []string{post.Id}, capture.postIDs(model.DeliveryMechanismProduct))
})
t.Run("getPinnedPosts", func(t *testing.T) {
th.LoginBasic(t)
_, err := th.Client.PinPost(context.Background(), post.Id)
require.NoError(t, err)
th.LoginBasic2(t)
capture := startDeliveryEventCapture(t, th)
_, _, err = client.GetPinnedPosts(context.Background(), th.BasicChannel.Id, "")
require.NoError(t, err)
require.Contains(t, capture.postIDs(model.DeliveryMechanismProduct), post.Id)
})
t.Run("records nothing for a DM", func(t *testing.T) {
dm, _, err := client.CreateDirectChannel(context.Background(), th.BasicUser.Id, th.BasicUser2.Id)
require.NoError(t, err)
th.LoginBasic(t)
dmPost, _, err := th.Client.CreatePost(context.Background(), &model.Post{
ChannelId: dm.Id,
Message: "dm message",
})
require.NoError(t, err)
th.LoginBasic2(t)
capture := startDeliveryEventCapture(t, th)
_, _, err = client.GetPost(context.Background(), dmPost.Id, "")
require.NoError(t, err)
require.Empty(t, capture.postIDs(model.DeliveryMechanismProduct))
})
}
func TestPostDeliveryEventsDisabled(t *testing.T) {
th := setupDeliveryEvents(t)
post, _, err := th.Client.CreatePost(context.Background(), &model.Post{
ChannelId: th.BasicChannel.Id,
Message: "tracked message",
})
require.NoError(t, err)
th.LoginBasic2(t)
// The feature flag is off in this helper, so nothing should be recorded even with the
// admin toggle on.
require.False(t, th.App.Config().FeatureFlags.PostDeliveryTracking)
capture := startDeliveryEventCapture(t, th)
_, _, err = th.Client.GetPost(context.Background(), post.Id, "")
require.NoError(t, err)
require.Empty(t, capture.postIDs(model.DeliveryMechanismProduct))
}