github.com/wgh-/mattermost-server@v4.8.0-rc2+incompatible/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 _ "image/gif" 9 _ "image/png" 10 "io/ioutil" 11 "strings" 12 "testing" 13 ) 14 15 func TestFileInfoIsValid(t *testing.T) { 16 info := &FileInfo{ 17 Id: NewId(), 18 CreatorId: NewId(), 19 CreateAt: 1234, 20 UpdateAt: 1234, 21 PostId: "", 22 Path: "fake/path.png", 23 } 24 25 if err := info.IsValid(); err != nil { 26 t.Fatal(err) 27 } 28 29 info.Id = "" 30 if err := info.IsValid(); err == nil { 31 t.Fatal("empty Id isn't valid") 32 } 33 34 info.Id = NewId() 35 info.CreateAt = 0 36 if err := info.IsValid(); err == nil { 37 t.Fatal("empty CreateAt isn't valid") 38 } 39 40 info.CreateAt = 1234 41 info.UpdateAt = 0 42 if err := info.IsValid(); err == nil { 43 t.Fatal("empty UpdateAt isn't valid") 44 } 45 46 info.UpdateAt = 1234 47 info.PostId = NewId() 48 if err := info.IsValid(); err != nil { 49 t.Fatal(err) 50 } 51 52 info.Path = "" 53 if err := info.IsValid(); err == nil { 54 t.Fatal("empty Path isn't valid") 55 } 56 57 info.Path = "fake/path.png" 58 if err := info.IsValid(); err != nil { 59 t.Fatal(err) 60 } 61 } 62 63 func TestFileInfoIsImage(t *testing.T) { 64 info := &FileInfo{ 65 MimeType: "image/png", 66 } 67 68 if !info.IsImage() { 69 t.Fatal("file is an image") 70 } 71 72 info.MimeType = "text/plain" 73 if info.IsImage() { 74 t.Fatal("file is not an image") 75 } 76 } 77 78 func TestGetInfoForFile(t *testing.T) { 79 fakeFile := make([]byte, 1000) 80 81 if info, err := GetInfoForBytes("file.txt", fakeFile); err != nil { 82 t.Fatal(err) 83 } else if info.Name != "file.txt" { 84 t.Fatalf("Got incorrect filename: %v", info.Name) 85 } else if info.Extension != "txt" { 86 t.Fatalf("Got incorrect extension: %v", info.Extension) 87 } else if info.Size != 1000 { 88 t.Fatalf("Got incorrect size: %v", info.Size) 89 } else if !strings.HasPrefix(info.MimeType, "text/plain") { 90 t.Fatalf("Got incorrect mime type: %v", info.MimeType) 91 } else if info.Width != 0 { 92 t.Fatalf("Got incorrect width: %v", info.Width) 93 } else if info.Height != 0 { 94 t.Fatalf("Got incorrect height: %v", info.Height) 95 } else if info.HasPreviewImage { 96 t.Fatalf("Got incorrect has preview image: %v", info.HasPreviewImage) 97 } 98 99 pngFile, err := ioutil.ReadFile("../tests/test.png") 100 if err != nil { 101 t.Fatalf("Failed to load test.png: %v", err.Error()) 102 } 103 if info, err := GetInfoForBytes("test.png", pngFile); err != nil { 104 t.Fatal(err) 105 } else if info.Name != "test.png" { 106 t.Fatalf("Got incorrect filename: %v", info.Name) 107 } else if info.Extension != "png" { 108 t.Fatalf("Got incorrect extension: %v", info.Extension) 109 } else if info.Size != 279591 { 110 t.Fatalf("Got incorrect size: %v", info.Size) 111 } else if info.MimeType != "image/png" { 112 t.Fatalf("Got incorrect mime type: %v", info.MimeType) 113 } else if info.Width != 408 { 114 t.Fatalf("Got incorrect width: %v", info.Width) 115 } else if info.Height != 336 { 116 t.Fatalf("Got incorrect height: %v", info.Height) 117 } else if !info.HasPreviewImage { 118 t.Fatalf("Got incorrect has preview image: %v", info.HasPreviewImage) 119 } 120 121 // base 64 encoded version of handtinywhite.gif from http://probablyprogramming.com/2009/03/15/the-tiniest-gif-ever 122 gifFile, _ := base64.StdEncoding.DecodeString("R0lGODlhAQABAIABAP///wAAACwAAAAAAQABAAACAkQBADs=") 123 if info, err := GetInfoForBytes("handtinywhite.gif", gifFile); err != nil { 124 t.Fatal(err) 125 } else if info.Name != "handtinywhite.gif" { 126 t.Fatalf("Got incorrect filename: %v", info.Name) 127 } else if info.Extension != "gif" { 128 t.Fatalf("Got incorrect extension: %v", info.Extension) 129 } else if info.Size != 35 { 130 t.Fatalf("Got incorrect size: %v", info.Size) 131 } else if info.MimeType != "image/gif" { 132 t.Fatalf("Got incorrect mime type: %v", info.MimeType) 133 } else if info.Width != 1 { 134 t.Fatalf("Got incorrect width: %v", info.Width) 135 } else if info.Height != 1 { 136 t.Fatalf("Got incorrect height: %v", info.Height) 137 } else if !info.HasPreviewImage { 138 t.Fatalf("Got incorrect has preview image: %v", info.HasPreviewImage) 139 } 140 141 animatedGifFile, err := ioutil.ReadFile("../tests/testgif.gif") 142 if err != nil { 143 t.Fatalf("Failed to load testgif.gif: %v", err.Error()) 144 } 145 if info, err := GetInfoForBytes("testgif.gif", animatedGifFile); err != nil { 146 t.Fatal(err) 147 } else if info.Name != "testgif.gif" { 148 t.Fatalf("Got incorrect filename: %v", info.Name) 149 } else if info.Extension != "gif" { 150 t.Fatalf("Got incorrect extension: %v", info.Extension) 151 } else if info.Size != 38689 { 152 t.Fatalf("Got incorrect size: %v", info.Size) 153 } else if info.MimeType != "image/gif" { 154 t.Fatalf("Got incorrect mime type: %v", info.MimeType) 155 } else if info.Width != 118 { 156 t.Fatalf("Got incorrect width: %v", info.Width) 157 } else if info.Height != 118 { 158 t.Fatalf("Got incorrect height: %v", info.Height) 159 } else if info.HasPreviewImage { 160 t.Fatalf("Got incorrect has preview image: %v", info.HasPreviewImage) 161 } 162 163 if info, err := GetInfoForBytes("filewithoutextension", fakeFile); err != nil { 164 t.Fatal(err) 165 } else if info.Name != "filewithoutextension" { 166 t.Fatalf("Got incorrect filename: %v", info.Name) 167 } else if info.Extension != "" { 168 t.Fatalf("Got incorrect extension: %v", info.Extension) 169 } else if info.Size != 1000 { 170 t.Fatalf("Got incorrect size: %v", info.Size) 171 } else if info.MimeType != "" { 172 t.Fatalf("Got incorrect mime type: %v", info.MimeType) 173 } else if info.Width != 0 { 174 t.Fatalf("Got incorrect width: %v", info.Width) 175 } else if info.Height != 0 { 176 t.Fatalf("Got incorrect height: %v", info.Height) 177 } else if info.HasPreviewImage { 178 t.Fatalf("Got incorrect has preview image: %v", info.HasPreviewImage) 179 } 180 181 // Always make the extension lower case to make it easier to use in other places 182 if info, err := GetInfoForBytes("file.TXT", fakeFile); err != nil { 183 t.Fatal(err) 184 } else if info.Name != "file.TXT" { 185 t.Fatalf("Got incorrect filename: %v", info.Name) 186 } else if info.Extension != "txt" { 187 t.Fatalf("Got incorrect extension: %v", info.Extension) 188 } 189 190 // Don't error out for image formats we don't support 191 if info, err := GetInfoForBytes("file.tif", fakeFile); err != nil { 192 t.Fatal(err) 193 } else if info.Name != "file.tif" { 194 t.Fatalf("Got incorrect filename: %v", info.Name) 195 } else if info.Extension != "tif" { 196 t.Fatalf("Got incorrect extension: %v", info.Extension) 197 } else if info.MimeType != "image/tiff" && info.MimeType != "image/x-tiff" { 198 t.Fatalf("Got incorrect mime type: %v", info.MimeType) 199 } 200 }