Files
upstream-mattermost/server/channels/app/channel_category_test.go
upstream-mirror 240db4e2a7
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
snapshot of https://github.com/mattermost/mattermost.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 00:29:55 +05:00

793 lines
26 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"
)
func TestSidebarCategory(t *testing.T) {
mainHelper.Parallel(t)
th := Setup(t).InitBasic(t)
basicChannel2 := th.CreateChannel(t, th.BasicTeam)
defer func() {
err := th.App.PermanentDeleteChannel(th.Context, basicChannel2)
require.Nil(t, err)
}()
user := th.CreateUser(t)
defer func() {
err := th.App.Srv().Store().User().PermanentDelete(th.Context, user.Id)
require.NoError(t, err)
}()
th.LinkUserToTeam(t, user, th.BasicTeam)
th.AddUserToChannel(t, user, basicChannel2)
var createdCategory *model.SidebarCategoryWithChannels
t.Run("CreateSidebarCategory", func(t *testing.T) {
catData := model.SidebarCategoryWithChannels{
SidebarCategory: model.SidebarCategory{
DisplayName: "TEST",
},
Channels: []string{th.BasicChannel.Id, basicChannel2.Id, basicChannel2.Id},
}
_, err := th.App.CreateSidebarCategory(th.Context, user.Id, th.BasicTeam.Id, &catData)
require.NotNil(t, err, "Should return error due to duplicate IDs")
catData.Channels = []string{th.BasicChannel.Id, basicChannel2.Id}
cat, err := th.App.CreateSidebarCategory(th.Context, user.Id, th.BasicTeam.Id, &catData)
require.Nil(t, err, "Expected no error")
require.NotNil(t, cat, "Expected category object, got nil")
createdCategory = cat
})
t.Run("UpdateSidebarCategories", func(t *testing.T) {
require.NotNil(t, createdCategory)
createdCategory.Channels = []string{th.BasicChannel.Id}
updatedCat, err := th.App.UpdateSidebarCategories(th.Context, user.Id, th.BasicTeam.Id, []*model.SidebarCategoryWithChannels{createdCategory})
require.Nil(t, err, "Expected no error")
require.NotNil(t, updatedCat, "Expected category object, got nil")
require.Len(t, updatedCat, 1)
require.Len(t, updatedCat[0].Channels, 1)
require.Equal(t, updatedCat[0].Channels[0], th.BasicChannel.Id)
})
t.Run("UpdateSidebarCategoryOrder", func(t *testing.T) {
err := th.App.UpdateSidebarCategoryOrder(th.Context, user.Id, th.BasicTeam.Id, []string{th.BasicChannel.Id, basicChannel2.Id})
require.NotNil(t, err, "Should return error due to invalid order")
actualOrder, err := th.App.GetSidebarCategoryOrder(th.Context, user.Id, th.BasicTeam.Id)
require.Nil(t, err, "Should fetch order successfully")
actualOrder[2], actualOrder[3] = actualOrder[3], actualOrder[2]
err = th.App.UpdateSidebarCategoryOrder(th.Context, user.Id, th.BasicTeam.Id, actualOrder)
require.Nil(t, err, "Should update order successfully")
// We create a copy of actualOrder to prevent racy read
// of the slice when the broadcast message is sent from webhub.
newOrder := make([]string, len(actualOrder))
copy(newOrder, actualOrder)
newOrder[2] = "asd"
err = th.App.UpdateSidebarCategoryOrder(th.Context, user.Id, th.BasicTeam.Id, newOrder)
require.NotNil(t, err, "Should return error due to invalid id")
})
t.Run("GetSidebarCategoryOrder", func(t *testing.T) {
catOrder, err := th.App.GetSidebarCategoryOrder(th.Context, user.Id, th.BasicTeam.Id)
require.Nil(t, err, "Expected no error")
require.Len(t, catOrder, 4)
require.Equal(t, catOrder[1], createdCategory.Id, "the newly created category should be after favorites")
})
}
func TestGetSidebarCategories(t *testing.T) {
mainHelper.Parallel(t)
t.Run("should return the sidebar categories for the given user/team", func(t *testing.T) {
th := Setup(t).InitBasic(t)
_, err := th.App.CreateSidebarCategory(th.Context, th.BasicUser.Id, th.BasicTeam.Id, &model.SidebarCategoryWithChannels{
SidebarCategory: model.SidebarCategory{
UserId: th.BasicUser.Id,
TeamId: th.BasicTeam.Id,
DisplayName: "new category",
},
})
require.Nil(t, err)
categories, err := th.App.GetSidebarCategoriesForTeamForUser(th.Context, th.BasicUser.Id, th.BasicTeam.Id)
assert.Nil(t, err)
assert.Len(t, categories.Categories, 4)
})
t.Run("should create the initial categories even if migration hasn't ran yet", func(t *testing.T) {
th := Setup(t).InitBasic(t)
// Manually add the user to the team without going through the app layer to simulate a pre-existing user/team
// relationship that hasn't been migrated yet
team := th.CreateTeam(t)
_, err := th.App.Srv().Store().Team().SaveMember(th.Context, &model.TeamMember{
TeamId: team.Id,
UserId: th.BasicUser.Id,
SchemeUser: true,
}, 100)
require.NoError(t, err)
categories, appErr := th.App.GetSidebarCategoriesForTeamForUser(th.Context, th.BasicUser.Id, team.Id)
assert.Nil(t, appErr)
assert.Len(t, categories.Categories, 3)
})
t.Run("should return a store error if a db table is missing", func(t *testing.T) {
th := Setup(t).InitBasic(t)
// Temporarily renaming a table to force a DB error.
_, err := th.SQLStore.GetMaster().Exec("ALTER TABLE SidebarCategories RENAME TO SidebarCategoriesTest")
require.NoError(t, err)
defer func() {
_, err := th.SQLStore.GetMaster().Exec("ALTER TABLE SidebarCategoriesTest RENAME TO SidebarCategories")
require.NoError(t, err)
}()
categories, appErr := th.App.GetSidebarCategoriesForTeamForUser(th.Context, th.BasicUser.Id, th.BasicTeam.Id)
assert.Nil(t, categories)
assert.NotNil(t, appErr)
assert.Equal(t, "app.channel.sidebar_categories.app_error", appErr.Id)
})
}
func TestUpdateSidebarCategories(t *testing.T) {
mainHelper.Parallel(t)
t.Run("should mute and unmute all channels in a category when it is muted or unmuted", func(t *testing.T) {
th := Setup(t).InitBasic(t)
categories, err := th.App.GetSidebarCategoriesForTeamForUser(th.Context, th.BasicUser.Id, th.BasicTeam.Id)
require.Nil(t, err)
channelsCategory := categories.Categories[1]
// Create some channels to be part of the channels category
channel1 := th.CreateChannel(t, th.BasicTeam)
th.AddUserToChannel(t, th.BasicUser, channel1)
channel2 := th.CreateChannel(t, th.BasicTeam)
th.AddUserToChannel(t, th.BasicUser, channel2)
// Mute the category
updated, err := th.App.UpdateSidebarCategories(th.Context, th.BasicUser.Id, th.BasicTeam.Id, []*model.SidebarCategoryWithChannels{
{
SidebarCategory: model.SidebarCategory{
Id: channelsCategory.Id,
Muted: true,
},
Channels: []string{channel1.Id, channel2.Id},
},
})
require.Nil(t, err)
assert.True(t, updated[0].Muted)
// Confirm that the channels are now muted
member1, err := th.App.GetChannelMember(th.Context, channel1.Id, th.BasicUser.Id)
require.Nil(t, err)
assert.True(t, member1.IsChannelMuted())
member2, err := th.App.GetChannelMember(th.Context, channel2.Id, th.BasicUser.Id)
require.Nil(t, err)
assert.True(t, member2.IsChannelMuted())
// Unmute the category
updated, err = th.App.UpdateSidebarCategories(th.Context, th.BasicUser.Id, th.BasicTeam.Id, []*model.SidebarCategoryWithChannels{
{
SidebarCategory: model.SidebarCategory{
Id: channelsCategory.Id,
Muted: false,
},
Channels: []string{channel1.Id, channel2.Id},
},
})
require.Nil(t, err)
assert.False(t, updated[0].Muted)
// Confirm that the channels are now unmuted
member1, err = th.App.GetChannelMember(th.Context, channel1.Id, th.BasicUser.Id)
require.Nil(t, err)
assert.False(t, member1.IsChannelMuted())
member2, err = th.App.GetChannelMember(th.Context, channel2.Id, th.BasicUser.Id)
require.Nil(t, err)
assert.False(t, member2.IsChannelMuted())
})
t.Run("should mute and unmute channels moved from an unmuted category to a muted one and back", func(t *testing.T) {
th := Setup(t).InitBasic(t)
// Create some channels
channel1 := th.CreateChannel(t, th.BasicTeam)
th.AddUserToChannel(t, th.BasicUser, channel1)
channel2 := th.CreateChannel(t, th.BasicTeam)
th.AddUserToChannel(t, th.BasicUser, channel2)
// And some categories
mutedCategory, err := th.App.CreateSidebarCategory(th.Context, th.BasicUser.Id, th.BasicTeam.Id, &model.SidebarCategoryWithChannels{
SidebarCategory: model.SidebarCategory{
DisplayName: "muted",
Muted: true,
},
})
require.Nil(t, err)
require.True(t, mutedCategory.Muted)
unmutedCategory, err := th.App.CreateSidebarCategory(th.Context, th.BasicUser.Id, th.BasicTeam.Id, &model.SidebarCategoryWithChannels{
SidebarCategory: model.SidebarCategory{
DisplayName: "unmuted",
Muted: false,
},
Channels: []string{channel1.Id, channel2.Id},
})
require.Nil(t, err)
require.False(t, unmutedCategory.Muted)
// Move the channels
_, err = th.App.UpdateSidebarCategories(th.Context, th.BasicUser.Id, th.BasicTeam.Id, []*model.SidebarCategoryWithChannels{
{
SidebarCategory: model.SidebarCategory{
Id: mutedCategory.Id,
DisplayName: mutedCategory.DisplayName,
Muted: mutedCategory.Muted,
},
Channels: []string{channel1.Id, channel2.Id},
},
{
SidebarCategory: model.SidebarCategory{
Id: unmutedCategory.Id,
DisplayName: unmutedCategory.DisplayName,
Muted: unmutedCategory.Muted,
},
Channels: []string{},
},
})
require.Nil(t, err)
// Confirm that the channels are now muted
member1, err := th.App.GetChannelMember(th.Context, channel1.Id, th.BasicUser.Id)
require.Nil(t, err)
assert.True(t, member1.IsChannelMuted())
member2, err := th.App.GetChannelMember(th.Context, channel2.Id, th.BasicUser.Id)
require.Nil(t, err)
assert.True(t, member2.IsChannelMuted())
// Move the channels back
_, err = th.App.UpdateSidebarCategories(th.Context, th.BasicUser.Id, th.BasicTeam.Id, []*model.SidebarCategoryWithChannels{
{
SidebarCategory: model.SidebarCategory{
Id: mutedCategory.Id,
DisplayName: mutedCategory.DisplayName,
Muted: mutedCategory.Muted,
},
Channels: []string{},
},
{
SidebarCategory: model.SidebarCategory{
Id: unmutedCategory.Id,
DisplayName: unmutedCategory.DisplayName,
Muted: unmutedCategory.Muted,
},
Channels: []string{channel1.Id, channel2.Id},
},
})
require.Nil(t, err)
// Confirm that the channels are now unmuted
member1, err = th.App.GetChannelMember(th.Context, channel1.Id, th.BasicUser.Id)
require.Nil(t, err)
assert.False(t, member1.IsChannelMuted())
member2, err = th.App.GetChannelMember(th.Context, channel2.Id, th.BasicUser.Id)
require.Nil(t, err)
assert.False(t, member2.IsChannelMuted())
})
t.Run("should not mute or unmute channels moved between muted categories", func(t *testing.T) {
th := Setup(t).InitBasic(t)
// Create some channels
channel1 := th.CreateChannel(t, th.BasicTeam)
th.AddUserToChannel(t, th.BasicUser, channel1)
channel2 := th.CreateChannel(t, th.BasicTeam)
th.AddUserToChannel(t, th.BasicUser, channel2)
// And some categories
category1, err := th.App.CreateSidebarCategory(th.Context, th.BasicUser.Id, th.BasicTeam.Id, &model.SidebarCategoryWithChannels{
SidebarCategory: model.SidebarCategory{
DisplayName: "category1",
Muted: true,
},
})
require.Nil(t, err)
require.True(t, category1.Muted)
category2, err := th.App.CreateSidebarCategory(th.Context, th.BasicUser.Id, th.BasicTeam.Id, &model.SidebarCategoryWithChannels{
SidebarCategory: model.SidebarCategory{
DisplayName: "category2",
Muted: true,
},
Channels: []string{channel1.Id, channel2.Id},
})
require.Nil(t, err)
require.True(t, category2.Muted)
// Move the unmuted channels
_, err = th.App.UpdateSidebarCategories(th.Context, th.BasicUser.Id, th.BasicTeam.Id, []*model.SidebarCategoryWithChannels{
{
SidebarCategory: model.SidebarCategory{
Id: category1.Id,
DisplayName: category1.DisplayName,
Muted: category1.Muted,
},
Channels: []string{channel1.Id, channel2.Id},
},
{
SidebarCategory: model.SidebarCategory{
Id: category2.Id,
DisplayName: category2.DisplayName,
Muted: category2.Muted,
},
Channels: []string{},
},
})
require.Nil(t, err)
// Confirm that the channels are still unmuted
member1, err := th.App.GetChannelMember(th.Context, channel1.Id, th.BasicUser.Id)
require.Nil(t, err)
assert.False(t, member1.IsChannelMuted())
member2, err := th.App.GetChannelMember(th.Context, channel2.Id, th.BasicUser.Id)
require.Nil(t, err)
assert.False(t, member2.IsChannelMuted())
// Mute the channels manually
_, err = th.App.ToggleMuteChannel(th.Context, channel1.Id, th.BasicUser.Id)
require.Nil(t, err)
_, err = th.App.ToggleMuteChannel(th.Context, channel2.Id, th.BasicUser.Id)
require.Nil(t, err)
// Move the muted channels back
_, err = th.App.UpdateSidebarCategories(th.Context, th.BasicUser.Id, th.BasicTeam.Id, []*model.SidebarCategoryWithChannels{
{
SidebarCategory: model.SidebarCategory{
Id: category1.Id,
DisplayName: category1.DisplayName,
Muted: category1.Muted,
},
Channels: []string{},
},
{
SidebarCategory: model.SidebarCategory{
Id: category2.Id,
DisplayName: category2.DisplayName,
Muted: category2.Muted,
},
Channels: []string{channel1.Id, channel2.Id},
},
})
require.Nil(t, err)
// Confirm that the channels are still muted
member1, err = th.App.GetChannelMember(th.Context, channel1.Id, th.BasicUser.Id)
require.Nil(t, err)
assert.True(t, member1.IsChannelMuted())
member2, err = th.App.GetChannelMember(th.Context, channel2.Id, th.BasicUser.Id)
require.Nil(t, err)
assert.True(t, member2.IsChannelMuted())
})
t.Run("should not mute or unmute channels moved between unmuted categories", func(t *testing.T) {
th := Setup(t).InitBasic(t)
// Create some channels
channel1 := th.CreateChannel(t, th.BasicTeam)
th.AddUserToChannel(t, th.BasicUser, channel1)
channel2 := th.CreateChannel(t, th.BasicTeam)
th.AddUserToChannel(t, th.BasicUser, channel2)
// And some categories
category1, err := th.App.CreateSidebarCategory(th.Context, th.BasicUser.Id, th.BasicTeam.Id, &model.SidebarCategoryWithChannels{
SidebarCategory: model.SidebarCategory{
DisplayName: "category1",
Muted: false,
},
})
require.Nil(t, err)
require.False(t, category1.Muted)
category2, err := th.App.CreateSidebarCategory(th.Context, th.BasicUser.Id, th.BasicTeam.Id, &model.SidebarCategoryWithChannels{
SidebarCategory: model.SidebarCategory{
DisplayName: "category2",
Muted: false,
},
Channels: []string{channel1.Id, channel2.Id},
})
require.Nil(t, err)
require.False(t, category2.Muted)
// Move the unmuted channels
_, err = th.App.UpdateSidebarCategories(th.Context, th.BasicUser.Id, th.BasicTeam.Id, []*model.SidebarCategoryWithChannels{
{
SidebarCategory: model.SidebarCategory{
Id: category1.Id,
DisplayName: category1.DisplayName,
Muted: category1.Muted,
},
Channels: []string{channel1.Id, channel2.Id},
},
{
SidebarCategory: model.SidebarCategory{
Id: category2.Id,
DisplayName: category2.DisplayName,
Muted: category2.Muted,
},
Channels: []string{},
},
})
require.Nil(t, err)
// Confirm that the channels are still unmuted
member1, err := th.App.GetChannelMember(th.Context, channel1.Id, th.BasicUser.Id)
require.Nil(t, err)
assert.False(t, member1.IsChannelMuted())
member2, err := th.App.GetChannelMember(th.Context, channel2.Id, th.BasicUser.Id)
require.Nil(t, err)
assert.False(t, member2.IsChannelMuted())
// Mute the channels manually
_, err = th.App.ToggleMuteChannel(th.Context, channel1.Id, th.BasicUser.Id)
require.Nil(t, err)
_, err = th.App.ToggleMuteChannel(th.Context, channel2.Id, th.BasicUser.Id)
require.Nil(t, err)
// Move the muted channels back
_, err = th.App.UpdateSidebarCategories(th.Context, th.BasicUser.Id, th.BasicTeam.Id, []*model.SidebarCategoryWithChannels{
{
SidebarCategory: model.SidebarCategory{
Id: category1.Id,
DisplayName: category1.DisplayName,
Muted: category1.Muted,
},
Channels: []string{},
},
{
SidebarCategory: model.SidebarCategory{
Id: category2.Id,
DisplayName: category2.DisplayName,
Muted: category2.Muted,
},
Channels: []string{channel1.Id, channel2.Id},
},
})
require.Nil(t, err)
// Confirm that the channels are still muted
member1, err = th.App.GetChannelMember(th.Context, channel1.Id, th.BasicUser.Id)
require.Nil(t, err)
assert.True(t, member1.IsChannelMuted())
member2, err = th.App.GetChannelMember(th.Context, channel2.Id, th.BasicUser.Id)
require.Nil(t, err)
assert.True(t, member2.IsChannelMuted())
})
}
func TestSetChannelManagedCategory(t *testing.T) {
mainHelper.Parallel(t)
th := Setup(t).InitBasic(t)
session := &model.Session{UserId: th.BasicUser.Id, Props: model.StringMap{}}
rctx := th.Context.WithSession(session)
t.Run("should set a managed category on a public channel", func(t *testing.T) {
channel := th.CreateChannel(t, th.BasicTeam)
th.AddUserToChannel(t, th.BasicUser, channel)
err := th.App.SetChannelManagedCategory(rctx, channel.Id, "Operations")
require.Nil(t, err)
mappings, err := th.App.GetVisibleManagedCategoryMappings(rctx, th.BasicTeam.Id)
require.Nil(t, err)
assert.Equal(t, "Operations", mappings[channel.Id])
})
t.Run("should overwrite an existing managed category", func(t *testing.T) {
channel := th.CreateChannel(t, th.BasicTeam)
th.AddUserToChannel(t, th.BasicUser, channel)
err := th.App.SetChannelManagedCategory(rctx, channel.Id, "Alpha")
require.Nil(t, err)
err = th.App.SetChannelManagedCategory(rctx, channel.Id, "Bravo")
require.Nil(t, err)
mappings, err := th.App.GetVisibleManagedCategoryMappings(rctx, th.BasicTeam.Id)
require.Nil(t, err)
assert.Equal(t, "Bravo", mappings[channel.Id])
})
}
func TestClearChannelManagedCategory(t *testing.T) {
mainHelper.Parallel(t)
th := Setup(t).InitBasic(t)
session := &model.Session{UserId: th.BasicUser.Id, Props: model.StringMap{}}
rctx := th.Context.WithSession(session)
t.Run("should clear an existing managed category", func(t *testing.T) {
channel := th.CreateChannel(t, th.BasicTeam)
th.AddUserToChannel(t, th.BasicUser, channel)
err := th.App.SetChannelManagedCategory(rctx, channel.Id, "Operations")
require.Nil(t, err)
err = th.App.ClearChannelManagedCategory(rctx, channel.Id)
require.Nil(t, err)
mappings, err := th.App.GetVisibleManagedCategoryMappings(rctx, th.BasicTeam.Id)
require.Nil(t, err)
_, exists := mappings[channel.Id]
assert.False(t, exists, "expected channel.Id to be absent from mappings")
})
t.Run("should not error when clearing a channel with no managed category", func(t *testing.T) {
channel := th.CreateChannel(t, th.BasicTeam)
err := th.App.ClearChannelManagedCategory(rctx, channel.Id)
require.Nil(t, err)
})
}
func TestGetVisibleManagedCategoryMappings(t *testing.T) {
mainHelper.Parallel(t)
th := Setup(t).InitBasic(t)
session := &model.Session{UserId: th.BasicUser.Id, Props: model.StringMap{}}
rctx := th.Context.WithSession(session)
t.Run("should return empty map when no managed categories exist", func(t *testing.T) {
team := th.CreateTeam(t)
th.LinkUserToTeam(t, th.BasicUser, team)
mappings, err := th.App.GetVisibleManagedCategoryMappings(rctx, team.Id)
require.Nil(t, err)
assert.Empty(t, mappings)
})
t.Run("should return mappings only for channels the user is a member of", func(t *testing.T) {
team := th.CreateTeam(t)
th.LinkUserToTeam(t, th.BasicUser, team)
channel1 := th.CreateChannel(t, team)
otherUser := th.CreateUser(t)
th.LinkUserToTeam(t, otherUser, team)
otherSession := &model.Session{UserId: otherUser.Id, Props: model.StringMap{}}
otherCtx := th.Context.WithSession(otherSession)
channel2, appErr := th.App.CreateChannel(otherCtx, &model.Channel{
DisplayName: "other_channel",
Name: "other_" + model.NewId(),
Type: model.ChannelTypeOpen,
TeamId: team.Id,
CreatorId: otherUser.Id,
}, true)
require.Nil(t, appErr)
err := th.App.SetChannelManagedCategory(rctx, channel1.Id, "Alpha")
require.Nil(t, err)
err = th.App.SetChannelManagedCategory(otherCtx, channel2.Id, "Bravo")
require.Nil(t, err)
mappings, err := th.App.GetVisibleManagedCategoryMappings(rctx, team.Id)
require.Nil(t, err)
assert.Equal(t, "Alpha", mappings[channel1.Id])
_, hasCh2 := mappings[channel2.Id]
assert.False(t, hasCh2, "should not include channel user is not a member of")
})
t.Run("should return multiple category mappings", func(t *testing.T) {
team := th.CreateTeam(t)
th.LinkUserToTeam(t, th.BasicUser, team)
channel1 := th.CreateChannel(t, team)
th.AddUserToChannel(t, th.BasicUser, channel1)
channel2 := th.CreateChannel(t, team)
th.AddUserToChannel(t, th.BasicUser, channel2)
err := th.App.SetChannelManagedCategory(rctx, channel1.Id, "Ops")
require.Nil(t, err)
err = th.App.SetChannelManagedCategory(rctx, channel2.Id, "Intel")
require.Nil(t, err)
mappings, err := th.App.GetVisibleManagedCategoryMappings(rctx, team.Id)
require.Nil(t, err)
assert.Equal(t, "Ops", mappings[channel1.Id])
assert.Equal(t, "Intel", mappings[channel2.Id])
})
}
func TestDiffChannelsBetweenCategories(t *testing.T) {
mainHelper.Parallel(t)
t.Run("should return nothing when the categories contain identical channels", func(t *testing.T) {
originalCategories := []*model.SidebarCategoryWithChannels{
{
SidebarCategory: model.SidebarCategory{
Id: "category1",
DisplayName: "Category One",
},
Channels: []string{"channel1", "channel2", "channel3"},
},
{
SidebarCategory: model.SidebarCategory{
Id: "category2",
DisplayName: "Category Two",
},
Channels: []string{"channel4", "channel5"},
},
{
SidebarCategory: model.SidebarCategory{
Id: "category3",
DisplayName: "Category Three",
},
Channels: []string{},
},
}
updatedCategories := []*model.SidebarCategoryWithChannels{
{
SidebarCategory: model.SidebarCategory{
Id: "category1",
DisplayName: "Category Won",
},
Channels: []string{"channel1", "channel2", "channel3"},
},
{
SidebarCategory: model.SidebarCategory{
Id: "category2",
DisplayName: "Category Too",
},
Channels: []string{"channel4", "channel5"},
},
{
SidebarCategory: model.SidebarCategory{
Id: "category3",
DisplayName: "Category 🌲",
},
Channels: []string{},
},
}
channelsDiff := diffChannelsBetweenCategories(updatedCategories, originalCategories)
assert.Equal(t, map[string]*categoryChannelDiff{}, channelsDiff)
})
t.Run("should return nothing when the categories contain identical channels", func(t *testing.T) {
originalCategories := []*model.SidebarCategoryWithChannels{
{
SidebarCategory: model.SidebarCategory{
Id: "category1",
DisplayName: "Category One",
},
Channels: []string{"channel1", "channel2", "channel3"},
},
{
SidebarCategory: model.SidebarCategory{
Id: "category2",
DisplayName: "Category Two",
},
Channels: []string{"channel4", "channel5"},
},
{
SidebarCategory: model.SidebarCategory{
Id: "category3",
DisplayName: "Category Three",
},
Channels: []string{},
},
}
updatedCategories := []*model.SidebarCategoryWithChannels{
{
SidebarCategory: model.SidebarCategory{
Id: "category1",
DisplayName: "Category Won",
},
Channels: []string{},
},
{
SidebarCategory: model.SidebarCategory{
Id: "category2",
DisplayName: "Category Too",
},
Channels: []string{"channel5", "channel2"},
},
{
SidebarCategory: model.SidebarCategory{
Id: "category3",
DisplayName: "Category 🌲",
},
Channels: []string{"channel4", "channel1", "channel3"},
},
}
channelsDiff := diffChannelsBetweenCategories(updatedCategories, originalCategories)
assert.Equal(
t,
map[string]*categoryChannelDiff{
"channel1": {
fromCategoryId: "category1",
toCategoryId: "category3",
},
"channel2": {
fromCategoryId: "category1",
toCategoryId: "category2",
},
"channel3": {
fromCategoryId: "category1",
toCategoryId: "category3",
},
"channel4": {
fromCategoryId: "category2",
toCategoryId: "category3",
},
},
channelsDiff,
)
})
t.Run("should not return channels that are moved in our out of the categories implicitly", func(t *testing.T) {
// This case could change to actually return the channels in the future, but we don't need to handle it right now
originalCategories := []*model.SidebarCategoryWithChannels{
{
SidebarCategory: model.SidebarCategory{
Id: "category1",
DisplayName: "Category One",
},
Channels: []string{"channel1", "channel2"},
},
{
SidebarCategory: model.SidebarCategory{
Id: "category2",
DisplayName: "Category Two",
},
Channels: []string{"channel3"},
},
}
updatedCategories := []*model.SidebarCategoryWithChannels{
{
SidebarCategory: model.SidebarCategory{
Id: "category1",
DisplayName: "Category Won",
},
Channels: []string{"channel1", "channel3"},
},
{
SidebarCategory: model.SidebarCategory{
Id: "category2",
DisplayName: "Category Too",
},
Channels: []string{"channel4"},
},
}
channelsDiff := diffChannelsBetweenCategories(updatedCategories, originalCategories)
assert.Equal(
t,
map[string]*categoryChannelDiff{
"channel3": {
fromCategoryId: "category2",
toCategoryId: "category1",
},
},
channelsDiff,
)
})
}