summaryrefslogtreecommitdiffstats
path: root/api/command_test.go
blob: 8e0c2580e59ca9b15cb81b998edc9174d8dd9404 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package api

import (
	"testing"

	"github.com/mattermost/platform/model"
	"github.com/mattermost/platform/store"
	"github.com/mattermost/platform/utils"
)

func TestListCommands(t *testing.T) {
	Setup()

	team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
	team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)

	user1 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
	user1 = Client.Must(Client.CreateUser(user1, "")).Data.(*model.User)
	store.Must(Srv.Store.User().VerifyEmail(user1.Id))

	Client.LoginByEmail(team.Name, user1.Email, "pwd")

	if results, err := Client.ListCommands(); err != nil {
		t.Fatal(err)
	} else {
		commands := results.Data.([]*model.Command)
		foundEcho := false

		for _, command := range commands {
			if command.Trigger == "echo" {
				foundEcho = true
			}
		}

		if !foundEcho {
			t.Fatal("Couldn't find echo command")
		}
	}
}

func TestCreateCommand(t *testing.T) {
	Setup()
	*utils.Cfg.ServiceSettings.EnableCommands = true

	team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
	team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)

	user := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
	user = Client.Must(Client.CreateUser(user, "")).Data.(*model.User)
	store.Must(Srv.Store.User().VerifyEmail(user.Id))

	Client.LoginByEmail(team.Name, user.Email, "pwd")

	cmd := &model.Command{URL: "http://nowhere.com", Method: model.COMMAND_METHOD_POST}

	if _, err := Client.CreateCommand(cmd); err == nil {
		t.Fatal("should have failed because not admin")
	}

	c := &Context{}
	c.RequestId = model.NewId()
	c.IpAddress = "cmd_line"
	UpdateRoles(c, user, model.ROLE_SYSTEM_ADMIN)
	Client.LoginByEmail(team.Name, user.Email, "pwd")

	var rcmd *model.Command
	if result, err := Client.CreateCommand(cmd); err != nil {
		t.Fatal(err)
	} else {
		rcmd = result.Data.(*model.Command)
	}

	if rcmd.CreatorId != user.Id {
		t.Fatal("user ids didn't match")
	}

	if rcmd.TeamId != team.Id {
		t.Fatal("team ids didn't match")
	}

	cmd = &model.Command{CreatorId: "123", TeamId: "456", URL: "http://nowhere.com", Method: model.COMMAND_METHOD_POST}
	if result, err := Client.CreateCommand(cmd); err != nil {
		t.Fatal(err)
	} else {
		if result.Data.(*model.Command).CreatorId != user.Id {
			t.Fatal("bad user id wasn't overwritten")
		}
		if result.Data.(*model.Command).TeamId != team.Id {
			t.Fatal("bad team id wasn't overwritten")
		}
	}

	*utils.Cfg.ServiceSettings.EnableCommands = false
}

func TestListTeamCommands(t *testing.T) {
	Setup()
	*utils.Cfg.ServiceSettings.EnableCommands = true

	team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
	team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)

	user := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
	user = Client.Must(Client.CreateUser(user, "")).Data.(*model.User)
	store.Must(Srv.Store.User().VerifyEmail(user.Id))

	Client.LoginByEmail(team.Name, user.Email, "pwd")

	cmd1 := &model.Command{URL: "http://nowhere.com", Method: model.COMMAND_METHOD_POST}
	cmd1 = Client.Must(Client.CreateCommand(cmd1)).Data.(*model.Command)

	if result, err := Client.ListTeamCommands(); err != nil {
		t.Fatal(err)
	} else {
		cmds := result.Data.([]*model.Command)

		if len(hooks) != 1 {
			t.Fatal("incorrect number of cmd")
		}
	}

	*utils.Cfg.ServiceSettings.EnableCommands = false
}

// func TestSuggestRootCommands(t *testing.T) {
// 	Setup()

// 	team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
// 	team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)

// 	user1 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
// 	user1 = Client.Must(Client.CreateUser(user1, "")).Data.(*model.User)
// 	store.Must(Srv.Store.User().VerifyEmail(user1.Id))

// 	Client.LoginByEmail(team.Name, user1.Email, "pwd")

// 	if _, err := Client.Command("", "", true); err == nil {
// 		t.Fatal("Should fail")
// 	}

