github.com/adacta-ru/mattermost-server/v6@v6.0.0/app/slashcommands/command_join_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package slashcommands
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/mattermost/go-i18n/i18n"
    10  	"github.com/stretchr/testify/assert"
    11  
    12  	"github.com/adacta-ru/mattermost-server/v6/model"
    13  )
    14  
    15  func TestJoinCommandNoChannel(t *testing.T) {
    16  	th := setup(t).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  	}, "asdsad")
    30  
    31  	assert.Equal(t, "api.command_join.list.app_error", resp.Text)
    32  }
    33  
    34  func TestJoinCommandForExistingChannel(t *testing.T) {
    35  	th := setup(t).initBasic()
    36  	defer th.tearDown()
    37  
    38  	if testing.Short() {
    39  		t.SkipNow()
    40  	}
    41  
    42  	channel2, _ := th.App.CreateChannel(&model.Channel{
    43  		DisplayName: "AA",
    44  		Name:        "aa" + model.NewId() + "a",
    45  		Type:        model.CHANNEL_OPEN,
    46  		TeamId:      th.BasicTeam.Id,
    47  		CreatorId:   th.BasicUser.Id,
    48  	}, false)
    49  
    50  	cmd := &JoinProvider{}
    51  	resp := cmd.DoCommand(th.App, &model.CommandArgs{
    52  		T:       i18n.IdentityTfunc(),
    53  		UserId:  th.BasicUser2.Id,
    54  		SiteURL: "http://test.url",
    55  		TeamId:  th.BasicTeam.Id,
    56  	}, channel2.Name)
    57  
    58  	assert.Equal(t, "", resp.Text)
    59  	assert.Equal(t, "http://test.url/"+th.BasicTeam.Name+"/channels/"+channel2.Name, resp.GotoLocation)
    60  }
    61  
    62  func TestJoinCommandWithTilde(t *testing.T) {
    63  	th := setup(t).initBasic()
    64  	defer th.tearDown()
    65  
    66  	if testing.Short() {
    67  		t.SkipNow()
    68  	}
    69  
    70  	channel2, _ := th.App.CreateChannel(&model.Channel{
    71  		DisplayName: "AA",
    72  		Name:        "aa" + model.NewId() + "a",
    73  		Type:        model.CHANNEL_OPEN,
    74  		TeamId:      th.BasicTeam.Id,
    75  		CreatorId:   th.BasicUser.Id,
    76  	}, false)
    77  
    78  	cmd := &JoinProvider{}
    79  	resp := cmd.DoCommand(th.App, &model.CommandArgs{
    80  		T:       i18n.IdentityTfunc(),
    81  		UserId:  th.BasicUser2.Id,
    82  		SiteURL: "http://test.url",
    83  		TeamId:  th.BasicTeam.Id,
    84  	}, "~"+channel2.Name)
    85  
    86  	assert.Equal(t, "", resp.Text)
    87  	assert.Equal(t, "http://test.url/"+th.BasicTeam.Name+"/channels/"+channel2.Name, resp.GotoLocation)
    88  }
    89  
    90  func TestJoinCommandPermissions(t *testing.T) {
    91  	th := setup(t).initBasic()
    92  	defer th.tearDown()
    93  
    94  	channel2, _ := th.App.CreateChannel(&model.Channel{
    95  		DisplayName: "AA",
    96  		Name:        "aa" + model.NewId() + "a",
    97  		Type:        model.CHANNEL_OPEN,
    98  		TeamId:      th.BasicTeam.Id,
    99  		CreatorId:   th.BasicUser.Id,
   100  	}, false)
   101  
   102  	cmd := &JoinProvider{}
   103  
   104  	user3 := th.createUser()
   105  
   106  	// Try a public channel *without* permission.
   107  	args := &model.CommandArgs{
   108  		T:       i18n.IdentityTfunc(),
   109  		UserId:  user3.Id,
   110  		SiteURL: "http://test.url",
   111  		TeamId:  th.BasicTeam.Id,
   112  	}
   113  
   114  	actual := cmd.DoCommand(th.App, args, "~"+channel2.Name).Text
   115  	assert.Equal(t, "api.command_join.fail.app_error", actual)
   116  
   117  	// Try a public channel with permission.
   118  	args = &model.CommandArgs{
   119  		T:       i18n.IdentityTfunc(),
   120  		UserId:  th.BasicUser2.Id,
   121  		SiteURL: "http://test.url",
   122  		TeamId:  th.BasicTeam.Id,
   123  	}
   124  
   125  	actual = cmd.DoCommand(th.App, args, "~"+channel2.Name).Text
   126  	assert.Equal(t, "", actual)
   127  
   128  	// Try a private channel *without* permission.
   129  	channel3, _ := th.App.CreateChannel(&model.Channel{
   130  		DisplayName: "BB",
   131  		Name:        "aa" + model.NewId() + "a",
   132  		Type:        model.CHANNEL_PRIVATE,
   133  		TeamId:      th.BasicTeam.Id,
   134  		CreatorId:   th.BasicUser.Id,
   135  	}, false)
   136  
   137  	args = &model.CommandArgs{
   138  		T:       i18n.IdentityTfunc(),
   139  		UserId:  th.BasicUser2.Id,
   140  		SiteURL: "http://test.url",
   141  		TeamId:  th.BasicTeam.Id,
   142  	}
   143  
   144  	actual = cmd.DoCommand(th.App, args, "~"+channel3.Name).Text
   145  	assert.Equal(t, "api.command_join.fail.app_error", actual)
   146  }