add first exiting test
This commit is contained in:
@@ -1,42 +1,10 @@
|
||||
package handler
|
||||
|
||||
import "fmt"
|
||||
|
||||
func validateUsername(username string) error {
|
||||
if username == "" {
|
||||
return fmt.Errorf("please enter the username")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validatePasswords(pOne, pTwo string) error {
|
||||
if pOne == "" || pTwo == "" {
|
||||
return fmt.Errorf("please enter the password twice")
|
||||
}
|
||||
if !(pOne == pTwo) {
|
||||
return fmt.Errorf("the two passwords entered were not the same")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateAPIKey(apiKey, secret string) error {
|
||||
if apiKey == "" || secret == "" {
|
||||
return fmt.Errorf("please enter both the api key and secret")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func firstExisting(or string, strings ...string) string {
|
||||
current := ""
|
||||
for _, s := range strings {
|
||||
if s == "" {
|
||||
continue
|
||||
if s != "" {
|
||||
return s
|
||||
}
|
||||
current = s
|
||||
break
|
||||
}
|
||||
if current == "" {
|
||||
return or
|
||||
}
|
||||
return current
|
||||
return or
|
||||
}
|
||||
|
||||
37
server/handler/handler_admin_utils_test.go
Normal file
37
server/handler/handler_admin_utils_test.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFirstExisting(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
values []string
|
||||
or string
|
||||
exp string
|
||||
}{
|
||||
{"none present",
|
||||
[]string{"one", "two", "three"}, "default",
|
||||
"one"},
|
||||
{"first missing",
|
||||
[]string{"", "two", "three"}, "default",
|
||||
"two"},
|
||||
{"middle missing",
|
||||
[]string{"", "two", ""}, "default",
|
||||
"two"},
|
||||
{"all missing",
|
||||
[]string{"", "", ""}, "default",
|
||||
"default"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
tc := tc // capture range variable?
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
actu := firstExisting(tc.or, tc.values...)
|
||||
if actu != tc.exp {
|
||||
t.Errorf("expected %q, got %q", tc.exp, actu)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
27
server/handler/handler_admin_validate.go
Normal file
27
server/handler/handler_admin_validate.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package handler
|
||||
|
||||
import "fmt"
|
||||
|
||||
func validateUsername(username string) error {
|
||||
if username == "" {
|
||||
return fmt.Errorf("please enter the username")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validatePasswords(pOne, pTwo string) error {
|
||||
if pOne == "" || pTwo == "" {
|
||||
return fmt.Errorf("please enter the password twice")
|
||||
}
|
||||
if !(pOne == pTwo) {
|
||||
return fmt.Errorf("the two passwords entered were not the same")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateAPIKey(apiKey, secret string) error {
|
||||
if apiKey == "" || secret == "" {
|
||||
return fmt.Errorf("please enter both the api key and secret")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user