github.com/jlevesy/mattermost-server@v5.3.2-0.20181003190404-7468f35cb0c8+incompatible/app/command_join_test.go (about)

     1  // Copyright (c) 2018-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/nicksnyder/go-i18n/i18n"
    10  	"github.com/stretchr/testify/assert"
    11  
    12  	"github.com/mattermost/mattermost-server/model"
    13  )
    14  
    15  func TestJoinCommandNoChannel(t *testing.T) {
    16  	th := Setup().InitBasic()
    17  	defer th.TearDown()
    18  
    19  	if testing.Short() {
    20  		t.SkipNow()
    21  	}
    22  
    23  	cmd := &JoinProvider{}
    24  	resp := cmd.DoCommand(th.App, &model.CommandArgs{
    25  		T:       i18n.IdentityTfunc(),
    26  		UserId:  th.BasicUser2.Id,
    27  		SiteURL: "http://test.url",
    28  		TeamId:  th.BasicTeam.Id,
    29  		Session: model.Session{UserId: th.BasicUser.Id, TeamMembers: []*model.TeamMember{{TeamId: th.BasicTeam.Id, Roles: model.TEAM_USER_ROLE_ID}}},
    30  	}, "asdsad")
    31  
    32  	assert.Equal(t, "api.command_join.list.app_error", resp.Text)
    33  }
    34  
    35  func TestJoinCommandForExistingChannel(t *testing.T) {
    36  	th := Setup().InitBasic()
    37  	defer th.TearDown()
    38  
    39  	if testing.Short() {
    40  		t.SkipNow()
    41  	}
    42  
    43  	channel2, _ := th.App.CreateChannel(&model.Channel{
    44  		DisplayName: "AA",
    45  		Name:        "aa" + model.NewId() + "a",
    46  		Type:        model.CHANNEL_OPEN,
    47  		TeamId:      th.BasicTeam.Id,
    48  		CreatorId:   th.BasicUser.Id,
    49  	}, false)
    50  
    51  	cmd := &JoinProvider{}
    52  	resp := cmd.DoCommand(th.App, &model.CommandArgs{
    53  		T:       i18n.IdentityTfunc(),
    54  		UserId:  th.BasicUser2.Id,
    55  		SiteURL: "http://test.url",
    56  		TeamId:  th.BasicTeam.Id,
    57  		Session: model.Session{UserId: th.BasicUser.Id, TeamMembers: []*model.TeamMember{{TeamId: th.BasicTeam.Id, Roles: model.TEAM_USER_ROLE_ID}}},
    58  	}, channel2.Name)
    59  
    60  	assert.Equal(t, "", resp.Text)
    61  	assert.Equal(t, "http://test.url/"+th.BasicTeam.Name+"/channels/"+channel2.Name, resp.GotoLocation)
    62  }
    63  
    64  func TestJoinCommandWithTilde(t *testing.T) {
    65  	th := Setup().InitBasic()
    66  	defer th.TearDown()
    67  
    68  	if testing.Short() {
    69  		t.SkipNow()
    70  	}
    71  
    72  	channel2, _ := th.App.CreateChannel(&model.Channel{
    73  		DisplayName: "AA",
    74  		Name:        "aa" + model.NewId() + "a",
    75  		Type:        model.CHANNEL_OPEN,
    76  		TeamId:      th.BasicTeam.Id,
    77  		CreatorId:   th.BasicUser.Id,
    78  	}, false)
    79  
    80  	cmd := &JoinProvider{}
    81  	resp := cmd.DoCommand(th.App, &model.CommandArgs{
    82  		T:       i18n.IdentityTfunc(),
    83  		UserId:  th.BasicUser2.Id,
    84  		SiteURL: "http://test.url",
    85  		TeamId:  th.BasicTeam.Id,
    86  		Session: model.Session{UserId: th.BasicUser.Id, TeamMembers: []*model.TeamMember{{TeamId: th.BasicTeam.Id, Roles: model.TEAM_USER_ROLE_ID}}},
    87  	}, "~"+channel2.Name)
    88  
    89  	assert.Equal(t, "", resp.Text)
    90  	assert.Equal(t, "http://test.url/"+th.BasicTeam.Name+"/channels/"+channel2.Name, resp.GotoLocation)
    91  }
    92  
    93  func TestJoinCommandPermissions(t *testing.T) {
    94  	th := Setup().InitBasic()
    95  	defer th.TearDown()
    96  
    97  	channel2, _ := th.App.CreateChannel(&model.Channel{
    98  		DisplayName: "AA",
    99  		Name:        "aa" + model.NewId() + "a",
   100  		Type:        model.CHANNEL_OPEN,
   101  		TeamId:      th.BasicTeam.Id,
   102  		CreatorId:   th.BasicUser.Id,
   103  	}, false)
   104  
   105  	cmd := &JoinProvider{}
   106  
   107  	// Try a public channel *without* permission.
   108  	args := &model.CommandArgs{
   109  		T:       i18n.IdentityTfunc(),
   110  		UserId:  th.BasicUser2.Id,
   111  		SiteURL: "http://test.url",
   112  		TeamId:  th.BasicTeam.Id,
   113  		Session: model.Session{UserId: th.BasicUser.Id, TeamMembers: []*model.TeamMember{{TeamId: th.BasicTeam.Id, Roles: ""}}},
   114  	}
   115  
   116  	actual := cmd.DoCommand(th.App, args, "~"+channel2.Name).Text
   117  	assert.Equal(t, "api.command_join.fail.app_error", actual)
   118  
   119  	// Try a public channel with permission.
   120  	args = &model.CommandArgs{
   121  		T:       i18n.IdentityTfunc(),
   122  		UserId:  th.BasicUser2.Id,
   123  		SiteURL: "http://test.url",
   124  		TeamId:  th.BasicTeam.Id,
   125  		Session: model.Session{UserId: th.BasicUser.Id, TeamMembers: []*model.TeamMember{{TeamId: th.BasicTeam.Id, Roles: model.TEAM_USER_ROLE_ID}}},
   126  	}
   127  
   128  	actual = cmd.DoCommand(th.App, args, "~"+channel2.Name).Text
   129  	assert.Equal(t, "", actual)
   130  
   131  	// Try a private channel *without* permission.
   132  	channel3, _ := th.App.CreateChannel(&model.Channel{
   133  		DisplayName: "BB",
   134  		Name:        "aa" + model.NewId() + "a",
   135  		Type:        model.CHANNEL_PRIVATE,
   136  		TeamId:      th.BasicTeam.Id,
   137  		CreatorId:   th.BasicUser.Id,
   138  	}, false)
   139  
   140  	args = &model.CommandArgs{
   141  		T:       i18n.IdentityTfunc(),
   142  		UserId:  th.BasicUser2.Id,
   143  		SiteURL: "http://test.url",
   144  		TeamId:  th.BasicTeam.Id,
   145  		Session: model.Session{UserId: th.BasicUser.Id, TeamMembers: []*model.TeamMember{{TeamId: th.BasicTeam.Id, Roles: model.TEAM_USER_ROLE_ID}}},
   146  	}
   147  
   148  	actual = cmd.DoCommand(th.App, args, "~"+channel3.Name).Text
   149  	assert.Equal(t, "api.command_join.fail.app_error", actual)
   150  }