// 	rs1 := Client.Must(Client.Command("", "/", true)).Data.(*model.Command)

// 	hasLogout := false
// 	for _, v := range rs1.Suggestions {
// 		if v.Suggestion == "/logout" {
// 			hasLogout = true
// 		}
// 	}

// 	if !hasLogout {
// 		t.Log(rs1.Suggestions)
// 		t.Fatal("should have logout cmd")
// 	}

// 	rs2 := Client.Must(Client.Command("", "/log", true)).Data.(*model.Command)

// 	if rs2.Suggestions[0].Suggestion != "/logout" {
// 		t.Fatal("should have logout cmd")
// 	}

// 	rs3 := Client.Must(Client.Command("", "/joi", true)).Data.(*model.Command)

// 	if rs3.Suggestions[0].Suggestion != "/join" {
// 		t.Fatal("should have join cmd")
// 	}

// 	rs4 := Client.Must(Client.Command("", "/ech", true)).Data.(*model.Command)

// 	if rs4.Suggestions[0].Suggestion != "/echo" {
// 		t.Fatal("should have echo cmd")
// 	}
// }

// func TestLogoutCommands(t *testing.T) {
// 	Setup()

// 	team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
// 	team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)

// 	user1 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
// 	user1 = Client.Must(Client.CreateUser(user1, "")).Data.(*model.User)
// 	store.Must(Srv.Store.User().VerifyEmail(user1.Id))

// 	Client.LoginByEmail(team.Name, user1.Email, "pwd")

// 	rs1 := Client.Must(Client.Command("", "/logout", false)).Data.(*model.Command)
// 	if rs1.GotoLocation != "/logout" {
// 		t.Fatal("failed to logout")
// 	}
// }

// func TestJoinCommands(t *testing.T) {
// 	Setup()

// 	team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
// 	team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)

// 	user1 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
// 	user1 = Client.Must(Client.CreateUser(user1, "")).Data.(*model.User)
// 	store.Must(Srv.Store.User().VerifyEmail(user1.Id))

// 	Client.LoginByEmail(team.Name, user1.Email, "pwd")

// 	channel1 := &model.Channel{DisplayName: "AA", Name: "aa" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
// 	channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel)
// 	Client.Must(Client.LeaveChannel(channel1.Id))

// 	channel2 := &model.Channel{DisplayName: "BB", Name: "bb" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
// 	channel2 = Client.Must(Client.CreateChannel(channel2)).Data.(*model.Channel)
// 	Client.Must(Client.LeaveChannel(channel2.Id))

// 	user2 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
// 	user2 = Client.Must(Client.CreateUser(user2, "")).Data.(*model.User)
// 	store.Must(Srv.Store.User().VerifyEmail(user1.Id))

// 	data := make(map[string]string)
// 	data["user_id"] = user2.Id
// 	channel3 := Client.Must(Client.CreateDirectChannel(data)).Data.(*model.Channel)

// 	rs1 := Client.Must(Client.Command("", "/join aa", true)).Data.(*model.Command)
// 	if rs1.Suggestions[0].Suggestion != "/join "+channel1.Name {
// 		t.Fatal("should have join cmd")
// 	}

// 	rs2 := Client.Must(Client.Command("", "/join bb", true)).Data.(*model.Command)
// 	if rs2.Suggestions[0].Suggestion != "/join "+channel2.Name {
// 		t.Fatal("should have join cmd")
// 	}

// 	rs3 := Client.Must(Client.Command("", "/join", true)).Data.(*model.Command)
// 	if len(rs3.Suggestions) != 2 {
// 		t.Fatal("should have 2 join cmd")
// 	}

// 	rs4 := Client.Must(Client.Command("", "/join ", true)).Data.(*model.Command)
// 	if len(rs4.Suggestions) != 2 {
// 		t.Fatal("should have 2 join cmd")
// 	}

// 	rs5 := Client.Must(Client.Command("", "/join "+channel2.Name, false)).Data.(*model.Command)
// 	if !strings.HasSuffix(rs5.GotoLocation, "/"+team.Name+"/channels/"+channel2.Name) {
// 		t.Fatal("failed to join channel")
// 	}

// 	rs6 := Client.Must(Client.Command("", "/join "+channel3.Name, false)).Data.(*model.Command)
// 	if strings.HasSuffix(rs6.GotoLocation, "/"+team.Name+"/channels/"+channel3.Name) {
// 		t.Fatal("should not have joined direct message channel")
// 	}

