github.com/jlevesy/mattermost-server@v5.3.2-0.20181003190404-7468f35cb0c8+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  	deactivatedUser := th.CreateUser()
    27  	th.App.UpdateActive(deactivatedUser, false)
    28  
    29  	InviteP := InviteProvider{}
    30  	args := &model.CommandArgs{
    31  		T:         func(s string, args ...interface{}) string { return s },
    32  		ChannelId: th.BasicChannel.Id,
    33  		TeamId:    th.BasicTeam.Id,
    34  		Session:   model.Session{UserId: th.BasicUser.Id, TeamMembers: []*model.TeamMember{{TeamId: th.BasicTeam.Id, Roles: model.TEAM_USER_ROLE_ID}}},
    35  	}
    36  
    37  	userAndWrongChannel := "@" + th.BasicUser2.Username + " wrongchannel1"
    38  	userAndChannel := "@" + th.BasicUser2.Username + " ~" + channel.Name + " "
    39  	userAndDisplayChannel := "@" + th.BasicUser2.Username + " ~" + channel.DisplayName + " "
    40  	userAndPrivateChannel := "@" + th.BasicUser2.Username + " ~" + privateChannel.Name
    41  	userAndDMChannel := "@" + basicUser3.Username + " ~" + dmChannel.Name
    42  	userAndInvalidPrivate := "@" + basicUser3.Username + " ~" + privateChannel2.Name
    43  	deactivatedUserPublicChannel := "@" + deactivatedUser.Username + " ~" + channel.Name
    44  
    45  	tests := []struct {
    46  		desc     string
    47  		expected string
    48  		msg      string
    49  	}{
    50  		{
    51  			desc:     "Missing user and channel in the command",
    52  			expected: "api.command_invite.missing_message.app_error",
    53  			msg:      "",
    54  		},
    55  		{
    56  			desc:     "User added in the current channel",
    57  			expected: "",
    58  			msg:      th.BasicUser2.Username,
    59  		},
    60  		{
    61  			desc:     "Add user to another channel not the current",
    62  			expected: "api.command_invite.success",
    63  			msg:      userAndChannel,
    64  		},
    65  		{
    66  			desc:     "try to add a user to a direct channel",
    67  			expected: "api.command_invite.directchannel.app_error",
    68  			msg:      userAndDMChannel,
    69  		},
    70  		{
    71  			desc:     "Try to add a user to a invalid channel",
    72  			expected: "api.command_invite.channel.error",
    73  			msg:      userAndWrongChannel,
    74  		},
    75  		{
    76  			desc:     "Try to add a user to an private channel",
    77  			expected: "api.command_invite.success",
    78  			msg:      userAndPrivateChannel,
    79  		},
    80  		{
    81  			desc:     "Using display channel name which is different form Channel name",
    82  			expected: "api.command_invite.channel.error",
    83  			msg:      userAndDisplayChannel,
    84  		},
    85  		{
    86  			desc:     "Invalid user to current channel",
    87  			expected: "api.command_invite.missing_user.app_error",
    88  			msg:      "@invalidUser123",
    89  		},
    90  		{
    91  			desc:     "Invalid user to current channel without @",
    92  			expected: "api.command_invite.missing_user.app_error",
    93  			msg:      "invalidUser321",
    94  		},
    95  		{
    96  			desc:     "try to add a user which is not part of the team",
    97  			expected: "api.command_invite.fail.app_error",
    98  			msg:      basicUser4.Username,
    99  		},
   100  		{
   101  			desc:     "try to add a user to a private channel with no permission",
   102  			expected: "api.command_invite.private_channel.app_error",
   103  			msg:      userAndInvalidPrivate,
   104  		},
   105  		{
   106  			desc:     "try to add a deleted user to a public channel",
   107  			expected: "api.command_invite.missing_user.app_error",
   108  			msg:      deactivatedUserPublicChannel,
   109  		},
   110  	}
   111  
   112  	for _, test := range tests {
   113  		t.Run(test.desc, func(t *testing.T) {
   114  			actual := InviteP.DoCommand(th.App, args, test.msg).Text
   115  			assert.Equal(t, test.expected, actual)
   116  		})
   117  	}
   118  }