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
+110
View File
@@ -0,0 +1,110 @@
package posting
import (
"path/filepath"
"testing"
"time"
"supermarket/internal/db"
"supermarket/internal/secrets"
)
func open(t *testing.T) (*db.DB, *secrets.Store) {
t.Helper()
dir := t.TempDir()
d, err := db.Open(filepath.Join(dir, "app.db"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { d.Close() })
sec, err := secrets.Open(filepath.Join(dir, "secrets.json"))
if db.OnPostgres() {
sec, err = secrets.OpenDB(d, []byte("0123456789abcdef0123456789abcdef"), nil)
}
if err != nil {
t.Fatal(err)
}
return d, sec
}
func TestAccounts(t *testing.T) {
d, sec := open(t)
now := time.Now()
co := &Account{Network: X, Name: "@acme", External: "acme"}
if err := Save(d, sec, co, "tok", "ref", now); err != nil {
t.Fatal(err)
}
if sec.Get(AccessName(co.ID)) != "tok" || sec.Get(RefreshName(co.ID)) != "ref" {
t.Error("tokens")
}
// The same account signed in again: updated, not doubled.
again := &Account{Network: X, Name: "@acme", External: "acme"}
Save(d, sec, again, "tok2", "", now)
if again.ID != co.ID || sec.Get(AccessName(co.ID)) != "tok2" || sec.Has(RefreshName(co.ID)) {
t.Errorf("again: %d %q", again.ID, sec.Get(AccessName(co.ID)))
}
jo := &Account{Network: X, Name: "@jo", External: "jo", Owner: 7}
Save(d, sec, jo, "t", "", now)
if err := Save(d, sec, &Account{Network: "myspace"}, "t", "", now); err == nil {
t.Error("unknown network")
}
if c, _ := Company(d, X); c == nil || c.ID != co.ID {
t.Errorf("company: %+v", c)
}
if c, _ := Company(d, Reddit); c != nil {
t.Error("no Reddit account")
}
list, _ := List(d)
if len(list) != 2 || list[0].ID != co.ID {
t.Errorf("list: %+v", list)
}
if own, _ := OwnedBy(d, 7); len(own) != 1 || own[0].ID != jo.ID {
t.Errorf("own: %+v", own)
}
if Channel(LinkedInPage) != LinkedIn || co.Label() != "X @acme" {
t.Error("labels")
}
if err := SetGrants(d, map[int64][]int64{co.ID: {7, 8}}); err != nil {
t.Fatal(err)
}
if ok, _ := Granted(d, co.ID, 8); !ok {
t.Error("not granted")
}
ForgetUser(d, 8)
if ok, _ := Granted(d, co.ID, 8); ok {
t.Error("forgotten user still granted")
}
g, _ := Grants(d)
if !g[co.ID][7] || len(g[co.ID]) != 1 {
t.Errorf("grants %v", g)
}
if err := Remove(d, sec, co.ID); err != nil {
t.Fatal(err)
}
if _, err := Get(d, co.ID); err != ErrNotFound || sec.Has(AccessName(co.ID)) {
t.Error("not removed")
}
if g, _ := Grants(d); len(g) != 0 {
t.Error("grants outlived the account")
}
}
func TestAdopt(t *testing.T) {
d, sec := open(t)
sec.Set(map[string]string{secrets.XAccessToken: "xt", secrets.XRefreshToken: "xr", secrets.RedditAccessToken: "rt"})
exp := time.Now().Add(time.Hour)
n, err := Adopt(d, sec, func(network string) (string, string, time.Time) {
return "n-" + network, "e-" + network, exp
}, time.Now())
if err != nil || n != 2 {
t.Fatalf("%d %v", n, err)
}
x, _ := Company(d, X)
if x == nil || x.Name != "n-x" || sec.Get(AccessName(x.ID)) != "xt" || sec.Get(RefreshName(x.ID)) != "xr" || sec.Has(secrets.XAccessToken) {
t.Errorf("x: %+v", x)
}
if n, _ := Adopt(d, sec, func(string) (string, string, time.Time) { return "", "", time.Time{} }, time.Now()); n != 0 {
t.Error("adopted twice")
}
}