command_leave_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
  2. // See LICENSE.txt for license information.
  3. package app
  4. import (
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. "github.com/mattermost/mattermost-server/v5/model"
  9. )
  10. func TestLeaveProviderDoCommand(t *testing.T) {
  11. th := Setup(t).InitBasic()
  12. defer th.TearDown()
  13. lp := LeaveProvider{}
  14. publicChannel, _ := th.App.CreateChannel(&model.Channel{
  15. DisplayName: "AA",
  16. Name: "aa" + model.NewId() + "a",
  17. Type: model.CHANNEL_OPEN,
  18. TeamId: th.BasicTeam.Id,
  19. CreatorId: th.BasicUser.Id,
  20. }, false)
  21. privateChannel, _ := th.App.CreateChannel(&model.Channel{
  22. DisplayName: "BB",
  23. Name: "aa" + model.NewId() + "a",
  24. Type: model.CHANNEL_OPEN,
  25. TeamId: th.BasicTeam.Id,
  26. CreatorId: th.BasicUser.Id,
  27. }, false)
  28. defaultChannel, err := th.App.GetChannelByName(model.DEFAULT_CHANNEL, th.BasicTeam.Id, false)
  29. require.Nil(t, err)
  30. guest := th.CreateGuest()
  31. th.App.AddUserToTeam(th.BasicTeam.Id, th.BasicUser.Id, th.BasicUser.Id)
  32. th.App.AddUserToChannel(th.BasicUser, publicChannel)
  33. th.App.AddUserToChannel(th.BasicUser, privateChannel)
  34. th.App.AddUserToTeam(th.BasicTeam.Id, guest.Id, guest.Id)
  35. th.App.AddUserToChannel(guest, publicChannel)
  36. th.App.AddUserToChannel(guest, defaultChannel)
  37. t.Run("Should error when no Channel ID in args", func(t *testing.T) {
  38. args := &model.CommandArgs{
  39. UserId: th.BasicUser.Id,
  40. T: func(s string, args ...interface{}) string { return s },
  41. }
  42. actual := lp.DoCommand(th.App, args, "")
  43. assert.Equal(t, "api.command_leave.fail.app_error", actual.Text)
  44. assert.Equal(t, model.COMMAND_RESPONSE_TYPE_EPHEMERAL, actual.ResponseType)
  45. })
  46. t.Run("Should error when no Team ID in args", func(t *testing.T) {
  47. args := &model.CommandArgs{
  48. UserId: th.BasicUser.Id,
  49. ChannelId: publicChannel.Id,
  50. T: func(s string, args ...interface{}) string { return s },
  51. }
  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. t.Run("Leave a public channel", func(t *testing.T) {
  57. args := &model.CommandArgs{
  58. UserId: th.BasicUser.Id,
  59. ChannelId: publicChannel.Id,
  60. T: func(s string, args ...interface{}) string { return s },
  61. TeamId: th.BasicTeam.Id,
  62. SiteURL: "http://localhost:8065",
  63. }
  64. actual := lp.DoCommand(th.App, args, "")
  65. assert.Equal(t, "", actual.Text)
  66. assert.Equal(t, args.SiteURL+"/"+th.BasicTeam.Name+"/channels/"+model.DEFAULT_CHANNEL, actual.GotoLocation)
  67. assert.Equal(t, "", actual.ResponseType)
  68. _, err = th.App.GetChannelMember(publicChannel.Id, th.BasicUser.Id)
  69. assert.NotNil(t, err)
  70. assert.NotNil(t, err.Id, "store.sql_channel.get_member.missing.app_error")
  71. })
  72. t.Run("Leave a private channel", func(t *testing.T) {
  73. args := &model.CommandArgs{
  74. UserId: th.BasicUser.Id,
  75. ChannelId: privateChannel.Id,
  76. T: func(s string, args ...interface{}) string { return s },
  77. TeamId: th.BasicTeam.Id,
  78. SiteURL: "http://localhost:8065",
  79. }
  80. actual := lp.DoCommand(th.App, args, "")
  81. assert.Equal(t, "", actual.Text)
  82. })
  83. t.Run("Should not leave a default channel", func(t *testing.T) {
  84. args := &model.CommandArgs{
  85. UserId: th.BasicUser.Id,
  86. ChannelId: defaultChannel.Id,
  87. T: func(s string, args ...interface{}) string { return s },
  88. TeamId: th.BasicTeam.Id,
  89. SiteURL: "http://localhost:8065",
  90. }
  91. actual := lp.DoCommand(th.App, args, "")
  92. assert.Equal(t, "api.channel.leave.default.app_error", actual.Text)
  93. })
  94. t.Run("Should allow to leave a default channel if user is guest", func(t *testing.T) {
  95. args := &model.CommandArgs{
  96. UserId: guest.Id,
  97. ChannelId: defaultChannel.Id,
  98. T: func(s string, args ...interface{}) string { return s },
  99. TeamId: th.BasicTeam.Id,
  100. SiteURL: "http://localhost:8065",
  101. }
  102. actual := lp.DoCommand(th.App, args, "")
  103. assert.Equal(t, "", actual.Text)
  104. assert.Equal(t, args.SiteURL+"/"+th.BasicTeam.Name+"/channels/"+publicChannel.Name, actual.GotoLocation)
  105. assert.Equal(t, "", actual.ResponseType)
  106. _, err = th.App.GetChannelMember(defaultChannel.Id, guest.Id)
  107. assert.NotNil(t, err)
  108. assert.NotNil(t, err.Id, "store.sql_channel.get_member.missing.app_error")
  109. })
  110. t.Run("Should redirect to the team if is the last channel", func(t *testing.T) {
  111. args := &model.CommandArgs{
  112. UserId: guest.Id,
  113. ChannelId: publicChannel.Id,
  114. T: func(s string, args ...interface{}) string { return s },
  115. TeamId: th.BasicTeam.Id,
  116. SiteURL: "http://localhost:8065",
  117. }
  118. actual := lp.DoCommand(th.App, args, "")
  119. assert.Equal(t, "", actual.Text)
  120. assert.Equal(t, args.SiteURL+"/", actual.GotoLocation)
  121. assert.Equal(t, "", actual.ResponseType)
  122. _, err = th.App.GetChannelMember(publicChannel.Id, guest.Id)
  123. assert.NotNil(t, err)
  124. assert.NotNil(t, err.Id, "store.sql_channel.get_member.missing.app_error")
  125. })
  126. }