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
181 lines
5.8 KiB
Go
181 lines
5.8 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package api4
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestGetDirectOrGroupMessageMembersCommonTeams(t *testing.T) {
|
|
mainHelper.Parallel(t)
|
|
|
|
th := Setup(t).InitBasic(t)
|
|
client := th.Client
|
|
|
|
t.Run("requires authentication", func(t *testing.T) {
|
|
user1 := th.BasicUser
|
|
user2 := th.BasicUser2
|
|
|
|
testClient := th.CreateClient()
|
|
_, _, err := testClient.Login(context.Background(), user1.Email, user1.Password)
|
|
require.NoError(t, err)
|
|
|
|
dmChannel, _, err := testClient.CreateDirectChannel(context.Background(), user1.Id, user2.Id)
|
|
require.NoError(t, err)
|
|
|
|
_, err = testClient.Logout(context.Background())
|
|
require.NoError(t, err)
|
|
|
|
_, resp, err := testClient.GetDirectOrGroupMessageMembersCommonTeams(context.Background(), dmChannel.Id)
|
|
require.Error(t, err)
|
|
CheckUnauthorizedStatus(t, resp)
|
|
})
|
|
|
|
t.Run("forbids guest users", func(t *testing.T) {
|
|
th.App.UpdateConfig(func(cfg *model.Config) {
|
|
*cfg.GuestAccountsSettings.Enable = true
|
|
})
|
|
th.App.Srv().SetLicense(model.NewTestLicense())
|
|
|
|
guestUser, guestClient := th.CreateGuestAndClient(t)
|
|
team1 := th.BasicTeam
|
|
th.LinkUserToTeam(t, guestUser, team1)
|
|
|
|
user2 := th.BasicUser2
|
|
th.LinkUserToTeam(t, user2, team1)
|
|
|
|
dmChannel, _, err := th.SystemAdminClient.CreateDirectChannel(context.Background(), guestUser.Id, user2.Id)
|
|
require.NoError(t, err)
|
|
|
|
_, resp, err := guestClient.GetDirectOrGroupMessageMembersCommonTeams(context.Background(), dmChannel.Id)
|
|
require.Error(t, err)
|
|
CheckForbiddenStatus(t, resp)
|
|
})
|
|
|
|
t.Run("requires read permission on channel", func(t *testing.T) {
|
|
user1 := th.CreateUser(t)
|
|
user2 := th.CreateUser(t)
|
|
team1 := th.CreateTeam(t)
|
|
|
|
th.LinkUserToTeam(t, user1, team1)
|
|
th.LinkUserToTeam(t, user2, team1)
|
|
|
|
dmChannel, _, err := th.SystemAdminClient.CreateDirectChannel(context.Background(), user1.Id, user2.Id)
|
|
require.NoError(t, err)
|
|
|
|
otherUser := th.CreateUser(t)
|
|
th.LinkUserToTeam(t, otherUser, team1)
|
|
|
|
otherClient := th.CreateClient()
|
|
_, _, err = otherClient.Login(context.Background(), otherUser.Email, otherUser.Password)
|
|
require.NoError(t, err)
|
|
|
|
_, resp, err := otherClient.GetDirectOrGroupMessageMembersCommonTeams(context.Background(), dmChannel.Id)
|
|
require.Error(t, err)
|
|
CheckForbiddenStatus(t, resp)
|
|
})
|
|
|
|
t.Run("returns bad request for non-DM/GM channel", func(t *testing.T) {
|
|
testClient := th.CreateClient()
|
|
_, _, err := testClient.Login(context.Background(), th.BasicUser.Email, th.BasicUser.Password)
|
|
require.NoError(t, err)
|
|
|
|
_, resp, err := testClient.GetDirectOrGroupMessageMembersCommonTeams(context.Background(), th.BasicChannel.Id)
|
|
require.Error(t, err)
|
|
CheckBadRequestStatus(t, resp)
|
|
})
|
|
|
|
t.Run("returns common teams for DM channel members", func(t *testing.T) {
|
|
user1 := th.BasicUser
|
|
user2 := th.BasicUser2
|
|
team1 := th.BasicTeam
|
|
team2 := th.CreateTeam(t)
|
|
|
|
th.LinkUserToTeam(t, user1, team1)
|
|
th.LinkUserToTeam(t, user1, team2)
|
|
th.LinkUserToTeam(t, user2, team1)
|
|
|
|
dmChannel, _, err := client.CreateDirectChannel(context.Background(), user1.Id, user2.Id)
|
|
require.NoError(t, err)
|
|
|
|
teams, _, err := client.GetDirectOrGroupMessageMembersCommonTeams(context.Background(), dmChannel.Id)
|
|
require.NoError(t, err)
|
|
require.Len(t, teams, 1, "should only return team1 since user2 is not in team2")
|
|
assert.Equal(t, team1.Id, teams[0].Id)
|
|
})
|
|
|
|
t.Run("returns common teams for GM channel members", func(t *testing.T) {
|
|
user1 := th.BasicUser
|
|
user2 := th.BasicUser2
|
|
user3 := th.CreateUser(t)
|
|
team1 := th.BasicTeam
|
|
team2 := th.CreateTeam(t)
|
|
team3 := th.CreateTeam(t)
|
|
|
|
th.LinkUserToTeam(t, user1, team1)
|
|
th.LinkUserToTeam(t, user1, team2)
|
|
th.LinkUserToTeam(t, user2, team1)
|
|
th.LinkUserToTeam(t, user2, team3)
|
|
th.LinkUserToTeam(t, user3, team1)
|
|
|
|
gmChannel, _, err := client.CreateGroupChannel(context.Background(), []string{user1.Id, user2.Id, user3.Id})
|
|
require.NoError(t, err)
|
|
|
|
teams, _, err := client.GetDirectOrGroupMessageMembersCommonTeams(context.Background(), gmChannel.Id)
|
|
require.NoError(t, err)
|
|
require.Len(t, teams, 1, "should only return team1 since it's the only team all three users share")
|
|
assert.Equal(t, team1.Id, teams[0].Id)
|
|
})
|
|
|
|
t.Run("returns empty list when requesting user in channel but has no common teams with other members", func(t *testing.T) {
|
|
user1 := th.CreateUser(t)
|
|
user2 := th.CreateUser(t)
|
|
team1 := th.CreateTeam(t)
|
|
team2 := th.CreateTeam(t)
|
|
|
|
th.LinkUserToTeam(t, user1, team1)
|
|
th.LinkUserToTeam(t, user2, team2)
|
|
|
|
testClient := th.CreateClient()
|
|
_, _, err := testClient.Login(context.Background(), user1.Email, user1.Password)
|
|
require.NoError(t, err)
|
|
|
|
dmChannel, _, err := testClient.CreateDirectChannel(context.Background(), user1.Id, user2.Id)
|
|
require.NoError(t, err)
|
|
|
|
teams, _, err := testClient.GetDirectOrGroupMessageMembersCommonTeams(context.Background(), dmChannel.Id)
|
|
require.NoError(t, err)
|
|
require.Empty(t, teams)
|
|
})
|
|
|
|
t.Run("filters teams to only those common with requesting user", func(t *testing.T) {
|
|
user1 := th.CreateUser(t)
|
|
user2 := th.CreateUser(t)
|
|
user3 := th.BasicUser
|
|
team1 := th.CreateTeam(t)
|
|
team2 := th.CreateTeam(t)
|
|
team3 := th.CreateTeam(t)
|
|
|
|
th.LinkUserToTeam(t, user1, team1)
|
|
th.LinkUserToTeam(t, user1, team2)
|
|
th.LinkUserToTeam(t, user2, team1)
|
|
th.LinkUserToTeam(t, user2, team3)
|
|
th.LinkUserToTeam(t, user3, team1)
|
|
th.LinkUserToTeam(t, user3, team3)
|
|
|
|
gmChannel, _, err := client.CreateGroupChannel(context.Background(), []string{user1.Id, user2.Id, user3.Id})
|
|
require.NoError(t, err)
|
|
|
|
teams, _, err := client.GetDirectOrGroupMessageMembersCommonTeams(context.Background(), gmChannel.Id)
|
|
require.NoError(t, err)
|
|
require.Len(t, teams, 1)
|
|
assert.Equal(t, team1.Id, teams[0].Id)
|
|
})
|
|
}
|