github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/app/slashcommands/command_invite_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/masterhung0112/hk_server/v5/app"
    13  	"github.com/masterhung0112/hk_server/v5/model"
    14  )
    15  
    16  func TestInviteProvider(t *testing.T) {
    17  	th := setup(t).initBasic()
    18  	defer th.tearDown()
    19  
    20  	channel := th.createChannel(th.BasicTeam, model.CHANNEL_OPEN)
    21  	privateChannel := th.createChannel(th.BasicTeam, model.CHANNEL_PRIVATE)
    22  	dmChannel := th.createDmChannel(th.BasicUser2)
    23  	privateChannel2 := th.createChannelWithAnotherUser(th.BasicTeam, model.CHANNEL_PRIVATE, th.BasicUser2.Id)
    24  
    25  	basicUser3 := th.createUser()
    26  	th.linkUserToTeam(basicUser3, th.BasicTeam)
    27  	basicUser4 := th.createUser()
    28  	deactivatedUser := th.createUser()
    29  	th.App.UpdateActive(th.Context, deactivatedUser, false)
    30  
    31  	var err *model.AppError
    32  	_, err = th.App.CreateBot(th.Context, &model.Bot{
    33  		Username:    "bot1",
    34  		OwnerId:     basicUser3.Id,
    35  		Description: "a test bot",
    36  	})
    37  	require.Nil(t, err)
    38  
    39  	bot2, err := th.App.CreateBot(th.Context, &model.Bot{
    40  		Username:    "bot2",
    41  		OwnerId:     basicUser3.Id,
    42  		Description: "a test bot",
    43  	})
    44  	require.Nil(t, err)
    45  	_, _, err = th.App.AddUserToTeam(th.Context, th.BasicTeam.Id, bot2.UserId, basicUser3.Id)
    46  	require.Nil(t, err)
    47  
    48  	bot3, err := th.App.CreateBot(th.Context, &model.Bot{
    49  		Username:    "bot3",
    50  		OwnerId:     basicUser3.Id,
    51  		Description: "a test bot",
    52  	})
    53  	require.Nil(t, err)
    54  	_, _, err = th.App.AddUserToTeam(th.Context, th.BasicTeam.Id, bot3.UserId, basicUser3.Id)
    55  	require.Nil(t, err)
    56  	err = th.App.RemoveUserFromTeam(th.Context, th.BasicTeam.Id, bot3.UserId, basicUser3.Id)
    57  	require.Nil(t, err)
    58  
    59  	InviteP := InviteProvider{}
    60  	args := &model.CommandArgs{
    61  		T:         func(s string, args ...interface{}) string { return s },
    62  		ChannelId: th.BasicChannel.Id,
    63  		TeamId:    th.BasicTeam.Id,
    64  		UserId:    th.BasicUser.Id,
    65  	}
    66  
    67  	userAndWrongChannel := "@" + th.BasicUser2.Username + " wrongchannel1"
    68  	userAndChannel := "@" + th.BasicUser2.Username + " ~" + channel.Name + " "
    69  	userAndDisplayChannel := "@" + th.BasicUser2.Username + " ~" + channel.DisplayName + " "
    70  	userAndPrivateChannel := "@" + th.BasicUser2.Username + " ~" + privateChannel.Name
    71  	userAndDMChannel := "@" + basicUser3.Username + " ~" + dmChannel.Name
    72  	userAndInvalidPrivate := "@" + basicUser3.Username + " ~" + privateChannel2.Name
    73  	deactivatedUserPublicChannel := "@" + deactivatedUser.Username + " ~" + channel.Name
    74  
    75  	groupChannel := th.createChannel(th.BasicTeam, model.CHANNEL_PRIVATE)
    76  	_, err = th.App.AddChannelMember(th.Context, th.BasicUser.Id, groupChannel, app.ChannelMemberOpts{})
    77  	require.Nil(t, err)
    78  	groupChannel.GroupConstrained = model.NewBool(true)
    79  	groupChannel, _ = th.App.UpdateChannel(groupChannel)
    80  
    81  	groupChannelNonUser := "@" + th.BasicUser2.Username + " ~" + groupChannel.Name
    82  
    83  	tests := []struct {
    84  		desc     string
    85  		expected string
    86  		msg      string
    87  	}{
    88  		{
    89  			desc:     "Missing user and channel in the command",
    90  			expected: "api.command_invite.missing_message.app_error",
    91  			msg:      "",
    92  		},
    93  		{
    94  			desc:     "User added in the current channel",
    95  			expected: "",
    96  			msg:      th.BasicUser2.Username,
    97  		},
    98  		{
    99  			desc:     "Add user to another channel not the current",
   100  			expected: "api.command_invite.success",
   101  			msg:      userAndChannel,
   102  		},
   103  		{
   104  			desc:     "try to add a user to a direct channel",
   105  			expected: "api.command_invite.directchannel.app_error",
   106  			msg:      userAndDMChannel,
   107  		},
   108  		{
   109  			desc:     "Try to add a user to a invalid channel",
   110  			expected: "api.command_invite.channel.error",
   111  			msg:      userAndWrongChannel,
   112  		},
   113  		{
   114  			desc:     "Try to add a user to an private channel",
   115  			expected: "api.command_invite.success",
   116  			msg:      userAndPrivateChannel,
   117  		},
   118  		{
   119  			desc:     "Using display channel name which is different form Channel name",
   120  			expected: "api.command_invite.channel.error",
   121  			msg:      userAndDisplayChannel,
   122  		},
   123  		{
   124  			desc:     "Invalid user to current channel",
   125  			expected: "api.command_invite.missing_user.app_error",
   126  			msg:      "@invalidUser123",
   127  		},
   128  		{
   129  			desc:     "Invalid user to current channel without @",
   130  			expected: "api.command_invite.missing_user.app_error",
   131  			msg:      "invalidUser321",
   132  		},
   133  		{
   134  			desc:     "try to add a user which is not part of the team",
   135  			expected: "api.command_invite.user_not_in_team.app_error",
   136  			msg:      basicUser4.Username,
   137  		},
   138  		{
   139  			desc:     "try to add a user not part of the group to a group channel",
   140  			expected: "api.command_invite.group_constrained_user_denied",
   141  			msg:      groupChannelNonUser,
   142  		},
   143  		{
   144  			desc:     "try to add a user to a private channel with no permission",
   145  			expected: "api.command_invite.private_channel.app_error",
   146  			msg:      userAndInvalidPrivate,
   147  		},
   148  		{
   149  			desc:     "try to add a deleted user to a public channel",
   150  			expected: "api.command_invite.missing_user.app_error",
   151  			msg:      deactivatedUserPublicChannel,
   152  		},
   153  		{
   154  			desc:     "try to add bot to a public channel",
   155  			expected: "api.command_invite.user_not_in_team.app_error",
   156  			msg:      "@bot1",
   157  		},
   158  		{
   159  			desc:     "add bot to a public channel",
   160  			expected: "",
   161  			msg:      "@bot2",
   162  		},
   163  		{
   164  			desc:     "try to add bot removed from a team to a public channel",
   165  			expected: "api.command_invite.user_not_in_team.app_error",
   166  			msg:      "@bot3",
   167  		},
   168  	}
   169  
   170  	for _, test := range tests {
   171  		t.Run(test.desc, func(t *testing.T) {
   172  			actual := InviteP.DoCommand(th.App, th.Context, args, test.msg).Text
   173  			assert.Equal(t, test.expected, actual)
   174  		})
   175  	}
   176  }
   177  
   178  func TestInviteGroup(t *testing.T) {
   179  	th := setup(t).initBasic()
   180  	defer th.tearDown()
   181  
   182  	th.BasicTeam.GroupConstrained = model.NewBool(true)
   183  	var err *model.AppError
   184  	_, _ = th.App.AddTeamMember(th.Context, th.BasicTeam.Id, th.BasicUser.Id)
   185  	_, err = th.App.AddTeamMember(th.Context, th.BasicTeam.Id, th.BasicUser2.Id)
   186  	require.Nil(t, err)
   187  	th.BasicTeam, _ = th.App.UpdateTeam(th.BasicTeam)
   188  
   189  	privateChannel := th.createChannel(th.BasicTeam, model.CHANNEL_PRIVATE)
   190  
   191  	groupChannelUser1 := "@" + th.BasicUser.Username + " ~" + privateChannel.Name
   192  	groupChannelUser2 := "@" + th.BasicUser2.Username + " ~" + privateChannel.Name
   193  	basicUser3 := th.createUser()
   194  	groupChannelUser3 := "@" + basicUser3.Username + " ~" + privateChannel.Name
   195  
   196  	InviteP := InviteProvider{}
   197  	args := &model.CommandArgs{
   198  		T:         func(s string, args ...interface{}) string { return s },
   199  		ChannelId: th.BasicChannel.Id,
   200  		TeamId:    th.BasicTeam.Id,
   201  		UserId:    th.BasicUser.Id,
   202  	}
   203  
   204  	tests := []struct {
   205  		desc     string
   206  		expected string
   207  		msg      string
   208  	}{
   209  		{
   210  			desc:     "try to add an existing user part of the group to a group channel",
   211  			expected: "api.command_invite.user_already_in_channel.app_error",
   212  			msg:      groupChannelUser1,
   213  		},
   214  		{
   215  			desc:     "try to add a user part of the group to a group channel",
   216  			expected: "api.command_invite.success",
   217  			msg:      groupChannelUser2,
   218  		},
   219  		{
   220  			desc:     "try to add a user NOT part of the group to a group channel",
   221  			expected: "api.command_invite.user_not_in_team.app_error",
   222  			msg:      groupChannelUser3,
   223  		},
   224  	}
   225  
   226  	for _, test := range tests {
   227  		t.Run(test.desc, func(t *testing.T) {
   228  			actual := InviteP.DoCommand(th.App, th.Context, args, test.msg).Text
   229  			assert.Equal(t, test.expected, actual)
   230  		})
   231  	}
   232  }