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:
@@ -100,3 +100,72 @@ func TestPostSendsTheArticleTitle(t *testing.T) {
|
||||
t.Error("a post without a link has no content")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOrganizations(t *testing.T) {
|
||||
var paths []string
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
paths = append(paths, r.URL.RequestURI())
|
||||
if r.Header.Get("Authorization") != "Bearer page-tok" || r.Header.Get("X-Restli-Protocol-Version") != "2.0.0" || r.Header.Get("LinkedIn-Version") == "" {
|
||||
t.Errorf("headers: %v", r.Header)
|
||||
}
|
||||
switch r.URL.Path {
|
||||
case "/rest/organizationAcls":
|
||||
w.Write([]byte(`{"elements":[
|
||||
{"role":"ADMINISTRATOR","state":"APPROVED","organizationTarget":"urn:li:organization:111"},
|
||||
{"role":"CONTENT_ADMIN","state":"APPROVED","organization":"urn:li:organization:222"},
|
||||
{"role":"ANALYST","state":"APPROVED","organizationTarget":"urn:li:organization:333"},
|
||||
{"role":"ADMINISTRATOR","state":"REQUESTED","organizationTarget":"urn:li:organization:444"},
|
||||
{"role":"DIRECT_SPONSORED_CONTENT_POSTER","state":"APPROVED","organizationTarget":"urn:li:organization:111"}]}`))
|
||||
case "/rest/organizationsLookup":
|
||||
w.Write([]byte(`{"results":{"111":{"localizedName":"Federated Computer","vanityName":"federated-computer"}},"statuses":{"111":200,"222":404}}`))
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
defer srv.Close()
|
||||
old := apiBase
|
||||
apiBase = srv.URL
|
||||
defer func() { apiBase = old }()
|
||||
orgs, err := New("page-tok", "").Organizations(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(orgs) != 2 || orgs[0].URN != "urn:li:organization:111" || orgs[0].Name != "Federated Computer" || orgs[0].Vanity != "federated-computer" ||
|
||||
orgs[0].Role != "ADMINISTRATOR" || orgs[1].ID != "222" || orgs[1].Name != "Page 222" {
|
||||
t.Errorf("orgs: %+v", orgs)
|
||||
}
|
||||
if !strings.Contains(paths[0], "q=roleAssignee") || !strings.Contains(paths[0], "state=APPROVED") || paths[1] != "/rest/organizationsLookup?ids=List(111,222)" {
|
||||
t.Errorf("paths: %v", paths)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOrganizationsRefused(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
w.Write([]byte(`{"message":"Not enough permissions to access: organizationAcls"}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
old := apiBase
|
||||
apiBase = srv.URL
|
||||
defer func() { apiBase = old }()
|
||||
if _, err := New("t", "").Organizations(context.Background()); err == nil || !strings.Contains(err.Error(), "may lack the permission") {
|
||||
t.Errorf("err: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostAsAPage(t *testing.T) {
|
||||
var body map[string]any
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
json.NewDecoder(r.Body).Decode(&body)
|
||||
w.Header().Set("X-RestLi-Id", "urn:li:share:9")
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
}))
|
||||
defer srv.Close()
|
||||
old := apiBase
|
||||
apiBase = srv.URL
|
||||
defer func() { apiBase = old }()
|
||||
u, err := New("t", "urn:li:organization:111").PostUpdate(context.Background(), "Hello", nil)
|
||||
if err != nil || u != "https://www.linkedin.com/feed/update/urn:li:share:9/" || body["author"] != "urn:li:organization:111" {
|
||||
t.Errorf("%s %v %v", u, err, body)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user