Skip to content

Commit

Permalink
tests: cover hasPermissionToExportChannel (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
fmartingr authored Sep 24, 2024
1 parent 24601bf commit ac821a1
Show file tree
Hide file tree
Showing 2 changed files with 251 additions and 24 deletions.
100 changes: 76 additions & 24 deletions server/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"sync"
Expand All @@ -22,11 +21,12 @@ import (
"github.com/mattermost/mattermost-plugin-channel-export/server/pluginapi/mock_pluginapi"
)

func setupAPI(t *testing.T, mockAPI *pluginapi.Wrapper, now time.Time, userID, _ /*channelID*/ string) string {
func setupAPI(t *testing.T, mockAPI *pluginapi.Wrapper, now time.Time, userID, _ /*channelID*/ string, pluginConfiguration *configuration) string {
router := mux.NewRouter()
p := Plugin{
router: router,
client: mockAPI,
router: router,
client: mockAPI,
configuration: pluginConfiguration,
}

err := registerAPI(&p, makeTestPostsIterator(t, now))
Expand Down Expand Up @@ -67,7 +67,7 @@ func TestHandler(t *testing.T) {

mockAPI := pluginapi.CustomWrapper(mockChannel, mockFile, mockLog, mockPost, mockSlashCommand, mockUser, mockSystem, mockConfiguration, mockCluster)

address := setupAPI(t, mockAPI, time.Now(), "", "channel_id")
address := setupAPI(t, mockAPI, time.Now(), "", "channel_id", nil)
client := NewClient(address)

err := client.ExportChannel(io.Discard, "channel_id", FormatCSV)
Expand All @@ -90,7 +90,7 @@ func TestHandler(t *testing.T) {

mockAPI := pluginapi.CustomWrapper(mockChannel, mockFile, mockLog, mockPost, mockSlashCommand, mockUser, mockSystem, mockConfiguration, mockCluster)

address := setupAPI(t, mockAPI, time.Now(), "user_id", "channel_id")
address := setupAPI(t, mockAPI, time.Now(), "user_id", "channel_id", nil)
client := NewClient(address)
client.SetToken("token")

Expand Down Expand Up @@ -125,7 +125,7 @@ func TestHandler(t *testing.T) {
now := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.FixedZone("UTC-8", -8*60*60))
userID := "user_id"
channelID := "channel_id"
address := setupAPI(t, mockAPI, now, userID, channelID)
address := setupAPI(t, mockAPI, now, userID, channelID, nil)
client := NewClient(address)
client.SetToken("token")

Expand Down Expand Up @@ -177,7 +177,7 @@ func TestHandler(t *testing.T) {

mockAPI := pluginapi.CustomWrapper(mockChannel, mockFile, mockLog, mockPost, mockSlashCommand, mockUser, mockSystem, mockConfiguration, mockCluster)

address := setupAPI(t, mockAPI, time.Now(), "user_id", "channel_id")
address := setupAPI(t, mockAPI, time.Now(), "user_id", "channel_id", nil)
client := NewClient(address)
client.SetToken("token")

Expand All @@ -186,7 +186,7 @@ func TestHandler(t *testing.T) {
}}).Times(2)
mockConfiguration.EXPECT().GetConfig().Return(&model.Config{}).Times(1)

err := client.ExportChannel(ioutil.Discard, "", FormatCSV)
err := client.ExportChannel(io.Discard, "", FormatCSV)
require.EqualError(t, err, "missing channel_id parameter")
})

Expand All @@ -206,7 +206,7 @@ func TestHandler(t *testing.T) {

mockAPI := pluginapi.CustomWrapper(mockChannel, mockFile, mockLog, mockPost, mockSlashCommand, mockUser, mockSystem, mockConfiguration, mockCluster)

address := setupAPI(t, mockAPI, time.Now(), "user_id", "channel_id")
address := setupAPI(t, mockAPI, time.Now(), "user_id", "channel_id", nil)
client := NewClient(address)
client.SetToken("token")

Expand All @@ -215,7 +215,7 @@ func TestHandler(t *testing.T) {
}}).Times(2)
mockConfiguration.EXPECT().GetConfig().Return(&model.Config{}).Times(1)

err := client.ExportChannel(ioutil.Discard, "channel_id", "")
err := client.ExportChannel(io.Discard, "channel_id", "")
require.EqualError(t, err, "missing format parameter")
})

