forked from baron/baron-sso
95 lines
1.9 KiB
Go
95 lines
1.9 KiB
Go
package utils
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestValidateSlug_Valid(t *testing.T) {
|
|
ok, msg := ValidateSlug("tenant-2026")
|
|
if !ok {
|
|
t.Fatalf("expected valid slug, got error: %s", msg)
|
|
}
|
|
}
|
|
|
|
func TestValidateSlug_ReservedKeywords(t *testing.T) {
|
|
cases := []string{
|
|
"stage",
|
|
"prod",
|
|
"metrics",
|
|
"prometheus",
|
|
"webmaster",
|
|
" Stage ",
|
|
}
|
|
|
|
for _, slug := range cases {
|
|
t.Run(slug, func(t *testing.T) {
|
|
ok, msg := ValidateSlug(slug)
|
|
if ok {
|
|
t.Fatalf("expected reserved slug to be rejected: %q", slug)
|
|
}
|
|
if msg != "slug is a reserved keyword" {
|
|
t.Fatalf("unexpected error message: %s", msg)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestValidateSlug_LengthRules(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
slug string
|
|
}{
|
|
{name: "too short", slug: "ab"},
|
|
{name: "too long", slug: strings.Repeat("a", 33)},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
ok, msg := ValidateSlug(tc.slug)
|
|
if ok {
|
|
t.Fatalf("expected invalid length slug: %q", tc.slug)
|
|
}
|
|
if msg != "slug must be between 3 and 32 characters" {
|
|
t.Fatalf("unexpected error message: %s", msg)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestValidateSlug_FormatRules(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
slug string
|
|
wantMsg string
|
|
}{
|
|
{
|
|
name: "invalid character",
|
|
slug: "tenant_name",
|
|
wantMsg: "slug can only contain lowercase letters, numbers, and hyphens",
|
|
},
|
|
{
|
|
name: "leading hyphen",
|
|
slug: "-tenant",
|
|
wantMsg: "slug cannot start or end with a hyphen",
|
|
},
|
|
{
|
|
name: "trailing hyphen",
|
|
slug: "tenant-",
|
|
wantMsg: "slug cannot start or end with a hyphen",
|
|
},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
ok, msg := ValidateSlug(tc.slug)
|
|
if ok {
|
|
t.Fatalf("expected invalid slug: %q", tc.slug)
|
|
}
|
|
if msg != tc.wantMsg {
|
|
t.Fatalf("unexpected error message: %s", msg)
|
|
}
|
|
})
|
|
}
|
|
}
|