gtsocial-umbx

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

mediaattachment_test.go (8571B)


      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 happyMediaAttachment() *gtsmodel.MediaAttachment {
     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 	mainFile, err := os.CreateTemp("", "gts_test_mainfile")
     36 	if err != nil {
     37 		panic(err)
     38 	}
     39 	if _, err := mainFile.WriteString("main"); err != nil {
     40 		panic(err)
     41 	}
     42 	mainPath := mainFile.Name()
     43 	if err := mainFile.Close(); err != nil {
     44 		panic(err)
     45 	}
     46 
     47 	thumbnailFile, err := os.CreateTemp("", "gts_test_thumbnail")
     48 	if err != nil {
     49 		panic(err)
     50 	}
     51 	if _, err := thumbnailFile.WriteString("thumbnail"); err != nil {
     52 		panic(err)
     53 	}
     54 	thumbnailPath := thumbnailFile.Name()
     55 	if err := thumbnailFile.Close(); err != nil {
     56 		panic(err)
     57 	}
     58 
     59 	return &gtsmodel.MediaAttachment{
     60 		ID:        "01F8MH6NEM8D7527KZAECTCR76",
     61 		CreatedAt: time.Now().Add(-71 * time.Hour),
     62 		UpdatedAt: time.Now().Add(-71 * time.Hour),
     63 		StatusID:  "01F8MH75CBF9JFX4ZAD54N0W0R",
     64 		URL:       "http://localhost:8080/fileserver/01F8MH17FWEB39HZJ76B6VXSKF/attachment/original/01F8MH6NEM8D7527KZAECTCR76.jpg",
     65 		RemoteURL: "",
     66 		Type:      gtsmodel.FileTypeImage,
     67 		FileMeta: gtsmodel.FileMeta{
     68 			Original: gtsmodel.Original{
     69 				Width:  1200,
     70 				Height: 630,
     71 				Size:   756000,
     72 				Aspect: 1.9047619047619047,
     73 			},
     74 			Small: gtsmodel.Small{
     75 				Width:  256,
     76 				Height: 134,
     77 				Size:   34304,
     78 				Aspect: 1.9104477611940298,
     79 			},
     80 		},
     81 		AccountID:         "01F8MH17FWEB39HZJ76B6VXSKF",
     82 		Description:       "Black and white image of some 50's style text saying: Welcome On Board",
     83 		ScheduledStatusID: "",
     84 		Blurhash:          "LNJRdVM{00Rj%Mayt7j[4nWBofRj",
     85 		Processing:        2,
     86 		File: gtsmodel.File{
     87 			Path:        mainPath,
     88 			ContentType: "image/jpeg",
     89 			FileSize:    62529,
     90 			UpdatedAt:   time.Now().Add(-71 * time.Hour),
     91 		},
     92 		Thumbnail: gtsmodel.Thumbnail{
     93 			Path:        thumbnailPath,
     94 			ContentType: "image/jpeg",
     95 			FileSize:    6872,
     96 			UpdatedAt:   time.Now().Add(-71 * time.Hour),
     97 			URL:         "http://localhost:8080/fileserver/01F8MH17FWEB39HZJ76B6VXSKF/attachment/small/01F8MH6NEM8D7527KZAECTCR76.jpg",
     98 			RemoteURL:   "",
     99 		},
    100 		Avatar: testrig.FalseBool(),
    101 		Header: testrig.FalseBool(),
    102 	}
    103 }
    104 
    105 type MediaAttachmentValidateTestSuite struct {
    106 	suite.Suite
    107 }
    108 
    109 func (suite *MediaAttachmentValidateTestSuite) TestValidateMediaAttachmentHappyPath() {
    110 	// no problem here
    111 	m := happyMediaAttachment()
    112 	err := validate.Struct(m)
    113 	suite.NoError(err)
    114 }
    115 
    116 func (suite *MediaAttachmentValidateTestSuite) TestValidateMediaAttachmentBadFilePaths() {
    117 	m := happyMediaAttachment()
    118 
    119 	m.File.Path = "/tmp/nonexistent/file/for/gotosocial/test"
    120 	err := validate.Struct(m)
    121 	suite.EqualError(err, "Key: 'MediaAttachment.File.Path' Error:Field validation for 'Path' failed on the 'file' tag")
    122 
    123 	m.File.Path = ""
    124 	err = validate.Struct(m)
    125 	suite.EqualError(err, "Key: 'MediaAttachment.File.Path' Error:Field validation for 'Path' failed on the 'required' tag")
    126 
    127 	m.File.Path = "???????????thisnot a valid path####"
    128 	err = validate.Struct(m)
    129 	suite.EqualError(err, "Key: 'MediaAttachment.File.Path' Error:Field validation for 'Path' failed on the 'file' tag")
    130 
    131 	m.Thumbnail.Path = "/tmp/nonexistent/file/for/gotosocial/test"
    132 	err = validate.Struct(m)
    133 	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")
    134 
    135 	m.Thumbnail.Path = ""
    136 	err = validate.Struct(m)
    137 	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")
    138 
    139 	m.Thumbnail.Path = "???????????thisnot a valid path####"
    140 	err = validate.Struct(m)
    141 	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")
    142 }
    143 
    144 func (suite *MediaAttachmentValidateTestSuite) TestValidateMediaAttachmentBadType() {
    145 	m := happyMediaAttachment()
    146 
    147 	m.Type = ""
    148 	err := validate.Struct(m)
    149 	suite.EqualError(err, "Key: 'MediaAttachment.Type' Error:Field validation for 'Type' failed on the 'oneof' tag")
    150 
    151 	m.Type = "Not Supported"
    152 	err = validate.Struct(m)
    153 	suite.EqualError(err, "Key: 'MediaAttachment.Type' Error:Field validation for 'Type' failed on the 'oneof' tag")
    154 }
    155 
    156 func (suite *MediaAttachmentValidateTestSuite) TestValidateMediaAttachmentBadFileMeta() {
    157 	m := happyMediaAttachment()
    158 
    159 	m.FileMeta.Original.Aspect = 0
    160 	err := validate.Struct(m)
    161 	suite.EqualError(err, "Key: 'MediaAttachment.FileMeta.Original.Aspect' Error:Field validation for 'Aspect' failed on the 'required_with' tag")
    162 
    163 	m.FileMeta.Original.Height = 0
    164 	err = validate.Struct(m)
    165 	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")
    166 
    167 	m.FileMeta.Original = gtsmodel.Original{}
    168 	err = validate.Struct(m)
    169 	suite.NoError(err)
    170 
    171 	m.FileMeta.Focus.X = 3.6
    172 	err = validate.Struct(m)
    173 	suite.EqualError(err, "Key: 'MediaAttachment.FileMeta.Focus.X' Error:Field validation for 'X' failed on the 'max' tag")
    174 
    175 	m.FileMeta.Focus.Y = -50
    176 	err = validate.Struct(m)
    177 	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")
    178 }
    179 
    180 func (suite *MediaAttachmentValidateTestSuite) TestValidateMediaAttachmentBadURLCombos() {
    181 	m := happyMediaAttachment()
    182 
    183 	m.URL = "aaaaaaaaaa"
    184 	err := validate.Struct(m)
    185 	suite.EqualError(err, "Key: 'MediaAttachment.URL' Error:Field validation for 'URL' failed on the 'url' tag")
    186 
    187 	m.URL = ""
    188 	err = validate.Struct(m)
    189 	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")
    190 
    191 	m.RemoteURL = "oooooooooo"
    192 	err = validate.Struct(m)
    193 	suite.EqualError(err, "Key: 'MediaAttachment.RemoteURL' Error:Field validation for 'RemoteURL' failed on the 'url' tag")
    194 
    195 	m.RemoteURL = "https://a-valid-url.gay"
    196 	err = validate.Struct(m)
    197 	suite.NoError(err)
    198 }
    199 
    200 func (suite *MediaAttachmentValidateTestSuite) TestValidateMediaAttachmentBlurhash() {
    201 	m := happyMediaAttachment()
    202 
    203 	m.Blurhash = ""
    204 	err := validate.Struct(m)
    205 	suite.EqualError(err, "Key: 'MediaAttachment.Blurhash' Error:Field validation for 'Blurhash' failed on the 'required_if' tag")
    206 
    207 	m.Type = gtsmodel.FileTypeAudio
    208 	err = validate.Struct(m)
    209 	suite.NoError(err)
    210 
    211 	m.Blurhash = "some_blurhash"
    212 	err = validate.Struct(m)
    213 	suite.NoError(err)
    214 }
    215 
    216 func (suite *MediaAttachmentValidateTestSuite) TestValidateMediaAttachmentProcessing() {
    217 	m := happyMediaAttachment()
    218 
    219 	m.Processing = 420
    220 	err := validate.Struct(m)
    221 	suite.EqualError(err, "Key: 'MediaAttachment.Processing' Error:Field validation for 'Processing' failed on the 'oneof' tag")
    222 
    223 	m.Processing = -5
    224 	err = validate.Struct(m)
    225 	suite.EqualError(err, "Key: 'MediaAttachment.Processing' Error:Field validation for 'Processing' failed on the 'oneof' tag")
    226 }
    227 
    228 func TestMediaAttachmentValidateTestSuite(t *testing.T) {
    229 	suite.Run(t, new(MediaAttachmentValidateTestSuite))
    230 }