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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package api
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strings"
"testing"
"time"
"github.com/mattermost/platform/app"
"github.com/mattermost/platform/model"
"github.com/mattermost/platform/store"
"github.com/mattermost/platform/utils"
s3 "github.com/minio/minio-go"
"github.com/minio/minio-go/pkg/credentials"
)
func TestUploadFile(t *testing.T) {
th := Setup().InitBasic()
if utils.Cfg.FileSettings.DriverName == "" {
t.Logf("skipping because no file driver is enabled")
return
}
Client := th.BasicClient
team := th.BasicTeam
user := th.BasicUser
channel := th.BasicChannel
var uploadInfo *model.FileInfo
if data, err := readTestFile("test.png"); err != nil {
t.Fatal(err)
} else if resp, err := Client.UploadPostAttachment(data, channel.Id, "test.png"); err != nil {
t.Fatal(err)
} else if len(resp.FileInfos) != 1 {
t.Fatal("should've returned a single file infos")
} else {
uploadInfo = resp.FileInfos[0]
}
// The returned file info from the upload call will be missing some fields that will be stored in the database
if uploadInfo.CreatorId != user.Id {
t.Fatal("file should be assigned to user")
} else if uploadInfo.PostId != "" {
t.Fatal("file shouldn't have a post")
} else if uploadInfo.Path != "" {
t.Fatal("file path should not be set on returned info")
} else if uploadInfo.ThumbnailPath != "" {
t.Fatal("file thumbnail path should not be set on returned info")
} else if uploadInfo.PreviewPath != "" {
t.Fatal("file preview path should not be set on returned info")
}
var info *model.FileInfo
if result := <-app.Srv.Store.FileInfo().Get(uploadInfo.Id); result.Err != nil {
t.Fatal(result.Err)
} else {
info = result.Data.(*model.FileInfo)
}
if info.Id != uploadInfo.Id {
t.Fatal("file id from response should match one stored in database")
} else if info.CreatorId != user.Id {
t.Fatal("file should be assigned to user")
} else if info.PostId != "" {
t.Fatal("file shouldn't have a post")
} else if info.Path == "" {
t.Fatal("file path should be set in database")
} else if info.ThumbnailPath == "" {
t.Fatal("file thumbnail path should be set in database")
} else if info.PreviewPath == "" {
t.Fatal("file preview path should be set in database")
}
// This also makes sure that the relative path provided above is sanitized out
expectedPath := fmt.Sprintf("teams/%v/channels/%v/users/%v/%v/test.png", team.Id, channel.Id, user.Id, info.Id)
if info.Path != expectedPath {
t.Logf("file is saved in %v", info.Path)
t.Fatalf("file should've been saved in %v", expectedPath)
}
expectedThumbnailPath := fmt.Sprintf("teams/%v/channels/%v/users/%v/%v/test_thumb.jpg", team.Id, channel.Id, user.Id, info.Id)
if info.ThumbnailPath != expectedThumbnailPath {
t.Logf("file thumbnail is saved in %v", info.ThumbnailPath)
t.Fatalf("file thumbnail should've been saved in %v", expectedThumbnailPath)
}
expectedPreviewPath := fmt.Sprintf("teams/%v/channels/%v/users/%v/%v/test_preview.jpg", team.Id, channel.Id, user.Id, info.Id)
if info.PreviewPath != expectedPreviewPath {
t.Logf("file preview is saved in %v", info.PreviewPath)
t.Fatalf("file preview should've been saved in %v", expectedPreviewPath)
}
enableFileAttachments := *utils.Cfg.FileSettings.EnableFileAttachments
defer func() {
*utils.Cfg.FileSettings.EnableFileAttachments = enableFileAttachments
}()
*utils.Cfg.FileSettings.EnableFileAttachments = false
if data, err := readTestFile("test.png"); err != nil {
t.Fatal(err)
} else if _, err = Client.UploadPostAttachment(data, channel.Id, "test.png"); err == nil {
t.Fatal("should have errored")
}
// Wait a bit for files to ready
time.Sleep(2 * time.Second)
if err := cleanupTestFile(info); err != nil {
t.Fatal(err)
}
}
func TestGetFileInfo(t *testing.T) {
th := Setup().InitBasic()
if utils.Cfg.FileSettings.DriverName == "" {
t.Skip("skipping because no file driver is enabled")
}
Client := th.BasicClient
user := th.BasicUser
channel := th.BasicChannel
var fileId string
if data, err := readTestFile("test.png"); err != nil {
t.Fatal(err)
} else {
fileId = Client.MustGeneric(Client.UploadPostAttachment(data, channel.Id, "test.png")).(*model.FileUploadResponse).FileInfos[0].Id
}
info, err := Client.GetFileInfo(fileId)
if err != nil {
t.Fatal(err)
} else if info.Id != fileId {
t.Fatal("got incorrect file")
} else if info.CreatorId != user.Id {
t.Fatal("file should be assigned to user")
} else if info.PostId != "" {
t.Fatal("file shouldn't have a post")
} else if info.Path != "" {
t.Fatal("file path shouldn't have been returned to client")
} else if info.ThumbnailPath != "" {
t.Fatal("file thumbnail path shouldn't have been returned to client")
} else if info.PreviewPath != "" {
t.Fatal("file preview path shouldn't have been returned to client")
} else if info.MimeType != "image/png" {
t.Fatal("mime type should've been image/png")
}
// Wait a bit for files to ready
time.Sleep(2 * time.Second)
// Other user shouldn't be able to get file info for this file before it's attached to a post
th.LoginBasic2()
if _, err := Client.GetFileInfo(fileId); err == nil {
t.Fatal("other user shouldn't be able to get file info before it's attached to a post")
}
// Hacky way to assign file to a post (usually would be done by CreatePost call)
store.Must(app.Srv.Store.FileInfo().AttachToPost(fileId, th.BasicPost.Id))
// Other user shouldn't be able to get file info for this file if they're not in the channel for it
if _, err := Client.GetFileInfo(fileId); err == nil {
t.Fatal("other user shouldn't be able to get file info when not in channel")
}
Client.Must(Client.JoinChannel(channel.Id))
// Other user should now be able to get file info
if info2, err := Client.GetFileInfo(fileId); err != nil {
t.Fatal(err)
} else if info2.Id != fileId {
t.Fatal("other user got incorrect file")
}
if err := cleanupTestFile(store.Must(app.Srv.Store.FileInfo().Get(fileId)).(*model.FileInfo)); err != nil {
t.Fatal(err)
}
}
func TestGetFile(t *testing.T) {
th := Setup().InitBasic()
if utils.Cfg.FileSettings.DriverName == "" {
t.Skip("skipping because no file driver is enabled")
}
Client := th.BasicClient
channel := th.BasicChannel
var fileId string
data, err := readTestFile("test.png")
if err != nil {
t.Fatal(err)
} else {
fileId = Client.MustGeneric(Client.UploadPostAttachment(data, channel.Id, "test.png")).(*model.FileUploadResponse).FileInfos[0].Id
}
// Wait a bit for files to ready
time.Sleep(2 * time.Second)
if body, err := Client.GetFile(fileId); err != nil {
t.Fatal(err)
} else {
received, err := ioutil.ReadAll(body)
if err != nil {
t.Fatal(err)
} else if len(received) != len(data) {
t.Fatal("received file should be the same size as the sent one")
}
for i := range data {
if data[i] != received[i] {
t.Fatal("received file didn't match sent one")
}
}
body.Close()
}
// Other user shouldn't be able to get file for this file before it's attached to a post
th.LoginBasic2()
if _, err := Client.GetFile(fileId); err == nil {
t.Fatal("other user shouldn't be able to get file before it's attached to a post")
}
// Hacky way to assign file to a post (usually would be done by CreatePost call)
store.Must(app.Srv.Store.FileInfo().AttachToPost(fileId, th.BasicPost.Id))
// Other user shouldn't be able to get file for this file if they're not in the channel for it
if _, err := Client.GetFile(fileId); err == nil {
t.Fatal("other user shouldn't be able to get file when not in channel")
}
Client.Must(Client.JoinChannel(channel.Id))
// Other user should now be able to get file
if body, err := Client.GetFile(fileId); err != nil {
t.Fatal(err)
} else {
received, err := ioutil.ReadAll(body)
if err != nil {
t.Fatal(err)
} else if len(received) != len(data) {
t.Fatal("received file should be the same size as the sent one")
}
for i := range data {
if data[i] != received[i] {
t.Fatal("received file didn't match sent one")
}
}
body.Close()
}
if err := cleanupTestFile(store.Must(app.Srv.Store.FileInfo().Get(fileId)).(*model.FileInfo)); err != nil {
t.Fatal(err)
}
}
func TestGetFileThumbnail(t *testing.T) {
th := Setup().InitBasic()
if utils.Cfg.FileSettings.DriverName == "" {
t.Skip("skipping because no file driver is enabled")
}
Client := th.BasicClient
channel := th.BasicChannel
var fileId string
data, err := readTestFile("test.png")
if err != nil {
t.Fatal(err)
} else {
fileId = Client.MustGeneric(Client.UploadPostAttachment(data, channel.Id, "test.png")).(*model.FileUploadResponse).FileInfos[0].Id
}
// Wait a bit for files to ready
time.Sleep(2 * time.Second)
if body, err := Client.GetFileThumbnail(fileId); err != nil {
t.Fatal(err)
} else {
body.Close()
}
// Other user shouldn't be able to get thumbnail for this file before it's attached to a post
th.LoginBasic2()
if _, err := Client.GetFileThumbnail(fileId); err == nil {
t.Fatal("other user shouldn't be able to get file before it's attached to a post")
}
// Hacky way to assign file to a post (usually would be done by CreatePost call)
store.Must(app.Srv.Store.FileInfo().AttachToPost(fileId, th.BasicPost.Id))
// Other user shouldn't be able to get thumbnail for this file if they're not in the channel for it
if _, err := Client.GetFileThumbnail(fileId); err == nil {
t.Fatal("other user shouldn't be able to get file when not in channel")
}
Client.Must(Client.JoinChannel(channel.Id))
// Other user should now be able to get thumbnail
if body, err := Client.GetFileThumbnail(fileId); err != nil {
t.Fatal(err)
} else {
body.Close()
}
if err := cleanupTestFile(store.Must(app.Srv.Store.FileInfo().Get(fileId)).(*model.FileInfo)); err != nil {
t.Fatal(err)
}
}
func TestGetFilePreview(t *testing.T) {
th := Setup().InitBasic()
if utils.Cfg.FileSettings.DriverName == "" {
t.Skip("skipping because no file driver is enabled")
}
Client := th.BasicClient
channel := th.BasicChannel
var fileId string
data, err := readTestFile("test.png")
if err != nil {
t.Fatal(err)
} else {
fileId = Client.MustGeneric(Client.UploadPostAttachment(data, channel.Id, "test.png")).(*model.FileUploadResponse).FileInfos[0].Id
}
// Wait a bit for files to ready
time.Sleep(2 * time.Second)
if body, err := Client.GetFilePreview(fileId); err != nil {
t.Fatal(err)
} else {
body.Close()
}
// Other user shouldn't be able to get preview for this file before it's attached to a post
th.LoginBasic2()
if _, err := Client.GetFilePreview(fileId); err == nil {
t.Fatal("other user shouldn't be able to get file before it's attached to a post")
}
// Hacky way to assign file to a post (usually would be done by CreatePost call)
store.Must(app.Srv.Store.FileInfo().AttachToPost(fileId, th.BasicPost.Id))
// Other user shouldn't be able to get preview for this file if they're not in the channel for it
if _, err := Client.GetFilePreview(fileId); err == nil {
t.Fatal("other user shouldn't be able to get file when not in channel")
}
Client.Must(Client.JoinChannel(channel.Id))
// Other user should now be able to get preview
if body, err := Client.GetFilePreview(fileId); err != nil {
t.Fatal(err)
} else {
body.Close()
}
if err := cleanupTestFile(store.Must(app.Srv.Store.FileInfo().Get(fileId)).(*model.FileInfo)); err != nil {
t.Fatal(err)
}
}
func TestGetPublicFile(t *testing.T) {
th := Setup().InitBasic()
if utils.Cfg.FileSettings.DriverName == "" {
t.Skip("skipping because no file driver is enabled")
}
enablePublicLink := utils.Cfg.FileSettings.EnablePublicLink
publicLinkSalt := *utils.Cfg.FileSettings.PublicLinkSalt
defer func() {
utils.Cfg.FileSettings.EnablePublicLink = enablePublicLink
*utils.Cfg.FileSettings.PublicLinkSalt = publicLinkSalt
}()
utils.Cfg.FileSettings.EnablePublicLink = true
*utils.Cfg.FileSettings.PublicLinkSalt = model.NewId()
Client := th.BasicClient
channel := th.BasicChannel
var fileId string
data, err := readTestFile("test.png")
if err != nil {
t.Fatal(err)
} else {
fileId = Client.MustGeneric(Client.UploadPostAttachment(data, channel.Id, "test.png")).(*model.FileUploadResponse).FileInfos[0].Id
}
// Hacky way to assign file to a post (usually would be done by CreatePost call)
store.Must(app.Srv.Store.FileInfo().AttachToPost(fileId, th.BasicPost.Id))
link := Client.MustGeneric(Client.GetPublicLink(fileId)).(string)
// Wait a bit for files to ready
time.Sleep(2 * time.Second)
if resp, err := http.Get(link); err != nil || resp.StatusCode != http.StatusOK {
t.Log(link)
t.Fatal("failed to get image with public link", err)
}
if resp, err := http.Get(link[:strings.LastIndex(link, "?")]); err == nil && resp.StatusCode != http.StatusBadRequest {
t.Fatal("should've failed to get image with public link without hash", resp.Status)
}
utils.Cfg.FileSettings.EnablePublicLink = false
if resp, err := http.Get(link); err == nil && resp.StatusCode != http.StatusNotImplemented {
t.Fatal("should've failed to get image with disabled public link")
}
utils.Cfg.FileSettings.EnablePublicLink = true
// test after the salt has changed
*utils.Cfg.FileSettings.PublicLinkSalt = model.NewId()
if resp, err := http.Get(link); err == nil && resp.StatusCode != http.StatusBadRequest {
t.Fatal("should've failed to get image with public link after salt changed")
}
if resp, err := http.Get(link); err == nil && resp.StatusCode != http.StatusBadRequest {
t.Fatal("should've failed to get image with public link after salt changed")
}
if err := cleanupTestFile(store.Must(app.Srv.Store.FileInfo().Get(fileId)).(*model.FileInfo)); err != nil {
t.Fatal(err)
}
}
func TestGetPublicFileOld(t *testing.T) {
th := Setup().InitBasic()
if utils.Cfg.FileSettings.DriverName == "" {
t.Skip("skipping because no file driver is enabled")
}
enablePublicLink := utils.Cfg.FileSettings.EnablePublicLink
publicLinkSalt := *utils.Cfg.FileSettings.PublicLinkSalt
defer func() {
utils.Cfg.FileSettings.EnablePublicLink = enablePublicLink
*utils.Cfg.FileSettings.PublicLinkSalt = publicLinkSalt
}()
utils.Cfg.FileSettings.EnablePublicLink = true
*utils.Cfg.FileSettings.PublicLinkSalt = model.NewId()
Client := th.BasicClient
channel := th.BasicChannel
var fileId string
data, err := readTestFile("test.png")
if err != nil {
t.Fatal(err)
} else {
fileId = Client.MustGeneric(Client.UploadPostAttachment(data, channel.Id, "test.png")).(*model.FileUploadResponse).FileInfos[0].Id
}
// Hacky way to assign file to a post (usually would be done by CreatePost call)
store.Must(app.Srv.Store.FileInfo().AttachToPost(fileId, th.BasicPost.Id))
// reconstruct old style of link
siteURL := *utils.Cfg.ServiceSettings.SiteURL
if siteURL == "" {
siteURL = "http://localhost" + utils.Cfg.ServiceSettings.ListenAddress
}
link := generatePublicLinkOld(siteURL, th.BasicTeam.Id, channel.Id, th.BasicUser.Id, fileId+"/test.png")
// Wait a bit for files to ready
time.Sleep(2 * time.Second)
if resp, err := http.Get(link); err != nil || resp.StatusCode != http.StatusOK {
t.Fatalf("failed to get image with public link err=%v resp=%v", err, resp)
}
if resp, err := http.Get(link[:strings.LastIndex(link, "?")]); err == nil && resp.StatusCode != http.StatusBadRequest {
t.Fatal("should've failed to get image with public link without hash", resp.Status)
}
utils.Cfg.FileSettings.EnablePublicLink = false
if resp, err := http.Get(link); err == nil && resp.StatusCode != http.StatusNotImplemented {
t.Fatal("should've failed to get image with disabled public link")
}
utils.Cfg.FileSettings.EnablePublicLink = true
// test after the salt has changed
*utils.Cfg.FileSettings.PublicLinkSalt = model.NewId()
if resp, err := http.Get(link); err == nil && resp.StatusCode != http.StatusBadRequest {
t.Fatal("should've failed to get image with public link after salt changed")
}
if resp, err := http.Get(link); err == nil && resp.StatusCode != http.StatusBadRequest {
t.Fatal("should've failed to get image with public link after salt changed")
}
if err := cleanupTestFile(store.Must(app.Srv.Store.FileInfo().Get(fileId)).(*model.FileInfo)); err != nil {
t.Fatal(err)
}
}
func generatePublicLinkOld(siteURL, teamId, channelId, userId, filename string) string {
hash := app.GeneratePublicLinkHash(filename, *utils.Cfg.FileSettings.PublicLinkSalt)
return fmt.Sprintf("%s%s/public/files/get/%s/%s/%s/%s?h=%s", siteURL, model.API_URL_SUFFIX_V3, teamId, channelId, userId, filename, hash)
}
func TestGetPublicLink(t *testing.T) {
th := Setup().InitBasic()
if utils.Cfg.FileSettings.DriverName == "" {
t.Skip("skipping because no file driver is enabled")
}
enablePublicLink := utils.Cfg.FileSettings.EnablePublicLink
defer func() {
utils.Cfg.FileSettings.EnablePublicLink = enablePublicLink
}()
utils.Cfg.FileSettings.EnablePublicLink = true
Client := th.BasicClient
channel := th.BasicChannel
var fileId string
data, err := readTestFile("test.png")
if err != nil {
t.Fatal(err)
} else {
fileId = Client.MustGeneric(Client.UploadPostAttachment(data, channel.Id, "test.png")).(*model.FileUploadResponse).FileInfos[0].Id
}
if _, err := Client.GetPublicLink(fileId); err == nil {
t.Fatal("should've failed to get public link before file is attached to a post")
}
// Hacky way to assign file to a post (usually would be done by CreatePost call)
store.Must(app.Srv.Store.FileInfo().AttachToPost(fileId, th.BasicPost.Id))
utils.Cfg.FileSettings.EnablePublicLink = false
if _, err := Client.GetPublicLink(fileId); err == nil {
t.Fatal("should've failed to get public link when disabled")
}
utils.Cfg.FileSettings.EnablePublicLink = true
if link, err := Client.GetPublicLink(fileId); err != nil {
t.Fatal(err)
} else if link == "" {
t.Fatal("should've received public link")
}
// Other user shouldn't be able to get public link for this file if they're not in the channel for it
th.LoginBasic2()
if _, err := Client.GetPublicLink(fileId); err == nil {
t.Fatal("other user shouldn't be able to get file when not in channel")
}
Client.Must(Client.JoinChannel(channel.Id))
// Other user should now be able to get public link
if link, err := Client.GetPublicLink(fileId); err != nil {
t.Fatal(err)
} else if link == "" {
t.Fatal("should've received public link")
}
// Wait a bit for files to ready
time.Sleep(2 * time.Second)
if err := cleanupTestFile(store.Must(app.Srv.Store.FileInfo().Get(fileId)).(*model.FileInfo)); err != nil {
t.Fatal(err)
}
}
func TestMigrateFilenamesToFileInfos(t *testing.T) {
th := Setup().InitBasic()
if utils.Cfg.FileSettings.DriverName == "" {
t.Skip("skipping because no file driver is enabled")
}
Client := th.BasicClient
user1 := th.BasicUser
channel1 := Client.Must(Client.CreateChannel(&model.Channel{
Name: model.NewId(),
Type: model.CHANNEL_OPEN,
// No TeamId set to simulate a direct channel
})).Data.(*model.Channel)
var fileId1 string
var fileId2 string
data, err := readTestFile("test.png")
if err != nil {
t.Fatal(err)
} else {
fileId1 = Client.MustGeneric(Client.UploadPostAttachment(data, channel1.Id, "test.png")).(*model.FileUploadResponse).FileInfos[0].Id
fileId2 = Client.MustGeneric(Client.UploadPostAttachment(data, channel1.Id, "test.png")).(*model.FileUploadResponse).FileInfos[0].Id
}
// Bypass the Client whenever possible since we're trying to simulate a pre-3.5 post
post1 := store.Must(app.Srv.Store.Post().Save(&model.Post{
UserId: user1.Id,
ChannelId: channel1.Id,
Message: "test",
Filenames: []string{
fmt.Sprintf("/%s/%s/%s/%s", channel1.Id, user1.Id, fileId1, "test.png"),
fmt.Sprintf("/%s/%s/%s/%s", channel1.Id, user1.Id, fileId2, "test.png"),
fmt.Sprintf("/%s/%s/%s/%s", channel1.Id, user1.Id, fileId2, "test.png"), // duplicate a filename to recreate a rare bug
},
})).(*model.Post)
if post1.FileIds != nil && len(post1.FileIds) > 0 {
t.Fatal("post shouldn't have file ids")
} else if post1.Filenames == nil || len(post1.Filenames) != 3 {
t.Fatal("post should have filenames")
}
// Indirectly call migrateFilenamesToFileInfos by calling Client.GetFileInfosForPost
var infos []*model.FileInfo
if infosResult, err := Client.GetFileInfosForPost(post1.ChannelId, post1.Id, ""); err != nil {
t.Fatal(err)
} else {
infos = infosResult
}
if len(infos) != 2 {
t.Log(infos)
t.Fatal("should've had 2 infos after migration")
} else if infos[0].Path != "" || infos[0].ThumbnailPath != "" || infos[0].PreviewPath != "" {
t.Fatal("shouldn't return paths to client")
}
// Should be able to get files after migration
if body, err := Client.GetFile(infos[0].Id); err != nil {
t.Fatal(err)
} else {
body.Close()
}
if body, err := Client.GetFile(infos[1].Id); err != nil {
t.Fatal(err)
} else {
body.Close()
}
// Make sure we aren't generating a new set of FileInfos on a second call to GetFileInfosForPost
if infos2 := Client.MustGeneric(Client.GetFileInfosForPost(post1.ChannelId, post1.Id, "")).([]*model.FileInfo); len(infos2) != len(infos) {
t.Fatal("should've received the same 2 infos after second call")
} else if (infos[0].Id != infos2[0].Id && infos[0].Id != infos2[1].Id) || (infos[1].Id != infos2[0].Id && infos[1].Id != infos2[1].Id) {
t.Fatal("should've returned the exact same 2 infos after second call")
}
if result, err := Client.GetPost(post1.ChannelId, post1.Id, ""); err != nil {
t.Fatal(err)
} else if post := result.Data.(*model.PostList).Posts[post1.Id]; len(post.Filenames) != 0 {
t.Fatal("post shouldn't have filenames")
} else if len(post.FileIds) != 2 {
t.Fatal("post should have 2 file ids")
} else if (infos[0].Id != post.FileIds[0] && infos[0].Id != post.FileIds[1]) || (infos[1].Id != post.FileIds[0] && infos[1].Id != post.FileIds[1]) {
t.Fatal("post file ids should match GetFileInfosForPost results")
}
}
func TestFindTeamIdForFilename(t *testing.T) {
th := Setup().InitBasic()
if utils.Cfg.FileSettings.DriverName == "" {
t.Skip("skipping because no file driver is enabled")
}
Client := th.BasicClient
user1 := th.BasicUser
team1 := th.BasicTeam
team2 := th.CreateTeam(th.BasicClient)
channel1 := th.BasicChannel
Client.SetTeamId(team2.Id)
channel2 := Client.Must(Client.CreateChannel(&model.Channel{
Name: model.NewId(),
Type: model.CHANNEL_OPEN,
// No TeamId set to simulate a direct channel
})).Data.(*model.Channel)
Client.SetTeamId(team1.Id)
var fileId1 string
var fileId2 string
data, err := readTestFile("test.png")
if err != nil {
t.Fatal(err)
} else {
fileId1 = Client.MustGeneric(Client.UploadPostAttachment(data, channel1.Id, "test.png")).(*model.FileUploadResponse).FileInfos[0].Id
Client.SetTeamId(team2.Id)
fileId2 = Client.MustGeneric(Client.UploadPostAttachment(data, channel2.Id, "test.png")).(*model.FileUploadResponse).FileInfos[0].Id
Client.SetTeamId(team1.Id)
}
// Bypass the Client whenever possible since we're trying to simulate a pre-3.5 post
post1 := store.Must(app.Srv.Store.Post().Save(&model.Post{
UserId: user1.Id,
ChannelId: channel1.Id,
Message: "test",
Filenames: []string{fmt.Sprintf("/%s/%s/%s/%s", channel1.Id, user1.Id, fileId1, "test.png")},
})).(*model.Post)
if teamId := app.FindTeamIdForFilename(post1, post1.Filenames[0]); teamId != team1.Id {
t.Fatal("file should've been found under team1")
}
Client.SetTeamId(team2.Id)
post2 := store.Must(app.Srv.Store.Post().Save(&model.Post{
UserId: user1.Id,
ChannelId: channel2.Id,
Message: "test",
Filenames: []string{fmt.Sprintf("/%s/%s/%s/%s", channel2.Id, user1.Id, fileId2, "test.png")},
})).(*model.Post)
Client.SetTeamId(team1.Id)
if teamId := app.FindTeamIdForFilename(post2, post2.Filenames[0]); teamId != team2.Id {
t.Fatal("file should've been found under team2")
}
}
func TestGetInfoForFilename(t *testing.T) {
th := Setup().InitBasic()
if utils.Cfg.FileSettings.DriverName == "" {
t.Skip("skipping because no file driver is enabled")
}
Client := th.BasicClient
user1 := th.BasicUser
team1 := th.BasicTeam
channel1 := th.BasicChannel
var fileId1 string
var path string
var thumbnailPath string
var previewPath string
data, err := readTestFile("test.png")
if err != nil {
t.Fatal(err)
} else {
fileId1 = Client.MustGeneric(Client.UploadPostAttachment(data, channel1.Id, "test.png")).(*model.FileUploadResponse).FileInfos[0].Id
path = store.Must(app.Srv.Store.FileInfo().Get(fileId1)).(*model.FileInfo).Path
thumbnailPath = store.Must(app.Srv.Store.FileInfo().Get(fileId1)).(*model.FileInfo).ThumbnailPath
previewPath = store.Must(app.Srv.Store.FileInfo().Get(fileId1)).(*model.FileInfo).PreviewPath
}
// Bypass the Client whenever possible since we're trying to simulate a pre-3.5 post
post1 := store.Must(app.Srv.Store.Post().Save(&model.Post{
UserId: user1.Id,
ChannelId: channel1.Id,
Message: "test",
Filenames: []string{fmt.Sprintf("/%s/%s/%s/%s", channel1.Id, user1.Id, fileId1, "test.png")},
})).(*model.Post)
if info := app.GetInfoForFilename(post1, team1.Id, post1.Filenames[0]); info == nil {
t.Fatal("info shouldn't be nil")
} else if info.Id == "" {
t.Fatal("info.Id shouldn't be empty")
} else if info.CreatorId != user1.Id {
t.Fatal("incorrect user id")
} else if info.PostId != post1.Id {
t.Fatal("incorrect user id")
} else if info.Path != path {
t.Fatal("incorrect path")
} else if info.ThumbnailPath != thumbnailPath {
t.Fatal("incorrect thumbnail path")
} else if info.PreviewPath != previewPath {
t.Fatal("incorrect preview path")
} else if info.Name != "test.png" {
t.Fatal("incorrect name")
}
}
func readTestFile(name string) ([]byte, error) {
path, _ := utils.FindDir("tests")
file, err := os.Open(path + "/" + name)
if err != nil {
return nil, err
}
defer file.Close()
data := &bytes.Buffer{}
if _, err := io.Copy(data, file); err != nil {
return nil, err
} else {
return data.Bytes(), nil
}
}
// Similar to s3.New() but allows initialization of signature v2 or signature v4 client.
// If signV2 input is false, function always returns signature v4.
//
// Additionally this function also takes a user defined region, if set
// disables automatic region lookup.
func s3New(endpoint, accessKey, secretKey string, secure bool, signV2 bool, region string) (*s3.Client, error) {
var creds *credentials.Credentials
if signV2 {
creds = credentials.NewStatic(accessKey, secretKey, "", credentials.SignatureV2)
} else {
creds = credentials.NewStatic(accessKey, secretKey, "", credentials.SignatureV4)
}
return s3.NewWithCredentials(endpoint, creds, secure, region)
}
func cleanupTestFile(info *model.FileInfo) error {
if utils.Cfg.FileSettings.DriverName == model.IMAGE_DRIVER_S3 {
endpoint := utils.Cfg.FileSettings.AmazonS3Endpoint
accessKey := utils.Cfg.FileSettings.AmazonS3AccessKeyId
secretKey := utils.Cfg.FileSettings.AmazonS3SecretAccessKey
secure := *utils.Cfg.FileSettings.AmazonS3SSL
signV2 := *utils.Cfg.FileSettings.AmazonS3SignV2
region := utils.Cfg.FileSettings.AmazonS3Region
s3Clnt, err := s3New(endpoint, accessKey, secretKey, secure, signV2, region)
if err != nil {
return err
}
bucket := utils.Cfg.FileSettings.AmazonS3Bucket
if err := s3Clnt.RemoveObject(bucket, info.Path); err != nil {
return err
}
if info.ThumbnailPath != "" {
if err := s3Clnt.RemoveObject(bucket, info.ThumbnailPath); err != nil {
return err
}
}
if info.PreviewPath != "" {
if err := s3Clnt.RemoveObject(bucket, info.PreviewPath); err != nil {
return err
}
}
} else if utils.Cfg.FileSettings.DriverName == model.IMAGE_DRIVER_LOCAL {
if err := os.Remove(utils.Cfg.FileSettings.Directory + info.Path); err != nil {
return err
}
if info.ThumbnailPath != "" {
if err := os.Remove(utils.Cfg.FileSettings.Directory + info.ThumbnailPath); err != nil {
return err
}
}
if info.PreviewPath != "" {
if err := os.Remove(utils.Cfg.FileSettings.Directory + info.PreviewPath); err != nil {
return err
}
}
}
return nil
}
|