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
246 lines
10 KiB
Go
246 lines
10 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/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
"github.com/mattermost/mattermost/server/public/shared/i18n"
|
|
"github.com/mattermost/mattermost/server/v8/channels/app/platform"
|
|
)
|
|
|
|
// TestWebConnShouldSendEvent is not exhaustive because some of the checks
|
|
// happen inside web_hub.go before the event is actually broadcasted, and checked
|
|
// via ShouldSendEvent.
|
|
func TestWebConnShouldSendEvent(t *testing.T) {
|
|
mainHelper.Parallel(t)
|
|
|
|
th := Setup(t).InitBasic(t)
|
|
|
|
session, err := th.App.CreateSession(th.Context, &model.Session{UserId: th.BasicUser.Id, Roles: th.BasicUser.GetRawRoles(), TeamMembers: []*model.TeamMember{
|
|
{
|
|
UserId: th.BasicUser.Id,
|
|
TeamId: th.BasicTeam.Id,
|
|
Roles: model.TeamUserRoleId,
|
|
},
|
|
}})
|
|
require.Nil(t, err)
|
|
|
|
basicUserWc := &platform.WebConn{
|
|
Platform: th.Server.Platform(),
|
|
Suite: th.App,
|
|
UserId: th.BasicUser.Id,
|
|
T: i18n.T,
|
|
}
|
|
|
|
user1ConnID := model.NewId()
|
|
basicUserWc.SetConnectionID(user1ConnID)
|
|
basicUserWc.SetSession(session)
|
|
basicUserWc.SetSessionToken(session.Token)
|
|
basicUserWc.SetSessionExpiresAt(session.ExpiresAt)
|
|
|
|
role, appErr := th.App.GetRoleByName(RequestContextWithMaster(th.Context), model.SystemManagerRoleId)
|
|
require.Nil(t, appErr)
|
|
role.Permissions = append(role.Permissions, model.PermissionReadDataRetentionJob.Id)
|
|
_, appErr = th.App.UpdateRole(role)
|
|
require.Nil(t, appErr)
|
|
|
|
session2, err := th.App.CreateSession(th.Context, &model.Session{UserId: th.BasicUser2.Id, Roles: model.SystemUserRoleId + " " + model.SystemManagerRoleId, TeamMembers: []*model.TeamMember{
|
|
{
|
|
UserId: th.BasicUser2.Id,
|
|
TeamId: th.BasicTeam.Id,
|
|
Roles: model.TeamAdminRoleId,
|
|
},
|
|
}})
|
|
require.Nil(t, err)
|
|
|
|
basicUser2Wc := &platform.WebConn{
|
|
Platform: th.Server.Platform(),
|
|
Suite: th.App,
|
|
UserId: th.BasicUser2.Id,
|
|
T: i18n.T,
|
|
}
|
|
|
|
user2ConnID := model.NewId()
|
|
basicUser2Wc.SetConnectionID(user2ConnID)
|
|
basicUser2Wc.SetSession(session2)
|
|
basicUser2Wc.SetSessionToken(session2.Token)
|
|
basicUser2Wc.SetSessionExpiresAt(session2.ExpiresAt)
|
|
|
|
session3, err := th.App.CreateSession(th.Context, &model.Session{UserId: th.SystemAdminUser.Id, Roles: th.SystemAdminUser.GetRawRoles()})
|
|
require.Nil(t, err)
|
|
|
|
adminUserWc := &platform.WebConn{
|
|
Platform: th.Server.Platform(),
|
|
Suite: th.App,
|
|
UserId: th.SystemAdminUser.Id,
|
|
T: i18n.T,
|
|
}
|
|
|
|
adminConnID := model.NewId()
|
|
adminUserWc.SetConnectionID(adminConnID)
|
|
adminUserWc.SetSession(session3)
|
|
adminUserWc.SetSessionToken(session3.Token)
|
|
adminUserWc.SetSessionExpiresAt(session3.ExpiresAt)
|
|
|
|
session4, err := th.App.CreateSession(th.Context, &model.Session{UserId: th.BasicUser.Id, Roles: th.BasicUser.GetRawRoles(), TeamMembers: []*model.TeamMember{
|
|
{
|
|
UserId: th.BasicUser.Id,
|
|
TeamId: th.BasicTeam.Id,
|
|
Roles: model.TeamUserRoleId,
|
|
},
|
|
}})
|
|
require.Nil(t, err)
|
|
|
|
basicUserWc2 := &platform.WebConn{
|
|
Platform: th.Server.Platform(),
|
|
Suite: th.App,
|
|
UserId: th.BasicUser.Id,
|
|
T: i18n.T,
|
|
}
|
|
|
|
user1Conn2ID := model.NewId()
|
|
basicUserWc2.SetConnectionID(user1Conn2ID)
|
|
basicUserWc2.SetSession(session4)
|
|
basicUserWc2.SetSessionToken(session4.Token)
|
|
basicUserWc2.SetSessionExpiresAt(session4.ExpiresAt)
|
|
|
|
// By default, only BasicUser and BasicUser2 get added to the BasicTeam.
|
|
th.LinkUserToTeam(t, th.SystemAdminUser, th.BasicTeam)
|
|
|
|
// Create another channel with just BasicUser (implicitly) and SystemAdminUser to test channel broadcast
|
|
channel2 := th.CreateChannel(t, th.BasicTeam)
|
|
th.AddUserToChannel(t, th.SystemAdminUser, channel2)
|
|
|
|
cases := []struct {
|
|
Description string
|
|
Broadcast *model.WebsocketBroadcast
|
|
User1Expected bool
|
|
User2Expected bool
|
|
AdminExpected bool
|
|
User1Conn2Expected bool
|
|
}{
|
|
{"should send to all", &model.WebsocketBroadcast{}, true, true, true, true},
|
|
{"should only send to basic user", &model.WebsocketBroadcast{UserId: th.BasicUser.Id}, true, false, false, true},
|
|
{"should only send to basic user conn 1", &model.WebsocketBroadcast{ConnectionId: user1ConnID}, true, false, false, false},
|
|
{"should only send to basic user conn 2", &model.WebsocketBroadcast{ConnectionId: user1Conn2ID}, false, false, false, true},
|
|
{"should omit basic user 2", &model.WebsocketBroadcast{OmitUsers: map[string]bool{th.BasicUser2.Id: true}}, true, false, true, true},
|
|
{"should only send to admin", &model.WebsocketBroadcast{ContainsSensitiveData: true}, false, false, true, false},
|
|
{"should only send to users with required permission", &model.WebsocketBroadcast{RequiredPermissions: []string{model.PermissionReadDataRetentionJob.Id}}, false, true, true, false},
|
|
{"should require all permissions", &model.WebsocketBroadcast{RequiredPermissions: []string{model.PermissionReadDataRetentionJob.Id, model.PermissionReadComplianceExportJob.Id}}, false, false, true, false},
|
|
{"should treat manage_system as a required permission", &model.WebsocketBroadcast{RequiredPermissions: []string{model.PermissionManageSystem.Id}}, false, false, true, false},
|
|
{"required permissions take precedence over sensitive data fallback", &model.WebsocketBroadcast{ContainsSensitiveData: true, RequiredPermissions: []string{model.PermissionReadDataRetentionJob.Id}}, false, true, true, false},
|
|
{"should only send to non-admins", &model.WebsocketBroadcast{ContainsSanitizedData: true}, true, true, false, true},
|
|
{"should send to nobody", &model.WebsocketBroadcast{ContainsSensitiveData: true, ContainsSanitizedData: true}, false, false, false, false},
|
|
{"should omit basic user 2 by connection id", &model.WebsocketBroadcast{OmitConnectionId: user2ConnID}, true, false, true, true},
|
|
{"should omit basic user 2 by connection id while user is set", &model.WebsocketBroadcast{UserId: th.BasicUser2.Id, OmitConnectionId: user2ConnID}, false, false, false, false},
|
|
// needs more cases to get full coverage
|
|
}
|
|
|
|
event := model.NewWebSocketEvent("some_event", "", "", "", nil, "")
|
|
for _, c := range cases {
|
|
t.Run(c.Description, func(t *testing.T) {
|
|
event = event.SetBroadcast(c.Broadcast)
|
|
if c.User1Expected {
|
|
assert.True(t, basicUserWc.ShouldSendEvent(event), "expected user 1")
|
|
} else {
|
|
assert.False(t, basicUserWc.ShouldSendEvent(event), "did not expect user 1")
|
|
}
|
|
if c.User2Expected {
|
|
assert.True(t, basicUser2Wc.ShouldSendEvent(event), "expected user 2")
|
|
} else {
|
|
assert.False(t, basicUser2Wc.ShouldSendEvent(event), "did not expect user 2")
|
|
}
|
|
if c.AdminExpected {
|
|
assert.True(t, adminUserWc.ShouldSendEvent(event), "expected admin")
|
|
} else {
|
|
assert.False(t, adminUserWc.ShouldSendEvent(event), "did not expect admin")
|
|
}
|
|
if c.User1Conn2Expected {
|
|
assert.True(t, basicUserWc2.ShouldSendEvent(event), "expected user 1 conn 2")
|
|
} else {
|
|
assert.False(t, basicUserWc2.ShouldSendEvent(event), "did not expect user 1 conn 2")
|
|
}
|
|
})
|
|
}
|
|
|
|
t.Run("should not send typing event unless in scope", func(t *testing.T) {
|
|
event2 := model.NewWebSocketEvent(model.WebsocketEventTyping, "", th.BasicChannel.Id, "", nil, "")
|
|
// Basic, unset case
|
|
basicUserWc.SetActiveChannelID(platform.UnsetPresenceIndicator)
|
|
basicUserWc.SetActiveRHSThreadChannelID(platform.UnsetPresenceIndicator)
|
|
basicUserWc.SetActiveThreadViewThreadChannelID(platform.UnsetPresenceIndicator)
|
|
assert.True(t, basicUserWc.ShouldSendEvent(event2))
|
|
|
|
// Active channel is set to something else, thread unset
|
|
basicUserWc.SetActiveChannelID("ch1")
|
|
basicUserWc.SetActiveRHSThreadChannelID(platform.UnsetPresenceIndicator)
|
|
basicUserWc.SetActiveThreadViewThreadChannelID(platform.UnsetPresenceIndicator)
|
|
assert.True(t, basicUserWc.ShouldSendEvent(event2))
|
|
|
|
// Active channel is unset, thread set
|
|
basicUserWc.SetActiveChannelID(platform.UnsetPresenceIndicator)
|
|
basicUserWc.SetActiveRHSThreadChannelID("ch1")
|
|
basicUserWc.SetActiveThreadViewThreadChannelID("ch2")
|
|
assert.True(t, basicUserWc.ShouldSendEvent(event2))
|
|
|
|
// both are set to correct channel
|
|
basicUserWc.SetActiveChannelID(th.BasicChannel.Id)
|
|
basicUserWc.SetActiveRHSThreadChannelID(th.BasicChannel.Id)
|
|
basicUserWc.SetActiveThreadViewThreadChannelID(th.BasicChannel.Id)
|
|
assert.True(t, basicUserWc.ShouldSendEvent(event2))
|
|
|
|
// channel is correct, thread is something else.
|
|
basicUserWc.SetActiveChannelID(th.BasicChannel.Id)
|
|
basicUserWc.SetActiveRHSThreadChannelID("ch1")
|
|
basicUserWc.SetActiveThreadViewThreadChannelID("ch2")
|
|
assert.True(t, basicUserWc.ShouldSendEvent(event2))
|
|
|
|
// channel is wrong, thread is correct.
|
|
basicUserWc.SetActiveChannelID("ch1")
|
|
basicUserWc.SetActiveRHSThreadChannelID(th.BasicChannel.Id)
|
|
basicUserWc.SetActiveThreadViewThreadChannelID(th.BasicChannel.Id)
|
|
assert.True(t, basicUserWc.ShouldSendEvent(event2))
|
|
|
|
// FINALLY, both are set to something else.
|
|
basicUserWc.SetActiveChannelID("ch1")
|
|
basicUserWc.SetActiveRHSThreadChannelID("ch1")
|
|
basicUserWc.SetActiveThreadViewThreadChannelID("ch2")
|
|
assert.False(t, basicUserWc.ShouldSendEvent(event2))
|
|
|
|
// Different threads and channel
|
|
basicUserWc.SetActiveChannelID("ch1")
|
|
basicUserWc.SetActiveRHSThreadChannelID("ch2")
|
|
basicUserWc.SetActiveThreadViewThreadChannelID("ch3")
|
|
assert.False(t, basicUserWc.ShouldSendEvent(event2))
|
|
|
|
// Other channel. Thread unset explicitly.
|
|
basicUserWc.SetActiveChannelID("ch1")
|
|
basicUserWc.SetActiveRHSThreadChannelID("")
|
|
basicUserWc.SetActiveThreadViewThreadChannelID("")
|
|
assert.False(t, basicUserWc.ShouldSendEvent(event2))
|
|
})
|
|
|
|
t.Run("channel member cache invalidated after user added to channel", func(t *testing.T) {
|
|
th.AddUserToChannel(t, th.BasicUser2, channel2)
|
|
basicUser2Wc.InvalidateCache()
|
|
|
|
event = event.SetBroadcast(&model.WebsocketBroadcast{ChannelId: channel2.Id})
|
|
assert.True(t, basicUserWc.ShouldSendEvent(event), "expected user 1")
|
|
assert.True(t, basicUser2Wc.ShouldSendEvent(event), "expected user 2")
|
|
assert.True(t, adminUserWc.ShouldSendEvent(event), "expected admin")
|
|
})
|
|
|
|
event2 := model.NewWebSocketEvent(model.WebsocketEventUpdateTeam, th.BasicTeam.Id, "", "", nil, "")
|
|
assert.True(t, basicUserWc.ShouldSendEvent(event2))
|
|
assert.True(t, basicUser2Wc.ShouldSendEvent(event2))
|
|
|
|
event3 := model.NewWebSocketEvent(model.WebsocketEventUpdateTeam, "wrongId", "", "", nil, "")
|
|
assert.False(t, basicUserWc.ShouldSendEvent(event3))
|
|
}
|