gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

emoji_test.go (8022B)


      1 // GoToSocial
      2 // Copyright (C) GoToSocial Authors admin@gotosocial.org
      3 // SPDX-License-Identifier: AGPL-3.0-or-later
      4 //
      5 // This program is free software: you can redistribute it and/or modify
      6 // it under the terms of the GNU Affero General Public License as published by
      7 // the Free Software Foundation, either version 3 of the License, or
      8 // (at your option) any later version.
      9 //
     10 // This program is distributed in the hope that it will be useful,
     11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 // GNU Affero General Public License for more details.
     14 //
     15 // You should have received a copy of the GNU Affero General Public License
     16 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17 
     18 package validate_test
     19 
     20 import (
     21 	"os"
     22 	"testing"
     23 	"time"
     24 
     25 	"github.com/stretchr/testify/suite"
     26 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
     27 	"github.com/superseriousbusiness/gotosocial/internal/validate"
     28 	"github.com/superseriousbusiness/gotosocial/testrig"
     29 )
     30 
     31 func happyEmoji() *gtsmodel.Emoji {
     32 	// the file validator actually runs os.Stat on given paths, so we need to just create small
     33 	// temp files for both the main attachment file and the thumbnail
     34 
     35 	imageFile, err := os.CreateTemp("", "gts_test_emoji")
     36 	if err != nil {
     37 		panic(err)
     38 	}
     39 	if _, err := imageFile.WriteString("main"); err != nil {
     40 		panic(err)
     41 	}
     42 	imagePath := imageFile.Name()
     43 	if err := imageFile.Close(); err != nil {
     44 		panic(err)
     45 	}
     46 
     47 	staticFile, err := os.CreateTemp("", "gts_test_emoji_static")
     48 	if err != nil {
     49 		panic(err)
     50 	}
     51 	if _, err := staticFile.WriteString("thumbnail"); err != nil {
     52 		panic(err)
     53 	}
     54 	imageStaticPath := staticFile.Name()
     55 	if err := staticFile.Close(); err != nil {
     56 		panic(err)
     57 	}
     58 
     59 	return &gtsmodel.Emoji{
     60 		ID:                     "01F8MH6NEM8D7527KZAECTCR76",
     61 		CreatedAt:              time.Now().Add(-71 * time.Hour),
     62 		UpdatedAt:              time.Now().Add(-71 * time.Hour),
     63 		Shortcode:              "blob_test",
     64 		Domain:                 "example.org",
     65 		ImageRemoteURL:         "https://example.org/emojis/blob_test.gif",
     66 		ImageStaticRemoteURL:   "https://example.org/emojis/blob_test.png",
     67 		ImageURL:               "",
     68 		ImageStaticURL:         "",
     69 		ImagePath:              imagePath,
     70 		ImageStaticPath:        imageStaticPath,
     71 		ImageContentType:       "image/gif",
     72 		ImageStaticContentType: "image/png",
     73 		ImageFileSize:          1024,
     74 		ImageStaticFileSize:    256,
     75 		ImageUpdatedAt:         time.Now(),
     76 		Disabled:               testrig.FalseBool(),
     77 		URI:                    "https://example.org/emojis/blob_test",
     78 		VisibleInPicker:        testrig.TrueBool(),
     79 		CategoryID:             "01FEE47ZH70PWDSEAVBRFNX325",
     80 	}
     81 }
     82 
     83 type EmojiValidateTestSuite struct {
     84 	suite.Suite
     85 }
     86 
     87 func (suite *EmojiValidateTestSuite) TestValidateEmojiHappyPath() {
     88 	// no problem here
     89 	m := happyEmoji()
     90 	err := validate.Struct(*m)
     91 	suite.NoError(err)
     92 }
     93 
     94 func (suite *EmojiValidateTestSuite) TestValidateEmojiBadFilePaths() {
     95 	e := happyEmoji()
     96 
     97 	e.ImagePath = "/tmp/nonexistent/file/for/gotosocial/test"
     98 	err := validate.Struct(e)
     99 	suite.EqualError(err, "Key: 'Emoji.ImagePath' Error:Field validation for 'ImagePath' failed on the 'file' tag")
    100 
    101 	e.ImagePath = ""
    102 	err = validate.Struct(e)
    103 	suite.EqualError(err, "Key: 'Emoji.ImagePath' Error:Field validation for 'ImagePath' failed on the 'required' tag")
    104 
    105 	e.ImagePath = "???????????thisnot a valid path####"
    106 	err = validate.Struct(e)
    107 	suite.EqualError(err, "Key: 'Emoji.ImagePath' Error:Field validation for 'ImagePath' failed on the 'file' tag")
    108 
    109 	e.ImageStaticPath = "/tmp/nonexistent/file/for/gotosocial/test"
    110 	err = validate.Struct(e)
    111 	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")
    112 
    113 	e.ImageStaticPath = ""
    114 	err = validate.Struct(e)
    115 	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")
    116 
    117 	e.ImageStaticPath = "???????????thisnot a valid path####"
    118 	err = validate.Struct(e)
    119 	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")
    120 }
    121 
    122 func (suite *EmojiValidateTestSuite) TestValidateEmojiURI() {
    123 	e := happyEmoji()
    124 
    125 	e.URI = "aaaaaaaaaa"
    126 	err := validate.Struct(e)
    127 	suite.EqualError(err, "Key: 'Emoji.URI' Error:Field validation for 'URI' failed on the 'url' tag")
    128 
    129 	e.URI = ""
    130 	err = validate.Struct(e)
    131 	suite.EqualError(err, "Key: 'Emoji.URI' Error:Field validation for 'URI' failed on the 'url' tag")
    132 }
    133 
    134 func (suite *EmojiValidateTestSuite) TestValidateEmojiURLCombos() {
    135 	e := happyEmoji()
    136 
    137 	e.ImageRemoteURL = ""
    138 	err := validate.Struct(e)
    139 	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")
    140 
    141 	e.ImageURL = "https://whatever.org"
    142 	err = validate.Struct(e)
    143 	suite.NoError(err)
    144 
    145 	e.ImageStaticRemoteURL = ""
    146 	err = validate.Struct(e)
    147 	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")
    148 
    149 	e.ImageStaticURL = "https://whatever.org"
    150 	err = validate.Struct(e)
    151 	suite.NoError(err)
    152 
    153 	e.ImageURL = ""
    154 	e.ImageStaticURL = ""
    155 	e.ImageRemoteURL = ""
    156 	e.ImageStaticRemoteURL = ""
    157 	err = validate.Struct(e)
    158 	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")
    159 }
    160 
    161 func (suite *EmojiValidateTestSuite) TestValidateFileSize() {
    162 	e := happyEmoji()
    163 
    164 	e.ImageFileSize = 0
    165 	err := validate.Struct(e)
    166 	suite.EqualError(err, "Key: 'Emoji.ImageFileSize' Error:Field validation for 'ImageFileSize' failed on the 'required' tag")
    167 
    168 	e.ImageStaticFileSize = 0
    169 	err = validate.Struct(e)
    170 	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")
    171 
    172 	e.ImageFileSize = -1
    173 	err = validate.Struct(e)
    174 	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")
    175 
    176 	e.ImageStaticFileSize = -1
    177 	err = validate.Struct(e)
    178 	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")
    179 }
    180 
    181 func (suite *EmojiValidateTestSuite) TestValidateDomain() {
    182 	e := happyEmoji()
    183 
    184 	e.Domain = ""
    185 	err := validate.Struct(e)
    186 	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")
    187 
    188 	e.Domain = "aaaaaaaaa"
    189 	err = validate.Struct(e)
    190 	suite.EqualError(err, "Key: 'Emoji.Domain' Error:Field validation for 'Domain' failed on the 'fqdn' tag")
    191 }
    192 
    193 func TestEmojiValidateTestSuite(t *testing.T) {
    194 	suite.Run(t, new(EmojiValidateTestSuite))
    195 }