summaryrefslogtreecommitdiffstats
path: root/api/emoji_test.go
blob: fb23cc4396cdf25c983eadce304d81f12262ece5 (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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package api

import (
	"bytes"
	"image"
	"image/color"
	"image/gif"
	"image/jpeg"
	"image/png"
	"testing"
	"time"

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

func TestGetEmoji(t *testing.T) {
	th := Setup().InitBasic()
	Client := th.BasicClient

	EnableCustomEmoji := *utils.Cfg.ServiceSettings.EnableCustomEmoji
	defer func() {
		*utils.Cfg.ServiceSettings.EnableCustomEmoji = EnableCustomEmoji
	}()
	*utils.Cfg.ServiceSettings.EnableCustomEmoji = true

	emojis := []*model.Emoji{
		{
			CreatorId: model.NewId(),
			Name:      model.NewId(),
		},
		{
			CreatorId: model.NewId(),
			Name:      model.NewId(),
		},
		{
			CreatorId: model.NewId(),
			Name:      model.NewId(),
		},
	}

	for i, emoji := range emojis {
		emojis[i] = store.Must(Srv.Store.Emoji().Save(emoji)).(*model.Emoji)
	}
	defer func() {
		for _, emoji := range emojis {
			store.Must(Srv.Store.Emoji().Delete(emoji.Id, time.Now().Unix()))
		}
	}()

	if returnedEmojis, err := Client.ListEmoji(); err != nil {
		t.Fatal(err)
	} else {
		for _, emoji := range emojis {
			found := false

			for _, savedEmoji := range returnedEmojis {
				if emoji.Id == savedEmoji.Id {
					found = true
					break
				}
			}

			if !found {
				t.Fatalf("failed to get emoji with id %v", emoji.Id)
			}
		}
	}

	deleted := &model.Emoji{
		CreatorId: model.NewId(),
		Name:      model.NewId(),
		DeleteAt:  1,
	}
	deleted = store.Must(Srv.Store.Emoji().Save(deleted)).(*model.Emoji)

	if returnedEmojis, err := Client.ListEmoji(); err != nil {
		t.Fatal(err)
	} else {
		found := false

		for _, savedEmoji := range returnedEmojis {
			if deleted.Id == savedEmoji.Id {
				found = true
				break
			}
		}

		if found {
			t.Fatalf("souldn't have gotten deleted emoji %v", deleted.Id)
		}
	}
}

func TestCreateEmoji(t *testing.T) {
	th := Setup().InitBasic().InitSystemAdmin()
	Client := th.BasicClient

	EnableCustomEmoji := *utils.Cfg.ServiceSettings.EnableCustomEmoji
	defer func() {
		*utils.Cfg.ServiceSettings.EnableCustomEmoji = EnableCustomEmoji
	}()
	*utils.Cfg.ServiceSettings.EnableCustomEmoji = false

	emoji := &model.Emoji{
		CreatorId: th.BasicUser.Id,
		Name:      model.NewId(),
	}

	// try to create an emoji when they're disabled
	if _, err := Client.CreateEmoji(emoji, createTestGif(t, 10, 10), "image.gif"); err == nil {
		t.Fatal("shouldn't be able to create an emoji when they're disabled")
	}

	*utils.Cfg.ServiceSettings.EnableCustomEmoji = true

	// try to create a valid gif emoji when they're enabled
	if emojiResult, err := Client.CreateEmoji(emoji, createTestGif(t, 10, 10), "image.gif"); err != nil {
		t.Fatal(err)
	} else {
		emoji = emojiResult
	}

	// try to create an emoji with a duplicate name
	emoji2 := &model.Emoji{
		CreatorId: th.BasicUser.Id,
		Name:      emoji.Name,
	}
	if _, err := Client.CreateEmoji(emoji2, createTestGif(t, 10, 10), "image.gif"); err == nil {
		t.Fatal("shouldn't be able to create an emoji with a duplicate name")
	}

	Client.MustGeneric(Client.DeleteEmoji(emoji.Id))

	// try to create a valid animated gif emoji
	emoji = &model.Emoji{
		CreatorId: th.BasicUser.Id,
		Name:      model.NewId(),
	}
	if emojiResult, err := Client.CreateEmoji(emoji, createTestAnimatedGif(t, 10, 10, 10), "image.gif"); err != nil {
		t.Fatal(err)
	} else {
		emoji = emojiResult
	}
	Client.MustGeneric(Client.DeleteEmoji(emoji.Id))

	// try to create a valid jpeg emoji
	emoji = &model.Emoji{
		CreatorId: th.BasicUser.Id,
		Name:      model.NewId(),
	}
	if emojiResult, err := Client.CreateEmoji(emoji, createTestJpeg(t, 10, 10), "image.jpeg"); err != nil {
		t.Fatal(err)
	} else {
		emoji = emojiResult
	}
	Client.MustGeneric(Client.DeleteEmoji(emoji.Id))

	// try to create a valid png emoji
	emoji = &model.Emoji{
		CreatorId: th.BasicUser.Id,
		Name:      model.NewId(),
	}
	if emojiResult, err := Client.CreateEmoji(emoji, createTestPng(t, 10, 10), "image.png"); err != nil {
		t.Fatal(err)
	} else {
		emoji = emojiResult
	}
	Client.MustGeneric(Client.DeleteEmoji(emoji.Id))

	// try to create an emoji that's too wide
	emoji = &model.Emoji{
		CreatorId: th.BasicUser.Id,
		Name:      model.NewId(),
	}
	if _, err := Client.CreateEmoji(emoji, createTestGif(t, 1000, 10), "image.gif"); err == nil {
		t.Fatal("shouldn't be able to create an emoji that's too wide")
	}

	// try to create an emoji that's too tall
	emoji = &model.Emoji{
		CreatorId: th.BasicUser.Id,
		Name:      model.NewId(),
	}
	if _, err := Client.CreateEmoji(emoji, createTestGif(t, 10, 1000), "image.gif"); err == nil {
		t.Fatal("shouldn't be able to create an emoji that's too tall")
	}

	// try to create an emoji that's too large
	emoji = &model.Emoji{
		CreatorId: th.BasicUser.Id,
		Name:      model.NewId(),
	}
	if _, err := Client.CreateEmoji(emoji, createTestAnimatedGif(t, 100, 100, 4000), "image.gif"); err == nil {
		t.Fatal("shouldn't be able to create an emoji that's too large")
	}

	// try to create an emoji with data that isn't an image
	emoji = &model.Emoji{
		CreatorId: th.BasicUser.Id,
		Name:      model.NewId(),
	}
	if _, err := Client.CreateEmoji(emoji, make([]byte, 100, 100), "image.gif"); err == nil {
		t.Fatal("shouldn't be able to create an emoji with non-image data")
	}

	// try to create an emoji as another user
	emoji = &model.Emoji{
		CreatorId: th.BasicUser2.Id,
		Name:      model.NewId(),
	}
	if _, err := Client.CreateEmoji(emoji, createTestGif(t, 10, 10), "image.gif"); err == nil {
		t.Fatal("shouldn't be able to create an emoji as another user")
	}
}

func TestDeleteEmoji(t *testing.T) {
	th := Setup().InitBasic().InitSystemAdmin()
	Client := th.BasicClient

	EnableCustomEmoji := *utils.Cfg.ServiceSettings.EnableCustomEmoji
	defer func() {
		*utils.Cfg.ServiceSettings.EnableCustomEmoji = EnableCustomEmoji
	}()
	*utils.Cfg.ServiceSettings.EnableCustomEmoji = false

	emoji1 := createTestEmoji(t, &model.Emoji{
		CreatorId: th.BasicUser.Id,
		Name:      model.NewId(),
	}, createTestGif(t, 10, 10))

	if _, err := Client.DeleteEmoji(emoji1.Id); err == nil {
		t.Fatal("shouldn't have been able to delete an emoji when they're disabled")
	}

	*utils.Cfg.ServiceSettings.EnableCustomEmoji = true

	if deleted, err := Client.DeleteEmoji(emoji1.Id); err != nil {
		t.Fatal(err)
	} else if !deleted {
		t.Fatalf("should be able to delete your own emoji %v", emoji1.Id)
	}

	if _, err := Client.DeleteEmoji(emoji1.Id); err == nil {
		t.Fatal("shouldn't be able to delete an already-deleted emoji")
	}

	emoji2 := createTestEmoji(t, &model.Emoji{
		CreatorId: th.BasicUser2.Id,
		Name:      model.NewId(),
	}, createTestGif(t, 10, 10))

	if _, err := Client.DeleteEmoji(emoji2.Id); err == nil {
		t.Fatal("shouldn't be able to delete another user's emoji")
	}

	if deleted, err := th.SystemAdminClient.DeleteEmoji(emoji2.Id); err != nil {
		t.Fatal(err)
	} else if !deleted {
		t.Fatalf("system admin should be able to delete anyone's emoji %v", emoji2.Id)
	}
}

func createTestGif(t *testing.T, width int, height int) []byte {
	var buffer bytes.Buffer

	if err := gif.Encode(&buffer, image.NewRGBA(image.Rect(0, 0, width, height)), nil); err != nil {
		t.Fatalf("failed to create gif: %v", err.Error())
	}

	return buffer.Bytes()
}

func createTestAnimatedGif(t *testing.T, width int, height int, frames int) []byte {
	var buffer bytes.Buffer

	img := gif.GIF{
		Image: make([]*image.Paletted, frames, frames),
		Delay: make([]int, frames, frames),
	}
	for i := 0; i < frames; i++ {
		img.Image[i] = image.NewPaletted(image.Rect(0, 0, width, height), color.Palette{color.Black})
		img.Delay[i] = 0
	}
	if err := gif.EncodeAll(&buffer, &img); err != nil {
		t.Fatalf("failed to create animated gif: %v", err.Error())
	}

	return buffer.Bytes()
}

func createTestJpeg(t *testing.T, width int, height int) []byte {
	var buffer bytes.Buffer

	if err := jpeg.Encode(&buffer, image.NewRGBA(image.Rect(0, 0, width, height)), nil); err != nil {
		t.Fatalf("failed to create jpeg: %v", err.Error())
	}

	return buffer.Bytes()
}

func createTestPng(t *testing.T, width int, height int) []byte {
	var buffer bytes.Buffer

	if err := png.Encode(&buffer, image.NewRGBA(image.Rect(0, 0, width, height))); err != nil {
		t.Fatalf("failed to create png: %v", err.Error())
	}

	return buffer.Bytes()
}

func createTestEmoji(t *testing.T, emoji *model.Emoji, imageData []byte) *model.Emoji {
	emoji = store.Must(Srv.Store.Emoji().Save(emoji)).(*model.Emoji)

	if err := WriteFile(imageData, "emoji/"+emoji.Id+"/image"); err != nil {
		store.Must(Srv.Store.Emoji().Delete(emoji.Id, time.Now().Unix()))
		t.Fatalf("failed to write image: %v", err.Error())
	}

	return emoji
}

func TestGetEmojiImage(t *testing.T) {
	th := Setup().InitBasic()
	Client := th.BasicClient

	EnableCustomEmoji := *utils.Cfg.ServiceSettings.EnableCustomEmoji
	RestrictCustomEmojiCreation := *utils.Cfg.ServiceSettings.RestrictCustomEmojiCreation
	defer func() {
		*utils.Cfg.ServiceSettings.EnableCustomEmoji = EnableCustomEmoji
		*utils.Cfg.ServiceSettings.RestrictCustomEmojiCreation = RestrictCustomEmojiCreation
	}()
	*utils.Cfg.ServiceSettings.EnableCustomEmoji = true
	*utils.Cfg.ServiceSettings.RestrictCustomEmojiCreation = model.RESTRICT_EMOJI_CREATION_ALL

	emoji1 := &model.Emoji{
		CreatorId: th.BasicUser.Id,
		Name:      model.NewId(),
	}
	emoji1 = Client.MustGeneric(Client.CreateEmoji(emoji1, createTestGif(t, 10, 10), "image.gif")).(*model.Emoji)
	defer func() { Client.MustGeneric(Client.DeleteEmoji(emoji1.Id)) }()

	*utils.Cfg.ServiceSettings.EnableCustomEmoji = false

	if _, err := Client.DoApiGet(Client.GetCustomEmojiImageUrl(emoji1.Id), "", ""); err == nil {
		t.Fatal("should've failed to get emoji image when disabled")
	}

	*utils.Cfg.ServiceSettings.EnableCustomEmoji = true

	if resp, err := Client.DoApiGet(Client.GetCustomEmojiImageUrl(emoji1.Id), "", ""); err != nil {
		t.Fatal(err)
	} else if resp.Header.Get("Content-Type") != "image/gif" {
		t.Fatal("should've received a gif")
	} else if _, imageType, err := image.DecodeConfig(resp.Body); err != nil {
		t.Fatalf("unable to identify received image: %v", err.Error())
	} else if imageType != "gif" {
		t.Fatal("should've received gif data")
	}

	emoji2 := &model.Emoji{
		CreatorId: th.BasicUser.Id,
		Name:      model.NewId(),
	}
	emoji2 = Client.MustGeneric(Client.CreateEmoji(emoji2, createTestAnimatedGif(t, 10, 10, 10), "image.gif")).(*model.Emoji)
	defer func() { Client.MustGeneric(Client.DeleteEmoji(emoji2.Id)) }()

	if resp, err := Client.DoApiGet(Client.GetCustomEmojiImageUrl(emoji2.Id), "", ""); err != nil {
		t.Fatal(err)
	} else if resp.Header.Get("Content-Type") != "image/gif" {
		t.Fatal("should've received a gif")
	} else if _, imageType, err := image.DecodeConfig(resp.Body); err != nil {
		t.Fatalf("unable to identify received image: %v", err.Error())
	} else if imageType != "gif" {
		t.Fatal("should've received gif data")
	}

	emoji3 := &model.Emoji{
		CreatorId: th.BasicUser.Id,
		Name:      model.NewId(),
	}
	emoji3 = Client.MustGeneric(Client.CreateEmoji(emoji3, createTestJpeg(t, 10, 10), "image.jpeg")).(*model.Emoji)
	defer func() { Client.MustGeneric(Client.DeleteEmoji(emoji3.Id)) }()

	if resp, err := Client.DoApiGet(Client.GetCustomEmojiImageUrl(emoji3.Id), "", ""); err != nil {
		t.Fatal(err)
	} else if resp.Header.Get("Content-Type") != "image/jpeg" {
		t.Fatal("should've received a jpeg")
	} else if _, imageType, err := image.DecodeConfig(resp.Body); err != nil {
		t.Fatalf("unable to identify received image: %v", err.Error())
	} else if imageType != "jpeg" {
		t.Fatal("should've received jpeg data")
	}

	emoji4 := &model.Emoji{
		CreatorId: th.BasicUser.Id,
		Name:      model.NewId(),
	}
	emoji4 = Client.MustGeneric(Client.CreateEmoji(emoji4, createTestPng(t, 10, 10), "image.png")).(*model.Emoji)
	defer func() { Client.MustGeneric(Client.DeleteEmoji(emoji4.Id)) }()

	if resp, err := Client.DoApiGet(Client.GetCustomEmojiImageUrl(emoji4.Id), "", ""); err != nil {
		t.Fatal(err)
	} else if resp.Header.Get("Content-Type") != "image/png" {
		t.Fatal("should've received a png")
	} else if _, imageType, err := image.DecodeConfig(resp.Body); err != nil {
		t.Fatalf("unable to identify received image: %v", err.Error())
	} else if imageType != "png" {
		t.Fatal("should've received png data")
	}

	emoji5 := &model.Emoji{
		CreatorId: th.BasicUser.Id,
		Name:      model.NewId(),
	}
	emoji5 = Client.MustGeneric(Client.CreateEmoji(emoji5, createTestPng(t, 10, 10), "image.png")).(*model.Emoji)
	Client.MustGeneric(Client.DeleteEmoji(emoji5.Id))

	if _, err := Client.DoApiGet(Client.GetCustomEmojiImageUrl(emoji5.Id), "", ""); err == nil {
		t.Fatal("should've failed to get image for deleted emoji")
	}
}