token_test.go (3011B)
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 happyToken() *gtsmodel.Token { 30 return >smodel.Token{ 31 ID: "01FE91RJR88PSEEE30EV35QR8N", 32 CreatedAt: time.Now(), 33 UpdatedAt: time.Now(), 34 ClientID: "01FEEDMF6C0QD589MRK7919Z0R", 35 UserID: "01FEK0BFJKYXB4Y51RBQ7P5P79", 36 RedirectURI: "oauth2redirect://com.keylesspalace.tusky/", 37 Scope: "read write follow", 38 } 39 } 40 41 type TokenValidateTestSuite struct { 42 suite.Suite 43 } 44 45 func (suite *TokenValidateTestSuite) TestValidateTokenHappyPath() { 46 // no problem here 47 t := happyToken() 48 err := validate.Struct(t) 49 suite.NoError(err) 50 } 51 52 func (suite *TokenValidateTestSuite) TestValidateTokenBadID() { 53 t := happyToken() 54 55 t.ID = "" 56 err := validate.Struct(t) 57 suite.EqualError(err, "Key: 'Token.ID' Error:Field validation for 'ID' failed on the 'required' tag") 58 59 t.ID = "01FE96W293ZPRG9FQQP48HK8N001FE96W32AT24VYBGM12WN3GKB" 60 err = validate.Struct(t) 61 suite.EqualError(err, "Key: 'Token.ID' Error:Field validation for 'ID' failed on the 'ulid' tag") 62 } 63 64 func (suite *TokenValidateTestSuite) TestValidateTokenNoCreatedAt() { 65 t := happyToken() 66 67 t.CreatedAt = time.Time{} 68 err := validate.Struct(t) 69 suite.NoError(err) 70 } 71 72 func (suite *TokenValidateTestSuite) TestValidateTokenRedirectURI() { 73 t := happyToken() 74 75 t.RedirectURI = "invalid-uri" 76 err := validate.Struct(t) 77 suite.EqualError(err, "Key: 'Token.RedirectURI' Error:Field validation for 'RedirectURI' failed on the 'uri' tag") 78 79 t.RedirectURI = "" 80 err = validate.Struct(t) 81 suite.EqualError(err, "Key: 'Token.RedirectURI' Error:Field validation for 'RedirectURI' failed on the 'required' tag") 82 83 t.RedirectURI = "urn:ietf:wg:oauth:2.0:oob" 84 err = validate.Struct(t) 85 suite.NoError(err) 86 } 87 88 func (suite *TokenValidateTestSuite) TestValidateTokenScope() { 89 t := happyToken() 90 91 t.Scope = "" 92 err := validate.Struct(t) 93 suite.EqualError(err, "Key: 'Token.Scope' Error:Field validation for 'Scope' failed on the 'required' tag") 94 } 95 96 func TestTokenValidateTestSuite(t *testing.T) { 97 suite.Run(t, new(TokenValidateTestSuite)) 98 }