Extend usernameRE to [a-z0-9_.-], preserve dots in SanitizeForUsername, and add tests for validation, sanitization, and nip05 sync precedence. Co-authored-by: Cursor <cursoragent@cursor.com>
59 lines
1.3 KiB
Go
59 lines
1.3 KiB
Go
package user
|
|
|
|
import "testing"
|
|
|
|
func TestValidateUsername(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
ok bool
|
|
}{
|
|
{"alice", true},
|
|
{"al-ice_42", true},
|
|
{"a", true},
|
|
{"alice.bob", true},
|
|
{"alice.smith.42", true},
|
|
{"", false},
|
|
{"-alice", false},
|
|
{"_alice", false},
|
|
{".alice", false},
|
|
{"thisusernameiswaytoolongtobevalid12345", false},
|
|
{"admin", false},
|
|
}
|
|
reserved := []string{"admin", "root"}
|
|
for _, tc := range cases {
|
|
err := ValidateUsername(tc.name, reserved)
|
|
if tc.ok && err != nil {
|
|
t.Errorf("%q expected ok, got %v", tc.name, err)
|
|
}
|
|
if !tc.ok && err == nil {
|
|
t.Errorf("%q expected fail", tc.name)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestProvisionalUsername(t *testing.T) {
|
|
const pk = "0e8c41ebcd55a8d8db2e0a8c3a4b9c5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b1c"
|
|
got := ProvisionalUsername(pk)
|
|
want := "u_0e8c41ebcd55a8d8"
|
|
if got != want {
|
|
t.Fatalf("got %q want %q", got, want)
|
|
}
|
|
if err := ValidateUsername(got, nil); err != nil {
|
|
t.Fatalf("provisional name should validate: %v", err)
|
|
}
|
|
|
|
short := ProvisionalUsername("AbC")
|
|
if short != "u_abc" {
|
|
t.Errorf("expected lowercase trimmed prefix, got %q", short)
|
|
}
|
|
}
|
|
|
|
func TestSubscriptionType(t *testing.T) {
|
|
if !SubYearly.Valid() || !SubLifetime.Valid() {
|
|
t.Fatal("valid types reported invalid")
|
|
}
|
|
if SubscriptionType("monthly").Valid() {
|
|
t.Fatal("invalid type reported valid")
|
|
}
|
|
}
|