github.com/vnforks/kid@v5.11.1+incompatible/app/command_remove_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 TestRemoveProviderDoCommand(t *testing.T) {
    15  	th := Setup(t).InitBasic()
    16  	defer th.TearDown()
    17  
    18  	rp := RemoveProvider{}
    19  
    20  	publicChannel, _ := th.App.CreateChannel(&model.Channel{
    21  		DisplayName: "AA",
    22  		Name:        "aa" + model.NewId() + "a",
    23  		Type:        model.CHANNEL_OPEN,
    24  		TeamId:      th.BasicTeam.Id,
    25  		CreatorId:   th.BasicUser.Id,
    26  	}, false)
    27  
    28  	privateChannel, _ := th.App.CreateChannel(&model.Channel{
    29  		DisplayName: "BB",
    30  		Name:        "aa" + model.NewId() + "a",
    31  		Type:        model.CHANNEL_OPEN,
    32  		TeamId:      th.BasicTeam.Id,
    33  		CreatorId:   th.BasicUser.Id,
    34  	}, false)
    35  
    36  	targetUser := th.CreateUser()
    37  	th.App.AddUserToTeam(th.BasicTeam.Id, targetUser.Id, targetUser.Id)
    38  	th.App.AddUserToChannel(targetUser, publicChannel)
    39  	th.App.AddUserToChannel(targetUser, privateChannel)
    40  
    41  	// Try a public channel *without* permission.
    42  	args := &model.CommandArgs{
    43  		T:         func(s string, args ...interface{}) string { return s },
    44  		ChannelId: publicChannel.Id,
    45  		Session:   model.Session{UserId: th.BasicUser.Id, TeamMembers: []*model.TeamMember{{TeamId: th.BasicTeam.Id, Roles: ""}}},
    46  	}
    47  
    48  	actual := rp.DoCommand(th.App, args, targetUser.Username).Text
    49  	assert.Equal(t, "api.command_remove.permission.app_error", actual)
    50  
    51  	// Try a public channel *with* permission.
    52  	th.App.AddUserToChannel(th.BasicUser, publicChannel)
    53  	args = &model.CommandArgs{
    54  		T:         func(s string, args ...interface{}) string { return s },
    55  		ChannelId: publicChannel.Id,
    56  		Session:   model.Session{UserId: th.BasicUser.Id, TeamMembers: []*model.TeamMember{{TeamId: th.BasicTeam.Id, Roles: ""}}},
    57  	}
    58  
    59  	actual = rp.DoCommand(th.App, args, targetUser.Username).Text
    60  	assert.Equal(t, "", actual)
    61  
    62  	// Try a private channel *without* permission.
    63  	args = &model.CommandArgs{
    64  		T:         func(s string, args ...interface{}) string { return s },
    65  		ChannelId: privateChannel.Id,
    66  		Session:   model.Session{UserId: th.BasicUser.Id, TeamMembers: []*model.TeamMember{{TeamId: th.BasicTeam.Id, Roles: ""}}},
    67  	}
    68  
    69  	actual = rp.DoCommand(th.App, args, targetUser.Username).Text
    70  	assert.Equal(t, "api.command_remove.permission.app_error", actual)
    71  
    72  	// Try a private channel *with* permission.
    73  	th.App.AddUserToChannel(th.BasicUser, privateChannel)
    74  	args = &model.CommandArgs{
    75  		T:         func(s string, args ...interface{}) string { return s },
    76  		ChannelId: privateChannel.Id,
    77  		Session:   model.Session{UserId: th.BasicUser.Id, TeamMembers: []*model.TeamMember{{TeamId: th.BasicTeam.Id, Roles: ""}}},
    78  	}
    79  
    80  	actual = rp.DoCommand(th.App, args, targetUser.Username).Text
    81  	assert.Equal(t, "", actual)
    82  
    83  	// Try a group channel
    84  	user1 := th.CreateUser()
    85  	user2 := th.CreateUser()
    86  
    87  	groupChannel := th.CreateGroupChannel(user1, user2)
    88  
    89  	args = &model.CommandArgs{
    90  		T:         func(s string, args ...interface{}) string { return s },
    91  		ChannelId: groupChannel.Id,
    92  		Session:   model.Session{UserId: th.BasicUser.Id, TeamMembers: []*model.TeamMember{{TeamId: th.BasicTeam.Id, Roles: ""}}},
    93  	}
    94  
    95  	actual = rp.DoCommand(th.App, args, user1.Username).Text
    96  	assert.Equal(t, "api.command_remove.direct_group.app_error", actual)
    97  
    98  	// Try a direct channel *with* being a member.
    99  	directChannel := th.CreateDmChannel(user1)
   100  
   101  	args = &model.CommandArgs{
   102  		T:         func(s string, args ...interface{}) string { return s },
   103  		ChannelId: directChannel.Id,
   104  		Session:   model.Session{UserId: th.BasicUser.Id, TeamMembers: []*model.TeamMember{{TeamId: th.BasicTeam.Id, Roles: ""}}},
   105  	}
   106  
   107  	actual = rp.DoCommand(th.App, args, user1.Username).Text
   108  	assert.Equal(t, "api.command_remove.direct_group.app_error", actual)
   109  
   110  	// Try a public channel with a deactivated user.
   111  	deactivatedUser := th.CreateUser()
   112  	th.App.AddUserToTeam(th.BasicTeam.Id, deactivatedUser.Id, deactivatedUser.Id)
   113  	th.App.AddUserToChannel(deactivatedUser, publicChannel)
   114  	th.App.UpdateActive(deactivatedUser, false)
   115  
   116  	args = &model.CommandArgs{
   117  		T:         func(s string, args ...interface{}) string { return s },
   118  		ChannelId: publicChannel.Id,
   119  		Session:   model.Session{UserId: th.BasicUser.Id, TeamMembers: []*model.TeamMember{{TeamId: th.BasicTeam.Id, Roles: ""}}},
   120  	}
   121  
   122  	actual = rp.DoCommand(th.App, args, deactivatedUser.Username).Text
   123  	assert.Equal(t, "api.command_remove.missing.app_error", actual)
   124  }