mirror of
https://github.com/ThisTine/Snip.git
synced 2026-08-18 23:18:47 +07:00
76 lines
1.8 KiB
Go
76 lines
1.8 KiB
Go
package service
|
|
|
|
import "testing"
|
|
|
|
func TestCodeFromSequenceUniqueAndShort(t *testing.T) {
|
|
seen := map[string]bool{}
|
|
for i := int64(1); i <= 5000; i++ {
|
|
c := CodeFromSequence(i)
|
|
if c == "" {
|
|
t.Fatalf("empty code for seq %d", i)
|
|
}
|
|
if seen[c] {
|
|
t.Fatalf("collision at seq %d -> %s", i, c)
|
|
}
|
|
seen[c] = true
|
|
if len(c) > 6 {
|
|
t.Fatalf("code unexpectedly long: %s (%d chars)", c, len(c))
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestValidateAlias(t *testing.T) {
|
|
cases := map[string]bool{ // alias -> valid?
|
|
"spring-launch": true,
|
|
"ab": false, // too short
|
|
"with space": false,
|
|
"good_one-2": true,
|
|
"api": false, // reserved
|
|
"dashboard": false, // reserved
|
|
"login": false, // reserved
|
|
}
|
|
for alias, valid := range cases {
|
|
err := ValidateAlias(alias)
|
|
if valid && err != nil {
|
|
t.Errorf("alias %q should be valid, got %v", alias, err)
|
|
}
|
|
if !valid && err == nil {
|
|
t.Errorf("alias %q should be invalid", alias)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestReservedBlocksAppPaths(t *testing.T) {
|
|
for _, p := range []string{"", "api", "login", "dashboard", "assets"} {
|
|
if !IsReserved(p) {
|
|
t.Errorf("%q must be reserved so it can't be claimed as a code", p)
|
|
}
|
|
}
|
|
if IsReserved("amber-otter-loop") {
|
|
t.Error("normal code should not be reserved")
|
|
}
|
|
}
|
|
|
|
func TestNormalizeAndValidateURL(t *testing.T) {
|
|
if got := NormalizeURL("acme.com/x"); got != "https://acme.com/x" {
|
|
t.Errorf("normalize added scheme wrong: %s", got)
|
|
}
|
|
if !ValidURL(NormalizeURL("acme.com/spring")) {
|
|
t.Error("expected valid url")
|
|
}
|
|
if ValidURL("not a url") {
|
|
t.Error("expected invalid url")
|
|
}
|
|
}
|
|
|
|
func TestValidPin(t *testing.T) {
|
|
if !ValidPin("123456") {
|
|
t.Error("123456 should be valid")
|
|
}
|
|
for _, bad := range []string{"12345", "1234567", "12a456", ""} {
|
|
if ValidPin(bad) {
|
|
t.Errorf("%q should be invalid", bad)
|
|
}
|
|
}
|
|
}
|