github.com/mad-app/mattermost-server@v5.11.1+incompatible/app/command_leave_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package app 5 6 import ( 7 "testing" 8 "time" 9 10 "github.com/stretchr/testify/assert" 11 12 "github.com/mattermost/mattermost-server/model" 13 ) 14 15 func TestLeaveProviderDoCommand(t *testing.T) { 16 th := Setup(t).InitBasic() 17 defer th.TearDown() 18 19 lp := LeaveProvider{} 20 21 publicChannel, _ := th.App.CreateChannel(&model.Channel{ 22 DisplayName: "AA", 23 Name: "aa" + model.NewId() + "a", 24 Type: model.CHANNEL_OPEN, 25 TeamId: th.BasicTeam.Id, 26 CreatorId: th.BasicUser.Id, 27 }, false) 28 29 privateChannel, _ := th.App.CreateChannel(&model.Channel{ 30 DisplayName: "BB", 31 Name: "aa" + model.NewId() + "a", 32 Type: model.CHANNEL_OPEN, 33 TeamId: th.BasicTeam.Id, 34 CreatorId: th.BasicUser.Id, 35 }, false) 36 37 th.App.AddUserToTeam(th.BasicTeam.Id, th.BasicUser.Id, th.BasicUser.Id) 38 th.App.AddUserToChannel(th.BasicUser, publicChannel) 39 th.App.AddUserToChannel(th.BasicUser, privateChannel) 40 41 args := &model.CommandArgs{ 42 T: func(s string, args ...interface{}) string { return s }, 43 } 44 45 // Should error when no Channel ID in args 46 actual := lp.DoCommand(th.App, args, "") 47 assert.Equal(t, "api.command_leave.fail.app_error", actual.Text) 48 assert.Equal(t, model.COMMAND_RESPONSE_TYPE_EPHEMERAL, actual.ResponseType) 49 50 // Should error when no Team ID in args 51 args.ChannelId = publicChannel.Id 52 actual = lp.DoCommand(th.App, args, "") 53 assert.Equal(t, "api.command_leave.fail.app_error", actual.Text) 54 assert.Equal(t, model.COMMAND_RESPONSE_TYPE_EPHEMERAL, actual.ResponseType) 55 56 // Leave a public channel 57 siteURL := "http://localhost:8065" 58 args.TeamId = th.BasicTeam.Id 59 args.SiteURL = siteURL 60 actual = lp.DoCommand(th.App, args, "") 61 assert.Equal(t, "", actual.Text) 62 assert.Equal(t, siteURL+"/"+th.BasicTeam.Name+"/channels/"+model.DEFAULT_CHANNEL, actual.GotoLocation) 63 assert.Equal(t, "", actual.ResponseType) 64 65 time.Sleep(100 * time.Millisecond) 66 67 member, err := th.App.GetChannelMember(publicChannel.Id, th.BasicUser.Id) 68 if member == nil { 69 t.Errorf("Expected member object, got nil") 70 } 71 72 if err != nil { 73 t.Errorf("Expected nil object, got %s", err) 74 } 75 76 // Leave a private channel 77 args.ChannelId = privateChannel.Id 78 actual = lp.DoCommand(th.App, args, "") 79 assert.Equal(t, "", actual.Text) 80 81 // Should not leave a default channel 82 defaultChannel, _ := th.App.GetChannelByName(model.DEFAULT_CHANNEL, th.BasicTeam.Id, false) 83 args.ChannelId = defaultChannel.Id 84 actual = lp.DoCommand(th.App, args, "") 85 assert.Equal(t, "api.channel.leave.default.app_error", actual.Text) 86 }