diff options
author | 2023-08-06 12:22:40 +0200 | |
---|---|---|
committer | 2023-08-06 12:22:40 +0200 | |
commit | c1375ca5c1d5a8a645151cedfced5a336f6b7467 (patch) | |
tree | c3cc68d992811de674458e646af16f4659926ec7 /internal/validate/emoji_test.go | |
parent | [bugfix] update cache library with nil ptr fix (#2070) (diff) | |
download | gotosocial-c1375ca5c1d5a8a645151cedfced5a336f6b7467.tar.xz |
[chore] Remove go-playground/validator (#2069)
* [chore] Remove go-playground/validator
It turns out we're not actually using the validator code. This is a
remnant from when we intended to use it, but the presence of it and its
struct tags creates the illusion we're validating a lot of things we're
not. It resulted in some confusion when we were trying to figure out
language valdiation.
Remove all this code, so that only the validation functions from the
validate package we actually use remain. I'm not touching the struct
tags in the migrations in order to avoid things potentially thinking
migrations need to be re-run.
* [chore] Bring back a struct tag on api
The validate on internal/api is Gin doing form validation, not the
validator from go-playground/validator.
Diffstat (limited to 'internal/validate/emoji_test.go')
-rw-r--r-- | internal/validate/emoji_test.go | 195 |
1 files changed, 0 insertions, 195 deletions
diff --git a/internal/validate/emoji_test.go b/internal/validate/emoji_test.go deleted file mode 100644 index 9192cb1fc..000000000 --- a/internal/validate/emoji_test.go +++ /dev/null @@ -1,195 +0,0 @@ -// 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/>. - -package validate_test - -import ( - "os" - "testing" - "time" - - "github.com/stretchr/testify/suite" - "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" - "github.com/superseriousbusiness/gotosocial/internal/validate" - "github.com/superseriousbusiness/gotosocial/testrig" -) - -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 >smodel.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(), - URI: "https://example.org/emojis/blob_test", - VisibleInPicker: testrig.TrueBool(), - CategoryID: "01FEE47ZH70PWDSEAVBRFNX325", - } -} - -type EmojiValidateTestSuite struct { - suite.Suite -} - -func (suite *EmojiValidateTestSuite) TestValidateEmojiHappyPath() { - // no problem here - m := happyEmoji() - err := validate.Struct(*m) - suite.NoError(err) -} - -func (suite *EmojiValidateTestSuite) TestValidateEmojiBadFilePaths() { - e := happyEmoji() - - e.ImagePath = "/tmp/nonexistent/file/for/gotosocial/test" - err := validate.Struct(e) - suite.EqualError(err, "Key: 'Emoji.ImagePath' Error:Field validation for 'ImagePath' failed on the 'file' tag") - - e.ImagePath = "" - err = validate.Struct(e) - suite.EqualError(err, "Key: 'Emoji.ImagePath' Error:Field validation for 'ImagePath' failed on the 'required' tag") - - e.ImagePath = "???????????thisnot a valid path####" - err = validate.Struct(e) - suite.EqualError(err, "Key: 'Emoji.ImagePath' Error:Field validation for 'ImagePath' failed on the 'file' tag") - - e.ImageStaticPath = "/tmp/nonexistent/file/for/gotosocial/test" - err = validate.Struct(e) - 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 = "" - err = validate.Struct(e) - 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####" - err = validate.Struct(e) - 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" - err := validate.Struct(e) - suite.EqualError(err, "Key: 'Emoji.URI' Error:Field validation for 'URI' failed on the 'url' tag") - - e.URI = "" - err = validate.Struct(e) - suite.EqualError(err, "Key: 'Emoji.URI' Error:Field validation for 'URI' failed on the 'url' tag") -} - -func (suite *EmojiValidateTestSuite) TestValidateEmojiURLCombos() { - e := happyEmoji() - - e.ImageRemoteURL = "" - err := validate.Struct(e) - 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" - err = validate.Struct(e) - suite.NoError(err) - - e.ImageStaticRemoteURL = "" - err = validate.Struct(e) - 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" - err = validate.Struct(e) - suite.NoError(err) - - e.ImageURL = "" - e.ImageStaticURL = "" - e.ImageRemoteURL = "" - e.ImageStaticRemoteURL = "" - err = validate.Struct(e) - 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 - err := validate.Struct(e) - suite.EqualError(err, "Key: 'Emoji.ImageFileSize' Error:Field validation for 'ImageFileSize' failed on the 'required' tag") - - e.ImageStaticFileSize = 0 - err = validate.Struct(e) - 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 - err = validate.Struct(e) - 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 - err = validate.Struct(e) - 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 = "" - err := validate.Struct(e) - 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" - err = validate.Struct(e) - 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)) -} |