Phase 6 part 4: post as the LinkedIn company page

A second LinkedIn app (Community Management API) with its own sign-in on
Settings → Social accounts: the pages the member may post for are listed
and one chosen, new LinkedIn drafts default to the page or to you, and
each draft can switch before it's published. Schema 25.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-22 15:05:30 -07:00
co-authored by Claude Opus 5.5
parent 2c42857419
commit 98913971ea
20 changed files with 841 additions and 74 deletions
+94
View File
@@ -0,0 +1,94 @@
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)
}
// A LinkedIn draft offers the page, and the choice is saved.
postForm(h, "/settings/accounts/linkedin-page", url.Values{"org": {"urn:li:organization:111"}, "default": {"page"}})
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()
if !strings.Contains(body, `name="linkedin_as"`) || !strings.Contains(body, `<option value="page" selected>Federated Computer</option>`) {
t.Errorf("posts: %s", body)
}
postForm(h, "/drafts/"+strconv.FormatInt(ds[0].ID, 10)+"/save", url.Values{"content": {"Hello"}, "linkedin_as": {""}})
if dr, _ := content.GetDraft(s.DB, ds[0].ID); dr.LinkedInAs != "" {
t.Errorf("switched: %q", dr.LinkedInAs)
}
// The page counts as LinkedIn being connected, even without the member sign-in.
if !s.connected()[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)
}
}