summaryrefslogtreecommitdiffstats
path: root/app/command_groupmsg_test.go
blob: 422679525eef119522c5e115dff7de2dd1c0a01d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package app

import (
	"testing"

	"github.com/nicksnyder/go-i18n/i18n"
	"github.com/stretchr/testify/assert"

	"github.com/mattermost/mattermost-server/model"
)

func TestGroupMsgUsernames(t *testing.T) {
	if users, parsedMessage := groupMsgUsernames(""); len(users) != 0 || parsedMessage != "" {
		t.Fatal("error parsing empty message")
	}
	if users, parsedMessage := groupMsgUsernames("test"); len(users) != 1 || parsedMessage != "" {
		t.Fatal("error parsing simple user")
	}
	if users, parsedMessage := groupMsgUsernames("test1, test2, test3 , test4"); len(users) != 4 || parsedMessage != "" {
		t.Fatal("error parsing various users")
	}

	if users, parsedMessage := groupMsgUsernames("test1, test2 message with spaces"); len(users) != 2 || parsedMessage != "message with spaces" {
		t.Fatal("error parsing message")
	}

	if users, parsedMessage := groupMsgUsernames("test1, test2 message with, comma"); len(users) != 2 || parsedMessage != "message with, comma" {
		t.Fatal("error parsing messages with comma")
	}

	if users, parsedMessage := groupMsgUsernames("test1,,,test2"); len(users) != 2 || parsedMessage != "" {
		t.Fatal("error parsing multiple commas in username ")
	}

	if users, parsedMessage := groupMsgUsernames("    test1,       test2     other message         "); len(users) != 2 || parsedMessage != "other message" {
		t.Fatal("error parsing strange usage of spaces")
	}

	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" {
		t.Fatal("error parsing different types of users")
	}
}

func TestGroupMsgProvider(t *testing.T) {
	th := Setup().InitBasic()
	defer th.TearDown()

	user3 := th.CreateUser()
	targetUsers := "@" + th.BasicUser2.Username + ",@" + user3.Username + " "

	team := th.CreateTeam()
	th.LinkUserToTeam(th.BasicUser, team)
	cmd := &groupmsgProvider{}

	// Check without permission to create a GM channel.
	resp := cmd.DoCommand(th.App, &model.CommandArgs{
		T:       i18n.IdentityTfunc(),
		SiteURL: "http://test.url",
		TeamId:  team.Id,
		UserId:  th.BasicUser.Id,
		Session: model.Session{
			Roles: "",
		},
	}, targetUsers+"hello")

	channelName := model.GetGroupNameFromUserIds([]string{th.BasicUser.Id, th.BasicUser2.Id, user3.Id})
	assert.Equal(t, "api.command_groupmsg.permission.app_error", resp.Text)
	assert.Equal(t, "", resp.GotoLocation)

	// Check with permission to create a GM channel.
	resp = cmd.DoCommand(th.App, &model.CommandArgs{
		T:       i18n.IdentityTfunc(),
		SiteURL: "http://test.url",
		TeamId:  team.Id,
		UserId:  th.BasicUser.Id,
		Session: model.Session{
			Roles: model.SYSTEM_USER_ROLE_ID,
		},
	}, targetUsers+"hello")

	assert.Equal(t, "", resp.Text)
	assert.Equal(t, "http://test.url/"+team.Name+"/channels/"+channelName, resp.GotoLocation)

	// Check without permission to post to an existing GM channel.
	resp = cmd.DoCommand(th.App, &model.CommandArgs{
		T:       i18n.IdentityTfunc(),
		SiteURL: "http://test.url",
		TeamId:  team.Id,
		UserId:  th.BasicUser.Id,
		Session: model.Session{
			Roles: "",
		},
	}, targetUsers+"hello")

	assert.Equal(t, "", resp.Text)
	assert.Equal(t, "http://test.url/"+team.Name+"/channels/"+channelName, resp.GotoLocation)
}