github.com/jlevesy/mattermost-server@v5.3.2-0.20181003190404-7468f35cb0c8+incompatible/app/file_test.go (about) 1 // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package app 5 6 import ( 7 "fmt" 8 "os" 9 "path/filepath" 10 "testing" 11 "time" 12 13 "github.com/stretchr/testify/assert" 14 "github.com/stretchr/testify/require" 15 16 "github.com/mattermost/mattermost-server/model" 17 "github.com/mattermost/mattermost-server/utils" 18 ) 19 20 func TestGeneratePublicLinkHash(t *testing.T) { 21 filename1 := model.NewId() + "/" + model.NewRandomString(16) + ".txt" 22 filename2 := model.NewId() + "/" + model.NewRandomString(16) + ".txt" 23 salt1 := model.NewRandomString(32) 24 salt2 := model.NewRandomString(32) 25 26 hash1 := GeneratePublicLinkHash(filename1, salt1) 27 hash2 := GeneratePublicLinkHash(filename2, salt1) 28 hash3 := GeneratePublicLinkHash(filename1, salt2) 29 30 if hash1 != GeneratePublicLinkHash(filename1, salt1) { 31 t.Fatal("hash should be equal for the same file name and salt") 32 } 33 34 if hash1 == hash2 { 35 t.Fatal("hashes for different files should not be equal") 36 } 37 38 if hash1 == hash3 { 39 t.Fatal("hashes for the same file with different salts should not be equal") 40 } 41 } 42 43 func TestDoUploadFile(t *testing.T) { 44 th := Setup() 45 defer th.TearDown() 46 47 teamId := model.NewId() 48 channelId := model.NewId() 49 userId := model.NewId() 50 filename := "test" 51 data := []byte("abcd") 52 53 info1, 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(info1.Id) 59 th.App.RemoveFile(info1.Path) 60 }() 61 } 62 63 if info1.Path != fmt.Sprintf("20070204/teams/%v/channels/%v/users/%v/%v/%v", teamId, channelId, userId, info1.Id, filename) { 64 t.Fatal("stored file at incorrect path", info1.Path) 65 } 66 67 info2, err := th.App.DoUploadFile(time.Date(2007, 2, 4, 1, 2, 3, 4, time.Local), teamId, channelId, userId, filename, data) 68 if err != nil { 69 t.Fatal(err) 70 } else { 71 defer func() { 72 <-th.App.Srv.Store.FileInfo().PermanentDelete(info2.Id) 73 th.App.RemoveFile(info2.Path) 74 }() 75 } 76 77 if info2.Path != fmt.Sprintf("20070204/teams/%v/channels/%v/users/%v/%v/%v", teamId, channelId, userId, info2.Id, filename) { 78 t.Fatal("stored file at incorrect path", info2.Path) 79 } 80 81 info3, err := th.App.DoUploadFile(time.Date(2008, 3, 5, 1, 2, 3, 4, time.Local), teamId, channelId, userId, filename, data) 82 if err != nil { 83 t.Fatal(err) 84 } else { 85 defer func() { 86 <-th.App.Srv.Store.FileInfo().PermanentDelete(info3.Id) 87 th.App.RemoveFile(info3.Path) 88 }() 89 } 90 91 if info3.Path != fmt.Sprintf("20080305/teams/%v/channels/%v/users/%v/%v/%v", teamId, channelId, userId, info3.Id, filename) { 92 t.Fatal("stored file at incorrect path", info3.Path) 93 } 94 95 info4, err := th.App.DoUploadFile(time.Date(2009, 3, 5, 1, 2, 3, 4, time.Local), "../../"+teamId, "../../"+channelId, "../../"+userId, "../../"+filename, data) 96 if err != nil { 97 t.Fatal(err) 98 } else { 99 defer func() { 100 <-th.App.Srv.Store.FileInfo().PermanentDelete(info3.Id) 101 th.App.RemoveFile(info3.Path) 102 }() 103 } 104 105 if info4.Path != fmt.Sprintf("20090305/teams/%v/channels/%v/users/%v/%v/%v", teamId, channelId, userId, info4.Id, filename) { 106 t.Fatal("stored file at incorrect path", info4.Path) 107 } 108 } 109 110 func TestGetInfoForFilename(t *testing.T) { 111 th := Setup().InitBasic() 112 defer th.TearDown() 113 114 post := th.BasicPost 115 teamId := th.BasicTeam.Id 116 117 info := th.App.GetInfoForFilename(post, teamId, "sometestfile") 118 assert.Nil(t, info, "Test bad filename") 119 120 info = th.App.GetInfoForFilename(post, teamId, "/somechannel/someuser/someid/somefile.png") 121 assert.Nil(t, info, "Test non-existent file") 122 } 123 124 func TestFindTeamIdForFilename(t *testing.T) { 125 th := Setup().InitBasic() 126 defer th.TearDown() 127 128 teamId := th.App.FindTeamIdForFilename(th.BasicPost, fmt.Sprintf("/%v/%v/%v/blargh.png", th.BasicChannel.Id, th.BasicUser.Id, "someid")) 129 assert.Equal(t, th.BasicTeam.Id, teamId) 130 131 _, 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) 132 require.Nil(t, err) 133 134 teamId = th.App.FindTeamIdForFilename(th.BasicPost, fmt.Sprintf("/%v/%v/%v/blargh.png", th.BasicChannel.Id, th.BasicUser.Id, "someid")) 135 assert.Equal(t, "", teamId) 136 } 137 138 func TestMigrateFilenamesToFileInfos(t *testing.T) { 139 th := Setup().InitBasic() 140 defer th.TearDown() 141 142 post := th.BasicPost 143 infos := th.App.MigrateFilenamesToFileInfos(post) 144 assert.Equal(t, 0, len(infos)) 145 146 post.Filenames = []string{fmt.Sprintf("/%v/%v/%v/blargh.png", th.BasicChannel.Id, th.BasicUser.Id, "someid")} 147 infos = th.App.MigrateFilenamesToFileInfos(post) 148 assert.Equal(t, 0, len(infos)) 149 150 path, _ := utils.FindDir("tests") 151 file, fileErr := os.Open(filepath.Join(path, "test.png")) 152 require.Nil(t, fileErr) 153 defer file.Close() 154 155 fpath := fmt.Sprintf("/teams/%v/channels/%v/users/%v/%v/test.png", th.BasicTeam.Id, th.BasicChannel.Id, th.BasicUser.Id, "someid") 156 _, err := th.App.WriteFile(file, fpath) 157 require.Nil(t, err) 158 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) 159 require.Nil(t, err) 160 161 infos = th.App.MigrateFilenamesToFileInfos(rpost) 162 assert.Equal(t, 1, len(infos)) 163 } 164 165 func TestCopyFileInfos(t *testing.T) { 166 th := Setup().InitBasic() 167 defer th.TearDown() 168 169 teamId := model.NewId() 170 channelId := model.NewId() 171 userId := model.NewId() 172 filename := "test" 173 data := []byte("abcd") 174 175 info1, err := th.App.DoUploadFile(time.Date(2007, 2, 4, 1, 2, 3, 4, time.Local), teamId, channelId, userId, filename, data) 176 require.Nil(t, err) 177 defer func() { 178 <-th.App.Srv.Store.FileInfo().PermanentDelete(info1.Id) 179 th.App.RemoveFile(info1.Path) 180 }() 181 182 infoIds, err := th.App.CopyFileInfos(userId, []string{info1.Id}) 183 require.Nil(t, err) 184 185 info2, err := th.App.GetFileInfo(infoIds[0]) 186 require.Nil(t, err) 187 defer func() { 188 <-th.App.Srv.Store.FileInfo().PermanentDelete(info2.Id) 189 th.App.RemoveFile(info2.Path) 190 }() 191 192 assert.NotEqual(t, info1.Id, info2.Id, "should not be equal") 193 assert.Equal(t, info2.PostId, "", "should be empty string") 194 }