feat(auth): implement user and device authentication
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNormalizeUsernameAndValidateUserInput(t *testing.T) {
|
||||
if got := NormalizeUsername(" Admin.User "); got != "admin.user" {
|
||||
t.Fatalf("NormalizeUsername() = %q", got)
|
||||
}
|
||||
if err := ValidateUserInput(
|
||||
"admin.user",
|
||||
"strong-password",
|
||||
UserRoleAdmin,
|
||||
); err != nil {
|
||||
t.Fatalf("ValidateUserInput(valid) error = %v", err)
|
||||
}
|
||||
for name, input := range map[string]struct {
|
||||
username string
|
||||
password string
|
||||
role UserRole
|
||||
}{
|
||||
"blank username": {" ", "password", UserRoleAdmin},
|
||||
"blank password": {"admin", "", UserRoleAdmin},
|
||||
"short password": {"admin", "short", UserRoleAdmin},
|
||||
"invalid password UTF-8": {
|
||||
"admin",
|
||||
string([]byte{0xff, 0xfe, 0xfd}),
|
||||
UserRoleAdmin,
|
||||
},
|
||||
"oversized password": {
|
||||
"admin",
|
||||
strings.Repeat("x", MaxPasswordBytes+1),
|
||||
UserRoleAdmin,
|
||||
},
|
||||
"invalid role": {"admin", "password", "AUDITOR"},
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
if err := ValidateUserInput(
|
||||
input.username,
|
||||
input.password,
|
||||
input.role,
|
||||
); err == nil {
|
||||
t.Fatal("ValidateUserInput() error = nil")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateDeviceName(t *testing.T) {
|
||||
if err := ValidateDeviceName("Test device"); err != nil {
|
||||
t.Fatalf("ValidateDeviceName(valid) error = %v", err)
|
||||
}
|
||||
if err := ValidateDeviceName(" "); err == nil {
|
||||
t.Fatal("ValidateDeviceName(blank) error = nil")
|
||||
}
|
||||
if err := ValidateDeviceName(
|
||||
strings.Repeat("x", MaxDeviceNameBytes+1),
|
||||
); err == nil {
|
||||
t.Fatal("ValidateDeviceName(oversized) error = nil")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user