package blog import ( "strings" "testing" "time" gnoblog "gno.land/p/gnoland/blog/v0" "gno.land/p/nt/avl/v0" "gno.land/p/nt/testutils/v0" "gno.land/p/nt/uassert/v0" ) var ( hot = address("g1e7ct57zwke7z4l96gl25w5l0f34ljnn37gtp3w") cold = address("g1ecsuj0q572jr0dhu29q9njtnmw03hyu7tyyvv6") bob = testutils.TestAddress("bob") alice = testutils.TestAddress("alice") ) const ( d1 = "2026-12-05T00:00:00Z" d2 = "2026-12-06T00:00:00Z" ) // reset puts the realm back to its state right after deployment. func reset() { owner = cold pendingOwner = "" writers = avl.Tree{} writers.Set(hot.String(), true) b = &gnoblog.Blog{Title: "Jae Kwon", Prefix: "/r/nym-nobody777/blog:"} } func TestRenderOnlyKnownRoutes(cur realm, t *testing.T) { reset() for _, path := range []string{ "t/[click](https:evil.com)", "t/UPPER", "t/", "p/a b", "p/x", "x/abc", "?foo", "t/" + strings.Repeat("a", 101), } { uassert.Equal(t, "404", Render(path), path) } uassert.True(t, strings.Contains(Render(""), "No posts.")) } func TestWritersPost(cur realm, t *testing.T) { reset() testing.SetOriginCaller(bob) testing.SetRealm(testing.NewUserRealm(bob)) uassert.AbortsWithMessage(t, cur, "unauthorized: writers only", func() { Post(cross(cur), "hello", "Hello", "body", d1, "jaekwon", "intro") }) testing.SetOriginCaller(hot) testing.SetRealm(testing.NewUserRealm(hot)) uassert.NotAborts(t, cur, func() { Post(cross(cur), "hello", "Hello", "body", d1, "jaekwon", "intro, ai") }) testing.SetOriginCaller(cold) testing.SetRealm(testing.NewUserRealm(cold)) uassert.NotAborts(t, cur, func() { Post(cross(cur), "second", "Second", "body 2", d2, "", "") }) page := Render("p/hello") uassert.True(t, strings.Contains(page, "# Hello"), page) uassert.True(t, strings.Contains(page, "Written by jaekwon"), page) uassert.True(t, strings.Contains(page, "Published by "+hot.String()), page) uassert.True(t, strings.Contains(page, "[#ai](/r/nym-nobody777/blog:t/ai)"), page) uassert.True(t, strings.Contains(Render("t/intro"), "Hello")) uassert.True(t, strings.Contains(Render(""), "Second")) } func TestPostValidation(cur realm, t *testing.T) { reset() testing.SetOriginCaller(hot) testing.SetRealm(testing.NewUserRealm(hot)) uassert.AbortsWithMessage(t, cur, "slug must be 1 to 100 characters of a-z, 0-9, - or _", func() { Post(cross(cur), "Bad Slug", "T", "b", d1, "", "") }) uassert.AbortsWithMessage(t, cur, "tags must be 1 to 100 characters of a-z, 0-9, - or _", func() { Post(cross(cur), "ok", "T", "b", d1, "", "fine,[evil](x)") }) uassert.AbortsWithMessage(t, cur, "post title is missing", func() { Post(cross(cur), "ok", " ", "b", d1, "", "") }) } func TestEditLeavesNoStaleCopy(cur realm, t *testing.T) { reset() testing.SetOriginCaller(hot) testing.SetRealm(testing.NewUserRealm(hot)) Post(cross(cur), "hello", "Hello", "body", d1, "", "old") Edit(cross(cur), "hello", "Hello again", "new body", d2, "", "new") uassert.Equal(t, 1, b.Posts.Size()) uassert.Equal(t, 1, b.PostsPublished.Size()) uassert.Equal(t, 1, b.PostsAlphabetical.Size()) home := Render("") uassert.Equal(t, 1, strings.Count(home, "/r/nym-nobody777/blog:p/hello"), home) uassert.True(t, strings.Contains(home, "Hello again"), home) uassert.True(t, strings.Contains(Render("t/old"), "No posts.")) uassert.True(t, strings.Contains(Render("t/new"), "Hello again")) // An empty pubDate keeps the post's date. Edit(cross(cur), "hello", "Hello again", "newer body", "", "", "") uassert.Equal(t, d2, b.GetPost("hello").CreatedAt.Format(time.RFC3339)) testing.SetOriginCaller(bob) testing.SetRealm(testing.NewUserRealm(bob)) uassert.AbortsWithMessage(t, cur, "unauthorized: writers only", func() { Edit(cross(cur), "hello", "Hijacked", "x", "", "", "") }) testing.SetOriginCaller(hot) testing.SetRealm(testing.NewUserRealm(hot)) uassert.AbortsWithMessage(t, cur, "no such post", func() { Edit(cross(cur), "missing", "T", "b", "", "", "") }) } func TestRemove(cur realm, t *testing.T) { reset() testing.SetOriginCaller(hot) testing.SetRealm(testing.NewUserRealm(hot)) Post(cross(cur), "hello", "Hello", "body", d1, "", "") testing.SetOriginCaller(bob) testing.SetRealm(testing.NewUserRealm(bob)) uassert.AbortsWithMessage(t, cur, "unauthorized: writers only", func() { Remove(cross(cur), "hello") }) testing.SetOriginCaller(hot) testing.SetRealm(testing.NewUserRealm(hot)) Remove(cross(cur), "hello") uassert.Equal(t, "404", Render("p/hello")) uassert.True(t, strings.Contains(Render(""), "No posts.")) uassert.AbortsWithMessage(t, cur, "no such post", func() { Remove(cross(cur), "hello") }) } func TestOwnerManagesWriters(cur realm, t *testing.T) { reset() testing.SetOriginCaller(hot) testing.SetRealm(testing.NewUserRealm(hot)) uassert.AbortsWithMessage(t, cur, "unauthorized: owner only", func() { AddWriter(cross(cur), bob) }) testing.SetOriginCaller(cold) testing.SetRealm(testing.NewUserRealm(cold)) AddWriter(cross(cur), bob) uassert.True(t, IsWriter(bob)) testing.SetOriginCaller(bob) testing.SetRealm(testing.NewUserRealm(bob)) uassert.NotAborts(t, cur, func() { Post(cross(cur), "bobs", "Bob's", "b", d1, "", "") }) testing.SetOriginCaller(cold) testing.SetRealm(testing.NewUserRealm(cold)) RemoveWriter(cross(cur), bob) RemoveWriter(cross(cur), hot) uassert.False(t, IsWriter(bob)) uassert.False(t, IsWriter(hot)) uassert.True(t, IsWriter(cold)) uassert.AbortsWithMessage(t, cur, "not a writer", func() { RemoveWriter(cross(cur), bob) }) testing.SetOriginCaller(hot) testing.SetRealm(testing.NewUserRealm(hot)) uassert.AbortsWithMessage(t, cur, "unauthorized: writers only", func() { Post(cross(cur), "x", "X", "b", d2, "", "") }) } func TestOwnershipHandover(cur realm, t *testing.T) { reset() testing.SetOriginCaller(cold) testing.SetRealm(testing.NewUserRealm(cold)) ProposeOwner(cross(cur), alice) uassert.Equal(t, alice, PendingOwner()) testing.SetOriginCaller(bob) testing.SetRealm(testing.NewUserRealm(bob)) uassert.AbortsWithMessage(t, cur, "not the proposed owner", func() { AcceptOwnership(cross(cur)) }) testing.SetOriginCaller(alice) testing.SetRealm(testing.NewUserRealm(alice)) AcceptOwnership(cross(cur)) uassert.Equal(t, alice, Owner()) uassert.Equal(t, address(""), PendingOwner()) testing.SetOriginCaller(cold) testing.SetRealm(testing.NewUserRealm(cold)) uassert.AbortsWithMessage(t, cur, "unauthorized: owner only", func() { AddWriter(cross(cur), bob) }) testing.SetOriginCaller(alice) testing.SetRealm(testing.NewUserRealm(alice)) uassert.AbortsWithMessage(t, cur, "not the proposed owner", func() { AcceptOwnership(cross(cur)) }) } func TestCodeRealmCannotWrite(cur realm, t *testing.T) { reset() testing.SetOriginCaller(hot) testing.SetRealm(testing.NewCodeRealm("gno.land/r/someone/proxy")) uassert.AbortsWithMessage(t, cur, "must be called directly by a user", func() { Post(cross(cur), "hello", "Hello", "body", d1, "", "") }) }