Files
supermarket/internal/web/settings_linkedin_test.go
T
davidandClaude Opus 5.5 bb4772637c 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>
2026-11-25 09:26:34 -07:00

102 lines
4.8 KiB
Go

package web
import (
"context"
"errors"
"net/http"
"net/url"
"strconv"
"strings"
"testing"
linkedinapi "supermarket/internal/api/linkedin"
"supermarket/internal/content"
"supermarket/internal/secrets"
)
func TestLinkedInPageSettings(t *testing.T) {
s, h := newTestServer(t, nil)
body := do(h, http.MethodGet, "/settings/accounts", nil, nil).Body.String()
if !strings.Contains(body, `id="network-LinkedIn page"`) || !strings.Contains(body, "/auth/linkedin-page/callback") ||
!strings.Contains(body, "Once connected, choose the page here") || !strings.Contains(body, `value="w_organization_social r_organization_admin"`) {
t.Errorf("card: %s", body)
}
// Without the app's keys, the sign-in can't start.
if rec := postForm(h, "/auth/linkedin-page/start", nil); rec.Code != http.StatusBadRequest {
t.Errorf("start without keys: %d", rec.Code)
}
s.Secrets.Set(map[string]string{secrets.LinkedInPageClientID: "cid", secrets.LinkedInPageClientSecret: "sec"})
postForm(h, "/settings/accounts/linkedin-page", url.Values{"scopes": {"w_organization_social rw_organization_admin"}})
rec := postForm(h, "/auth/linkedin-page/start", nil)
loc, _ := url.Parse(rec.Header().Get("Location"))
if rec.Code != http.StatusSeeOther || loc.Query().Get("scope") != "w_organization_social rw_organization_admin" || loc.Query().Get("client_id") != "cid" {
t.Errorf("start: %d %s", rec.Code, loc)
}
// Signed in (the callback's work), with two pages listed.
s.Secrets.Set(map[string]string{secrets.LinkedInPageAccessToken: "page-tok"})
s.DB.PutJSON(liPageOrgsKey, []linkedinapi.Org{{URN: "urn:li:organization:111", ID: "111", Name: "Federated Computer", Role: "ADMINISTRATOR"},
{URN: "urn:li:organization:222", ID: "222", Name: "Other", Role: "CONTENT_ADMIN"}})
body = do(h, http.MethodGet, "/settings/accounts", nil, nil).Body.String()
if !strings.Contains(body, `value="urn:li:organization:111"`) || !strings.Contains(body, "Federated Computer") {
t.Errorf("pages: %s", body)
}
postForm(h, "/settings/accounts/linkedin-page", url.Values{"org": {"urn:li:organization:111"}, "default": {"page"}})
if o := s.liPage(); o == nil || o.Name != "Federated Computer" {
t.Errorf("chosen: %+v", o)
}
var def string
s.DB.GetJSON(content.LinkedInDefaultKey, &def)
if def != content.AsPage {
t.Errorf("default: %q", def)
}
// Typed in by hand, from the page's admin address.
postForm(h, "/settings/accounts/linkedin-page", url.Values{"org_manual": {"https://www.linkedin.com/company/87654321/admin/"}})
if o := s.liPage(); o == nil || o.URN != "urn:li:organization:87654321" {
t.Errorf("manual: %+v", o)
}
if body := postForm(h, "/settings/accounts/linkedin-page", url.Values{"org_manual": {"federated"}}).Body.String(); !strings.Contains(body, "isn&#39;t a company page&#39;s number") {
t.Errorf("bad manual: %s", body)
}
// The sign-in is now the company's page account, posting as the page
// chosen.
postForm(h, "/settings/accounts/linkedin-page", url.Values{"org": {"urn:li:organization:111"}, "default": {"page"}})
pa := s.companyPage()
if pa == nil || pa.External != "urn:li:organization:111" || pa.Name != "Federated Computer" || s.Secrets.Has(secrets.LinkedInPageAccessToken) {
t.Fatalf("page account: %+v", pa)
}
// A LinkedIn draft offers it, and the choice is saved.
if _, err := content.CreateDrafts(s.DB, []content.Draft{{Channel: content.ChannelLinkedIn, Content: "Hello"}}); err != nil {
t.Fatal(err)
}
ds, _ := content.ListDrafts(s.DB, 1, content.StatusDraft)
body = do(h, http.MethodGet, "/posts", nil, nil).Body.String()
want := `<input type="checkbox" name="accounts" value="` + strconv.FormatInt(pa.ID, 10) + `" checked> LinkedIn page Federated Computer`
if !strings.Contains(body, want) {
t.Errorf("posts: %s", body)
}
postForm(h, "/drafts/"+strconv.FormatInt(ds[0].ID, 10)+"/save", url.Values{"content": {"Hello"}, "accounts": {strconv.FormatInt(pa.ID, 10)}})
if dr, _ := content.GetDraft(s.DB, ds[0].ID); dr.AccountID != pa.ID {
t.Errorf("chosen: %d", dr.AccountID)
}
// The page counts as LinkedIn being connected, even without the member sign-in.
if !s.connected(localOwner)[content.ChannelLinkedIn] {
t.Error("LinkedIn not connected with only the page")
}
}
func TestLinkedInPublisherChoosesTheAuthor(t *testing.T) {
ctx := context.Background()
if _, err := (linkedInPublisher{}).Publish(ctx, content.Post{Text: "x", AsPage: true}); !errors.Is(err, errNoPage) {
t.Errorf("no page: %v", err)
}
if _, err := (linkedInPublisher{page: linkedinapi.New("t", "urn:li:organization:1")}).Publish(ctx, content.Post{Text: "x"}); !errors.Is(err, errNoMe) {
t.Errorf("no member: %v", err)
}
expired := errors.New("expired")
if _, err := (linkedInPublisher{pageErr: expired}).Publish(ctx, content.Post{Text: "x", AsPage: true}); !errors.Is(err, expired) {
t.Errorf("expired page: %v", err)
}
}