github.com/rr250/mattermost-server@v5.11.1+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/fileutils"
    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(t)
    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(info4.Id)
   101  			th.App.RemoveFile(info4.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 TestUploadFile(t *testing.T) {
   111  	th := Setup(t)
   112  	defer th.TearDown()
   113  
   114  	channelId := model.NewId()
   115  	filename := "test"
   116  	data := []byte("abcd")
   117  
   118  	info1, err := th.App.UploadFile(data, channelId, filename)
   119  	if err != nil {
   120  		t.Fatal(err)
   121  	} else {
   122  		defer func() {
   123  			<-th.App.Srv.Store.FileInfo().PermanentDelete(info1.Id)
   124  			th.App.RemoveFile(info1.Path)
   125  		}()
   126  	}
   127  
   128  	if info1.Path != fmt.Sprintf("%v/teams/noteam/channels/%v/users/nouser/%v/%v",
   129  		time.Now().Format("20060102"), channelId, info1.Id, filename) {
   130  		t.Fatal("stored file at incorrect path", info1.Path)
   131  	}
   132  }
   133  
   134  func TestGetInfoForFilename(t *testing.T) {
   135  	th := Setup(t).InitBasic()
   136  	defer th.TearDown()
   137  
   138  	post := th.BasicPost
   139  	teamId := th.BasicTeam.Id
   140  
   141  	info := th.App.GetInfoForFilename(post, teamId, "sometestfile")
   142  	assert.Nil(t, info, "Test bad filename")
   143  
   144  	info = th.App.GetInfoForFilename(post, teamId, "/somechannel/someuser/someid/somefile.png")
   145  	assert.Nil(t, info, "Test non-existent file")
   146  }
   147  
   148  func TestFindTeamIdForFilename(t *testing.T) {
   149  	th := Setup(t).InitBasic()
   150  	defer th.TearDown()
   151  
   152  	teamId := th.App.FindTeamIdForFilename(th.BasicPost, fmt.Sprintf("/%v/%v/%v/blargh.png", th.BasicChannel.Id, th.BasicUser.Id, "someid"))
   153  	assert.Equal(t, th.BasicTeam.Id, teamId)
   154  
   155  	_, 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)
   156  	require.Nil(t, err)
   157  
   158  	teamId = th.App.FindTeamIdForFilename(th.BasicPost, fmt.Sprintf("/%v/%v/%v/blargh.png", th.BasicChannel.Id, th.BasicUser.Id, "someid"))
   159  	assert.Equal(t, "", teamId)
   160  }
   161  
   162  func TestMigrateFilenamesToFileInfos(t *testing.T) {
   163  	th := Setup(t).InitBasic()
   164  	defer th.TearDown()
   165  
   166  	post := th.BasicPost
   167  	infos := th.App.MigrateFilenamesToFileInfos(post)
   168  	assert.Equal(t, 0, len(infos))
   169  
   170  	post.Filenames = []string{fmt.Sprintf("/%v/%v/%v/blargh.png", th.BasicChannel.Id, th.BasicUser.Id, "someid")}
   171  	infos = th.App.MigrateFilenamesToFileInfos(post)
   172  	assert.Equal(t, 0, len(infos))
   173  
   174  	path, _ := fileutils.FindDir("tests")
   175  	file, fileErr := os.Open(filepath.Join(path, "test.png"))
   176  	require.Nil(t, fileErr)
   177  	defer file.Close()
   178  
   179  	fpath := fmt.Sprintf("/teams/%v/channels/%v/users/%v/%v/test.png", th.BasicTeam.Id, th.BasicChannel.Id, th.BasicUser.Id, "someid")
   180  	_, err := th.App.WriteFile(file, fpath)
   181  	require.Nil(t, err)
   182  	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)
   183  	require.Nil(t, err)
   184  
   185  	infos = th.App.MigrateFilenamesToFileInfos(rpost)
   186  	assert.Equal(t, 1, len(infos))
   187  }
   188  
   189  func TestCopyFileInfos(t *testing.T) {
   190  	th := Setup(t).InitBasic()
   191  	defer th.TearDown()
   192  
   193  	teamId := model.NewId()
   194  	channelId := model.NewId()
   195  	userId := model.NewId()
   196  	filename := "test"
   197  	data := []byte("abcd")
   198  
   199  	info1, err := th.App.DoUploadFile(time.Date(2007, 2, 4, 1, 2, 3, 4, time.Local), teamId, channelId, userId, filename, data)
   200  	require.Nil(t, err)
   201  	defer func() {
   202  		<-th.App.Srv.Store.FileInfo().PermanentDelete(info1.Id)
   203  		th.App.RemoveFile(info1.Path)
   204  	}()
   205  
   206  	infoIds, err := th.App.CopyFileInfos(userId, []string{info1.Id})
   207  	require.Nil(t, err)
   208  
   209  	info2, err := th.App.GetFileInfo(infoIds[0])
   210  	require.Nil(t, err)
   211  	defer func() {
   212  		<-th.App.Srv.Store.FileInfo().PermanentDelete(info2.Id)
   213  		th.App.RemoveFile(info2.Path)
   214  	}()
   215  
   216  	assert.NotEqual(t, info1.Id, info2.Id, "should not be equal")
   217  	assert.Equal(t, info2.PostId, "", "should be empty string")
   218  }