gtsocial-umbx

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

application_test.go (4323B)


      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 happyApplication() *gtsmodel.Application {
     30 	return &gtsmodel.Application{
     31 		ID:           "01FE91RJR88PSEEE30EV35QR8N",
     32 		CreatedAt:    time.Now(),
     33 		UpdatedAt:    time.Now(),
     34 		Name:         "Tusky",
     35 		Website:      "https://tusky.app",
     36 		RedirectURI:  "oauth2redirect://com.keylesspalace.tusky/",
     37 		ClientID:     "01FEEDMF6C0QD589MRK7919Z0R",
     38 		ClientSecret: "bd740cf1-024a-4e4d-8c39-866538f52fe6",
     39 		Scopes:       "read write follow",
     40 	}
     41 }
     42 
     43 type ApplicationValidateTestSuite struct {
     44 	suite.Suite
     45 }
     46 
     47 func (suite *ApplicationValidateTestSuite) TestValidateApplicationHappyPath() {
     48 	// no problem here
     49 	a := happyApplication()
     50 	err := validate.Struct(a)
     51 	suite.NoError(err)
     52 }
     53 
     54 func (suite *ApplicationValidateTestSuite) TestValidateApplicationBadID() {
     55 	a := happyApplication()
     56 
     57 	a.ID = ""
     58 	err := validate.Struct(a)
     59 	suite.EqualError(err, "Key: 'Application.ID' Error:Field validation for 'ID' failed on the 'required' tag")
     60 
     61 	a.ID = "01FE96W293ZPRG9FQQP48HK8N001FE96W32AT24VYBGM12WN3GKB"
     62 	err = validate.Struct(a)
     63 	suite.EqualError(err, "Key: 'Application.ID' Error:Field validation for 'ID' failed on the 'ulid' tag")
     64 }
     65 
     66 func (suite *ApplicationValidateTestSuite) TestValidateApplicationNoCreatedAt() {
     67 	a := happyApplication()
     68 
     69 	a.CreatedAt = time.Time{}
     70 	err := validate.Struct(a)
     71 	suite.NoError(err)
     72 }
     73 
     74 func (suite *ApplicationValidateTestSuite) TestValidateApplicationName() {
     75 	a := happyApplication()
     76 
     77 	a.Name = ""
     78 	err := validate.Struct(a)
     79 	suite.EqualError(err, "Key: 'Application.Name' Error:Field validation for 'Name' failed on the 'required' tag")
     80 }
     81 
     82 func (suite *ApplicationValidateTestSuite) TestValidateApplicationWebsite() {
     83 	a := happyApplication()
     84 
     85 	a.Website = "invalid-website"
     86 	err := validate.Struct(a)
     87 	suite.EqualError(err, "Key: 'Application.Website' Error:Field validation for 'Website' failed on the 'url' tag")
     88 
     89 	a.Website = ""
     90 	err = validate.Struct(a)
     91 	suite.NoError(err)
     92 }
     93 
     94 func (suite *ApplicationValidateTestSuite) TestValidateApplicationRedirectURI() {
     95 	a := happyApplication()
     96 
     97 	a.RedirectURI = "invalid-uri"
     98 	err := validate.Struct(a)
     99 	suite.EqualError(err, "Key: 'Application.RedirectURI' Error:Field validation for 'RedirectURI' failed on the 'uri' tag")
    100 
    101 	a.RedirectURI = ""
    102 	err = validate.Struct(a)
    103 	suite.EqualError(err, "Key: 'Application.RedirectURI' Error:Field validation for 'RedirectURI' failed on the 'required' tag")
    104 
    105 	a.RedirectURI = "urn:ietf:wg:oauth:2.0:oob"
    106 	err = validate.Struct(a)
    107 	suite.NoError(err)
    108 }
    109 
    110 func (suite *ApplicationValidateTestSuite) TestValidateApplicationClientSecret() {
    111 	a := happyApplication()
    112 
    113 	a.ClientSecret = "invalid-uuid"
    114 	err := validate.Struct(a)
    115 	suite.EqualError(err, "Key: 'Application.ClientSecret' Error:Field validation for 'ClientSecret' failed on the 'uuid' tag")
    116 
    117 	a.ClientSecret = ""
    118 	err = validate.Struct(a)
    119 	suite.EqualError(err, "Key: 'Application.ClientSecret' Error:Field validation for 'ClientSecret' failed on the 'required' tag")
    120 }
    121 
    122 func (suite *ApplicationValidateTestSuite) TestValidateApplicationScopes() {
    123 	a := happyApplication()
    124 
    125 	a.Scopes = ""
    126 	err := validate.Struct(a)
    127 	suite.EqualError(err, "Key: 'Application.Scopes' Error:Field validation for 'Scopes' failed on the 'required' tag")
    128 }
    129 
    130 func TestApplicationValidateTestSuite(t *testing.T) {
    131 	suite.Run(t, new(ApplicationValidateTestSuite))
    132 }