Reddit's API isn't open to apps like this one, so Supermarket no longer signs in to Reddit, publishes or schedules there, or reads subreddits and the account's posts. The internal/api/reddit client, Reddit's OAuth, its keys and its Social accounts card are gone. Supermarket still writes the post. Each Reddit draft in Posts has a box for its subreddit's rules, with a link to them on Reddit: pasted once, they're kept (subreddits.data, as pasted_rules) and given to the model for later drafts. Open in Reddit saves the edits and opens the submit page filled in; Copy title and Copy text (with the tagged source link) cover a box that comes up empty; Mark as posted records it. Warnings come from the pasted rules and the posts marked as posted. Fill's subreddit suggestions are kept unchecked, and say so. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
111 lines
3.1 KiB
Go
111 lines
3.1 KiB
Go
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.LinkedInAccessToken: "lt"})
|
|
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")
|
|
}
|
|
}
|