github.com/lologarithm/mattermost-server@v5.3.2-0.20181002060438-c82a84ed765b+incompatible/app/command_channel_purpose_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 TestPurposeProviderDoCommand(t *testing.T) {
    15  	th := Setup().InitBasic()
    16  	defer th.TearDown()
    17  
    18  	pp := PurposeProvider{}
    19  
    20  	// Try a public channel *with* permission.
    21  	args := &model.CommandArgs{
    22  		T:         func(s string, args ...interface{}) string { return s },
    23  		ChannelId: th.BasicChannel.Id,
    24  		Session:   model.Session{UserId: th.BasicUser.Id, TeamMembers: []*model.TeamMember{{TeamId: th.BasicTeam.Id, Roles: model.TEAM_USER_ROLE_ID}}},
    25  	}
    26  
    27  	for msg, expected := range map[string]string{
    28  		"":      "api.command_channel_purpose.message.app_error",
    29  		"hello": "",
    30  	} {
    31  		actual := pp.DoCommand(th.App, args, msg).Text
    32  		assert.Equal(t, expected, actual)
    33  	}
    34  
    35  	// Try a public channel *without* permission.
    36  	args = &model.CommandArgs{
    37  		T:         func(s string, args ...interface{}) string { return s },
    38  		ChannelId: th.BasicChannel.Id,
    39  		Session:   model.Session{UserId: th.BasicUser.Id, TeamMembers: []*model.TeamMember{{TeamId: th.BasicTeam.Id, Roles: ""}}},
    40  	}
    41  
    42  	actual := pp.DoCommand(th.App, args, "hello").Text
    43  	assert.Equal(t, "api.command_channel_purpose.permission.app_error", actual)
    44  
    45  	// Try a private channel *with* permission.
    46  	privateChannel := th.CreatePrivateChannel(th.BasicTeam)
    47  
    48  	args = &model.CommandArgs{
    49  		T:         func(s string, args ...interface{}) string { return s },
    50  		ChannelId: privateChannel.Id,
    51  		Session:   model.Session{UserId: th.BasicUser.Id, TeamMembers: []*model.TeamMember{{TeamId: th.BasicTeam.Id, Roles: model.TEAM_USER_ROLE_ID}}},
    52  	}
    53  
    54  	actual = pp.DoCommand(th.App, args, "hello").Text
    55  	assert.Equal(t, "", actual)
    56  
    57  	// Try a private channel *without* permission.
    58  	args = &model.CommandArgs{
    59  		T:         func(s string, args ...interface{}) string { return s },
    60  		ChannelId: privateChannel.Id,
    61  		Session:   model.Session{UserId: th.BasicUser.Id, TeamMembers: []*model.TeamMember{{TeamId: th.BasicTeam.Id, Roles: ""}}},
    62  	}
    63  
    64  	actual = pp.DoCommand(th.App, args, "hello").Text
    65  	assert.Equal(t, "api.command_channel_purpose.permission.app_error", actual)
    66  
    67  	// Try a group channel *with* being a member.
    68  	user1 := th.CreateUser()
    69  	user2 := th.CreateUser()
    70  
    71  	groupChannel := th.CreateGroupChannel(user1, user2)
    72  
    73  	args = &model.CommandArgs{
    74  		T:         func(s string, args ...interface{}) string { return s },
    75  		ChannelId: groupChannel.Id,
    76  		Session:   model.Session{UserId: th.BasicUser.Id, TeamMembers: []*model.TeamMember{{TeamId: th.BasicTeam.Id, Roles: ""}}},
    77  	}
    78  
    79  	actual = pp.DoCommand(th.App, args, "hello").Text
    80  	assert.Equal(t, "api.command_channel_purpose.direct_group.app_error", actual)
    81  
    82  	// Try a direct channel *with* being a member.
    83  	directChannel := th.CreateDmChannel(user1)
    84  
    85  	args = &model.CommandArgs{
    86  		T:         func(s string, args ...interface{}) string { return s },
    87  		ChannelId: directChannel.Id,
    88  		Session:   model.Session{UserId: th.BasicUser.Id, TeamMembers: []*model.TeamMember{{TeamId: th.BasicTeam.Id, Roles: ""}}},
    89  	}
    90  
    91  	actual = pp.DoCommand(th.App, args, "hello").Text
    92  	assert.Equal(t, "api.command_channel_purpose.direct_group.app_error", actual)
    93  }