gtsocial-umbx

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

client_test.go (3119B)


      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 happyClient() *gtsmodel.Client {
     30 	return &gtsmodel.Client{
     31 		ID:        "01FE91RJR88PSEEE30EV35QR8N",
     32 		CreatedAt: time.Now(),
     33 		UpdatedAt: time.Now(),
     34 		Secret:    "bd740cf1-024a-4e4d-8c39-866538f52fe6",
     35 		Domain:    "oauth2redirect://com.keylesspalace.tusky/",
     36 		UserID:    "01FEEDMF6C0QD589MRK7919Z0R",
     37 	}
     38 }
     39 
     40 type ClientValidateTestSuite struct {
     41 	suite.Suite
     42 }
     43 
     44 func (suite *ClientValidateTestSuite) TestValidateClientHappyPath() {
     45 	// no problem here
     46 	c := happyClient()
     47 	err := validate.Struct(c)
     48 	suite.NoError(err)
     49 }
     50 
     51 func (suite *ClientValidateTestSuite) TestValidateClientBadID() {
     52 	c := happyClient()
     53 
     54 	c.ID = ""
     55 	err := validate.Struct(c)
     56 	suite.EqualError(err, "Key: 'Client.ID' Error:Field validation for 'ID' failed on the 'required' tag")
     57 
     58 	c.ID = "01FE96W293ZPRG9FQQP48HK8N001FE96W32AT24VYBGM12WN3GKB"
     59 	err = validate.Struct(c)
     60 	suite.EqualError(err, "Key: 'Client.ID' Error:Field validation for 'ID' failed on the 'ulid' tag")
     61 }
     62 
     63 func (suite *ClientValidateTestSuite) TestValidateClientNoCreatedAt() {
     64 	c := happyClient()
     65 
     66 	c.CreatedAt = time.Time{}
     67 	err := validate.Struct(c)
     68 	suite.NoError(err)
     69 }
     70 
     71 func (suite *ClientValidateTestSuite) TestValidateClientDomain() {
     72 	c := happyClient()
     73 
     74 	c.Domain = "invalid-uri"
     75 	err := validate.Struct(c)
     76 	suite.EqualError(err, "Key: 'Client.Domain' Error:Field validation for 'Domain' failed on the 'uri' tag")
     77 
     78 	c.Domain = ""
     79 	err = validate.Struct(c)
     80 	suite.EqualError(err, "Key: 'Client.Domain' Error:Field validation for 'Domain' failed on the 'required' tag")
     81 
     82 	c.Domain = "urn:ietf:wg:oauth:2.0:oob"
     83 	err = validate.Struct(c)
     84 	suite.NoError(err)
     85 }
     86 
     87 func (suite *ClientValidateTestSuite) TestValidateSecret() {
     88 	c := happyClient()
     89 
     90 	c.Secret = "invalid-uuid"
     91 	err := validate.Struct(c)
     92 	suite.EqualError(err, "Key: 'Client.Secret' Error:Field validation for 'Secret' failed on the 'uuid' tag")
     93 
     94 	c.Secret = ""
     95 	err = validate.Struct(c)
     96 	suite.EqualError(err, "Key: 'Client.Secret' Error:Field validation for 'Secret' failed on the 'required' tag")
     97 }
     98 
     99 func TestClientValidateTestSuite(t *testing.T) {
    100 	suite.Run(t, new(ClientValidateTestSuite))
    101 }