Changes tests to use testify.require at api4/file_test.go fixe… (#12814)

Этот коммит содержится в:
Maurício Linhares
2019-10-23 08:45:15 -04:00
коммит произвёл Ben Schumacher
родитель a96565671d
Коммит 23aa193f28
2 изменённых файлов: 88 добавлений и 145 удалений

Просмотреть файл

@@ -5,6 +5,7 @@ package api4
import ( import (
"fmt" "fmt"
"github.com/stretchr/testify/require"
"io/ioutil" "io/ioutil"
"net" "net"
"net/http" "net/http"
@@ -673,6 +674,10 @@ func CheckErrorMessage(t *testing.T, resp *model.Response, errorId string) {
} }
} }
func CheckStartsWith(t *testing.T, value, prefix, message string) {
require.True(t, strings.HasPrefix(value, prefix), message, value)
}
// Similar to s3.New() but allows initialization of signature v2 or signature v4 client. // Similar to s3.New() but allows initialization of signature v2 or signature v4 client.
// If signV2 input is false, function always returns signature v4. // If signV2 input is false, function always returns signature v4.
// //

Просмотреть файл

@@ -40,26 +40,20 @@ func escapeQuotes(s string) string {
return quoteEscaper.Replace(s) return quoteEscaper.Replace(s)
} }
func randomBytes(n int) []byte { func randomBytes(t *testing.T, n int) []byte {
bb := make([]byte, n) bb := make([]byte, n)
_, err := rand.Read(bb) _, err := rand.Read(bb)
if err != nil { require.NoError(t, err)
panic(err.Error())
}
return bb return bb
} }
func fileBytes(path string) []byte { func fileBytes(t *testing.T, path string) []byte {
path = filepath.Join(testDir, path) path = filepath.Join(testDir, path)
f, err := os.Open(path) f, err := os.Open(path)
if err != nil { require.NoError(t, err)
panic(err.Error())
}
defer f.Close() defer f.Close()
bb, err := ioutil.ReadAll(f) bb, err := ioutil.ReadAll(f)
if err != nil { require.NoError(t, err)
panic(err.Error())
}
return bb return bb
} }
@@ -183,7 +177,7 @@ func testUploadFilesMultipart(
require.Nil(t, err) require.Nil(t, err)
} }
mw.Close() require.NoError(t, mw.Close())
return testDoUploadFileRequest(t, c, "", mwBody.Bytes(), mw.FormDataContentType(), -1) return testDoUploadFileRequest(t, c, "", mwBody.Bytes(), mw.FormDataContentType(), -1)
} }
@@ -251,7 +245,7 @@ func TestUploadFiles(t *testing.T) {
{ {
title: "Happy invalid image", title: "Happy invalid image",
names: []string{"testgif.gif"}, names: []string{"testgif.gif"},
blobs: [][]byte{fileBytes("test-search.md")}, blobs: [][]byte{fileBytes(t, "test-search.md")},
skipPayloadValidation: true, skipPayloadValidation: true,
expectedCreatorId: th.BasicUser.Id, expectedCreatorId: th.BasicUser.Id,
}, },
@@ -382,7 +376,7 @@ func TestUploadFiles(t *testing.T) {
useChunkedInSimplePost: true, useChunkedInSimplePost: true,
skipPayloadValidation: true, skipPayloadValidation: true,
names: []string{"1Mb-stream"}, names: []string{"1Mb-stream"},
blobs: [][]byte{randomBytes(1024 * 1024)}, blobs: [][]byte{randomBytes(t, 1024*1024)},
setupConfig: func(a *app.App) func(a *app.App) { setupConfig: func(a *app.App) func(a *app.App) {
maxFileSize := *a.Config().FileSettings.MaxFileSize maxFileSize := *a.Config().FileSettings.MaxFileSize
a.UpdateConfig(func(cfg *model.Config) { *cfg.FileSettings.MaxFileSize = 1024 * 1024 }) a.UpdateConfig(func(cfg *model.Config) { *cfg.FileSettings.MaxFileSize = 1024 * 1024 })
@@ -480,7 +474,7 @@ func TestUploadFiles(t *testing.T) {
title: "Error stream too large", title: "Error stream too large",
skipPayloadValidation: true, skipPayloadValidation: true,
names: []string{"1Mb-stream"}, names: []string{"1Mb-stream"},
blobs: [][]byte{randomBytes(1024 * 1024)}, blobs: [][]byte{randomBytes(t, 1024*1024)},
skipSuccessValidation: true, skipSuccessValidation: true,
checkResponse: CheckRequestEntityTooLargeStatus, checkResponse: CheckRequestEntityTooLargeStatus,
setupConfig: func(a *app.App) func(a *app.App) { setupConfig: func(a *app.App) func(a *app.App) {
@@ -532,7 +526,7 @@ func TestUploadFiles(t *testing.T) {
blobs := tc.blobs blobs := tc.blobs
if len(blobs) == 0 { if len(blobs) == 0 {
for _, name := range tc.names { for _, name := range tc.names {
blobs = append(blobs, fileBytes(name)) blobs = append(blobs, fileBytes(t, name))
} }
} }
@@ -555,9 +549,9 @@ func TestUploadFiles(t *testing.T) {
return return
} }
if fileResp == nil || len(fileResp.FileInfos) == 0 || len(fileResp.FileInfos) != len(tc.names) { require.NotNil(t, fileResp, "Nil fileResp")
t.Fatal("Empty or mismatched actual or expected FileInfos") require.NotEqual(t, 0, len(fileResp.FileInfos), "Empty FileInfos")
} require.Equal(t, len(tc.names), len(fileResp.FileInfos), "Mismatched actual or expected FileInfos")
for i, ri := range fileResp.FileInfos { for i, ri := range fileResp.FileInfos {
// The returned file info from the upload call will be missing some fields that will be stored in the database // The returned file info from the upload call will be missing some fields that will be stored in the database
@@ -653,29 +647,20 @@ func TestGetFile(t *testing.T) {
t.Skip("skipping because no file driver is enabled") t.Skip("skipping because no file driver is enabled")
} }
fileId := "" sent, err := testutils.ReadTestFile("test.png")
var sent []byte require.NoError(t, err)
var err error
if sent, err = testutils.ReadTestFile("test.png"); err != nil {
t.Fatal(err)
} else {
fileResp, resp := Client.UploadFile(sent, channel.Id, "test.png")
CheckNoError(t, resp)
fileId = fileResp.FileInfos[0].Id fileResp, resp := Client.UploadFile(sent, channel.Id, "test.png")
} CheckNoError(t, resp)
fileId := fileResp.FileInfos[0].Id
data, resp := Client.GetFile(fileId) data, resp := Client.GetFile(fileId)
CheckNoError(t, resp) CheckNoError(t, resp)
require.NotEqual(t, 0, len(data), "should not be empty")
if len(data) == 0 {
t.Fatal("should not be empty")
}
for i := range data { for i := range data {
if data[i] != sent[i] { require.Equal(t, sent[i], data[i], "received file didn't match sent one")
t.Fatal("received file didn't match sent one")
}
} }
_, resp = Client.GetFile("junk") _, resp = Client.GetFile("junk")
@@ -713,30 +698,19 @@ func TestGetFileHeaders(t *testing.T) {
_, resp = Client.GetFile(fileId) _, resp = Client.GetFile(fileId)
CheckNoError(t, resp) CheckNoError(t, resp)
if contentType := resp.Header.Get("Content-Type"); !strings.HasPrefix(contentType, expectedContentType) { CheckStartsWith(t, resp.Header.Get("Content-Type"), expectedContentType, "returned incorrect Content-Type")
t.Fatal("returned incorrect Content-Type", contentType)
}
if getInline { if getInline {
if contentDisposition := resp.Header.Get("Content-Disposition"); !strings.HasPrefix(contentDisposition, "inline") { CheckStartsWith(t, resp.Header.Get("Content-Disposition"), "inline", "returned incorrect Content-Disposition")
t.Fatal("returned incorrect Content-Disposition", contentDisposition)
}
} else { } else {
if contentDisposition := resp.Header.Get("Content-Disposition"); !strings.HasPrefix(contentDisposition, "attachment") { CheckStartsWith(t, resp.Header.Get("Content-Disposition"), "attachment", "returned incorrect Content-Disposition")
t.Fatal("returned incorrect Content-Disposition", contentDisposition)
}
} }
_, resp = Client.DownloadFile(fileId, true) _, resp = Client.DownloadFile(fileId, true)
CheckNoError(t, resp) CheckNoError(t, resp)
if contentType := resp.Header.Get("Content-Type"); !strings.HasPrefix(contentType, expectedContentType) { CheckStartsWith(t, resp.Header.Get("Content-Type"), expectedContentType, "returned incorrect Content-Type")
t.Fatal("returned incorrect Content-Type", contentType) CheckStartsWith(t, resp.Header.Get("Content-Disposition"), "attachment", "returned incorrect Content-Disposition")
}
if contentDisposition := resp.Header.Get("Content-Disposition"); !strings.HasPrefix(contentDisposition, "attachment") {
t.Fatal("returned incorrect Content-Disposition", contentDisposition)
}
} }
} }
@@ -768,27 +742,20 @@ func TestGetFileThumbnail(t *testing.T) {
t.Skip("skipping because no file driver is enabled") t.Skip("skipping because no file driver is enabled")
} }
fileId := "" sent, err := testutils.ReadTestFile("test.png")
var sent []byte require.NoError(t, err)
var err error
if sent, err = testutils.ReadTestFile("test.png"); err != nil {
t.Fatal(err)
} else {
fileResp, resp := Client.UploadFile(sent, channel.Id, "test.png")
CheckNoError(t, resp)
fileId = fileResp.FileInfos[0].Id fileResp, resp := Client.UploadFile(sent, channel.Id, "test.png")
} CheckNoError(t, resp)
fileId := fileResp.FileInfos[0].Id
// Wait a bit for files to ready // Wait a bit for files to ready
time.Sleep(2 * time.Second) time.Sleep(2 * time.Second)
data, resp := Client.GetFileThumbnail(fileId) data, resp := Client.GetFileThumbnail(fileId)
CheckNoError(t, resp) CheckNoError(t, resp)
require.NotEqual(t, 0, len(data), "should not be empty")
if len(data) == 0 {
t.Fatal("should not be empty")
}
_, resp = Client.GetFileThumbnail("junk") _, resp = Client.GetFileThumbnail("junk")
CheckBadRequestStatus(t, resp) CheckBadRequestStatus(t, resp)
@@ -823,21 +790,19 @@ func TestGetFileLink(t *testing.T) {
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.FileSettings.EnablePublicLink = true }) th.App.UpdateConfig(func(cfg *model.Config) { *cfg.FileSettings.EnablePublicLink = true })
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.FileSettings.PublicLinkSalt = model.NewRandomString(32) }) th.App.UpdateConfig(func(cfg *model.Config) { *cfg.FileSettings.PublicLinkSalt = model.NewRandomString(32) })
fileId := "" data, err := testutils.ReadTestFile("test.png")
if data, err := testutils.ReadTestFile("test.png"); err != nil { require.NoError(t, err)
t.Fatal(err)
} else {
fileResp, resp := Client.UploadFile(data, channel.Id, "test.png")
CheckNoError(t, resp)
fileId = fileResp.FileInfos[0].Id fileResp, uploadResp := Client.UploadFile(data, channel.Id, "test.png")
} CheckNoError(t, uploadResp)
fileId := fileResp.FileInfos[0].Id
_, resp := Client.GetFileLink(fileId) _, resp := Client.GetFileLink(fileId)
CheckBadRequestStatus(t, resp) CheckBadRequestStatus(t, resp)
// Hacky way to assign file to a post (usually would be done by CreatePost call) // Hacky way to assign file to a post (usually would be done by CreatePost call)
err := th.App.Srv.Store.FileInfo().AttachToPost(fileId, th.BasicPost.Id, th.BasicUser.Id) err = th.App.Srv.Store.FileInfo().AttachToPost(fileId, th.BasicPost.Id, th.BasicUser.Id)
require.Nil(t, err) require.Nil(t, err)
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.FileSettings.EnablePublicLink = false }) th.App.UpdateConfig(func(cfg *model.Config) { *cfg.FileSettings.EnablePublicLink = false })
@@ -850,10 +815,7 @@ func TestGetFileLink(t *testing.T) {
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.FileSettings.EnablePublicLink = true }) th.App.UpdateConfig(func(cfg *model.Config) { *cfg.FileSettings.EnablePublicLink = true })
link, resp := Client.GetFileLink(fileId) link, resp := Client.GetFileLink(fileId)
CheckNoError(t, resp) CheckNoError(t, resp)
require.NotEqual(t, "", link, "should've received public link")
if link == "" {
t.Fatal("should've received public link")
}
_, resp = Client.GetFileLink("junk") _, resp = Client.GetFileLink("junk")
CheckBadRequestStatus(t, resp) CheckBadRequestStatus(t, resp)
@@ -889,27 +851,19 @@ func TestGetFilePreview(t *testing.T) {
t.Skip("skipping because no file driver is enabled") t.Skip("skipping because no file driver is enabled")
} }
fileId := "" sent, err := testutils.ReadTestFile("test.png")
var sent []byte require.NoError(t, err)
var err error
if sent, err = testutils.ReadTestFile("test.png"); err != nil {
t.Fatal(err)
} else {
fileResp, resp := Client.UploadFile(sent, channel.Id, "test.png")
CheckNoError(t, resp)
fileId = fileResp.FileInfos[0].Id fileResp, resp := Client.UploadFile(sent, channel.Id, "test.png")
} CheckNoError(t, resp)
fileId := fileResp.FileInfos[0].Id
// Wait a bit for files to ready // Wait a bit for files to ready
time.Sleep(2 * time.Second) time.Sleep(2 * time.Second)
data, resp := Client.GetFilePreview(fileId) data, resp := Client.GetFilePreview(fileId)
CheckNoError(t, resp) CheckNoError(t, resp)
require.NotEqual(t, 0, len(data), "should not be empty")
if len(data) == 0 {
t.Fatal("should not be empty")
}
_, resp = Client.GetFilePreview("junk") _, resp = Client.GetFilePreview("junk")
CheckBadRequestStatus(t, resp) CheckBadRequestStatus(t, resp)
@@ -942,17 +896,12 @@ func TestGetFileInfo(t *testing.T) {
t.Skip("skipping because no file driver is enabled") t.Skip("skipping because no file driver is enabled")
} }
fileId := "" sent, err := testutils.ReadTestFile("test.png")
var sent []byte require.NoError(t, err)
var err error
if sent, err = testutils.ReadTestFile("test.png"); err != nil {
t.Fatal(err)
} else {
fileResp, resp := Client.UploadFile(sent, channel.Id, "test.png")
CheckNoError(t, resp)
fileId = fileResp.FileInfos[0].Id fileResp, resp := Client.UploadFile(sent, channel.Id, "test.png")
} CheckNoError(t, resp)
fileId := fileResp.FileInfos[0].Id
// Wait a bit for files to ready // Wait a bit for files to ready
time.Sleep(2 * time.Second) time.Sleep(2 * time.Second)
@@ -960,23 +909,14 @@ func TestGetFileInfo(t *testing.T) {
info, resp := Client.GetFileInfo(fileId) info, resp := Client.GetFileInfo(fileId)
CheckNoError(t, resp) CheckNoError(t, resp)
if err != nil { require.NoError(t, err)
t.Fatal(err) require.Equal(t, fileId, info.Id, "got incorrect file")
} else if info.Id != fileId { require.Equal(t, user.Id, info.CreatorId, "file should be assigned to user")
t.Fatal("got incorrect file") require.Equal(t, "", info.PostId, "file shouldn't have a post")
} else if info.CreatorId != user.Id { require.Equal(t, "", info.Path, "file path shouldn't have been returned to client")
t.Fatal("file should be assigned to user") require.Equal(t, "", info.ThumbnailPath, "file thumbnail path shouldn't have been returned to client")
} else if info.PostId != "" { require.Equal(t, "", info.PreviewPath, "file preview path shouldn't have been returned to client")
t.Fatal("file shouldn't have a post") require.Equal(t, "image/png", info.MimeType, "mime type should've been image/png")
} else if info.Path != "" {
t.Fatal("file path shouldn't have been returned to client")
} else if info.ThumbnailPath != "" {
t.Fatal("file thumbnail path shouldn't have been returned to client")
} else if info.PreviewPath != "" {
t.Fatal("file preview path shouldn't have been returned to client")
} else if info.MimeType != "image/png" {
t.Fatal("mime type should've been image/png")
}
_, resp = Client.GetFileInfo("junk") _, resp = Client.GetFileInfo("junk")
CheckBadRequestStatus(t, resp) CheckBadRequestStatus(t, resp)
@@ -1007,18 +947,16 @@ func TestGetPublicFile(t *testing.T) {
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.FileSettings.EnablePublicLink = true }) th.App.UpdateConfig(func(cfg *model.Config) { *cfg.FileSettings.EnablePublicLink = true })
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.FileSettings.PublicLinkSalt = model.NewRandomString(32) }) th.App.UpdateConfig(func(cfg *model.Config) { *cfg.FileSettings.PublicLinkSalt = model.NewRandomString(32) })
fileId := "" data, err := testutils.ReadTestFile("test.png")
if data, err := testutils.ReadTestFile("test.png"); err != nil { require.NoError(t, err)
t.Fatal(err)
} else {
fileResp, resp := Client.UploadFile(data, channel.Id, "test.png")
CheckNoError(t, resp)
fileId = fileResp.FileInfos[0].Id fileResp, httpResp := Client.UploadFile(data, channel.Id, "test.png")
} CheckNoError(t, httpResp)
fileId := fileResp.FileInfos[0].Id
// Hacky way to assign file to a post (usually would be done by CreatePost call) // Hacky way to assign file to a post (usually would be done by CreatePost call)
err := th.App.Srv.Store.FileInfo().AttachToPost(fileId, th.BasicPost.Id, th.BasicUser.Id) err = th.App.Srv.Store.FileInfo().AttachToPost(fileId, th.BasicPost.Id, th.BasicUser.Id)
require.Nil(t, err) require.Nil(t, err)
info, err := th.App.Srv.Store.FileInfo().Get(fileId) info, err := th.App.Srv.Store.FileInfo().Get(fileId)
@@ -1028,31 +966,31 @@ func TestGetPublicFile(t *testing.T) {
// Wait a bit for files to ready // Wait a bit for files to ready
time.Sleep(2 * time.Second) time.Sleep(2 * time.Second)
if resp, err := http.Get(link); err != nil || resp.StatusCode != http.StatusOK { resp, err := http.Get(link)
t.Log(link) require.NoError(t, err)
t.Fatal("failed to get image with public link", err) require.Equal(t, http.StatusOK, resp.StatusCode, "failed to get image with public link")
}
if resp, err := http.Get(link[:strings.LastIndex(link, "?")]); err == nil && resp.StatusCode != http.StatusBadRequest { resp, err = http.Get(link[:strings.LastIndex(link, "?")])
t.Fatal("should've failed to get image with public link without hash", resp.Status) require.NoError(t, err)
} require.Equal(t, http.StatusBadRequest, resp.StatusCode, "should've failed to get image with public link without hash", resp.Status)
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.FileSettings.EnablePublicLink = false }) th.App.UpdateConfig(func(cfg *model.Config) { *cfg.FileSettings.EnablePublicLink = false })
if resp, err := http.Get(link); err == nil && resp.StatusCode != http.StatusNotImplemented {
t.Fatal("should've failed to get image with disabled public link") resp, err = http.Get(link)
} require.NoError(t, err)
require.Equal(t, http.StatusNotImplemented, resp.StatusCode, "should've failed to get image with disabled public link")
// test after the salt has changed // test after the salt has changed
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.FileSettings.EnablePublicLink = true }) th.App.UpdateConfig(func(cfg *model.Config) { *cfg.FileSettings.EnablePublicLink = true })
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.FileSettings.PublicLinkSalt = model.NewRandomString(32) }) th.App.UpdateConfig(func(cfg *model.Config) { *cfg.FileSettings.PublicLinkSalt = model.NewRandomString(32) })
if resp, err := http.Get(link); err == nil && resp.StatusCode != http.StatusBadRequest { resp, err = http.Get(link)
t.Fatal("should've failed to get image with public link after salt changed") require.NoError(t, err)
} require.Equal(t, http.StatusBadRequest, resp.StatusCode, "should've failed to get image with public link after salt changed")
if resp, err := http.Get(link); err == nil && resp.StatusCode != http.StatusBadRequest { resp, err = http.Get(link)
t.Fatal("should've failed to get image with public link after salt changed") require.NoError(t, err)
} require.Equal(t, http.StatusBadRequest, resp.StatusCode, "should've failed to get image with public link after salt changed")
fileInfo, err := th.App.Srv.Store.FileInfo().Get(fileId) fileInfo, err := th.App.Srv.Store.FileInfo().Get(fileId)
require.Nil(t, err) require.Nil(t, err)