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