github.com/adacta-ru/mattermost-server/v6@v6.0.0/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  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/adacta-ru/mattermost-server/v6/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  	defaultChannel, err := th.App.GetChannelByName(model.DEFAULT_CHANNEL, th.BasicTeam.Id, false)
    38  	require.Nil(t, err)
    39  
    40  	guest := th.createGuest()
    41  
    42  	th.App.AddUserToTeam(th.BasicTeam.Id, th.BasicUser.Id, th.BasicUser.Id)
    43  	th.App.AddUserToChannel(th.BasicUser, publicChannel)
    44  	th.App.AddUserToChannel(th.BasicUser, privateChannel)
    45  	th.App.AddUserToTeam(th.BasicTeam.Id, guest.Id, guest.Id)
    46  	th.App.AddUserToChannel(guest, publicChannel)
    47  	th.App.AddUserToChannel(guest, defaultChannel)
    48  
    49  	t.Run("Should error when no Channel ID in args", func(t *testing.T) {
    50  		args := &model.CommandArgs{
    51  			UserId: th.BasicUser.Id,
    52  			T:      func(s string, args ...interface{}) string { return s },
    53  		}
    54  		actual := lp.DoCommand(th.App, args, "")
    55  		assert.Equal(t, "api.command_leave.fail.app_error", actual.Text)
    56  		assert.Equal(t, model.COMMAND_RESPONSE_TYPE_EPHEMERAL, actual.ResponseType)
    57  	})
    58  
    59  	t.Run("Should error when no Team ID in args", func(t *testing.T) {
    60  		args := &model.CommandArgs{
    61  			UserId:    th.BasicUser.Id,
    62  			ChannelId: publicChannel.Id,
    63  			T:         func(s string, args ...interface{}) string { return s },
    64  		}
    65  		actual := lp.DoCommand(th.App, args, "")
    66  		assert.Equal(t, "api.command_leave.fail.app_error", actual.Text)
    67  		assert.Equal(t, model.COMMAND_RESPONSE_TYPE_EPHEMERAL, actual.ResponseType)
    68  	})
    69  
    70  	t.Run("Leave a public channel", func(t *testing.T) {
    71  		args := &model.CommandArgs{
    72  			UserId:    th.BasicUser.Id,
    73  			ChannelId: publicChannel.Id,
    74  			T:         func(s string, args ...interface{}) string { return s },
    75  			TeamId:    th.BasicTeam.Id,
    76  			SiteURL:   "http://localhost:8065",
    77  		}
    78  		actual := lp.DoCommand(th.App, args, "")
    79  		assert.Equal(t, "", actual.Text)
    80  		assert.Equal(t, args.SiteURL+"/"+th.BasicTeam.Name+"/channels/"+model.DEFAULT_CHANNEL, actual.GotoLocation)
    81  		assert.Equal(t, "", actual.ResponseType)
    82  
    83  		_, err = th.App.GetChannelMember(publicChannel.Id, th.BasicUser.Id)
    84  		assert.NotNil(t, err)
    85  		assert.NotNil(t, err.Id, "app.channel.get_member.missing.app_error")
    86  	})
    87  
    88  	t.Run("Leave a private channel", func(t *testing.T) {
    89  		args := &model.CommandArgs{
    90  			UserId:    th.BasicUser.Id,
    91  			ChannelId: privateChannel.Id,
    92  			T:         func(s string, args ...interface{}) string { return s },
    93  			TeamId:    th.BasicTeam.Id,
    94  			SiteURL:   "http://localhost:8065",
    95  		}
    96  		actual := lp.DoCommand(th.App, args, "")
    97  		assert.Equal(t, "", actual.Text)
    98  	})
    99  
   100  	t.Run("Should not leave a default channel", func(t *testing.T) {
   101  		args := &model.CommandArgs{
   102  			UserId:    th.BasicUser.Id,
   103  			ChannelId: defaultChannel.Id,
   104  			T:         func(s string, args ...interface{}) string { return s },
   105  			TeamId:    th.BasicTeam.Id,
   106  			SiteURL:   "http://localhost:8065",
   107  		}
   108  		actual := lp.DoCommand(th.App, args, "")
   109  		assert.Equal(t, "api.channel.leave.default.app_error", actual.Text)
   110  	})
   111  
   112  	t.Run("Should allow to leave a default channel if user is guest", func(t *testing.T) {
   113  		args := &model.CommandArgs{
   114  			UserId:    guest.Id,
   115  			ChannelId: defaultChannel.Id,
   116  			T:         func(s string, args ...interface{}) string { return s },
   117  			TeamId:    th.BasicTeam.Id,
   118  			SiteURL:   "http://localhost:8065",
   119  		}
   120  		actual := lp.DoCommand(th.App, args, "")
   121  		assert.Equal(t, "", actual.Text)
   122  		assert.Equal(t, args.SiteURL+"/"+th.BasicTeam.Name+"/channels/"+publicChannel.Name, actual.GotoLocation)
   123  		assert.Equal(t, "", actual.ResponseType)
   124  
   125  		_, err = th.App.GetChannelMember(defaultChannel.Id, guest.Id)
   126  		assert.NotNil(t, err)
   127  		assert.NotNil(t, err.Id, "app.channel.get_member.missing.app_error")
   128  	})
   129  
   130  	t.Run("Should redirect to the team if is the last channel", func(t *testing.T) {
   131  		args := &model.CommandArgs{
   132  			UserId:    guest.Id,
   133  			ChannelId: publicChannel.Id,
   134  			T:         func(s string, args ...interface{}) string { return s },
   135  			TeamId:    th.BasicTeam.Id,
   136  			SiteURL:   "http://localhost:8065",
   137  		}
   138  		actual := lp.DoCommand(th.App, args, "")
   139  		assert.Equal(t, "", actual.Text)
   140  		assert.Equal(t, args.SiteURL+"/", actual.GotoLocation)
   141  		assert.Equal(t, "", actual.ResponseType)
   142  
   143  		_, err = th.App.GetChannelMember(publicChannel.Id, guest.Id)
   144  		assert.NotNil(t, err)
   145  		assert.NotNil(t, err.Id, "app.channel.get_member.missing.app_error")
   146  	})
   147  }