gtsocial-umbx

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

domainblock_test.go (4052B)


      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 	"github.com/superseriousbusiness/gotosocial/testrig"
     28 )
     29 
     30 func happyDomainBlock() *gtsmodel.DomainBlock {
     31 	return &gtsmodel.DomainBlock{
     32 		ID:                 "01FE91RJR88PSEEE30EV35QR8N",
     33 		CreatedAt:          time.Now(),
     34 		UpdatedAt:          time.Now(),
     35 		Domain:             "baddudes.suck",
     36 		CreatedByAccountID: "01FEED79PRMVWPRMFHFQM8MJQN",
     37 		PrivateComment:     "we don't like em",
     38 		PublicComment:      "poo poo dudes",
     39 		Obfuscate:          testrig.FalseBool(),
     40 		SubscriptionID:     "",
     41 	}
     42 }
     43 
     44 type DomainBlockValidateTestSuite struct {
     45 	suite.Suite
     46 }
     47 
     48 func (suite *DomainBlockValidateTestSuite) TestValidateDomainBlockHappyPath() {
     49 	// no problem here
     50 	d := happyDomainBlock()
     51 	err := validate.Struct(d)
     52 	suite.NoError(err)
     53 }
     54 
     55 func (suite *DomainBlockValidateTestSuite) TestValidateDomainBlockBadID() {
     56 	d := happyDomainBlock()
     57 
     58 	d.ID = ""
     59 	err := validate.Struct(d)
     60 	suite.EqualError(err, "Key: 'DomainBlock.ID' Error:Field validation for 'ID' failed on the 'required' tag")
     61 
     62 	d.ID = "01FE96W293ZPRG9FQQP48HK8N001FE96W32AT24VYBGM12WN3GKB"
     63 	err = validate.Struct(d)
     64 	suite.EqualError(err, "Key: 'DomainBlock.ID' Error:Field validation for 'ID' failed on the 'ulid' tag")
     65 }
     66 
     67 func (suite *DomainBlockValidateTestSuite) TestValidateDomainBlockNoCreatedAt() {
     68 	d := happyDomainBlock()
     69 
     70 	d.CreatedAt = time.Time{}
     71 	err := validate.Struct(d)
     72 	suite.NoError(err)
     73 }
     74 
     75 func (suite *DomainBlockValidateTestSuite) TestValidateDomainBlockBadDomain() {
     76 	d := happyDomainBlock()
     77 
     78 	d.Domain = ""
     79 	err := validate.Struct(d)
     80 	suite.EqualError(err, "Key: 'DomainBlock.Domain' Error:Field validation for 'Domain' failed on the 'required' tag")
     81 
     82 	d.Domain = "this-is-not-a-valid-domain"
     83 	err = validate.Struct(d)
     84 	suite.EqualError(err, "Key: 'DomainBlock.Domain' Error:Field validation for 'Domain' failed on the 'fqdn' tag")
     85 }
     86 
     87 func (suite *DomainBlockValidateTestSuite) TestValidateDomainBlockCreatedByAccountID() {
     88 	d := happyDomainBlock()
     89 
     90 	d.CreatedByAccountID = ""
     91 	err := validate.Struct(d)
     92 	suite.EqualError(err, "Key: 'DomainBlock.CreatedByAccountID' Error:Field validation for 'CreatedByAccountID' failed on the 'required' tag")
     93 
     94 	d.CreatedByAccountID = "this-is-not-a-valid-ulid"
     95 	err = validate.Struct(d)
     96 	suite.EqualError(err, "Key: 'DomainBlock.CreatedByAccountID' Error:Field validation for 'CreatedByAccountID' failed on the 'ulid' tag")
     97 }
     98 
     99 func (suite *DomainBlockValidateTestSuite) TestValidateDomainBlockComments() {
    100 	d := happyDomainBlock()
    101 
    102 	d.PrivateComment = ""
    103 	d.PublicComment = ""
    104 	err := validate.Struct(d)
    105 	suite.NoError(err)
    106 }
    107 
    108 func (suite *DomainBlockValidateTestSuite) TestValidateDomainSubscriptionID() {
    109 	d := happyDomainBlock()
    110 
    111 	d.SubscriptionID = "invalid-ulid"
    112 	err := validate.Struct(d)
    113 	suite.EqualError(err, "Key: 'DomainBlock.SubscriptionID' Error:Field validation for 'SubscriptionID' failed on the 'ulid' tag")
    114 
    115 	d.SubscriptionID = "01FEEDHX4G7EGHF5GD9E82Y51Q"
    116 	err = validate.Struct(d)
    117 	suite.NoError(err)
    118 }
    119 
    120 func TestDomainBlockValidateTestSuite(t *testing.T) {
    121 	suite.Run(t, new(DomainBlockValidateTestSuite))
    122 }