Skip to content
quarry Artifact [31e7b102fe]
Login

Artifact [31e7b102fe]

Artifact 31e7b102fe867c19053c0e92f586652059dceeb55e6f7339ed45e1cff13cc7a3:


package api

import (
	"context"
	"testing"

	"craftdesign.group/quarry/internal/ops"
)

// modForumStub answers only the one question these tests ask. Every
// other ForumStore method is unreachable from the moderation state path
// and stays unimplemented on purpose, so a future call from this path
// fails loudly rather than silently.
type modForumStub struct {
	ForumStore
	on bool
}

func (m modForumStub) ModerationEnabled(string) bool { return m.on }

// A CALLER WITH NO ACCOUNT IS NOT A POSTER.
//
// The guard for this was written and could not fire. forumModerationFor
// tested `who == ""`, and every caller passed author(ctx), which returns
// the NAME "anonymous" for a signed-out caller and never the empty
// string — so the branch was unreachable and a stranger was told their
// posts would be held, which is a claim about an account that does not
// exist.
//
// The sentinel is a name; whether there is an account is a different
// fact, and only that one has an empty spelling. forumModerationFor now
// takes ops.User(ctx) and says so.
func TestASignedOutReaderIsNeverToldTheirPostsWouldBeHeld(t *testing.T) {
	d := Deps{Version: "v", Repos: NewRepos(map[string]*Repo{"demo": {}}),
		Forum: modForumStub{on: true}, TrustedAuthor: func(_, _ string) bool { return false }}
	r := ops.NewRegistry()
	if err := RegisterAll(r, d); err != nil {
		t.Fatal(err)
	}

	// ANONYMOUS: moderated is true — that is a fact about the forum —
	// and wouldHold must not be, because there is nobody to hold it for.
	anon, err := r.Invoke(context.Background(), "forum.moderation.state", moderationStateIn{Repo: "demo"})
	if err != nil {
		t.Fatal(err)
	}
	if a := anon.(moderationStateOut); !a.Enabled || a.WouldHold {
		t.Errorf("an anonymous caller was told their posts would be held: %+v\n"+
			"there is no account for that claim to be about", a)
	}

	// A SIGNED-IN, untrusted account IS told — the fix must not have
	// turned the answer off for everybody.
	jan, err := r.Invoke(ops.WithUser(context.Background(), "jan"),
		"forum.moderation.state", moderationStateIn{Repo: "demo"})
	if err != nil {
		t.Fatal(err)
	}
	if j := jan.(moderationStateOut); !j.Enabled || !j.WouldHold {
		t.Errorf("a signed-in untrusted account was NOT warned their post would be held: %+v", j)
	}

	// And the helper itself, at the spelling it is now handed.
	if m := forumModerationFor(context.Background(), d, "demo", ""); m.yours {
		t.Error("a caller with no account was told their posts would be held")
	}
}