// 	c1 := Client.Must(Client.GetChannels("")).Data.(*model.ChannelList)

// 	if len(c1.Channels) != 4 { // 4 because of town-square, off-topic and direct
// 		t.Fatal("didn't join channel")
// 	}

// 	found := false
// 	for _, c := range c1.Channels {
// 		if c.Name == channel2.Name {
// 			found = true
// 			break
// 		}
// 	}
// 	if !found {
// 		t.Fatal("didn't join channel")
// 	}
// }

// func TestEchoCommand(t *testing.T) {
// 	Setup()

// 	team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
// 	team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)

// 	user1 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
// 	user1 = Client.Must(Client.CreateUser(user1, "")).Data.(*model.User)
// 	store.Must(Srv.Store.User().VerifyEmail(user1.Id))

// 	Client.LoginByEmail(team.Name, user1.Email, "pwd")

// 	channel1 := &model.Channel{DisplayName: "AA", Name: "aa" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
// 	channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel)

// 	echoTestString := "/echo test"

// 	r1 := Client.Must(Client.Command(channel1.Id, echoTestString, false)).Data.(*model.Command)
// 	if r1.Response != model.RESP_EXECUTED {
// 		t.Fatal("Echo command failed to execute")
// 	}

// 	time.Sleep(100 * time.Millisecond)

// 	p1 := Client.Must(Client.GetPosts(channel1.Id, 0, 2, "")).Data.(*model.PostList)
// 	if len(p1.Order) != 1 {
// 		t.Fatal("Echo command failed to send")
// 	}
// }

// func TestLoadTestUrlCommand(t *testing.T) {
// 	Setup()

// 	// enable testing to use /loadtest but don't save it since we don't want to overwrite config.json
// 	enableTesting := utils.Cfg.ServiceSettings.EnableTesting
// 	defer func() {
// 		utils.Cfg.ServiceSettings.EnableTesting = enableTesting
// 	}()

// 	utils.Cfg.ServiceSettings.EnableTesting = true

// 	team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
// 	team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)

// 	user := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
// 	user = Client.Must(Client.CreateUser(user, "")).Data.(*model.User)
// 	store.Must(Srv.Store.User().VerifyEmail(user.Id))

// 	Client.LoginByEmail(team.Name, user.Email, "pwd")

// 	channel := &model.Channel{DisplayName: "AA", Name: "aa" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
// 	channel = Client.Must(Client.CreateChannel(channel)).Data.(*model.Channel)

// 	command := "/loadtest url "
// 	if _, err := Client.Command(channel.Id, command, false); err == nil {
// 		t.Fatal("/loadtest url with no url should've failed")
// 	}

// 	command = "/loadtest url http://www.hopefullynonexistent.file/path/asdf/qwerty"
// 	if _, err := Client.Command(channel.Id, command, false); err == nil {
// 		t.Fatal("/loadtest url with invalid url should've failed")
// 	}

// 	command = "/loadtest url https://raw.githubusercontent.com/mattermost/platform/master/README.md"
// 	if r := Client.Must(Client.Command(channel.Id, command, false)).Data.(*model.Command); r.Response != model.RESP_EXECUTED {
// 		t.Fatal("/loadtest url for README.md should've executed")
// 	}

// 	command = "/loadtest url test-emoticons.md"
// 	if r := Client.Must(Client.Command(channel.Id, command, false)).Data.(*model.Command); r.Response != model.RESP_EXECUTED {
// 		t.Fatal("/loadtest url for test-emoticons.md should've executed")
// 	}

// 	command = "/loadtest url test-emoticons"
// 	if r := Client.Must(Client.Command(channel.Id, command, false)).Data.(*model.Command); r.Response != model.RESP_EXECUTED {
// 		t.Fatal("/loadtest url for test-emoticons should've executed")
// 	}

// 	posts := Client.Must(Client.GetPosts(channel.Id, 0, 5, "")).Data.(*model.PostList)
// 	// note that this may make more than 3 posts if files are too long to fit in an individual post
// 	if len(posts.Order) < 3 {
// 		t.Fatal("/loadtest url made too few posts, perhaps there needs to be a delay before GetPosts in the test?")
// 	}
// }