PageRenderTime 185ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/app/file_test.go

https://gitlab.com/unofficial-mirrors/mattermost-platform
Go | 163 lines | 128 code | 33 blank | 2 comment | 35 complexity | 77713eebb2068c9af72e37a1c4b80e17 MD5 | raw file
  1. // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
  2. // See License.txt for license information.
  3. package app
  4. import (
  5. "fmt"
  6. "os"
  7. "path/filepath"
  8. "testing"
  9. "time"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/require"
  12. "github.com/mattermost/mattermost-server/model"
  13. "github.com/mattermost/mattermost-server/utils"
  14. )
  15. func TestGeneratePublicLinkHash(t *testing.T) {
  16. filename1 := model.NewId() + "/" + model.NewRandomString(16) + ".txt"
  17. filename2 := model.NewId() + "/" + model.NewRandomString(16) + ".txt"
  18. salt1 := model.NewRandomString(32)
  19. salt2 := model.NewRandomString(32)
  20. hash1 := GeneratePublicLinkHash(filename1, salt1)
  21. hash2 := GeneratePublicLinkHash(filename2, salt1)
  22. hash3 := GeneratePublicLinkHash(filename1, salt2)
  23. if hash1 != GeneratePublicLinkHash(filename1, salt1) {
  24. t.Fatal("hash should be equal for the same file name and salt")
  25. }
  26. if hash1 == hash2 {
  27. t.Fatal("hashes for different files should not be equal")
  28. }
  29. if hash1 == hash3 {
  30. t.Fatal("hashes for the same file with different salts should not be equal")
  31. }
  32. }
  33. func TestDoUploadFile(t *testing.T) {
  34. th := Setup()
  35. defer th.TearDown()
  36. teamId := model.NewId()
  37. channelId := model.NewId()
  38. userId := model.NewId()
  39. filename := "test"
  40. data := []byte("abcd")
  41. info1, err := th.App.DoUploadFile(time.Date(2007, 2, 4, 1, 2, 3, 4, time.Local), teamId, channelId, userId, filename, data)
  42. if err != nil {
  43. t.Fatal(err)
  44. } else {
  45. defer func() {
  46. <-th.App.Srv.Store.FileInfo().PermanentDelete(info1.Id)
  47. th.App.RemoveFile(info1.Path)
  48. }()
  49. }
  50. if info1.Path != fmt.Sprintf("20070204/teams/%v/channels/%v/users/%v/%v/%v", teamId, channelId, userId, info1.Id, filename) {
  51. t.Fatal("stored file at incorrect path", info1.Path)
  52. }
  53. info2, err := th.App.DoUploadFile(time.Date(2007, 2, 4, 1, 2, 3, 4, time.Local), teamId, channelId, userId, filename, data)
  54. if err != nil {
  55. t.Fatal(err)
  56. } else {
  57. defer func() {
  58. <-th.App.Srv.Store.FileInfo().PermanentDelete(info2.Id)
  59. th.App.RemoveFile(info2.Path)
  60. }()
  61. }
  62. if info2.Path != fmt.Sprintf("20070204/teams/%v/channels/%v/users/%v/%v/%v", teamId, channelId, userId, info2.Id, filename) {
  63. t.Fatal("stored file at incorrect path", info2.Path)
  64. }
  65. info3, err := th.App.DoUploadFile(time.Date(2008, 3, 5, 1, 2, 3, 4, time.Local), teamId, channelId, userId, filename, data)
  66. if err != nil {
  67. t.Fatal(err)
  68. } else {
  69. defer func() {
  70. <-th.App.Srv.Store.FileInfo().PermanentDelete(info3.Id)
  71. th.App.RemoveFile(info3.Path)
  72. }()
  73. }
  74. if info3.Path != fmt.Sprintf("20080305/teams/%v/channels/%v/users/%v/%v/%v", teamId, channelId, userId, info3.Id, filename) {
  75. t.Fatal("stored file at incorrect path", info3.Path)
  76. }
  77. info4, err := th.App.DoUploadFile(time.Date(2009, 3, 5, 1, 2, 3, 4, time.Local), "../../"+teamId, "../../"+channelId, "../../"+userId, "../../"+filename, data)
  78. if err != nil {
  79. t.Fatal(err)
  80. } else {
  81. defer func() {
  82. <-th.App.Srv.Store.FileInfo().PermanentDelete(info3.Id)
  83. th.App.RemoveFile(info3.Path)
  84. }()
  85. }
  86. if info4.Path != fmt.Sprintf("20090305/teams/%v/channels/%v/users/%v/%v/%v", teamId, channelId, userId, info4.Id, filename) {
  87. t.Fatal("stored file at incorrect path", info4.Path)
  88. }
  89. }
  90. func TestGetInfoForFilename(t *testing.T) {
  91. th := Setup().InitBasic()
  92. defer th.TearDown()
  93. post := th.BasicPost
  94. teamId := th.BasicTeam.Id
  95. info := th.App.GetInfoForFilename(post, teamId, "sometestfile")
  96. assert.Nil(t, info, "Test bad filename")
  97. info = th.App.GetInfoForFilename(post, teamId, "/somechannel/someuser/someid/somefile.png")
  98. assert.Nil(t, info, "Test non-existent file")
  99. }
  100. func TestFindTeamIdForFilename(t *testing.T) {
  101. th := Setup().InitBasic()
  102. defer th.TearDown()
  103. teamId := th.App.FindTeamIdForFilename(th.BasicPost, fmt.Sprintf("/%v/%v/%v/blargh.png", th.BasicChannel.Id, th.BasicUser.Id, "someid"))
  104. assert.Equal(t, th.BasicTeam.Id, teamId)
  105. _, err := th.App.CreateTeamWithUser(&model.Team{Email: th.BasicUser.Email, Name: "zz" + model.NewId(), DisplayName: "Joram's Test Team", Type: model.TEAM_OPEN}, th.BasicUser.Id)
  106. require.Nil(t, err)
  107. teamId = th.App.FindTeamIdForFilename(th.BasicPost, fmt.Sprintf("/%v/%v/%v/blargh.png", th.BasicChannel.Id, th.BasicUser.Id, "someid"))
  108. assert.Equal(t, "", teamId)
  109. }
  110. func TestMigrateFilenamesToFileInfos(t *testing.T) {
  111. th := Setup().InitBasic()
  112. defer th.TearDown()
  113. post := th.BasicPost
  114. infos := th.App.MigrateFilenamesToFileInfos(post)
  115. assert.Equal(t, 0, len(infos))
  116. post.Filenames = []string{fmt.Sprintf("/%v/%v/%v/blargh.png", th.BasicChannel.Id, th.BasicUser.Id, "someid")}
  117. infos = th.App.MigrateFilenamesToFileInfos(post)
  118. assert.Equal(t, 0, len(infos))
  119. path, _ := utils.FindDir("tests")
  120. file, fileErr := os.Open(filepath.Join(path, "test.png"))
  121. require.Nil(t, fileErr)
  122. defer file.Close()
  123. fpath := fmt.Sprintf("/teams/%v/channels/%v/users/%v/%v/test.png", th.BasicTeam.Id, th.BasicChannel.Id, th.BasicUser.Id, "someid")
  124. _, err := th.App.WriteFile(file, fpath)
  125. require.Nil(t, err)
  126. rpost, err := th.App.CreatePost(&model.Post{UserId: th.BasicUser.Id, ChannelId: th.BasicChannel.Id, Filenames: []string{fmt.Sprintf("/%v/%v/%v/test.png", th.BasicChannel.Id, th.BasicUser.Id, "someid")}}, th.BasicChannel, false)
  127. require.Nil(t, err)
  128. infos = th.App.MigrateFilenamesToFileInfos(rpost)
  129. assert.Equal(t, 1, len(infos))
  130. }