git.iko.soy/typeable/oauth2-proxy
on v2.1 help sign in
128 lines 3.7 kB history raw
1package providers
3import (
4 "net/http"
5 "net/http/httptest"
6 "net/url"
7 "testing"
9 "github.com/bmizerany/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/v3/user"
32 query := "access_token=imaginary_access_token"
34 return httptest.NewServer(http.HandlerFunc(
35 func(w http.ResponseWriter, r *http.Request) {
36 url := r.URL
37 if url.Path != path || url.RawQuery != query {
38 w.WriteHeader(404)
39 } else {
40 w.WriteHeader(200)
41 w.Write([]byte(payload))
42 }
43 }))
46func TestGitLabProviderDefaults(t *testing.T) {
47 p := testGitLabProvider("")
48 assert.NotEqual(t, nil, p)
49 assert.Equal(t, "GitLab", p.Data().ProviderName)
50 assert.Equal(t, "https://gitlab.com/oauth/authorize",
51 p.Data().LoginURL.String())
52 assert.Equal(t, "https://gitlab.com/oauth/token",
53 p.Data().RedeemURL.String())
54 assert.Equal(t, "https://gitlab.com/api/v3/user",
55 p.Data().ValidateURL.String())
56 assert.Equal(t, "api", p.Data().Scope)
59func TestGitLabProviderOverrides(t *testing.T) {
60 p := NewGitLabProvider(
61 &ProviderData{
62 LoginURL: &url.URL{
63 Scheme: "https",
64 Host: "example.com",
65 Path: "/oauth/auth"},
66 RedeemURL: &url.URL{
67 Scheme: "https",
68 Host: "example.com",
69 Path: "/oauth/token"},
70 ValidateURL: &url.URL{
71 Scheme: "https",
72 Host: "example.com",
73 Path: "/api/v3/user"},
74 Scope: "profile"})
75 assert.NotEqual(t, nil, p)
76 assert.Equal(t, "GitLab", p.Data().ProviderName)
77 assert.Equal(t, "https://example.com/oauth/auth",
78 p.Data().LoginURL.String())
79 assert.Equal(t, "https://example.com/oauth/token",
80 p.Data().RedeemURL.String())
81 assert.Equal(t, "https://example.com/api/v3/user",
82 p.Data().ValidateURL.String())
83 assert.Equal(t, "profile", p.Data().Scope)
86func TestGitLabProviderGetEmailAddress(t *testing.T) {
87 b := testGitLabBackend("{\"email\": \"michael.bland@gsa.gov\"}")
88 defer b.Close()
90 b_url, _ := url.Parse(b.URL)
91 p := testGitLabProvider(b_url.Host)
93 session := &SessionState{AccessToken: "imaginary_access_token"}
94 email, err := p.GetEmailAddress(session)
95 assert.Equal(t, nil, err)
96 assert.Equal(t, "michael.bland@gsa.gov", email)
99// Note that trying to trigger the "failed building request" case is not
100// practical, since the only way it can fail is if the URL fails to parse.
101func TestGitLabProviderGetEmailAddressFailedRequest(t *testing.T) {
102 b := testGitLabBackend("unused payload")
103 defer b.Close()
105 b_url, _ := url.Parse(b.URL)
106 p := testGitLabProvider(b_url.Host)
108 // We'll trigger a request failure by using an unexpected access
109 // token. Alternatively, we could allow the parsing of the payload as
110 // JSON to fail.
111 session := &SessionState{AccessToken: "unexpected_access_token"}
112 email, err := p.GetEmailAddress(session)
113 assert.NotEqual(t, nil, err)
114 assert.Equal(t, "", email)
117func TestGitLabProviderGetEmailAddressEmailNotPresentInPayload(t *testing.T) {
118 b := testGitLabBackend("{\"foo\": \"bar\"}")
119 defer b.Close()
121 b_url, _ := url.Parse(b.URL)
122 p := testGitLabProvider(b_url.Host)
124 session := &SessionState{AccessToken: "imaginary_access_token"}
125 email, err := p.GetEmailAddress(session)
126 assert.NotEqual(t, nil, err)
127 assert.Equal(t, "", email)