kopia lustrzana https://github.com/reiver/greatape
feat(app): 🦺 add username validator
rodzic
e1e30ffe93
commit
a273e3c7a0
|
@ -0,0 +1,21 @@
|
|||
package validators
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
|
||||
. "github.com/reiver/greatape/components/constants"
|
||||
)
|
||||
|
||||
func UsernameIsValid(username string) bool {
|
||||
if !regexp.MustCompile(USERNAME).MatchString(username) {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, reservedUsername := range ReservedUsernames {
|
||||
if username == reservedUsername {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
|
@ -0,0 +1,84 @@
|
|||
package validators_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/reiver/greatape/app/validators"
|
||||
)
|
||||
|
||||
func TestUsernameValidator(test *testing.T) {
|
||||
type arguments struct {
|
||||
username string
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
expectation bool
|
||||
arguments arguments
|
||||
}{
|
||||
{
|
||||
"Case1",
|
||||
false,
|
||||
arguments{
|
||||
username: "",
|
||||
},
|
||||
},
|
||||
{
|
||||
"Case2",
|
||||
false,
|
||||
arguments{
|
||||
username: "admin",
|
||||
},
|
||||
},
|
||||
{
|
||||
"Case3",
|
||||
true,
|
||||
arguments{
|
||||
username: "johnny",
|
||||
},
|
||||
},
|
||||
{
|
||||
"Case4",
|
||||
false,
|
||||
arguments{
|
||||
username: "webmaster",
|
||||
},
|
||||
},
|
||||
{
|
||||
"Case5",
|
||||
true,
|
||||
arguments{
|
||||
username: "susan235",
|
||||
},
|
||||
},
|
||||
{
|
||||
"Case6",
|
||||
true,
|
||||
arguments{
|
||||
username: "new_user",
|
||||
},
|
||||
},
|
||||
{
|
||||
"Case7",
|
||||
true,
|
||||
arguments{
|
||||
username: "someone",
|
||||
},
|
||||
},
|
||||
{
|
||||
"Case8",
|
||||
true,
|
||||
arguments{
|
||||
username: "north10star",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
test.Run(testCase.name, func(test *testing.T) {
|
||||
if result := validators.UsernameIsValid(testCase.arguments.username); result != testCase.expectation {
|
||||
test.Errorf("UsernameIsValid() = %v, expected %v", result, testCase.expectation)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Ładowanie…
Reference in New Issue