1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-02-03 13:11:48 +02:00
goreleaser/internal/pipe/linkedin/client_test.go
Gabriel Cipriano 59a3eeb56d
fix: linkedin announce api changes (#4428)
Closes #4421 

I chose to keep `getProfileID` as `getProfileIDLegacy` and use it as a
fallback if `getProfileSub` fails because of permission scope.

In this way, it's not a breaking change because one that has only a
deprecated permissions such as `r_liteprofile` will still be able to hit
`v2/me`

this logic is encapsulated in the new function `getProfileURN`, that
resolves the user identifier and returns it formatted as a URN

---------

Co-authored-by: Gabriel F Cipriano <gabriel.cipriano@farme.com.br>
2023-11-18 09:51:42 -03:00

121 lines
2.4 KiB
Go

package linkedin
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/stretchr/testify/require"
)
func TestCreateLinkedInClient(t *testing.T) {
tests := []struct {
name string
cfg oauthClientConfig
wantErr error
}{
{
"non-empty context and access token",
oauthClientConfig{
Context: testctx.New(),
AccessToken: "foo",
},
nil,
},
{
"empty context",
oauthClientConfig{
Context: nil,
AccessToken: "foo",
},
fmt.Errorf("context is nil"),
},
{
"empty access token",
oauthClientConfig{
Context: testctx.New(),
AccessToken: "",
},
fmt.Errorf("empty access token"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := createLinkedInClient(tt.cfg)
if tt.wantErr != nil {
require.EqualError(t, err, tt.wantErr.Error())
} else {
require.NoError(t, err)
}
})
}
}
func TestClient_Share(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
_, _ = io.WriteString(rw, `
{
"sub": "foo",
"activity": "123456789"
}
`)
}))
defer server.Close()
c, err := createLinkedInClient(oauthClientConfig{
Context: testctx.New(),
AccessToken: "foo",
})
if err != nil {
t.Fatalf("could not create client: %v", err)
}
c.baseURL = server.URL
link, err := c.Share("test")
if err != nil {
t.Fatalf("could not share: %v", err)
}
wantLink := "https://www.linkedin.com/feed/update/123456789"
require.Equal(t, wantLink, link)
}
func TestClientLegacyProfile_Share(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
if req.URL.Path == "/v2/userinfo" {
rw.WriteHeader(http.StatusForbidden)
return
}
// this is the response from /v2/me (legacy as a fallback)
_, _ = io.WriteString(rw, `
{
"id": "foo",
"activity": "123456789"
}
`)
}))
defer server.Close()
c, err := createLinkedInClient(oauthClientConfig{
Context: testctx.New(),
AccessToken: "foo",
})
if err != nil {
t.Fatalf("could not create client: %v", err)
}
c.baseURL = server.URL
link, err := c.Share("test")
if err != nil {
t.Fatalf("could not share: %v", err)
}
wantLink := "https://www.linkedin.com/feed/update/123456789"
require.Equal(t, wantLink, link)
}