Phase 10, part 7: posting accounts, and who may post to which

Social accounts become posting accounts: several per network, the
company's and each person's own, with their tokens kept per account. The
old sign-ins are adopted as the company's. Admins say who may post to which
(Settings → Posting accounts); a draft can go out from one account or a
copy to several, each recording who posted it and as what. Signing an
account out, or removing its owner, holds the posts scheduled to it and
tells whoever scheduled them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 09:26:34 -07:00
co-authored by Claude Opus 5.5
parent a4dd36f5a4
commit bb4772637c
31 changed files with 1758 additions and 325 deletions
+193
View File
@@ -0,0 +1,193 @@
package web
import (
"context"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"testing"
"time"
"supermarket/internal/content"
"supermarket/internal/oauth"
"supermarket/internal/posting"
"supermarket/internal/secrets"
"supermarket/internal/users"
)
// fakePosts records what went out, and from which account.
type fakePosts struct {
mu sync.Mutex
sent []string // "<account label>: <text>"
}
type fakePub struct {
f *fakePosts
a *posting.Account
}
func (p fakePub) Publish(_ context.Context, post content.Post) (string, error) {
p.f.mu.Lock()
defer p.f.mu.Unlock()
p.f.sent = append(p.f.sent, p.a.Label()+": "+post.Text)
return "https://x.com/" + p.a.External + "/status/" + strconv.Itoa(len(p.f.sent)), nil
}
func ids(as ...*posting.Account) []string {
var out []string
for _, a := range as {
out = append(out, strconv.FormatInt(a.ID, 10))
}
return out
}
func TestPostingAccounts(t *testing.T) {
s, h := newServerEdition(t)
sent := &fakePosts{}
s.testPublisher = func(a *posting.Account) content.Publisher { return fakePub{sent, a} }
owner := addUser(t, s, "owner@example.com", users.Owner, nil)
oc := sessionFor(t, s, owner)
jo := addUser(t, s, "jo@example.com", users.Member, map[string]users.Access{"content": users.Edit})
jc := sessionFor(t, s, jo)
viewer := addUser(t, s, "vi@example.com", users.Member, map[string]users.Access{"content": users.View})
now := time.Now()
company := &posting.Account{Network: posting.X, Name: "@acme", External: "acme"}
posting.Save(s.DB, s.Secrets, company, "t1", "", now)
own := &posting.Account{Network: posting.X, Name: "@jo", External: "jo", Owner: jo.ID}
posting.Save(s.DB, s.Secrets, own, "t2", "", now)
content.CreateDrafts(s.DB, []content.Draft{{Channel: content.ChannelX, Content: "Hello", WrittenBy: jo.ID}})
ds, _ := content.ListDrafts(s.DB, 1, content.StatusDraft)
draft := strconv.FormatInt(ds[0].ID, 10)
// Jo sees only her own account; the owner sees both.
body := as(h, jc, http.MethodGet, "/posts", nil).Body.String()
if !strings.Contains(body, "X @jo") || strings.Contains(body, "X @acme") {
t.Errorf("jo's choices: %s", body)
}
if body := as(h, oc, http.MethodGet, "/posts", nil).Body.String(); !strings.Contains(body, "X @jo") || !strings.Contains(body, "X @acme") {
t.Error("owner's choices")
}
// The draft goes to the company's account by default, which Jo may not
// post to; nor may she choose it.
if rec := as(h, jc, http.MethodPost, "/drafts/"+draft+"/publish", url.Values{"content": {"Hello"}}); rec.Code == http.StatusSeeOther || !strings.Contains(rec.Body.String(), "may not post as X @acme") {
t.Errorf("jo to the company's: %d %s", rec.Code, rec.Body.String())
}
if rec := as(h, jc, http.MethodPost, "/drafts/"+draft+"/publish", url.Values{"content": {"Hello"}, "accounts": ids(company)}); rec.Code == http.StatusSeeOther {
t.Errorf("jo chose the company's: %d", rec.Code)
}
if len(sent.sent) != 0 {
t.Fatalf("sent %v", sent.sent)
}
// The owner lets her: the grid.
if rec := as(h, sessionFor(t, s, viewer), http.MethodGet, "/settings/posting", nil); rec.Code != http.StatusForbidden {
t.Errorf("member on the grid: %d", rec.Code)
}
grid := as(h, oc, http.MethodGet, "/settings/posting", nil).Body.String()
if !strings.Contains(grid, `name="grant" value="`+strconv.FormatInt(company.ID, 10)+`:`+strconv.FormatInt(jo.ID, 10)+`"`) {
t.Errorf("grid: %s", grid)
}
as(h, oc, http.MethodPost, "/settings/posting", url.Values{"grant": {strconv.FormatInt(company.ID, 10) + ":" + strconv.FormatInt(jo.ID, 10), "999:1"}})
if ok, _ := posting.Granted(s.DB, company.ID, jo.ID); !ok {
t.Fatal("not granted")
}
// Now to both at once: a copy each, and each records who and as what.
rec := as(h, jc, http.MethodPost, "/drafts/"+draft+"/publish", url.Values{"content": {"Hello all"}, "accounts": ids(own, company)})
if rec.Code != http.StatusSeeOther {
t.Fatalf("publish to both: %d %s", rec.Code, rec.Body.String())
}
if len(sent.sent) != 2 || sent.sent[0] != "X @jo: Hello all" || sent.sent[1] != "X @acme: Hello all" {
t.Errorf("sent %v", sent.sent)
}
pub, _ := content.ListDrafts(s.DB, 10, content.StatusPublished)
if len(pub) != 2 {
t.Fatalf("published %d", len(pub))
}
for _, d := range pub {
if d.PostedBy != jo.ID || d.WrittenBy != jo.ID || d.PostedAs == "" || d.Batch != ds[0].Batch {
t.Errorf("published %+v", d)
}
}
if body := as(h, jc, http.MethodGet, "/content/published", nil).Body.String(); !strings.Contains(body, "as X @acme") {
t.Error("published list doesn't say where")
}
// Jo schedules a post to her own account, then signs it out: it's held,
// and she's told.
content.CreateDrafts(s.DB, []content.Draft{{Channel: content.ChannelX, Content: "Later", WrittenBy: jo.ID}})
ds, _ = content.ListDrafts(s.DB, 1, content.StatusDraft)
later := strconv.FormatInt(ds[0].ID, 10)
when := time.Now().Add(48 * time.Hour).Format(dateTimeLocal)
if rec := as(h, jc, http.MethodPost, "/drafts/"+later+"/schedule", url.Values{"content": {"Later"}, "accounts": ids(own), "when": {when}}); rec.Code != http.StatusSeeOther {
t.Fatalf("schedule: %d %s", rec.Code, rec.Body.String())
}
// Someone else can't sign Jo's account out; she can.
member2 := addUser(t, s, "kim@example.com", users.Member, map[string]users.Access{"content": users.Edit})
if rec := as(h, sessionFor(t, s, member2), http.MethodPost, "/settings/account/posting/"+strconv.FormatInt(own.ID, 10)+"/remove", url.Values{}); rec.Code != http.StatusForbidden {
t.Errorf("someone else signs it out: %d", rec.Code)
}
if rec := as(h, jc, http.MethodPost, "/settings/account/posting/"+strconv.FormatInt(own.ID, 10)+"/remove", url.Values{}); rec.Code != http.StatusSeeOther {
t.Fatalf("sign out own: %d", rec.Code)
}
d, _ := content.GetDraft(s.DB, ds[0].ID)
if d.Status != content.StatusFailed || !strings.Contains(d.Error, "X @jo was signed out") || d.AccountID != 0 {
t.Errorf("held: %+v", d)
}
m := waitMail(t, s, "jo@example.com", 1)
if !strings.Contains(m.Subject, "held back") {
t.Errorf("mail %+v", m)
}
// A scheduled post whose writer lost the right to post as its account
// is held when it falls due.
content.CreateDrafts(s.DB, []content.Draft{{Channel: content.ChannelX, Content: "Due", WrittenBy: jo.ID}})
ds, _ = content.ListDrafts(s.DB, 1, content.StatusDraft)
content.SetAccount(s.DB, ds[0].ID, company.ID)
content.Schedule(s.DB, ds[0].ID, time.Now().Add(time.Minute))
content.SetPostedBy(s.DB, ds[0].ID, jo.ID)
posting.SetGrants(s.DB, nil)
s.publishDue(context.Background(), time.Now().Add(2*time.Minute))
if d, _ := content.GetDraft(s.DB, ds[0].ID); d.Status != content.StatusFailed || !strings.Contains(d.Error, "may not post as") {
t.Errorf("due without the right: %+v", d)
}
if len(sent.sent) != 2 {
t.Errorf("sent %v", sent.sent)
}
}
func TestOwnAccountSignIn(t *testing.T) {
s, h := newServerEdition(t)
addUser(t, s, "owner@example.com", users.Owner, nil)
jo := addUser(t, s, "jo@example.com", users.Member, map[string]users.Access{"content": users.Edit})
jc := sessionFor(t, s, jo)
seo := addUser(t, s, "sam@example.com", users.Member, map[string]users.Access{"seo": users.Edit})
s.Secrets.Set(map[string]string{secrets.XClientID: "cid"})
if body := as(h, jc, http.MethodGet, "/settings/account", nil).Body.String(); !strings.Contains(body, "Connect your X") {
t.Error("no connect button")
}
// The company's accounts need an admin.
if rec := as(h, jc, http.MethodPost, "/auth/x/start", url.Values{}); rec.Code != http.StatusForbidden {
t.Errorf("member, company account: %d", rec.Code)
}
if rec := as(h, sessionFor(t, s, seo), http.MethodPost, "/auth/x/start?for=me", url.Values{}); rec.Code != http.StatusForbidden {
t.Errorf("member without Content: %d", rec.Code)
}
rec := as(h, jc, http.MethodPost, "/auth/x/start?for=me", url.Values{})
if rec.Code != http.StatusSeeOther || !strings.Contains(rec.Header().Get("Location"), "client_id=cid") {
t.Fatalf("start: %d %s", rec.Code, rec.Header().Get("Location"))
}
loc, _ := url.Parse(rec.Header().Get("Location"))
var p oauth.Pending
if ok, _ := s.DB.GetJSON(oauthFlowKey+loc.Query().Get("state"), &p); !ok || p.For != jo.ID {
t.Errorf("pending %+v", p)
}
// Another person can't finish Jo's sign-in.
other := sessionFor(t, s, seo)
if rec := as(h, other, http.MethodGet, "/auth/x/callback?code=c&state="+loc.Query().Get("state"), nil); rec.Code != http.StatusForbidden {
t.Errorf("someone else's callback: %d", rec.Code)
}
}