github.com/spline-fu/mattermost-server@v4.10.10+incompatible/app/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 app
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  
    11  	"github.com/mattermost/mattermost-server/model"
    12  )
    13  
    14  func TestInviteProvider(t *testing.T) {
    15  	th := Setup().InitBasic()
    16  	defer th.TearDown()
    17  
    18  	channel := th.createChannel(th.BasicTeam, model.CHANNEL_OPEN)
    19  	privateChannel := th.createChannel(th.BasicTeam, model.CHANNEL_PRIVATE)
    20  	dmChannel := th.CreateDmChannel(th.BasicUser2)
    21  	privateChannel2 := th.createChannelWithAnotherUser(th.BasicTeam, model.CHANNEL_PRIVATE, th.BasicUser2.Id)
    22  
    23  	basicUser3 := th.CreateUser()
    24  	th.LinkUserToTeam(basicUser3, th.BasicTeam)
    25  	basicUser4 := th.CreateUser()
    26  
    27  	InviteP := InviteProvider{}
    28  	args := &model.CommandArgs{
    29  		T:         func(s string, args ...interface{}) string { return s },
    30  		ChannelId: th.BasicChannel.Id,
    31  		TeamId:    th.BasicTeam.Id,
    32  		Session:   model.Session{UserId: th.BasicUser.Id, TeamMembers: []*model.TeamMember{{TeamId: th.BasicTeam.Id, Roles: model.TEAM_USER_ROLE_ID}}},
    33  	}
    34  
    35  	userAndWrongChannel := "@" + th.BasicUser2.Username + " wrongchannel1"
    36  	userAndChannel := "@" + th.BasicUser2.Username + " ~" + channel.Name + " "
    37  	userAndDisplayChannel := "@" + th.BasicUser2.Username + " ~" + channel.DisplayName + " "
    38  	userAndPrivateChannel := "@" + th.BasicUser2.Username + " ~" + privateChannel.Name
    39  	userAndDMChannel := "@" + basicUser3.Username + " ~" + dmChannel.Name
    40  	userAndInvalidPrivate := "@" + basicUser3.Username + " ~" + privateChannel2.Name
    41  
    42  	tests := []struct {
    43  		desc     string
    44  		expected string
    45  		msg      string
    46  	}{
    47  		{
    48  			desc:     "Missing user and channel in the command",
    49  			expected: "api.command_invite.missing_message.app_error",
    50  			msg:      "",
    51  		},
    52  		{
    53  			desc:     "User added in the current channel",
    54  			expected: "",
    55  			msg:      th.BasicUser2.Username,
    56  		},
    57  		{
    58  			desc:     "Add user to another channel not the current",
    59  			expected: "api.command_invite.success",
    60  			msg:      userAndChannel,
    61  		},
    62  		{
    63  			desc:     "try to add a user to a direct channel",
    64  			expected: "api.command_invite.directchannel.app_error",
    65  			msg:      userAndDMChannel,
    66  		},
    67  		{
    68  			desc:     "Try to add a user to a invalid channel",
    69  			expected: "api.command_invite.channel.error",
    70  			msg:      userAndWrongChannel,
    71  		},
    72  		{
    73  			desc:     "Try to add a user to an private channel",
    74  			expected: "api.command_invite.success",
    75  			msg:      userAndPrivateChannel,
    76  		},
    77  		{
    78  			desc:     "Using display channel name which is different form Channel name",
    79  			expected: "api.command_invite.channel.error",
    80  			msg:      userAndDisplayChannel,
    81  		},
    82  		{
    83  			desc:     "Invalid user to current channel",
    84  			expected: "api.command_invite.missing_user.app_error",
    85  			msg:      "@invalidUser123",
    86  		},
    87  		{
    88  			desc:     "Invalid user to current channel without @",
    89  			expected: "api.command_invite.missing_user.app_error",
    90  			msg:      "invalidUser321",
    91  		},
    92  		{
    93  			desc:     "try to add a user which is not part of the team",
    94  			expected: "api.command_invite.fail.app_error",
    95  			msg:      basicUser4.Username,
    96  		},
    97  		{
    98  			desc:     "try to add a user to a direct channel",
    99  			expected: "api.command_invite.directchannel.app_error",
   100  			msg:      userAndDMChannel,
   101  		},
   102  		{
   103  			desc:     "try to add a user to a privante channel with no permission",
   104  			expected: "api.command_invite.private_channel.app_error",
   105  			msg:      userAndInvalidPrivate,
   106  		},
   107  	}
   108  
   109  	for _, test := range tests {
   110  		t.Run(test.desc, func(t *testing.T) {
   111  			actual := InviteP.DoCommand(th.App, args, test.msg).Text
   112  			assert.Equal(t, test.expected, actual)
   113  		})
   114  	}
   115  }