Expand All @@ -235,7 +235,7 @@ func TestHandler(t *testing.T) {

mockAPI := pluginapi.CustomWrapper(mockChannel, mockFile, mockLog, mockPost, mockSlashCommand, mockUser, mockSystem, mockConfiguration, mockCluster)

address := setupAPI(t, mockAPI, time.Now(), "user_id", "channel_id")
address := setupAPI(t, mockAPI, time.Now(), "user_id", "channel_id", nil)
client := NewClient(address)
client.SetToken("token")

Expand All @@ -244,7 +244,7 @@ func TestHandler(t *testing.T) {
}}).Times(2)
mockConfiguration.EXPECT().GetConfig().Return(&model.Config{}).Times(1)

err := client.ExportChannel(ioutil.Discard, "channel_id", "pdf2")
err := client.ExportChannel(io.Discard, "channel_id", "pdf2")
require.EqualError(t, err, "unsupported format parameter 'pdf2'")
})

Expand All @@ -265,7 +265,7 @@ func TestHandler(t *testing.T) {
mockAPI := pluginapi.CustomWrapper(mockChannel, mockFile, mockLog, mockPost, mockSlashCommand, mockUser, mockSystem, mockConfiguration, mockCluster)

channelID := "channel_id"
address := setupAPI(t, mockAPI, time.Now(), "user_id", channelID)
address := setupAPI(t, mockAPI, time.Now(), "user_id", channelID, nil)
client := NewClient(address)
client.SetToken("token")

Expand All @@ -275,7 +275,7 @@ func TestHandler(t *testing.T) {
mockConfiguration.EXPECT().GetConfig().Return(&model.Config{}).Times(1)
mockChannel.EXPECT().Get(channelID).Return(nil, &model.AppError{StatusCode: http.StatusNotFound}).Times(1)

err := client.ExportChannel(ioutil.Discard, channelID, FormatCSV)
err := client.ExportChannel(io.Discard, channelID, FormatCSV)
require.EqualError(t, err, "channel 'channel_id' not found or user does not have permission")
})

Expand All @@ -296,7 +296,7 @@ func TestHandler(t *testing.T) {
mockAPI := pluginapi.CustomWrapper(mockChannel, mockFile, mockLog, mockPost, mockSlashCommand, mockUser, mockSystem, mockConfiguration, mockCluster)

channelID := "channel_id"
address := setupAPI(t, mockAPI, time.Now(), "user_id", channelID)
address := setupAPI(t, mockAPI, time.Now(), "user_id", channelID, nil)
client := NewClient(address)
client.SetToken("token")

Expand All @@ -306,7 +306,7 @@ func TestHandler(t *testing.T) {
mockConfiguration.EXPECT().GetConfig().Return(&model.Config{}).Times(1)
mockChannel.EXPECT().Get(channelID).Return(nil, &model.AppError{StatusCode: http.StatusInternalServerError}).Times(1)

err := client.ExportChannel(ioutil.Discard, channelID, FormatCSV)
err := client.ExportChannel(io.Discard, channelID, FormatCSV)
require.EqualError(t, err, "channel 'channel_id' not found or user does not have permission")
})

Expand All @@ -328,7 +328,7 @@ func TestHandler(t *testing.T) {

userID := "user_id"
channelID := "channel_id"
address := setupAPI(t, mockAPI, time.Now(), userID, channelID)
address := setupAPI(t, mockAPI, time.Now(), userID, channelID, nil)
client := NewClient(address)
client.SetToken("token")

Expand All @@ -339,7 +339,7 @@ func TestHandler(t *testing.T) {
mockChannel.EXPECT().Get(channelID).Return(&model.Channel{Id: channelID}, nil).Times(1)
mockUser.EXPECT().HasPermissionToChannel(userID, channelID, model.PermissionReadChannel).Return(false).Times(1)

err := client.ExportChannel(ioutil.Discard, channelID, FormatCSV)
err := client.ExportChannel(io.Discard, channelID, FormatCSV)
require.EqualError(t, err, "channel 'channel_id' not found or user does not have permission")
})

Expand All @@ -362,7 +362,7 @@ func TestHandler(t *testing.T) {
now := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.FixedZone("UTC-8", -8*60*60))
userID := "user_id"
channelID := "channel_id"
address := setupAPI(t, mockAPI, now, userID, channelID)
address := setupAPI(t, mockAPI, now, userID, channelID, nil)
client := NewClient(address)
client.SetToken("token")

