2021-04-01 21:46:45 +03:00
/ *
GoToSocial
Copyright ( C ) 2021 GoToSocial Authors admin @ gotosocial . org
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-05-08 15:25:55 +03:00
package util_test
2021-04-01 21:46:45 +03:00
import (
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
2021-05-08 15:25:55 +03:00
"github.com/superseriousbusiness/gotosocial/internal/util"
2021-04-01 21:46:45 +03:00
)
type ValidationTestSuite struct {
suite . Suite
}
func ( suite * ValidationTestSuite ) TestCheckPasswordStrength ( ) {
empty := ""
terriblePassword := "password"
weakPassword := "OKPassword"
shortPassword := "Ok12"
specialPassword := "Ok12%"
longPassword := "thisisafuckinglongpasswordbutnospecialchars"
tooLong := "Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Quisque a enim nibh. Vestibulum bibendum leo ac porttitor auctor."
strongPassword := "3dX5@Zc%mV*W2MBNEy$@"
var err error
2021-05-08 15:25:55 +03:00
err = util . ValidateNewPassword ( empty )
2021-04-01 21:46:45 +03:00
if assert . Error ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , errors . New ( "no password provided" ) , err )
}
2021-05-08 15:25:55 +03:00
err = util . ValidateNewPassword ( terriblePassword )
2021-04-01 21:46:45 +03:00
if assert . Error ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , errors . New ( "insecure password, try including more special characters, using uppercase letters, using numbers or using a longer password" ) , err )
}
2021-05-08 15:25:55 +03:00
err = util . ValidateNewPassword ( weakPassword )
2021-04-01 21:46:45 +03:00
if assert . Error ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , errors . New ( "insecure password, try including more special characters, using numbers or using a longer password" ) , err )
}
2021-05-08 15:25:55 +03:00
err = util . ValidateNewPassword ( shortPassword )
2021-04-01 21:46:45 +03:00
if assert . Error ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , errors . New ( "insecure password, try including more special characters or using a longer password" ) , err )
}
2021-05-08 15:25:55 +03:00
err = util . ValidateNewPassword ( specialPassword )
2021-04-01 21:46:45 +03:00
if assert . Error ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , errors . New ( "insecure password, try including more special characters or using a longer password" ) , err )
}
2021-05-08 15:25:55 +03:00
err = util . ValidateNewPassword ( longPassword )
2021-04-01 21:46:45 +03:00
if assert . NoError ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , nil , err )
}
2021-05-08 15:25:55 +03:00
err = util . ValidateNewPassword ( tooLong )
2021-04-01 21:46:45 +03:00
if assert . Error ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , errors . New ( "password should be no more than 64 chars" ) , err )
}
2021-05-08 15:25:55 +03:00
err = util . ValidateNewPassword ( strongPassword )
2021-04-01 21:46:45 +03: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-05-08 15:25:55 +03:00
err = util . ValidateUsername ( empty )
2021-04-01 21:46:45 +03:00
if assert . Error ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , errors . New ( "no username provided" ) , err )
}
2021-05-08 15:25:55 +03:00
err = util . ValidateUsername ( tooLong )
2021-04-01 21:46:45 +03:00
if assert . Error ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , fmt . Errorf ( "username should be no more than 64 chars but '%s' was 66" , tooLong ) , err )
}
2021-05-08 15:25:55 +03:00
err = util . ValidateUsername ( withSpaces )
2021-04-01 21:46:45 +03:00
if assert . Error ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , fmt . Errorf ( "given username %s was invalid: must contain only lowercase letters, numbers, and underscores" , withSpaces ) , err )
}
2021-05-08 15:25:55 +03:00
err = util . ValidateUsername ( weirdChars )
2021-04-01 21:46:45 +03:00
if assert . Error ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , fmt . Errorf ( "given username %s was invalid: must contain only lowercase letters, numbers, and underscores" , weirdChars ) , err )
}
2021-05-08 15:25:55 +03:00
err = util . ValidateUsername ( leadingSpace )
2021-04-01 21:46:45 +03:00
if assert . Error ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , fmt . Errorf ( "given username %s was invalid: must contain only lowercase letters, numbers, and underscores" , leadingSpace ) , err )
}
2021-05-08 15:25:55 +03:00
err = util . ValidateUsername ( trailingSpace )
2021-04-01 21:46:45 +03:00
if assert . Error ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , fmt . Errorf ( "given username %s was invalid: must contain only lowercase letters, numbers, and underscores" , trailingSpace ) , err )
}
2021-05-08 15:25:55 +03:00
err = util . ValidateUsername ( newlines )
2021-04-01 21:46:45 +03:00
if assert . Error ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , fmt . Errorf ( "given username %s was invalid: must contain only lowercase letters, numbers, and underscores" , newlines ) , err )
}
2021-05-08 15:25:55 +03:00
err = util . ValidateUsername ( goodUsername )
2021-04-01 21:46:45 +03:00
if assert . NoError ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , nil , err )
}
}
func ( suite * ValidationTestSuite ) TestValidateEmail ( ) {
empty := ""
notAnEmailAddress := "this-is-no-email-address!"
almostAnEmailAddress := "@thisisalmostan@email.address"
aWebsite := "https://thisisawebsite.com"
tooLong := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhggggggggggggggggggggggggggggggggggggggghhhhhhhhhhhhhhhhhggggggggggggggggggggghhhhhhhhhhhhhhhhhhhhhhhhhhhhhh@gmail.com"
emailAddress := "thisis.actually@anemail.address"
var err error
2021-05-08 15:25:55 +03:00
err = util . ValidateEmail ( empty )
2021-04-01 21:46:45 +03:00
if assert . Error ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , errors . New ( "no email provided" ) , err )
}
2021-05-08 15:25:55 +03:00
err = util . ValidateEmail ( notAnEmailAddress )
2021-04-01 21:46:45 +03:00
if assert . Error ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , errors . New ( "mail: missing '@' or angle-addr" ) , err )
}
2021-05-08 15:25:55 +03:00
err = util . ValidateEmail ( almostAnEmailAddress )
2021-04-01 21:46:45 +03:00
if assert . Error ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , errors . New ( "mail: no angle-addr" ) , err )
}
2021-05-08 15:25:55 +03:00
err = util . ValidateEmail ( aWebsite )
2021-04-01 21:46:45 +03:00
if assert . Error ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , errors . New ( "mail: missing '@' or angle-addr" ) , err )
}
2021-05-08 15:25:55 +03:00
err = util . ValidateEmail ( tooLong )
2021-04-01 21:46:45 +03:00
if assert . Error ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , fmt . Errorf ( "email address should be no more than 256 chars but '%s' was 286" , tooLong ) , err )
}
2021-05-08 15:25:55 +03:00
err = util . ValidateEmail ( emailAddress )
2021-04-01 21:46:45 +03: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-05-08 15:25:55 +03:00
err = util . ValidateLanguage ( empty )
2021-04-01 21:46:45 +03:00
if assert . Error ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , errors . New ( "no language provided" ) , err )
}
2021-05-08 15:25:55 +03:00
err = util . ValidateLanguage ( notALanguage )
2021-04-01 21:46:45 +03:00
if assert . Error ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , errors . New ( "language: tag is not well-formed" ) , err )
}
2021-05-08 15:25:55 +03:00
err = util . ValidateLanguage ( english )
2021-04-01 21:46:45 +03:00
if assert . NoError ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , nil , err )
}
2021-05-08 15:25:55 +03:00
err = util . ValidateLanguage ( capitalEnglish )
2021-04-01 21:46:45 +03:00
if assert . NoError ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , nil , err )
}
2021-05-08 15:25:55 +03:00
err = util . ValidateLanguage ( arabic3Letters )
2021-04-01 21:46:45 +03:00
if assert . NoError ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , nil , err )
}
2021-05-08 15:25:55 +03:00
err = util . ValidateLanguage ( mixedCapsEnglish )
2021-04-01 21:46:45 +03:00
if assert . NoError ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , nil , err )
}
2021-05-08 15:25:55 +03:00
err = util . ValidateLanguage ( englishUS )
2021-04-01 21:46:45 +03:00
if assert . Error ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , errors . New ( "language: tag is not well-formed" ) , err )
}
2021-05-08 15:25:55 +03:00
err = util . ValidateLanguage ( dutch )
2021-04-01 21:46:45 +03:00
if assert . NoError ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , nil , err )
}
2021-05-08 15:25:55 +03:00
err = util . ValidateLanguage ( german )
2021-04-01 21:46:45 +03: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."
var err error
// check with no reason required
2021-05-08 15:25:55 +03:00
err = util . ValidateSignUpReason ( empty , false )
2021-04-01 21:46:45 +03:00
if assert . NoError ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , nil , err )
}
2021-05-08 15:25:55 +03:00
err = util . ValidateSignUpReason ( badReason , false )
2021-04-01 21:46:45 +03:00
if assert . NoError ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , nil , err )
}
2021-05-08 15:25:55 +03:00
err = util . ValidateSignUpReason ( tooLong , false )
2021-04-01 21:46:45 +03:00
if assert . NoError ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , nil , err )
}
2021-05-08 15:25:55 +03:00
err = util . ValidateSignUpReason ( goodReason , false )
2021-04-01 21:46:45 +03:00
if assert . NoError ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , nil , err )
}
// check with reason required
2021-05-08 15:25:55 +03:00
err = util . ValidateSignUpReason ( empty , true )
2021-04-01 21:46:45 +03:00
if assert . Error ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , errors . New ( "no reason provided" ) , err )
}
2021-05-08 15:25:55 +03:00
err = util . ValidateSignUpReason ( badReason , true )
2021-04-01 21:46:45 +03: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-05-08 15:25:55 +03:00
err = util . ValidateSignUpReason ( tooLong , true )
2021-04-01 21:46:45 +03: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-05-08 15:25:55 +03:00
err = util . ValidateSignUpReason ( goodReason , true )
2021-04-01 21:46:45 +03:00
if assert . NoError ( suite . T ( ) , err ) {
assert . Equal ( suite . T ( ) , nil , err )
}
}
func TestValidationTestSuite ( t * testing . T ) {
suite . Run ( t , new ( ValidationTestSuite ) )
}