git.iko.soy/typeable/oauth2-proxy
on v3.0.0 help sign in
127 lines 3.6 kB history raw
1package providers
3import (
4 "net/http"
5 "net/http/httptest"
6 "net/url"
7 "testing"
9 "github.com/stretchr/testify/assert"
12func testGitLabProvider(hostname string) *GitLabProvider {
13 p := NewGitLabProvider(
14 &ProviderData{
15 ProviderName: "",
16 LoginURL: &url.URL{},
17 RedeemURL: &url.URL{},
18 ProfileURL: &url.URL{},
19 ValidateURL: &url.URL{},
20 Scope: ""})
21 if hostname != "" {
22 updateURL(p.Data().LoginURL, hostname)
23 updateURL(p.Data().RedeemURL, hostname)
24 updateURL(p.Data().ProfileURL, hostname)
25 updateURL(p.Data().ValidateURL, hostname)
26 }
27 return p
30func testGitLabBackend(payload string) *httptest.Server {
31 path := "/api/v4/user"
32 query := "access_token=imaginary_access_token"
34 return httptest.NewServer(http.HandlerFunc(
35 func(w http.ResponseWriter, r *http.Request) {
36 if r.URL.Path != path || r.URL.RawQuery != query {
37 w.WriteHeader(404)
38 } else {
39 w.WriteHeader(200)
40 w.Write([]byte(payload))
41 }
42 }))
45func TestGitLabProviderDefaults(t *testing.T) {
46 p := testGitLabProvider("")
47 assert.NotEqual(t, nil, p)
48 assert.Equal(t, "GitLab", p.Data().ProviderName)
49 assert.Equal(t, "https://gitlab.com/oauth/authorize",
50 p.Data().LoginURL.String())
51 assert.Equal(t, "https://gitlab.com/oauth/token",
52 p.Data().RedeemURL.String())
53 assert.Equal(t, "https://gitlab.com/api/v4/user",
54 p.Data().ValidateURL.String())
55 assert.Equal(t, "read_user", p.Data().Scope)
58func TestGitLabProviderOverrides(t *testing.T) {
59 p := NewGitLabProvider(
60 &ProviderData{
61 LoginURL: &url.URL{
62 Scheme: "https",
63 Host: "example.com",
64 Path: "/oauth/auth"},
65 RedeemURL: &url.URL{
66 Scheme: "https",
67 Host: "example.com",
68 Path: "/oauth/token"},
69 ValidateURL: &url.URL{
70 Scheme: "https",
71 Host: "example.com",
72 Path: "/api/v4/user"},
73 Scope: "profile"})
74 assert.NotEqual(t, nil, p)
75 assert.Equal(t, "GitLab", p.Data().ProviderName)
76 assert.Equal(t, "https://example.com/oauth/auth",
77 p.Data().LoginURL.String())
78 assert.Equal(t, "https://example.com/oauth/token",
79 p.Data().RedeemURL.String())
80 assert.Equal(t, "https://example.com/api/v4/user",
81 p.Data().ValidateURL.String())
82 assert.Equal(t, "profile", p.Data().Scope)
85func TestGitLabProviderGetEmailAddress(t *testing.T) {
86 b := testGitLabBackend("{\"email\": \"michael.bland@gsa.gov\"}")
87 defer b.Close()
89 bURL, _ := url.Parse(b.URL)
90 p := testGitLabProvider(bURL.Host)
92 session := &SessionState{AccessToken: "imaginary_access_token"}
93 email, err := p.GetEmailAddress(session)
94 assert.Equal(t, nil, err)
95 assert.Equal(t, "michael.bland@gsa.gov", email)
98// Note that trying to trigger the "failed building request" case is not
99// practical, since the only way it can fail is if the URL fails to parse.
100func TestGitLabProviderGetEmailAddressFailedRequest(t *testing.T) {
101 b := testGitLabBackend("unused payload")
102 defer b.Close()
104 bURL, _ := url.Parse(b.URL)
105 p := testGitLabProvider(bURL.Host)
107 // We'll trigger a request failure by using an unexpected access
108 // token. Alternatively, we could allow the parsing of the payload as
109 // JSON to fail.
110 session := &SessionState{AccessToken: "unexpected_access_token"}
111 email, err := p.GetEmailAddress(session)
112 assert.NotEqual(t, nil, err)
113 assert.Equal(t, "", email)
116func TestGitLabProviderGetEmailAddressEmailNotPresentInPayload(t *testing.T) {
117 b := testGitLabBackend("{\"foo\": \"bar\"}")
118 defer b.Close()
120 bURL, _ := url.Parse(b.URL)
121 p := testGitLabProvider(bURL.Host)
123 session := &SessionState{AccessToken: "imaginary_access_token"}
124 email, err := p.GetEmailAddress(session)
125 assert.NotEqual(t, nil, err)
126 assert.Equal(t, "", email)