github.com/coincircle/mattermost-server@v4.8.1-0.20180321182714-9d701c704416+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  	"testing"
     9  	"time"
    10  
    11  	"github.com/mattermost/mattermost-server/model"
    12  )
    13  
    14  func TestGeneratePublicLinkHash(t *testing.T) {
    15  	filename1 := model.NewId() + "/" + model.NewRandomString(16) + ".txt"
    16  	filename2 := model.NewId() + "/" + model.NewRandomString(16) + ".txt"
    17  	salt1 := model.NewRandomString(32)
    18  	salt2 := model.NewRandomString(32)
    19  
    20  	hash1 := GeneratePublicLinkHash(filename1, salt1)
    21  	hash2 := GeneratePublicLinkHash(filename2, salt1)
    22  	hash3 := GeneratePublicLinkHash(filename1, salt2)
    23  
    24  	if hash1 != GeneratePublicLinkHash(filename1, salt1) {
    25  		t.Fatal("hash should be equal for the same file name and salt")
    26  	}
    27  
    28  	if hash1 == hash2 {
    29  		t.Fatal("hashes for different files should not be equal")
    30  	}
    31  
    32  	if hash1 == hash3 {
    33  		t.Fatal("hashes for the same file with different salts should not be equal")
    34  	}
    35  }
    36  
    37  func TestDoUploadFile(t *testing.T) {
    38  	th := Setup()
    39  	defer th.TearDown()
    40  
    41  	teamId := model.NewId()
    42  	channelId := model.NewId()
    43  	userId := model.NewId()
    44  	filename := "test"
    45  	data := []byte("abcd")
    46  
    47  	info1, err := th.App.DoUploadFile(time.Date(2007, 2, 4, 1, 2, 3, 4, time.Local), teamId, channelId, userId, filename, data)
    48  	if err != nil {
    49  		t.Fatal(err)
    50  	} else {
    51  		defer func() {
    52  			<-th.App.Srv.Store.FileInfo().PermanentDelete(info1.Id)
    53  			th.App.RemoveFile(info1.Path)
    54  		}()
    55  	}
    56  
    57  	if info1.Path != fmt.Sprintf("20070204/teams/%v/channels/%v/users/%v/%v/%v", teamId, channelId, userId, info1.Id, filename) {
    58  		t.Fatal("stored file at incorrect path", info1.Path)
    59  	}
    60  
    61  	info2, err := th.App.DoUploadFile(time.Date(2007, 2, 4, 1, 2, 3, 4, time.Local), teamId, channelId, userId, filename, data)
    62  	if err != nil {
    63  		t.Fatal(err)
    64  	} else {
    65  		defer func() {
    66  			<-th.App.Srv.Store.FileInfo().PermanentDelete(info2.Id)
    67  			th.App.RemoveFile(info2.Path)
    68  		}()
    69  	}
    70  
    71  	if info2.Path != fmt.Sprintf("20070204/teams/%v/channels/%v/users/%v/%v/%v", teamId, channelId, userId, info2.Id, filename) {
    72  		t.Fatal("stored file at incorrect path", info2.Path)
    73  	}
    74  
    75  	info3, err := th.App.DoUploadFile(time.Date(2008, 3, 5, 1, 2, 3, 4, time.Local), teamId, channelId, userId, filename, data)
    76  	if err != nil {
    77  		t.Fatal(err)
    78  	} else {
    79  		defer func() {
    80  			<-th.App.Srv.Store.FileInfo().PermanentDelete(info3.Id)
    81  			th.App.RemoveFile(info3.Path)
    82  		}()
    83  	}
    84  
    85  	if info3.Path != fmt.Sprintf("20080305/teams/%v/channels/%v/users/%v/%v/%v", teamId, channelId, userId, info3.Id, filename) {
    86  		t.Fatal("stored file at incorrect path", info3.Path)
    87  	}
    88  
    89  	info4, err := th.App.DoUploadFile(time.Date(2009, 3, 5, 1, 2, 3, 4, time.Local), "../../"+teamId, "../../"+channelId, "../../"+userId, "../../"+filename, data)
    90  	if err != nil {
    91  		t.Fatal(err)
    92  	} else {
    93  		defer func() {
    94  			<-th.App.Srv.Store.FileInfo().PermanentDelete(info3.Id)
    95  			th.App.RemoveFile(info3.Path)
    96  		}()
    97  	}
    98  
    99  	if info4.Path != fmt.Sprintf("20090305/teams/%v/channels/%v/users/%v/%v/%v", teamId, channelId, userId, info4.Id, filename) {
   100  		t.Fatal("stored file at incorrect path", info4.Path)
   101  	}
   102  }