mirror of
1
Fork 0
gotosocial/internal/validate/mediaattachment_test.go

231 lines
8.3 KiB
Go
Raw Normal View History

2021-08-30 20:20:27 +02:00
/*
GoToSocial
2021-12-20 18:42:19 +01:00
Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org
2021-08-30 20:20:27 +02:00
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2021-09-01 18:29:25 +02:00
package validate_test
2021-08-30 20:20:27 +02:00
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/suite"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
2021-09-01 18:29:25 +02:00
"github.com/superseriousbusiness/gotosocial/internal/validate"
2021-08-30 20:20:27 +02:00
)
func happyMediaAttachment() *gtsmodel.MediaAttachment {
// the file validator actually runs os.Stat on given paths, so we need to just create small
// temp files for both the main attachment file and the thumbnail
mainFile, err := os.CreateTemp("", "gts_test_mainfile")
if err != nil {
panic(err)
}
if _, err := mainFile.WriteString("main"); err != nil {
panic(err)
}
mainPath := mainFile.Name()
if err := mainFile.Close(); err != nil {
panic(err)
}
thumbnailFile, err := os.CreateTemp("", "gts_test_thumbnail")
if err != nil {
panic(err)
}
if _, err := thumbnailFile.WriteString("thumbnail"); err != nil {
panic(err)
}
thumbnailPath := thumbnailFile.Name()
if err := thumbnailFile.Close(); err != nil {
panic(err)
}
return &gtsmodel.MediaAttachment{
ID: "01F8MH6NEM8D7527KZAECTCR76",
CreatedAt: time.Now().Add(-71 * time.Hour),
UpdatedAt: time.Now().Add(-71 * time.Hour),
StatusID: "01F8MH75CBF9JFX4ZAD54N0W0R",
URL: "http://localhost:8080/fileserver/01F8MH17FWEB39HZJ76B6VXSKF/attachment/original/01F8MH6NEM8D7527KZAECTCR76.jpeg",
RemoteURL: "",
Type: gtsmodel.FileTypeImage,
FileMeta: gtsmodel.FileMeta{
Original: gtsmodel.Original{
Width: 1200,
Height: 630,
Size: 756000,
Aspect: 1.9047619047619047,
},
Small: gtsmodel.Small{
Width: 256,
Height: 134,
Size: 34304,
Aspect: 1.9104477611940298,
},
},
AccountID: "01F8MH17FWEB39HZJ76B6VXSKF",
Description: "Black and white image of some 50's style text saying: Welcome On Board",
ScheduledStatusID: "",
Blurhash: "LNJRdVM{00Rj%Mayt7j[4nWBofRj",
Processing: 2,
File: gtsmodel.File{
Path: mainPath,
ContentType: "image/jpeg",
FileSize: 62529,
UpdatedAt: time.Now().Add(-71 * time.Hour),
},
Thumbnail: gtsmodel.Thumbnail{
Path: thumbnailPath,
ContentType: "image/jpeg",
FileSize: 6872,
UpdatedAt: time.Now().Add(-71 * time.Hour),
URL: "http://localhost:8080/fileserver/01F8MH17FWEB39HZJ76B6VXSKF/attachment/small/01F8MH6NEM8D7527KZAECTCR76.jpeg",
RemoteURL: "",
},
Avatar: false,
Header: false,
}
}
type MediaAttachmentValidateTestSuite struct {
suite.Suite
}
func (suite *MediaAttachmentValidateTestSuite) TestValidateMediaAttachmentHappyPath() {
// no problem here
m := happyMediaAttachment()
2021-09-03 10:30:40 +02:00
err := validate.Struct(m)
2021-08-30 20:20:27 +02:00
suite.NoError(err)
}
func (suite *MediaAttachmentValidateTestSuite) TestValidateMediaAttachmentBadFilePaths() {
m := happyMediaAttachment()
m.File.Path = "/tmp/nonexistent/file/for/gotosocial/test"
2021-09-03 10:30:40 +02:00
err := validate.Struct(m)
2021-08-30 20:20:27 +02:00
suite.EqualError(err, "Key: 'MediaAttachment.File.Path' Error:Field validation for 'Path' failed on the 'file' tag")
m.File.Path = ""
2021-09-03 10:30:40 +02:00
err = validate.Struct(m)
2021-08-30 20:20:27 +02:00
suite.EqualError(err, "Key: 'MediaAttachment.File.Path' Error:Field validation for 'Path' failed on the 'required' tag")
m.File.Path = "???????????thisnot a valid path####"
2021-09-03 10:30:40 +02:00
err = validate.Struct(m)
2021-08-30 20:20:27 +02:00
suite.EqualError(err, "Key: 'MediaAttachment.File.Path' Error:Field validation for 'Path' failed on the 'file' tag")
m.Thumbnail.Path = "/tmp/nonexistent/file/for/gotosocial/test"
2021-09-03 10:30:40 +02:00
err = validate.Struct(m)
2021-08-30 20:20:27 +02:00
suite.EqualError(err, "Key: 'MediaAttachment.File.Path' Error:Field validation for 'Path' failed on the 'file' tag\nKey: 'MediaAttachment.Thumbnail.Path' Error:Field validation for 'Path' failed on the 'file' tag")
m.Thumbnail.Path = ""
2021-09-03 10:30:40 +02:00
err = validate.Struct(m)
2021-08-30 20:20:27 +02:00
suite.EqualError(err, "Key: 'MediaAttachment.File.Path' Error:Field validation for 'Path' failed on the 'file' tag\nKey: 'MediaAttachment.Thumbnail.Path' Error:Field validation for 'Path' failed on the 'required' tag")
m.Thumbnail.Path = "???????????thisnot a valid path####"
2021-09-03 10:30:40 +02:00
err = validate.Struct(m)
2021-08-30 20:20:27 +02:00
suite.EqualError(err, "Key: 'MediaAttachment.File.Path' Error:Field validation for 'Path' failed on the 'file' tag\nKey: 'MediaAttachment.Thumbnail.Path' Error:Field validation for 'Path' failed on the 'file' tag")
}
func (suite *MediaAttachmentValidateTestSuite) TestValidateMediaAttachmentBadType() {
m := happyMediaAttachment()
m.Type = ""
2021-09-03 10:30:40 +02:00
err := validate.Struct(m)
2021-08-30 20:20:27 +02:00
suite.EqualError(err, "Key: 'MediaAttachment.Type' Error:Field validation for 'Type' failed on the 'oneof' tag")
m.Type = "Not Supported"
2021-09-03 10:30:40 +02:00
err = validate.Struct(m)
2021-08-30 20:20:27 +02:00
suite.EqualError(err, "Key: 'MediaAttachment.Type' Error:Field validation for 'Type' failed on the 'oneof' tag")
}
func (suite *MediaAttachmentValidateTestSuite) TestValidateMediaAttachmentBadFileMeta() {
m := happyMediaAttachment()
m.FileMeta.Original.Aspect = 0
2021-09-03 10:30:40 +02:00
err := validate.Struct(m)
2021-08-30 20:20:27 +02:00
suite.EqualError(err, "Key: 'MediaAttachment.FileMeta.Original.Aspect' Error:Field validation for 'Aspect' failed on the 'required_with' tag")
m.FileMeta.Original.Height = 0
2021-09-03 10:30:40 +02:00
err = validate.Struct(m)
2021-08-30 20:20:27 +02:00
suite.EqualError(err, "Key: 'MediaAttachment.FileMeta.Original.Height' Error:Field validation for 'Height' failed on the 'required_with' tag\nKey: 'MediaAttachment.FileMeta.Original.Aspect' Error:Field validation for 'Aspect' failed on the 'required_with' tag")
m.FileMeta.Original = gtsmodel.Original{}
2021-09-03 10:30:40 +02:00
err = validate.Struct(m)
2021-08-30 20:20:27 +02:00
suite.NoError(err)
m.FileMeta.Focus.X = 3.6
2021-09-03 10:30:40 +02:00
err = validate.Struct(m)
2021-08-30 20:20:27 +02:00
suite.EqualError(err, "Key: 'MediaAttachment.FileMeta.Focus.X' Error:Field validation for 'X' failed on the 'max' tag")
m.FileMeta.Focus.Y = -50
2021-09-03 10:30:40 +02:00
err = validate.Struct(m)
2021-08-30 20:20:27 +02:00
suite.EqualError(err, "Key: 'MediaAttachment.FileMeta.Focus.X' Error:Field validation for 'X' failed on the 'max' tag\nKey: 'MediaAttachment.FileMeta.Focus.Y' Error:Field validation for 'Y' failed on the 'min' tag")
}
func (suite *MediaAttachmentValidateTestSuite) TestValidateMediaAttachmentBadURLCombos() {
m := happyMediaAttachment()
m.URL = "aaaaaaaaaa"
2021-09-03 10:30:40 +02:00
err := validate.Struct(m)
2021-08-30 20:20:27 +02:00
suite.EqualError(err, "Key: 'MediaAttachment.URL' Error:Field validation for 'URL' failed on the 'url' tag")
m.URL = ""
2021-09-03 10:30:40 +02:00
err = validate.Struct(m)
2021-08-30 20:20:27 +02:00
suite.EqualError(err, "Key: 'MediaAttachment.URL' Error:Field validation for 'URL' failed on the 'required_without' tag\nKey: 'MediaAttachment.RemoteURL' Error:Field validation for 'RemoteURL' failed on the 'required_without' tag")
m.RemoteURL = "oooooooooo"
2021-09-03 10:30:40 +02:00
err = validate.Struct(m)
2021-08-30 20:20:27 +02:00
suite.EqualError(err, "Key: 'MediaAttachment.RemoteURL' Error:Field validation for 'RemoteURL' failed on the 'url' tag")
m.RemoteURL = "https://a-valid-url.gay"
2021-09-03 10:30:40 +02:00
err = validate.Struct(m)
2021-08-30 20:20:27 +02:00
suite.NoError(err)
}
func (suite *MediaAttachmentValidateTestSuite) TestValidateMediaAttachmentBlurhash() {
m := happyMediaAttachment()
m.Blurhash = ""
2021-09-03 10:30:40 +02:00
err := validate.Struct(m)
2021-08-30 20:20:27 +02:00
suite.EqualError(err, "Key: 'MediaAttachment.Blurhash' Error:Field validation for 'Blurhash' failed on the 'required_if' tag")
m.Type = gtsmodel.FileTypeAudio
2021-09-03 10:30:40 +02:00
err = validate.Struct(m)
2021-08-30 20:20:27 +02:00
suite.NoError(err)
m.Blurhash = "some_blurhash"
2021-09-03 10:30:40 +02:00
err = validate.Struct(m)
2021-08-30 20:20:27 +02:00
suite.NoError(err)
}
2021-08-31 15:59:12 +02:00
func (suite *MediaAttachmentValidateTestSuite) TestValidateMediaAttachmentProcessing() {
m := happyMediaAttachment()
m.Processing = 420
2021-09-03 10:30:40 +02:00
err := validate.Struct(m)
2021-08-31 15:59:12 +02:00
suite.EqualError(err, "Key: 'MediaAttachment.Processing' Error:Field validation for 'Processing' failed on the 'oneof' tag")
m.Processing = -5
2021-09-03 10:30:40 +02:00
err = validate.Struct(m)
2021-08-31 15:59:12 +02:00
suite.EqualError(err, "Key: 'MediaAttachment.Processing' Error:Field validation for 'Processing' failed on the 'oneof' tag")
}
2021-08-30 20:20:27 +02:00
func TestMediaAttachmentValidateTestSuite(t *testing.T) {
suite.Run(t, new(MediaAttachmentValidateTestSuite))
}