github.com/turgay/mattermost-server@v5.3.2-0.20181002173352-2945e8a2b0ce+incompatible/app/command_channel_header_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 TestHeaderProviderDoCommand(t *testing.T) {
    15  	th := Setup().InitBasic()
    16  	defer th.TearDown()
    17  
    18  	hp := HeaderProvider{}
    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_header.message.app_error",
    29  		"hello": "",
    30  	} {
    31  		actual := hp.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 := hp.DoCommand(th.App, args, "hello").Text
    43  	assert.Equal(t, "api.command_channel_header.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 = hp.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 = hp.DoCommand(th.App, args, "hello").Text
    65  	assert.Equal(t, "api.command_channel_header.permission.app_error", actual)
    66  
    67  	// Try a group channel *with* being a member.
    68  	user1 := th.CreateUser()
    69  	user2 := th.CreateUser()
    70  	user3 := th.CreateUser()
    71  
    72  	groupChannel := th.CreateGroupChannel(user1, user2)
    73  
    74  	args = &model.CommandArgs{
    75  		T:         func(s string, args ...interface{}) string { return s },
    76  		ChannelId: groupChannel.Id,
    77  		Session:   model.Session{UserId: th.BasicUser.Id, TeamMembers: []*model.TeamMember{{TeamId: th.BasicTeam.Id, Roles: ""}}},
    78  	}
    79  
    80  	actual = hp.DoCommand(th.App, args, "hello").Text
    81  	assert.Equal(t, "", actual)
    82  
    83  	// Try a group channel *without* being a member.
    84  	args = &model.CommandArgs{
    85  		T:         func(s string, args ...interface{}) string { return s },
    86  		ChannelId: groupChannel.Id,
    87  		Session:   model.Session{UserId: user3.Id, TeamMembers: []*model.TeamMember{{TeamId: th.BasicTeam.Id, Roles: ""}}},
    88  	}
    89  
    90  	actual = hp.DoCommand(th.App, args, "hello").Text
    91  	assert.Equal(t, "api.command_channel_header.permission.app_error", actual)
    92  
    93  	// Try a direct channel *with* being a member.
    94  	directChannel := th.CreateDmChannel(user1)
    95  
    96  	args = &model.CommandArgs{
    97  		T:         func(s string, args ...interface{}) string { return s },
    98  		ChannelId: directChannel.Id,
    99  		Session:   model.Session{UserId: th.BasicUser.Id, TeamMembers: []*model.TeamMember{{TeamId: th.BasicTeam.Id, Roles: ""}}},
   100  	}
   101  
   102  	actual = hp.DoCommand(th.App, args, "hello").Text
   103  	assert.Equal(t, "", actual)
   104  
   105  	// Try a direct channel *without* being a member.
   106  	args = &model.CommandArgs{
   107  		T:         func(s string, args ...interface{}) string { return s },
   108  		ChannelId: directChannel.Id,
   109  		Session:   model.Session{UserId: user2.Id, TeamMembers: []*model.TeamMember{{TeamId: th.BasicTeam.Id, Roles: ""}}},
   110  	}
   111  
   112  	actual = hp.DoCommand(th.App, args, "hello").Text
   113  	assert.Equal(t, "api.command_channel_header.permission.app_error", actual)
   114  }