gtsocial-umbx

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

block_test.go (3697B)


      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 	"testing"
     22 	"time"
     23 
     24 	"github.com/stretchr/testify/suite"
     25 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
     26 	"github.com/superseriousbusiness/gotosocial/internal/validate"
     27 )
     28 
     29 func happyBlock() *gtsmodel.Block {
     30 	return &gtsmodel.Block{
     31 		ID:              "01FE91RJR88PSEEE30EV35QR8N",
     32 		CreatedAt:       time.Now(),
     33 		UpdatedAt:       time.Now(),
     34 		URI:             "https://example.org/accounts/someone/blocks/01FE91RJR88PSEEE30EV35QR8N",
     35 		AccountID:       "01FEED79PRMVWPRMFHFQM8MJQN",
     36 		Account:         nil,
     37 		TargetAccountID: "01FEEDMF6C0QD589MRK7919Z0R",
     38 		TargetAccount:   nil,
     39 	}
     40 }
     41 
     42 type BlockValidateTestSuite struct {
     43 	suite.Suite
     44 }
     45 
     46 func (suite *BlockValidateTestSuite) TestValidateBlockHappyPath() {
     47 	// no problem here
     48 	b := happyBlock()
     49 	err := validate.Struct(b)
     50 	suite.NoError(err)
     51 }
     52 
     53 func (suite *BlockValidateTestSuite) TestValidateBlockBadID() {
     54 	b := happyBlock()
     55 
     56 	b.ID = ""
     57 	err := validate.Struct(b)
     58 	suite.EqualError(err, "Key: 'Block.ID' Error:Field validation for 'ID' failed on the 'required' tag")
     59 
     60 	b.ID = "01FE96W293ZPRG9FQQP48HK8N001FE96W32AT24VYBGM12WN3GKB"
     61 	err = validate.Struct(b)
     62 	suite.EqualError(err, "Key: 'Block.ID' Error:Field validation for 'ID' failed on the 'ulid' tag")
     63 }
     64 
     65 func (suite *BlockValidateTestSuite) TestValidateBlockNoCreatedAt() {
     66 	b := happyBlock()
     67 
     68 	b.CreatedAt = time.Time{}
     69 	err := validate.Struct(b)
     70 	suite.NoError(err)
     71 }
     72 
     73 func (suite *BlockValidateTestSuite) TestValidateBlockCreatedByAccountID() {
     74 	b := happyBlock()
     75 
     76 	b.AccountID = ""
     77 	err := validate.Struct(b)
     78 	suite.EqualError(err, "Key: 'Block.AccountID' Error:Field validation for 'AccountID' failed on the 'required' tag")
     79 
     80 	b.AccountID = "this-is-not-a-valid-ulid"
     81 	err = validate.Struct(b)
     82 	suite.EqualError(err, "Key: 'Block.AccountID' Error:Field validation for 'AccountID' failed on the 'ulid' tag")
     83 }
     84 
     85 func (suite *BlockValidateTestSuite) TestValidateBlockTargetAccountID() {
     86 	b := happyBlock()
     87 
     88 	b.TargetAccountID = "invalid-ulid"
     89 	err := validate.Struct(b)
     90 	suite.EqualError(err, "Key: 'Block.TargetAccountID' Error:Field validation for 'TargetAccountID' failed on the 'ulid' tag")
     91 
     92 	b.TargetAccountID = "01FEEDHX4G7EGHF5GD9E82Y51Q"
     93 	err = validate.Struct(b)
     94 	suite.NoError(err)
     95 
     96 	b.TargetAccountID = ""
     97 	err = validate.Struct(b)
     98 	suite.EqualError(err, "Key: 'Block.TargetAccountID' Error:Field validation for 'TargetAccountID' failed on the 'required' tag")
     99 }
    100 
    101 func (suite *BlockValidateTestSuite) TestValidateBlockURI() {
    102 	b := happyBlock()
    103 
    104 	b.URI = "invalid-uri"
    105 	err := validate.Struct(b)
    106 	suite.EqualError(err, "Key: 'Block.URI' Error:Field validation for 'URI' failed on the 'url' tag")
    107 
    108 	b.URI = ""
    109 	err = validate.Struct(b)
    110 	suite.EqualError(err, "Key: 'Block.URI' Error:Field validation for 'URI' failed on the 'required' tag")
    111 }
    112 
    113 func TestBlockValidateTestSuite(t *testing.T) {
    114 	suite.Run(t, new(BlockValidateTestSuite))
    115 }