gotosocial/internal/validate/emoji_test.go

196 lines
7.8 KiB
Go
Raw Normal View History

// GoToSocial
// Copyright (C) GoToSocial Authors admin@gotosocial.org
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// 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-08-31 15:59:12 +02:00
2021-09-01 18:29:25 +02:00
package validate_test
2021-08-31 15:59:12 +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"
"github.com/superseriousbusiness/gotosocial/testrig"
2021-08-31 15:59:12 +02:00
)
func happyEmoji() *gtsmodel.Emoji {
// 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
imageFile, err := os.CreateTemp("", "gts_test_emoji")
if err != nil {
panic(err)
}
if _, err := imageFile.WriteString("main"); err != nil {
panic(err)
}
imagePath := imageFile.Name()
if err := imageFile.Close(); err != nil {
panic(err)
}
staticFile, err := os.CreateTemp("", "gts_test_emoji_static")
if err != nil {
panic(err)
}
if _, err := staticFile.WriteString("thumbnail"); err != nil {
panic(err)
}
imageStaticPath := staticFile.Name()
if err := staticFile.Close(); err != nil {
panic(err)
}
return &gtsmodel.Emoji{
ID: "01F8MH6NEM8D7527KZAECTCR76",
CreatedAt: time.Now().Add(-71 * time.Hour),
UpdatedAt: time.Now().Add(-71 * time.Hour),
Shortcode: "blob_test",
Domain: "example.org",
ImageRemoteURL: "https://example.org/emojis/blob_test.gif",
ImageStaticRemoteURL: "https://example.org/emojis/blob_test.png",
ImageURL: "",
ImageStaticURL: "",
ImagePath: imagePath,
ImageStaticPath: imageStaticPath,
ImageContentType: "image/gif",
ImageStaticContentType: "image/png",
ImageFileSize: 1024,
ImageStaticFileSize: 256,
ImageUpdatedAt: time.Now(),
Disabled: testrig.FalseBool(),
2021-08-31 15:59:12 +02:00
URI: "https://example.org/emojis/blob_test",
VisibleInPicker: testrig.TrueBool(),
2021-08-31 15:59:12 +02:00
CategoryID: "01FEE47ZH70PWDSEAVBRFNX325",
}
}
type EmojiValidateTestSuite struct {
suite.Suite
}
func (suite *EmojiValidateTestSuite) TestValidateEmojiHappyPath() {
// no problem here
m := happyEmoji()
2021-09-01 18:29:25 +02:00
err := validate.Struct(*m)
2021-08-31 15:59:12 +02:00
suite.NoError(err)
}
func (suite *EmojiValidateTestSuite) TestValidateEmojiBadFilePaths() {
e := happyEmoji()
e.ImagePath = "/tmp/nonexistent/file/for/gotosocial/test"
2021-09-03 10:30:40 +02:00
err := validate.Struct(e)
2021-08-31 15:59:12 +02:00
suite.EqualError(err, "Key: 'Emoji.ImagePath' Error:Field validation for 'ImagePath' failed on the 'file' tag")
e.ImagePath = ""
2021-09-03 10:30:40 +02:00
err = validate.Struct(e)
2021-08-31 15:59:12 +02:00
suite.EqualError(err, "Key: 'Emoji.ImagePath' Error:Field validation for 'ImagePath' failed on the 'required' tag")
e.ImagePath = "???????????thisnot a valid path####"
2021-09-03 10:30:40 +02:00
err = validate.Struct(e)
2021-08-31 15:59:12 +02:00
suite.EqualError(err, "Key: 'Emoji.ImagePath' Error:Field validation for 'ImagePath' failed on the 'file' tag")
e.ImageStaticPath = "/tmp/nonexistent/file/for/gotosocial/test"
2021-09-03 10:30:40 +02:00
err = validate.Struct(e)
2021-08-31 15:59:12 +02:00
suite.EqualError(err, "Key: 'Emoji.ImagePath' Error:Field validation for 'ImagePath' failed on the 'file' tag\nKey: 'Emoji.ImageStaticPath' Error:Field validation for 'ImageStaticPath' failed on the 'file' tag")
e.ImageStaticPath = ""
2021-09-03 10:30:40 +02:00
err = validate.Struct(e)
2021-08-31 15:59:12 +02:00
suite.EqualError(err, "Key: 'Emoji.ImagePath' Error:Field validation for 'ImagePath' failed on the 'file' tag\nKey: 'Emoji.ImageStaticPath' Error:Field validation for 'ImageStaticPath' failed on the 'required' tag")
e.ImageStaticPath = "???????????thisnot a valid path####"
2021-09-03 10:30:40 +02:00
err = validate.Struct(e)
2021-08-31 15:59:12 +02:00
suite.EqualError(err, "Key: 'Emoji.ImagePath' Error:Field validation for 'ImagePath' failed on the 'file' tag\nKey: 'Emoji.ImageStaticPath' Error:Field validation for 'ImageStaticPath' failed on the 'file' tag")
}
func (suite *EmojiValidateTestSuite) TestValidateEmojiURI() {
e := happyEmoji()
e.URI = "aaaaaaaaaa"
2021-09-03 10:30:40 +02:00
err := validate.Struct(e)
2021-08-31 15:59:12 +02:00
suite.EqualError(err, "Key: 'Emoji.URI' Error:Field validation for 'URI' failed on the 'url' tag")
e.URI = ""
2021-09-03 10:30:40 +02:00
err = validate.Struct(e)
2021-08-31 15:59:12 +02:00
suite.EqualError(err, "Key: 'Emoji.URI' Error:Field validation for 'URI' failed on the 'url' tag")
}
func (suite *EmojiValidateTestSuite) TestValidateEmojiURLCombos() {
e := happyEmoji()
e.ImageRemoteURL = ""
2021-09-03 10:30:40 +02:00
err := validate.Struct(e)
2021-08-31 15:59:12 +02:00
suite.EqualError(err, "Key: 'Emoji.ImageRemoteURL' Error:Field validation for 'ImageRemoteURL' failed on the 'required_without' tag\nKey: 'Emoji.ImageURL' Error:Field validation for 'ImageURL' failed on the 'required_without' tag")
e.ImageURL = "https://whatever.org"
2021-09-03 10:30:40 +02:00
err = validate.Struct(e)
2021-08-31 15:59:12 +02:00
suite.NoError(err)
e.ImageStaticRemoteURL = ""
2021-09-03 10:30:40 +02:00
err = validate.Struct(e)
2021-08-31 15:59:12 +02:00
suite.EqualError(err, "Key: 'Emoji.ImageStaticRemoteURL' Error:Field validation for 'ImageStaticRemoteURL' failed on the 'required_without' tag\nKey: 'Emoji.ImageStaticURL' Error:Field validation for 'ImageStaticURL' failed on the 'required_without' tag")
e.ImageStaticURL = "https://whatever.org"
2021-09-03 10:30:40 +02:00
err = validate.Struct(e)
2021-08-31 15:59:12 +02:00
suite.NoError(err)
e.ImageURL = ""
e.ImageStaticURL = ""
e.ImageRemoteURL = ""
e.ImageStaticRemoteURL = ""
2021-09-03 10:30:40 +02:00
err = validate.Struct(e)
2021-08-31 15:59:12 +02:00
suite.EqualError(err, "Key: 'Emoji.ImageRemoteURL' Error:Field validation for 'ImageRemoteURL' failed on the 'required_without' tag\nKey: 'Emoji.ImageStaticRemoteURL' Error:Field validation for 'ImageStaticRemoteURL' failed on the 'required_without' tag\nKey: 'Emoji.ImageURL' Error:Field validation for 'ImageURL' failed on the 'required_without' tag\nKey: 'Emoji.ImageStaticURL' Error:Field validation for 'ImageStaticURL' failed on the 'required_without' tag")
}
func (suite *EmojiValidateTestSuite) TestValidateFileSize() {
e := happyEmoji()
e.ImageFileSize = 0
2021-09-03 10:30:40 +02:00
err := validate.Struct(e)
2021-08-31 15:59:12 +02:00
suite.EqualError(err, "Key: 'Emoji.ImageFileSize' Error:Field validation for 'ImageFileSize' failed on the 'required' tag")
e.ImageStaticFileSize = 0
2021-09-03 10:30:40 +02:00
err = validate.Struct(e)
2021-08-31 15:59:12 +02:00
suite.EqualError(err, "Key: 'Emoji.ImageFileSize' Error:Field validation for 'ImageFileSize' failed on the 'required' tag\nKey: 'Emoji.ImageStaticFileSize' Error:Field validation for 'ImageStaticFileSize' failed on the 'required' tag")
e.ImageFileSize = -1
2021-09-03 10:30:40 +02:00
err = validate.Struct(e)
2021-08-31 15:59:12 +02:00
suite.EqualError(err, "Key: 'Emoji.ImageFileSize' Error:Field validation for 'ImageFileSize' failed on the 'min' tag\nKey: 'Emoji.ImageStaticFileSize' Error:Field validation for 'ImageStaticFileSize' failed on the 'required' tag")
e.ImageStaticFileSize = -1
2021-09-03 10:30:40 +02:00
err = validate.Struct(e)
2021-08-31 15:59:12 +02:00
suite.EqualError(err, "Key: 'Emoji.ImageFileSize' Error:Field validation for 'ImageFileSize' failed on the 'min' tag\nKey: 'Emoji.ImageStaticFileSize' Error:Field validation for 'ImageStaticFileSize' failed on the 'min' tag")
}
func (suite *EmojiValidateTestSuite) TestValidateDomain() {
e := happyEmoji()
e.Domain = ""
2021-09-03 10:30:40 +02:00
err := validate.Struct(e)
2021-08-31 15:59:12 +02:00
suite.EqualError(err, "Key: 'Emoji.ImageURL' Error:Field validation for 'ImageURL' failed on the 'required_without' tag\nKey: 'Emoji.ImageStaticURL' Error:Field validation for 'ImageStaticURL' failed on the 'required_without' tag")
e.Domain = "aaaaaaaaa"
2021-09-03 10:30:40 +02:00
err = validate.Struct(e)
2021-08-31 15:59:12 +02:00
suite.EqualError(err, "Key: 'Emoji.Domain' Error:Field validation for 'Domain' failed on the 'fqdn' tag")
}
func TestEmojiValidateTestSuite(t *testing.T) {
suite.Run(t, new(EmojiValidateTestSuite))
}