Expand Down Expand Up @@ -414,7 +414,7 @@ func TestHandler(t *testing.T) {
now := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.FixedZone("UTC-8", -8*60*60))
userID := "user_id"
channelID := "channel_id"
address := setupAPI(t, mockAPI, now, userID, channelID)
address := setupAPI(t, mockAPI, now, userID, channelID, nil)
client := NewClient(address)
client.SetToken("token")

Expand Down Expand Up @@ -466,7 +466,7 @@ func TestHandler(t *testing.T) {
now := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.FixedZone("UTC-8", -8*60*60))
userID := "user_id"
channelID := "channel_id"
address := setupAPI(t, mockAPI, now, userID, channelID)
address := setupAPI(t, mockAPI, now, userID, channelID, nil)
client := NewClient(address)
client.SetToken("token")

Expand Down Expand Up @@ -538,7 +538,7 @@ func TestHandler(t *testing.T) {
now := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.FixedZone("UTC-8", -8*60*60))
userID := "user_id"
channelID := "channel_id"
address := setupAPI(t, mockAPI, now, userID, channelID)
address := setupAPI(t, mockAPI, now, userID, channelID, nil)
client := NewClient(address)
client.SetToken("token")

Expand Down Expand Up @@ -588,7 +588,7 @@ func TestHandler(t *testing.T) {
now := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.FixedZone("UTC-8", -8*60*60))
userID := "user_id"
channelID := "channel_id"
address := setupAPI(t, mockAPI, now, userID, channelID)
address := setupAPI(t, mockAPI, now, userID, channelID, nil)
client := NewClient(address)
client.SetToken("token")

Expand Down Expand Up @@ -619,4 +619,56 @@ func TestHandler(t *testing.T) {
`
require.EqualValues(t, expected, buffer.String())
})

t.Run("no permissions", func(t *testing.T) {
mockCtrl := gomock.NewController(t)

mockChannel := mock_pluginapi.NewMockChannel(mockCtrl)
mockFile := mock_pluginapi.NewMockFile(mockCtrl)
mockLog := mock_pluginapi.NewMockLog(mockCtrl)
mockPost := mock_pluginapi.NewMockPost(mockCtrl)
mockSlashCommand := mock_pluginapi.NewMockSlashCommand(mockCtrl)
mockUser := mock_pluginapi.NewMockUser(mockCtrl)
mockSystem := mock_pluginapi.NewMockSystem(mockCtrl)
mockConfiguration := mock_pluginapi.NewMockConfiguration(mockCtrl)
mockCluster := mock_pluginapi.NewMockCluster(mockCtrl)
mockCluster.EXPECT().NewMutex(gomock.Eq(KeyClusterMutex)).Return(pluginapi.NewClusterMutexMock(), nil)

mockAPI := pluginapi.CustomWrapper(mockChannel, mockFile, mockLog, mockPost, mockSlashCommand, mockUser, mockSystem, mockConfiguration, mockCluster)

now := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.FixedZone("UTC-8", -8*60*60))
userID := "user_id"
channelID := "channel_id"
address := setupAPI(t, mockAPI, now, userID, channelID, &configuration{
EnableAdminRestrictions: true,
})
client := NewClient(address)
client.SetToken("token")

mockSystem.EXPECT().GetLicense().Return(&model.License{Features: &model.Features{
FutureFeatures: &trueValue,
}}).Times(2)
mockConfiguration.EXPECT().GetConfig().Return(&model.Config{}).Times(1)
mockChannel.EXPECT().Get(channelID).Return(&model.Channel{Id: channelID, DeleteAt: 1}, nil).Times(1)
mockUser.EXPECT().HasPermissionToChannel(userID, channelID, model.PermissionReadChannel).Return(false).Times(1)
mockUser.EXPECT().HasPermissionTo(userID, model.PermissionManageSystem).Return(false).Times(1)
mockConfiguration.EXPECT().GetConfig().Return(&model.Config{
TeamSettings: model.TeamSettings{
ExperimentalViewArchivedChannels: &falseValue,
},
}).Times(2)
mockConfiguration.EXPECT().GetConfig().Return(&model.Config{
PrivacySettings: model.PrivacySettings{
ShowEmailAddress: &trueValue,
},
})

var buffer bytes.Buffer
err := client.ExportChannel(&buffer, channelID, FormatCSV)
require.EqualValues(t, "", buffer.String())

expectedErr := fmt.Sprintf("channel '%s' not found or user does not have permission", channelID)

require.EqualError(t, err, expectedErr)
})
}
Loading

0 comments on commit ac821a1

Please sign in to comment.