github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/app/slashcommands/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 slashcommands 5 6 import ( 7 "context" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 13 "github.com/masterhung0112/hk_server/v5/model" 14 ) 15 16 func TestLeaveProviderDoCommand(t *testing.T) { 17 th := setup(t).initBasic() 18 defer th.tearDown() 19 20 lp := LeaveProvider{} 21 22 publicChannel, _ := th.App.CreateChannel(th.Context, &model.Channel{ 23 DisplayName: "AA", 24 Name: "aa" + model.NewId() + "a", 25 Type: model.CHANNEL_OPEN, 26 TeamId: th.BasicTeam.Id, 27 CreatorId: th.BasicUser.Id, 28 }, false) 29 30 privateChannel, _ := th.App.CreateChannel(th.Context, &model.Channel{ 31 DisplayName: "BB", 32 Name: "aa" + model.NewId() + "a", 33 Type: model.CHANNEL_OPEN, 34 TeamId: th.BasicTeam.Id, 35 CreatorId: th.BasicUser.Id, 36 }, false) 37 38 defaultChannel, err := th.App.GetChannelByName(model.DEFAULT_CHANNEL, th.BasicTeam.Id, false) 39 require.Nil(t, err) 40 41 guest := th.createGuest() 42 43 th.App.AddUserToTeam(th.Context, th.BasicTeam.Id, th.BasicUser.Id, th.BasicUser.Id) 44 th.App.AddUserToChannel(th.BasicUser, publicChannel, false) 45 th.App.AddUserToChannel(th.BasicUser, privateChannel, false) 46 th.App.AddUserToTeam(th.Context, th.BasicTeam.Id, guest.Id, guest.Id) 47 th.App.AddUserToChannel(guest, publicChannel, false) 48 th.App.AddUserToChannel(guest, defaultChannel, false) 49 50 t.Run("Should error when no Channel ID in args", func(t *testing.T) { 51 args := &model.CommandArgs{ 52 UserId: th.BasicUser.Id, 53 T: func(s string, args ...interface{}) string { return s }, 54 } 55 actual := lp.DoCommand(th.App, th.Context, args, "") 56 assert.Equal(t, "api.command_leave.fail.app_error", actual.Text) 57 assert.Equal(t, model.COMMAND_RESPONSE_TYPE_EPHEMERAL, actual.ResponseType) 58 }) 59 60 t.Run("Should error when no Team ID in args", func(t *testing.T) { 61 args := &model.CommandArgs{ 62 UserId: th.BasicUser.Id, 63 ChannelId: publicChannel.Id, 64 T: func(s string, args ...interface{}) string { return s }, 65 } 66 actual := lp.DoCommand(th.App, th.Context, args, "") 67 assert.Equal(t, "api.command_leave.fail.app_error", actual.Text) 68 assert.Equal(t, model.COMMAND_RESPONSE_TYPE_EPHEMERAL, actual.ResponseType) 69 }) 70 71 t.Run("Leave a public channel", func(t *testing.T) { 72 args := &model.CommandArgs{ 73 UserId: th.BasicUser.Id, 74 ChannelId: publicChannel.Id, 75 T: func(s string, args ...interface{}) string { return s }, 76 TeamId: th.BasicTeam.Id, 77 SiteURL: "http://localhost:8065", 78 } 79 actual := lp.DoCommand(th.App, th.Context, args, "") 80 assert.Equal(t, "", actual.Text) 81 assert.Equal(t, args.SiteURL+"/"+th.BasicTeam.Name+"/channels/"+model.DEFAULT_CHANNEL, actual.GotoLocation) 82 assert.Equal(t, "", actual.ResponseType) 83 84 _, err = th.App.GetChannelMember(context.Background(), publicChannel.Id, th.BasicUser.Id) 85 assert.NotNil(t, err) 86 assert.NotNil(t, err.Id, "app.channel.get_member.missing.app_error") 87 }) 88 89 t.Run("Leave a private channel", func(t *testing.T) { 90 args := &model.CommandArgs{ 91 UserId: th.BasicUser.Id, 92 ChannelId: privateChannel.Id, 93 T: func(s string, args ...interface{}) string { return s }, 94 TeamId: th.BasicTeam.Id, 95 SiteURL: "http://localhost:8065", 96 } 97 actual := lp.DoCommand(th.App, th.Context, args, "") 98 assert.Equal(t, "", actual.Text) 99 }) 100 101 t.Run("Should not leave a default channel", func(t *testing.T) { 102 args := &model.CommandArgs{ 103 UserId: th.BasicUser.Id, 104 ChannelId: defaultChannel.Id, 105 T: func(s string, args ...interface{}) string { return s }, 106 TeamId: th.BasicTeam.Id, 107 SiteURL: "http://localhost:8065", 108 } 109 actual := lp.DoCommand(th.App, th.Context, args, "") 110 assert.Equal(t, "api.channel.leave.default.app_error", actual.Text) 111 }) 112 113 t.Run("Should allow to leave a default channel if user is guest", func(t *testing.T) { 114 args := &model.CommandArgs{ 115 UserId: guest.Id, 116 ChannelId: defaultChannel.Id, 117 T: func(s string, args ...interface{}) string { return s }, 118 TeamId: th.BasicTeam.Id, 119 SiteURL: "http://localhost:8065", 120 } 121 actual := lp.DoCommand(th.App, th.Context, args, "") 122 assert.Equal(t, "", actual.Text) 123 assert.Equal(t, args.SiteURL+"/"+th.BasicTeam.Name+"/channels/"+publicChannel.Name, actual.GotoLocation) 124 assert.Equal(t, "", actual.ResponseType) 125 126 _, err = th.App.GetChannelMember(context.Background(), defaultChannel.Id, guest.Id) 127 assert.NotNil(t, err) 128 assert.NotNil(t, err.Id, "app.channel.get_member.missing.app_error") 129 }) 130 131 t.Run("Should redirect to the team if is the last channel", func(t *testing.T) { 132 args := &model.CommandArgs{ 133 UserId: guest.Id, 134 ChannelId: publicChannel.Id, 135 T: func(s string, args ...interface{}) string { return s }, 136 TeamId: th.BasicTeam.Id, 137 SiteURL: "http://localhost:8065", 138 } 139 actual := lp.DoCommand(th.App, th.Context, args, "") 140 assert.Equal(t, "", actual.Text) 141 assert.Equal(t, args.SiteURL+"/", actual.GotoLocation) 142 assert.Equal(t, "", actual.ResponseType) 143 144 _, err = th.App.GetChannelMember(context.Background(), publicChannel.Id, guest.Id) 145 assert.NotNil(t, err) 146 assert.NotNil(t, err.Id, "app.channel.get_member.missing.app_error") 147 }) 148 }