Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

urlshort_test.gno

2.95 Kb · 116 lines
  1package urlshort
  2
  3import (
  4	"strings"
  5	"testing"
  6
  7	"gno.land/p/nt/testutils/v0"
  8	"gno.land/p/nt/uassert/v0"
  9)
 10
 11func TestIsAlphanumeric(t *testing.T) {
 12	cases := []struct {
 13		in   string
 14		want bool
 15	}{
 16		{"", false},
 17		{"abc", true},
 18		{"ABC123", true},
 19		{"a b", false},
 20		{"a-b", false},
 21		{"under_score", false},
 22		{"9", true},
 23	}
 24	for _, c := range cases {
 25		if got := isAlphanumeric(c.in); got != c.want {
 26			t.Errorf("isAlphanumeric(%q) = %v, want %v", c.in, got, c.want)
 27		}
 28	}
 29}
 30
 31func TestItoa(t *testing.T) {
 32	cases := []struct {
 33		in   int
 34		want string
 35	}{
 36		{0, "0"},
 37		{7, "7"},
 38		{42, "42"},
 39		{1000, "1000"},
 40	}
 41	for _, c := range cases {
 42		if got := itoa(c.in); got != c.want {
 43			t.Errorf("itoa(%d) = %q, want %q", c.in, got, c.want)
 44		}
 45	}
 46}
 47
 48func TestShortenAndRender(cur realm, t *testing.T) {
 49	alice := testutils.TestAddress("alice")
 50	testing.SetRealm(testing.NewUserRealm(alice))
 51
 52	Shorten(cross(cur), "gno", "https://gno.land", "the chain")
 53
 54	url, ok := Lookup("gno")
 55	uassert.True(t, ok, "alias should exist")
 56	uassert.Equal(t, "https://gno.land", url)
 57
 58	// Detail render bumps the click counter.
 59	out := Render("/gno")
 60	uassert.True(t, strings.Contains(out, "https://gno.land"), "detail shows url")
 61	uassert.True(t, strings.Contains(out, "Clicks:** 1"), "click counted")
 62
 63	// Index lists the alias.
 64	idx := Render("/")
 65	uassert.True(t, strings.Contains(idx, "[gno](/gno)"), "index lists alias")
 66}
 67
 68func TestShortenOwnershipRules(cur realm, t *testing.T) {
 69	alice := testutils.TestAddress("alice2")
 70	bob := testutils.TestAddress("bob2")
 71
 72	testing.SetRealm(testing.NewUserRealm(alice))
 73	Shorten(cross(cur), "mine", "https://a.example", "")
 74
 75	// Bob cannot overwrite alice's alias.
 76	testing.SetRealm(testing.NewUserRealm(bob))
 77	uassert.AbortsWithMessage(t, cur, "alias already taken by another owner", func() {
 78		Shorten(cross(cur), "mine", "https://b.example", "")
 79	})
 80
 81	// Alice can update her own.
 82	testing.SetRealm(testing.NewUserRealm(alice))
 83	Shorten(cross(cur), "mine", "https://a2.example", "updated")
 84	url, _ := Lookup("mine")
 85	uassert.Equal(t, "https://a2.example", url)
 86
 87	// Invalid alias aborts.
 88	uassert.AbortsWithMessage(t, cur, "alias must be non-empty and alphanumeric", func() {
 89		Shorten(cross(cur), "bad alias", "https://x.example", "")
 90	})
 91
 92	// Empty url aborts.
 93	uassert.AbortsWithMessage(t, cur, "url must be non-empty", func() {
 94		Shorten(cross(cur), "okalias", "", "")
 95	})
 96}
 97
 98func TestRemoveRules(cur realm, t *testing.T) {
 99	alice := testutils.TestAddress("alice3")
100	bob := testutils.TestAddress("bob3")
101
102	testing.SetRealm(testing.NewUserRealm(alice))
103	Shorten(cross(cur), "rm", "https://rm.example", "")
104
105	// Bob cannot remove alice's alias.
106	testing.SetRealm(testing.NewUserRealm(bob))
107	uassert.AbortsWithMessage(t, cur, "only the owner can remove an alias", func() {
108		Remove(cross(cur), "rm")
109	})
110
111	// Alice removes her own.
112	testing.SetRealm(testing.NewUserRealm(alice))
113	Remove(cross(cur), "rm")
114	_, ok := Lookup("rm")
115	uassert.False(t, ok, "alias should be gone")
116}