2023-03-12 15:00:57 +00:00
// GoToSocial
// Copyright (C) GoToSocial Authors admin@gotosocial.org
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
2021-04-01 18:46:45 +00:00
2021-09-01 16:29:25 +00:00
package validate_test
2021-04-01 18:46:45 +00:00
import (
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
2023-05-09 10:16:10 +00:00
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
2021-09-01 16:29:25 +00:00
"github.com/superseriousbusiness/gotosocial/internal/validate"
2021-04-01 18:46:45 +00:00
)
type ValidationTestSuite struct {
suite . Suite
}
func ( suite * ValidationTestSuite ) TestCheckPasswordStrength ( ) {
empty := ""
terriblePassword := "password"
weakPassword := "OKPassword"
shortPassword := "Ok12"
specialPassword := "Ok12%"
longPassword := "thisisafuckinglongpasswordbutnospecialchars"
2023-01-22 22:25:11 +00:00
tooLong := "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed euismod, ante id iaculis suscipit, nibh nibh varius enim, eget euismod augue augue eget mi. Praesent tincidunt, ex id finibus congue, enim nunc euismod nulla, id tincidunt ipsum neque at nunc. Sed id convallis libero. Sed euismod augue augue eget mi. Praesent tincidunt, ex id finibus congue, enim nunc euismod nulla, id tincidunt ipsum neque at nunc. Sed id convallis libero. Sed euismod augue augue eget mi. Praesent tincidunt, ex id finibus congue, enim nunc euismod nulla, id tincidunt ipsum neque at nunc."
2021-04-01 18:46:45 +00:00
strongPassword := "3dX5@Zc%mV*W2MBNEy$@"
var err error
2021-09-01 16:29:25 +00:00
err = validate . NewPassword ( empty )
2021-04-01 18:46:45 +00:00
if assert . Error ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , errors . New ( "no password provided" ) , err )
}
2021-09-01 16:29:25 +00:00
err = validate . NewPassword ( terriblePassword )
2021-04-01 18:46:45 +00:00
if assert . Error ( suite . T ( ) , err ) {
2022-08-08 08:40:51 +00:00
assert . Equal ( suite . T ( ) , errors . New ( "password is only 62% strength, try including more special characters, using uppercase letters, using numbers or using a longer password" ) , err )
2021-04-01 18:46:45 +00:00
}
2021-09-01 16:29:25 +00:00
err = validate . NewPassword ( weakPassword )
2021-04-01 18:46:45 +00:00
if assert . Error ( suite . T ( ) , err ) {
2022-08-08 08:40:51 +00:00
assert . Equal ( suite . T ( ) , errors . New ( "password is only 95% strength, try including more special characters, using numbers or using a longer password" ) , err )
2021-04-01 18:46:45 +00:00
}
2021-09-01 16:29:25 +00:00
err = validate . NewPassword ( shortPassword )
2021-04-01 18:46:45 +00:00
if assert . Error ( suite . T ( ) , err ) {
2022-08-08 08:40:51 +00:00
assert . Equal ( suite . T ( ) , errors . New ( "password is only 39% strength, try including more special characters or using a longer password" ) , err )
2021-04-01 18:46:45 +00:00
}
2021-09-01 16:29:25 +00:00
err = validate . NewPassword ( specialPassword )
2021-04-01 18:46:45 +00:00
if assert . Error ( suite . T ( ) , err ) {
2022-08-08 08:40:51 +00:00
assert . Equal ( suite . T ( ) , errors . New ( "password is only 53% strength, try including more special characters or using a longer password" ) , err )
2021-04-01 18:46:45 +00:00
}
2021-09-01 16:29:25 +00:00
err = validate . NewPassword ( longPassword )
2021-04-01 18:46:45 +00:00
if assert . NoError ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , nil , err )
}
2021-09-01 16:29:25 +00:00
err = validate . NewPassword ( tooLong )
2021-04-01 18:46:45 +00:00
if assert . Error ( suite . T ( ) , err ) {
2023-01-22 22:25:11 +00:00
assert . Equal ( suite . T ( ) , errors . New ( "password should be no more than 256 chars" ) , err )
2021-04-01 18:46:45 +00:00
}
2021-09-01 16:29:25 +00:00
err = validate . NewPassword ( strongPassword )
2021-04-01 18:46:45 +00:00
if assert . NoError ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , nil , err )
}
}
func ( suite * ValidationTestSuite ) TestValidateUsername ( ) {
empty := ""
tooLong := "holycrapthisisthelongestusernameiveeverseeninmylifethatstoomuchman"
withSpaces := "this username has spaces in it"
weirdChars := "thisusername&&&&&&&istooweird!!"
leadingSpace := " see_that_leading_space"
trailingSpace := "thisusername_ends_with_a_space "
newlines := "this_is\n_almost_ok"
goodUsername := "this_is_a_good_username"
var err error
2021-09-01 16:29:25 +00:00
err = validate . Username ( empty )
2023-05-07 17:53:21 +00:00
suite . EqualError ( err , "no username provided" )
2021-04-01 18:46:45 +00:00
2021-09-01 16:29:25 +00:00
err = validate . Username ( tooLong )
2023-05-07 17:53:21 +00:00
suite . EqualError ( err , fmt . Sprintf ( "given username %s was invalid: must contain only lowercase letters, numbers, and underscores, max 64 characters" , tooLong ) )
2021-04-01 18:46:45 +00:00
2021-09-01 16:29:25 +00:00
err = validate . Username ( withSpaces )
2023-05-07 17:53:21 +00:00
suite . EqualError ( err , fmt . Sprintf ( "given username %s was invalid: must contain only lowercase letters, numbers, and underscores, max 64 characters" , withSpaces ) )
2021-04-01 18:46:45 +00:00
2021-09-01 16:29:25 +00:00
err = validate . Username ( weirdChars )
2023-05-07 17:53:21 +00:00
suite . EqualError ( err , fmt . Sprintf ( "given username %s was invalid: must contain only lowercase letters, numbers, and underscores, max 64 characters" , weirdChars ) )
2021-04-01 18:46:45 +00:00
2021-09-01 16:29:25 +00:00
err = validate . Username ( leadingSpace )
2023-05-07 17:53:21 +00:00
suite . EqualError ( err , fmt . Sprintf ( "given username %s was invalid: must contain only lowercase letters, numbers, and underscores, max 64 characters" , leadingSpace ) )
2021-04-01 18:46:45 +00:00
2021-09-01 16:29:25 +00:00
err = validate . Username ( trailingSpace )
2023-05-07 17:53:21 +00:00
suite . EqualError ( err , fmt . Sprintf ( "given username %s was invalid: must contain only lowercase letters, numbers, and underscores, max 64 characters" , trailingSpace ) )
2021-04-01 18:46:45 +00:00
2021-09-01 16:29:25 +00:00
err = validate . Username ( newlines )
2023-05-07 17:53:21 +00:00
suite . EqualError ( err , fmt . Sprintf ( "given username %s was invalid: must contain only lowercase letters, numbers, and underscores, max 64 characters" , newlines ) )
2021-04-01 18:46:45 +00:00
2021-09-01 16:29:25 +00:00
err = validate . Username ( goodUsername )
2023-05-07 17:53:21 +00:00
suite . NoError ( err )
2021-04-01 18:46:45 +00:00
}
func ( suite * ValidationTestSuite ) TestValidateEmail ( ) {
empty := ""
notAnEmailAddress := "this-is-no-email-address!"
almostAnEmailAddress := "@thisisalmostan@email.address"
aWebsite := "https://thisisawebsite.com"
emailAddress := "thisis.actually@anemail.address"
var err error
2021-09-01 16:29:25 +00:00
err = validate . Email ( empty )
2021-04-01 18:46:45 +00:00
if assert . Error ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , errors . New ( "no email provided" ) , err )
}
2021-09-01 16:29:25 +00:00
err = validate . Email ( notAnEmailAddress )
2021-04-01 18:46:45 +00:00
if assert . Error ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , errors . New ( "mail: missing '@' or angle-addr" ) , err )
}
2021-09-01 16:29:25 +00:00
err = validate . Email ( almostAnEmailAddress )
2021-04-01 18:46:45 +00:00
if assert . Error ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , errors . New ( "mail: no angle-addr" ) , err )
}
2021-09-01 16:29:25 +00:00
err = validate . Email ( aWebsite )
2021-04-01 18:46:45 +00:00
if assert . Error ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , errors . New ( "mail: missing '@' or angle-addr" ) , err )
}
2021-09-01 16:29:25 +00:00
err = validate . Email ( emailAddress )
2021-04-01 18:46:45 +00:00
if assert . NoError ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , nil , err )
}
}
func ( suite * ValidationTestSuite ) TestValidateLanguage ( ) {
empty := ""
notALanguage := "this isn't a language at all!"
english := "en"
capitalEnglish := "EN"
arabic3Letters := "ara"
mixedCapsEnglish := "eN"
englishUS := "en-us"
dutch := "nl"
german := "de"
var err error
2021-09-01 16:29:25 +00:00
err = validate . Language ( empty )
2021-04-01 18:46:45 +00:00
if assert . Error ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , errors . New ( "no language provided" ) , err )
}
2021-09-01 16:29:25 +00:00
err = validate . Language ( notALanguage )
2021-04-01 18:46:45 +00:00
if assert . Error ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , errors . New ( "language: tag is not well-formed" ) , err )
}
2021-09-01 16:29:25 +00:00
err = validate . Language ( english )
2021-04-01 18:46:45 +00:00
if assert . NoError ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , nil , err )
}
2021-09-01 16:29:25 +00:00
err = validate . Language ( capitalEnglish )
2021-04-01 18:46:45 +00:00
if assert . NoError ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , nil , err )
}
2021-09-01 16:29:25 +00:00
err = validate . Language ( arabic3Letters )
2021-04-01 18:46:45 +00:00
if assert . NoError ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , nil , err )
}
2021-09-01 16:29:25 +00:00
err = validate . Language ( mixedCapsEnglish )
2021-04-01 18:46:45 +00:00
if assert . NoError ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , nil , err )
}
2021-09-01 16:29:25 +00:00
err = validate . Language ( englishUS )
2021-04-01 18:46:45 +00:00
if assert . Error ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , errors . New ( "language: tag is not well-formed" ) , err )
}
2021-09-01 16:29:25 +00:00
err = validate . Language ( dutch )
2021-04-01 18:46:45 +00:00
if assert . NoError ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , nil , err )
}
2021-09-01 16:29:25 +00:00
err = validate . Language ( german )
2021-04-01 18:46:45 +00:00
if assert . NoError ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , nil , err )
}
}
func ( suite * ValidationTestSuite ) TestValidateReason ( ) {
empty := ""
badReason := "because"
goodReason := "to smash the state and destroy capitalism ultimately and completely"
tooLong := "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris auctor mollis viverra. Maecenas maximus mollis sem, nec fermentum velit consectetur non. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Quisque a enim nibh. Vestibulum bibendum leo ac porttitor auctor. Curabitur velit tellus, facilisis vitae lorem a, ullamcorper efficitur leo. Sed a auctor tortor. Sed ut finibus ante, sit amet laoreet sapien. Donec ullamcorper tellus a nibh sodales vulputate. Donec id dolor eu odio mollis bibendum. Pellentesque habitant morbi tristique senectus et netus at."
2022-11-03 13:38:06 +00:00
unicode := "⎾⎿⏀⏁⏂⏃⏄⏅⏆⏇"
2021-04-01 18:46:45 +00:00
var err error
// check with no reason required
2021-09-01 16:29:25 +00:00
err = validate . SignUpReason ( empty , false )
2021-04-01 18:46:45 +00:00
if assert . NoError ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , nil , err )
}
2021-09-01 16:29:25 +00:00
err = validate . SignUpReason ( badReason , false )
2021-04-01 18:46:45 +00:00
if assert . NoError ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , nil , err )
}
2021-09-01 16:29:25 +00:00
err = validate . SignUpReason ( tooLong , false )
2021-04-01 18:46:45 +00:00
if assert . NoError ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , nil , err )
}
2021-09-01 16:29:25 +00:00
err = validate . SignUpReason ( goodReason , false )
2021-04-01 18:46:45 +00:00
if assert . NoError ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , nil , err )
}
2022-11-03 13:38:06 +00:00
err = validate . SignUpReason ( unicode , false )
if assert . NoError ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , nil , err )
}
2021-04-01 18:46:45 +00:00
// check with reason required
2021-09-01 16:29:25 +00:00
err = validate . SignUpReason ( empty , true )
2021-04-01 18:46:45 +00:00
if assert . Error ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , errors . New ( "no reason provided" ) , err )
}
2021-09-01 16:29:25 +00:00
err = validate . SignUpReason ( badReason , true )
2021-04-01 18:46:45 +00:00
if assert . Error ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , errors . New ( "reason should be at least 40 chars but 'because' was 7" ) , err )
}
2021-09-01 16:29:25 +00:00
err = validate . SignUpReason ( tooLong , true )
2021-04-01 18:46:45 +00:00
if assert . Error ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , errors . New ( "reason should be no more than 500 chars but given reason was 600" ) , err )
}
2021-09-01 16:29:25 +00:00
err = validate . SignUpReason ( goodReason , true )
2021-04-01 18:46:45 +00:00
if assert . NoError ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , nil , err )
}
}
2023-03-06 09:30:19 +00:00
func ( suite * ValidationTestSuite ) TestValidateProfileField ( ) {
2023-05-09 10:16:10 +00:00
var (
shortProfileField = "pronouns"
tooLongProfileField = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer eu bibendum elit. Sed ac interdum nisi. Vestibulum vulputate eros quis euismod imperdiet. Nulla sit amet dui sit amet lorem consectetur iaculis. Mauris eget lacinia metus. Curabitur nec dui eleifend massa nunc."
trimmedProfileField = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer eu bibendum elit. Sed ac interdum nisi. Vestibulum vulputate eros quis euismod imperdiet. Nulla sit amet dui sit amet lorem consectetur iaculis. Mauris eget lacinia metus. Curabitur nec dui "
err error
)
okFields := [ ] * gtsmodel . Field {
{
Name : "example" ,
Value : shortProfileField ,
} ,
}
err = validate . ProfileFields ( okFields )
suite . NoError ( err )
suite . Equal ( shortProfileField , okFields [ 0 ] . Value )
2023-03-06 09:30:19 +00:00
2023-05-09 10:16:10 +00:00
dodgyFields := [ ] * gtsmodel . Field {
{
Name : "example" ,
Value : tooLongProfileField ,
} ,
}
err = validate . ProfileFields ( dodgyFields )
suite . NoError ( err )
suite . Equal ( trimmedProfileField , dodgyFields [ 0 ] . Value )
suite . Len ( dodgyFields [ 0 ] . Value , 255 )
2023-03-06 09:30:19 +00:00
}
2021-04-01 18:46:45 +00:00
func TestValidationTestSuite ( t * testing . T ) {
suite . Run ( t , new ( ValidationTestSuite ) )
}