10 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/sessions"
11 . "github.com/onsi/gomega"
12 "github.com/stretchr/testify/assert"
15func testLinkedInProvider(hostname string) *LinkedInProvider {
16 p := NewLinkedInProvider(
20 RedeemURL: &url.URL{},
21 ProfileURL: &url.URL{},
22 ValidateURL: &url.URL{},
25 updateURL(p.Data().LoginURL, hostname)
26 updateURL(p.Data().RedeemURL, hostname)
27 updateURL(p.Data().ProfileURL, hostname)
32func testLinkedInBackend(payload string) *httptest.Server {
33 path := "/v2/emailAddress"
35 return httptest.NewServer(http.HandlerFunc(
36 func(w http.ResponseWriter, r *http.Request) {
37 if r.URL.Path != path {
39 } else if !IsAuthorizedInHeader(r.Header) {
43 w.Write([]byte(payload))
48func TestNewLinkedInProvider(t *testing.T) {
51 // Test that defaults are set when calling for a new provider with nothing set
52 providerData := NewLinkedInProvider(&ProviderData{}).Data()
53 g.Expect(providerData.ProviderName).To(Equal("LinkedIn"))
54 g.Expect(providerData.LoginURL.String()).To(Equal("https://www.linkedin.com/oauth/v2/authorization"))
55 g.Expect(providerData.RedeemURL.String()).To(Equal("https://www.linkedin.com/uas/oauth2/accessToken"))
56 g.Expect(providerData.ProfileURL.String()).To(Equal("https://api.linkedin.com/v2/emailAddress"))
57 g.Expect(providerData.ValidateURL.String()).To(Equal("https://api.linkedin.com/v2/me"))
58 g.Expect(providerData.Scope).To(Equal("r_emailaddress r_liteprofile"))
61func TestLinkedInProviderOverrides(t *testing.T) {
62 p := NewLinkedInProvider(
71 Path: "/oauth/token"},
75 Path: "/oauth/profile"},
76 ValidateURL: &url.URL{
79 Path: "/oauth/tokeninfo"},
81 assert.NotEqual(t, nil, p)
82 assert.Equal(t, "LinkedIn", p.Data().ProviderName)
83 assert.Equal(t, "https://example.com/oauth/auth",
84 p.Data().LoginURL.String())
85 assert.Equal(t, "https://example.com/oauth/token",
86 p.Data().RedeemURL.String())
87 assert.Equal(t, "https://example.com/oauth/profile",
88 p.Data().ProfileURL.String())
89 assert.Equal(t, "https://example.com/oauth/tokeninfo",
90 p.Data().ValidateURL.String())
91 assert.Equal(t, "profile", p.Data().Scope)
94func TestLinkedInProviderGetEmailAddress(t *testing.T) {
95 b := testLinkedInBackend(`{"elements":[{"handle~":{"emailAddress": "user@linkedin.com"}}]}`)
98 bURL, _ := url.Parse(b.URL)
99 p := testLinkedInProvider(bURL.Host)
101 session := CreateAuthorizedSession()
102 email, err := p.GetEmailAddress(context.Background(), session)
103 assert.Equal(t, nil, err)
104 assert.Equal(t, "user@linkedin.com", email)
107func TestLinkedInProviderGetEmailAddressFailedRequest(t *testing.T) {
108 b := testLinkedInBackend("unused payload")
111 bURL, _ := url.Parse(b.URL)
112 p := testLinkedInProvider(bURL.Host)
114 // We'll trigger a request failure by using an unexpected access
115 // token. Alternatively, we could allow the parsing of the payload as
117 session := &sessions.SessionState{AccessToken: "unexpected_access_token"}
118 email, err := p.GetEmailAddress(context.Background(), session)
119 assert.NotEqual(t, nil, err)
120 assert.Equal(t, "", email)
123func TestLinkedInProviderGetEmailAddressEmailNotPresentInPayload(t *testing.T) {
124 b := testLinkedInBackend("{\"foo\": \"bar\"}")
127 bURL, _ := url.Parse(b.URL)
128 p := testLinkedInProvider(bURL.Host)
130 session := CreateAuthorizedSession()
131 email, err := p.GetEmailAddress(context.Background(), session)
132 assert.NotEqual(t, nil, err)
133 assert.Equal(t, "", email)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list