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

     1  package app
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/nicksnyder/go-i18n/i18n"
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/mattermost/mattermost-server/model"
    10  )
    11  
    12  func TestGroupMsgUsernames(t *testing.T) {
    13  	if users, parsedMessage := groupMsgUsernames(""); len(users) != 0 || parsedMessage != "" {
    14  		t.Fatal("error parsing empty message")
    15  	}
    16  	if users, parsedMessage := groupMsgUsernames("test"); len(users) != 1 || parsedMessage != "" {
    17  		t.Fatal("error parsing simple user")
    18  	}
    19  	if users, parsedMessage := groupMsgUsernames("test1, test2, test3 , test4"); len(users) != 4 || parsedMessage != "" {
    20  		t.Fatal("error parsing various users")
    21  	}
    22  
    23  	if users, parsedMessage := groupMsgUsernames("test1, test2 message with spaces"); len(users) != 2 || parsedMessage != "message with spaces" {
    24  		t.Fatal("error parsing message")
    25  	}
    26  
    27  	if users, parsedMessage := groupMsgUsernames("test1, test2 message with, comma"); len(users) != 2 || parsedMessage != "message with, comma" {
    28  		t.Fatal("error parsing messages with comma")
    29  	}
    30  
    31  	if users, parsedMessage := groupMsgUsernames("test1,,,test2"); len(users) != 2 || parsedMessage != "" {
    32  		t.Fatal("error parsing multiple commas in username ")
    33  	}
    34  
    35  	if users, parsedMessage := groupMsgUsernames("    test1,       test2     other message         "); len(users) != 2 || parsedMessage != "other message" {
    36  		t.Fatal("error parsing strange usage of spaces")
    37  	}
    38  
    39  	if users, _ := groupMsgUsernames("    test1,       test2,,123,@321,+123"); len(users) != 5 || users[0] != "test1" || users[1] != "test2" || users[2] != "123" || users[3] != "321" || users[4] != "+123" {
    40  		t.Fatal("error parsing different types of users")
    41  	}
    42  }
    43  
    44  func TestGroupMsgProvider(t *testing.T) {
    45  	th := Setup().InitBasic()
    46  	defer th.TearDown()
    47  
    48  	user3 := th.CreateUser()
    49  	targetUsers := "@" + th.BasicUser2.Username + ",@" + user3.Username + " "
    50  
    51  	team := th.CreateTeam()
    52  	th.LinkUserToTeam(th.BasicUser, team)
    53  	cmd := &groupmsgProvider{}
    54  
    55  	// Check without permission to create a GM channel.
    56  	resp := cmd.DoCommand(th.App, &model.CommandArgs{
    57  		T:       i18n.IdentityTfunc(),
    58  		SiteURL: "http://test.url",
    59  		TeamId:  team.Id,
    60  		UserId:  th.BasicUser.Id,
    61  		Session: model.Session{
    62  			Roles: "",
    63  		},
    64  	}, targetUsers+"hello")
    65  
    66  	channelName := model.GetGroupNameFromUserIds([]string{th.BasicUser.Id, th.BasicUser2.Id, user3.Id})
    67  	assert.Equal(t, "api.command_groupmsg.permission.app_error", resp.Text)
    68  	assert.Equal(t, "", resp.GotoLocation)
    69  
    70  	// Check with permission to create a GM channel.
    71  	resp = cmd.DoCommand(th.App, &model.CommandArgs{
    72  		T:       i18n.IdentityTfunc(),
    73  		SiteURL: "http://test.url",
    74  		TeamId:  team.Id,
    75  		UserId:  th.BasicUser.Id,
    76  		Session: model.Session{
    77  			Roles: model.SYSTEM_USER_ROLE_ID,
    78  		},
    79  	}, targetUsers+"hello")
    80  
    81  	assert.Equal(t, "", resp.Text)
    82  	assert.Equal(t, "http://test.url/"+team.Name+"/channels/"+channelName, resp.GotoLocation)
    83  
    84  	// Check without permission to post to an existing GM channel.
    85  	resp = cmd.DoCommand(th.App, &model.CommandArgs{
    86  		T:       i18n.IdentityTfunc(),
    87  		SiteURL: "http://test.url",
    88  		TeamId:  team.Id,
    89  		UserId:  th.BasicUser.Id,
    90  		Session: model.Session{
    91  			Roles: "",
    92  		},
    93  	}, targetUsers+"hello")
    94  
    95  	assert.Equal(t, "", resp.Text)
    96  	assert.Equal(t, "http://test.url/"+team.Name+"/channels/"+channelName, resp.GotoLocation)
    97  }