github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/model/file_info_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package model 5 6 import ( 7 "encoding/base64" 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 _ "image/gif" 11 _ "image/png" 12 "io/ioutil" 13 "strings" 14 "testing" 15 ) 16 17 func TestFileInfoIsValid(t *testing.T) { 18 info := &FileInfo{ 19 Id: NewId(), 20 CreatorId: NewId(), 21 CreateAt: 1234, 22 UpdateAt: 1234, 23 PostId: "", 24 Path: "fake/path.png", 25 } 26 27 t.Run("Valid File Info", func(t *testing.T) { 28 assert.Nil(t, info.IsValid()) 29 }) 30 31 t.Run("Empty ID is not valid", func(t *testing.T) { 32 info.Id = "" 33 assert.NotNil(t, info.IsValid(), "empty Id isn't valid") 34 info.Id = NewId() 35 }) 36 37 t.Run("CreateAt 0 is not valid", func(t *testing.T) { 38 info.CreateAt = 0 39 assert.NotNil(t, info.IsValid(), "empty CreateAt isn't valid") 40 info.CreateAt = 1234 41 }) 42 43 t.Run("UpdateAt 0 is not valid", func(t *testing.T) { 44 info.UpdateAt = 0 45 assert.NotNil(t, info.IsValid(), "empty UpdateAt isn't valid") 46 info.UpdateAt = 1234 47 }) 48 49 t.Run("New Post ID is valid", func(t *testing.T) { 50 info.PostId = NewId() 51 assert.Nil(t, info.IsValid()) 52 }) 53 54 t.Run("Empty path is not valid", func(t *testing.T) { 55 info.Path = "" 56 assert.NotNil(t, info.IsValid(), "empty Path isn't valid") 57 info.Path = "fake/path.png" 58 }) 59 } 60 61 func TestFileInfoIsImage(t *testing.T) { 62 info := &FileInfo{} 63 t.Run("MimeType set to image/png is considered an image", func(t *testing.T) { 64 info.MimeType = "image/png" 65 assert.True(t, info.IsImage(), "PNG file should be considered as an image") 66 }) 67 68 t.Run("MimeType set to text/plain is not considered an image", func(t *testing.T) { 69 info.MimeType = "text/plain" 70 assert.False(t, info.IsImage(), "Text file should not be considered as an image") 71 }) 72 } 73 74 func TestGetInfoForFile(t *testing.T) { 75 fakeFile := make([]byte, 1000) 76 77 pngFile, err := ioutil.ReadFile("../tests/test.png") 78 require.Nilf(t, err, "Failed to load test.png") 79 80 // base 64 encoded version of handtinywhite.gif from http://probablyprogramming.com/2009/03/15/the-tiniest-gif-ever 81 gifFile, _ := base64.StdEncoding.DecodeString("R0lGODlhAQABAIABAP///wAAACwAAAAAAQABAAACAkQBADs=") 82 83 animatedGifFile, err := ioutil.ReadFile("../tests/testgif.gif") 84 require.Nilf(t, err, "Failed to load testgif.gif") 85 86 var ttc = []struct { 87 testName string 88 filename string 89 file []byte 90 usePrefixForMime bool 91 expectedExtension string 92 expectedSize int 93 expectedMime string 94 expectedWidth int 95 expectedHeight int 96 expectedHasPreviewImage bool 97 }{ 98 { 99 testName: "Text File", 100 filename: "file.txt", 101 file: fakeFile, 102 usePrefixForMime: true, 103 expectedExtension: "txt", 104 expectedSize: 1000, 105 expectedMime: "text/plain", 106 expectedWidth: 0, 107 expectedHeight: 0, 108 expectedHasPreviewImage: false, 109 }, 110 { 111 testName: "PNG file", 112 filename: "test.png", 113 file: pngFile, 114 usePrefixForMime: false, 115 expectedExtension: "png", 116 expectedSize: 279591, 117 expectedMime: "image/png", 118 expectedWidth: 408, 119 expectedHeight: 336, 120 expectedHasPreviewImage: true, 121 }, 122 { 123 testName: "Static Gif File", 124 filename: "handtinywhite.gif", 125 file: gifFile, 126 usePrefixForMime: false, 127 expectedExtension: "gif", 128 expectedSize: 35, 129 expectedMime: "image/gif", 130 expectedWidth: 1, 131 expectedHeight: 1, 132 expectedHasPreviewImage: true, 133 }, 134 { 135 testName: "Animated Gif File", 136 filename: "testgif.gif", 137 file: animatedGifFile, 138 usePrefixForMime: false, 139 expectedExtension: "gif", 140 expectedSize: 38689, 141 expectedMime: "image/gif", 142 expectedWidth: 118, 143 expectedHeight: 118, 144 expectedHasPreviewImage: false, 145 }, 146 { 147 testName: "No extension File", 148 filename: "filewithoutextension", 149 file: fakeFile, 150 usePrefixForMime: false, 151 expectedExtension: "", 152 expectedSize: 1000, 153 expectedMime: "", 154 expectedWidth: 0, 155 expectedHeight: 0, 156 expectedHasPreviewImage: false, 157 }, 158 { 159 // Always make the extension lower case to make it easier to use in other places 160 testName: "Uppercase extension File", 161 filename: "file.TXT", 162 file: fakeFile, 163 usePrefixForMime: true, 164 expectedExtension: "txt", 165 expectedSize: 1000, 166 expectedMime: "text/plain", 167 expectedWidth: 0, 168 expectedHeight: 0, 169 expectedHasPreviewImage: false, 170 }, 171 { 172 // Don't error out for image formats we don't support 173 testName: "Not supported File", 174 filename: "file.tif", 175 file: fakeFile, 176 usePrefixForMime: false, 177 expectedExtension: "tif", 178 expectedSize: 1000, 179 expectedMime: "image/tiff", 180 expectedWidth: 0, 181 expectedHeight: 0, 182 expectedHasPreviewImage: false, 183 }, 184 } 185 186 for _, tc := range ttc { 187 t.Run(tc.testName, func(t *testing.T) { 188 info, errApp := GetInfoForBytes(tc.filename, tc.file) 189 require.Nil(t, errApp) 190 191 assert.Equalf(t, tc.filename, info.Name, "Got incorrect filename: %v", info.Name) 192 assert.Equalf(t, tc.expectedExtension, info.Extension, "Got incorrect extension: %v", info.Extension) 193 assert.EqualValuesf(t, tc.expectedSize, info.Size, "Got incorrect size: %v", info.Size) 194 assert.Equalf(t, tc.expectedWidth, info.Width, "Got incorrect width: %v", info.Width) 195 assert.Equalf(t, tc.expectedHeight, info.Height, "Got incorrect height: %v", info.Height) 196 assert.Equalf(t, tc.expectedHasPreviewImage, info.HasPreviewImage, "Got incorrect has preview image: %v", info.HasPreviewImage) 197 198 if tc.usePrefixForMime { 199 assert.Truef(t, strings.HasPrefix(info.MimeType, tc.expectedMime), "Got incorrect mime type: %v", info.MimeType) 200 } else { 201 assert.Equalf(t, tc.expectedMime, info.MimeType, "Got incorrect mime type: %v", info.MimeType) 202 } 203 }) 204 } 205 }