mattermost/server/public/shared/request/context_test.go
Felipe Martin 0afef7760c
Include connection ID in plugin context (#36074)
* feat: include connection id in the plugin context

* refactor: group ConnectionId next to SessionId in plugin Context

Addresses review feedback to keep related identifier fields adjacent.

* fix(files): forward Connection-Id on file uploads to plugin hooks

The webapp uploadFile XHR didn't attach the Connection-Id header, so
FileWillBeUploaded plugin hooks always received an empty ConnectionId.
Read it from the websocket selector and set it on the request, matching
how drafts and channel bookmarks already do it. Adds a server-side test
asserting the connection id propagates through pluginContext.

* fix(lint): reorder file_actions imports to satisfy import/order

* Document ConnectionId on request.Context
2026-05-11 10:09:47 +02:00

72 lines
2.3 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package request
import (
"testing"
"github.com/mattermost/mattermost/server/public/model"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestContext_WithConnectionId(t *testing.T) {
t.Run("returns new context with connection id", func(t *testing.T) {
originalCtx := TestContext(t)
connectionId := "test-connection-id-123"
newCtx := originalCtx.WithConnectionId(connectionId)
require.NotNil(t, newCtx)
assert.NotSame(t, originalCtx, newCtx, "should return a new context instance")
assert.Equal(t, connectionId, newCtx.ConnectionId())
assert.Empty(t, originalCtx.ConnectionId(), "original context should remain unchanged")
})
t.Run("returns new context with empty connection id", func(t *testing.T) {
originalCtx := TestContext(t)
originalCtx = originalCtx.WithConnectionId("existing-id").(*Context)
newCtx := originalCtx.WithConnectionId("")
require.NotNil(t, newCtx)
assert.NotSame(t, originalCtx, newCtx, "should return a new context instance")
assert.Empty(t, newCtx.ConnectionId())
assert.Equal(t, "existing-id", originalCtx.ConnectionId(), "original context should remain unchanged")
})
}
func TestContext_WithSession(t *testing.T) {
t.Run("returns new context with empty session when session is nil", func(t *testing.T) {
originalCtx := TestContext(t)
newCtx := originalCtx.WithSession(nil)
require.NotNil(t, newCtx)
assert.NotSame(t, originalCtx, newCtx, "should return a new context instance")
assert.Empty(t, newCtx.Session().Id)
assert.Empty(t, newCtx.Session().UserId)
assert.Empty(t, newCtx.Session().Token)
})
t.Run("returns new context when session is provided", func(t *testing.T) {
originalCtx := TestContext(t)
session := &model.Session{
Id: "session-id",
UserId: "user-id",
Token: "token",
}
newCtx := originalCtx.WithSession(session)
require.NotNil(t, newCtx)
assert.NotSame(t, originalCtx, newCtx, "should return a new context instance")
assert.Equal(t, "session-id", newCtx.Session().Id)
assert.Equal(t, "user-id", newCtx.Session().UserId)
assert.Equal(t, "token", newCtx.Session().Token)
assert.Empty(t, originalCtx.Session().Id)
})
}