-
-
Notifications
You must be signed in to change notification settings - Fork 910
/
client_test.go
120 lines (107 loc) · 2.36 KB
/
client_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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)
}