mattermost/server/platform/services/sharedchannel/permalink_test.go
Daniel Espino García b5a816a657
Some checks are pending
API / build (push) Waiting to run
Server CI / Compute Go Version (push) Waiting to run
Server CI / Check mocks (push) Blocked by required conditions
Server CI / Check go mod tidy (push) Blocked by required conditions
Server CI / check-style (push) Blocked by required conditions
Server CI / Check serialization methods for hot structs (push) Blocked by required conditions
Server CI / Vet API (push) Blocked by required conditions
Server CI / Check migration files (push) Blocked by required conditions
Server CI / Generate email templates (push) Blocked by required conditions
Server CI / Check store layers (push) Blocked by required conditions
Server CI / Check mmctl docs (push) Blocked by required conditions
Server CI / Postgres with binary parameters (push) Blocked by required conditions
Server CI / Postgres (push) Blocked by required conditions
Server CI / Postgres (FIPS) (push) Blocked by required conditions
Server CI / Generate Test Coverage (push) Blocked by required conditions
Server CI / Run mmctl tests (push) Blocked by required conditions
Server CI / Run mmctl tests (FIPS) (push) Blocked by required conditions
Server CI / Build mattermost server app (push) Blocked by required conditions
Web App CI / check-lint (push) Waiting to run
Web App CI / check-i18n (push) Blocked by required conditions
Web App CI / check-types (push) Blocked by required conditions
Web App CI / test (platform) (push) Blocked by required conditions
Web App CI / test (mattermost-redux) (push) Blocked by required conditions
Web App CI / test (channels shard 1/4) (push) Blocked by required conditions
Web App CI / test (channels shard 2/4) (push) Blocked by required conditions
Web App CI / test (channels shard 3/4) (push) Blocked by required conditions
Web App CI / test (channels shard 4/4) (push) Blocked by required conditions
Web App CI / upload-coverage (push) Blocked by required conditions
Web App CI / build (push) Blocked by required conditions
Add audits for accessing posts without membership (#31266)
* Add audits for accessing posts without membership

* Fix tests

* Use correct audit level

* Address feedback

* Add missing checks all over the app

* Fix lint

* Fix test

* Fix tests

* Fix enterprise test

* Add missing test and docs

* Fix merge

* Fix lint

* Add audit logs on the web socket hook for permalink posts

* Fix lint

* Fix merge conflicts

* Handle all events with "non_channel_member_access" parameter

* Fix lint and tests

* Fix merge

* Fix tests
2026-01-20 10:38:27 +01:00

113 lines
3.3 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package sharedchannel
import (
"net/url"
"testing"
"github.com/stretchr/testify/assert"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/plugin/plugintest/mock"
"github.com/mattermost/mattermost/server/public/shared/mlog"
"github.com/mattermost/mattermost/server/public/shared/request"
"github.com/mattermost/mattermost/server/v8/channels/store/storetest/mocks"
"github.com/mattermost/mattermost/server/v8/channels/utils"
)
func TestProcessPermalinkToRemote(t *testing.T) {
scs := &Service{
server: &MockServerIface{},
app: &MockAppIface{},
}
mockStore := &mocks.Store{}
mockPostStore := mocks.PostStore{}
utils.TranslationsPreInit()
logger := mlog.CreateConsoleTestLogger(t)
pl := &model.PostList{}
mockPostStore.On("Get", mock.MatchedBy(func(ctx *request.Context) bool { return true }), "postID", model.GetPostsOptions{SkipFetchThreads: true}, "", map[string]bool{}).Return(pl, nil)
mockStore.On("Post").Return(&mockPostStore)
mockServer := scs.server.(*MockServerIface)
mockServer.On("GetStore").Return(mockStore)
mockServer.On("Log").Return(logger)
mockApp := scs.app.(*MockAppIface)
mockApp.On("SendEphemeralPost", mock.Anything, "user", mock.AnythingOfType("*model.Post")).Return(&model.Post{}, true).Times(1)
defer mockApp.AssertExpectations(t)
t.Run("same channel", func(t *testing.T) {
post := &model.Post{
Message: "hello world https://comm.matt.com/team/pl/postID link",
ChannelId: "sourceChan",
UserId: "user",
}
*pl = model.PostList{
Order: []string{"1"},
Posts: map[string]*model.Post{
"1": {
ChannelId: "sourceChan",
UserId: "user",
},
},
}
out := scs.processPermalinkToRemote(post)
assert.Equal(t, "hello world https://comm.matt.com/team/plshared/postID link", out)
})
t.Run("different channel", func(t *testing.T) {
post := &model.Post{
Message: "hello world https://comm.matt.com/team/pl/postID link https://comm.matt.com/team/pl/postID ",
ChannelId: "sourceChan",
UserId: "user",
}
*pl = model.PostList{
Order: []string{"1"},
Posts: map[string]*model.Post{
"1": {
ChannelId: "otherChan",
},
},
}
out := scs.processPermalinkToRemote(post)
assert.Equal(t, "hello world https://comm.matt.com/team/pl/postID link https://comm.matt.com/team/pl/postID ", out)
})
}
func TestProcessPermalinkFromRemote(t *testing.T) {
t.Run("has match", func(t *testing.T) {
parsed, _ := url.Parse("http://mysite.com")
scs := &Service{
server: &MockServerIface{},
siteURL: parsed,
}
out := scs.processPermalinkFromRemote(&model.Post{Message: "hello world https://comm.matt.com/team/plshared/postID link"},
&model.Team{Name: "myteam"})
assert.Equal(t,
"hello world http://mysite.com/myteam/pl/postID link",
out)
})
t.Run("does not match", func(t *testing.T) {
parsed, _ := url.Parse("http://mysite.com")
scs := &Service{
server: &MockServerIface{},
siteURL: parsed,
}
out := scs.processPermalinkFromRemote(&model.Post{Message: "hello world https://comm.matt.com/team/pl/postID link"},
&model.Team{Name: "myteam"})
assert.Equal(t,
"hello world https://comm.matt.com/team/pl/postID link",
out)
})
}