oauth2_proxy/providers/gitlab_test.go

129 lines
3.6 KiB
Go
Raw Normal View History

2016-02-17 12:19:52 +00:00
package providers
import (
"net/http"
"net/http/httptest"
"net/url"
"testing"
2019-05-05 12:33:13 +00:00
"github.com/pusher/oauth2_proxy/pkg/apis/sessions"
"github.com/stretchr/testify/assert"
2016-02-17 12:19:52 +00:00
)
func testGitLabProvider(hostname string) *GitLabProvider {
p := NewGitLabProvider(
&ProviderData{
ProviderName: "",
LoginURL: &url.URL{},
RedeemURL: &url.URL{},
ProfileURL: &url.URL{},
ValidateURL: &url.URL{},
Scope: ""})
if hostname != "" {
updateURL(p.Data().LoginURL, hostname)
updateURL(p.Data().RedeemURL, hostname)
updateURL(p.Data().ProfileURL, hostname)
updateURL(p.Data().ValidateURL, hostname)
}
return p
}
func testGitLabBackend(payload string) *httptest.Server {
path := "/api/v4/user"
2016-02-17 12:19:52 +00:00
query := "access_token=imaginary_access_token"
return httptest.NewServer(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
2018-11-29 14:26:41 +00:00
if r.URL.Path != path || r.URL.RawQuery != query {
2016-02-17 12:19:52 +00:00
w.WriteHeader(404)
} else {
w.WriteHeader(200)
w.Write([]byte(payload))
}
}))
}
func TestGitLabProviderDefaults(t *testing.T) {
p := testGitLabProvider("")
assert.NotEqual(t, nil, p)
assert.Equal(t, "GitLab", p.Data().ProviderName)
assert.Equal(t, "https://gitlab.com/oauth/authorize",
p.Data().LoginURL.String())
assert.Equal(t, "https://gitlab.com/oauth/token",
p.Data().RedeemURL.String())
assert.Equal(t, "https://gitlab.com/api/v4/user",
2016-02-17 12:19:52 +00:00
p.Data().ValidateURL.String())
2017-09-12 21:43:49 +00:00
assert.Equal(t, "read_user", p.Data().Scope)
2016-02-17 12:19:52 +00:00
}
func TestGitLabProviderOverrides(t *testing.T) {
p := NewGitLabProvider(
&ProviderData{
LoginURL: &url.URL{
Scheme: "https",
Host: "example.com",
Path: "/oauth/auth"},
RedeemURL: &url.URL{
Scheme: "https",
Host: "example.com",
Path: "/oauth/token"},
ValidateURL: &url.URL{
Scheme: "https",
Host: "example.com",
Path: "/api/v4/user"},
2016-02-17 12:19:52 +00:00
Scope: "profile"})
assert.NotEqual(t, nil, p)
assert.Equal(t, "GitLab", p.Data().ProviderName)
assert.Equal(t, "https://example.com/oauth/auth",
p.Data().LoginURL.String())
assert.Equal(t, "https://example.com/oauth/token",
p.Data().RedeemURL.String())
assert.Equal(t, "https://example.com/api/v4/user",
2016-02-17 12:19:52 +00:00
p.Data().ValidateURL.String())
assert.Equal(t, "profile", p.Data().Scope)
}
func TestGitLabProviderGetEmailAddress(t *testing.T) {
b := testGitLabBackend("{\"email\": \"michael.bland@gsa.gov\"}")
defer b.Close()
2018-11-29 14:26:41 +00:00
bURL, _ := url.Parse(b.URL)
p := testGitLabProvider(bURL.Host)
2016-02-17 12:19:52 +00:00
2019-05-05 12:33:13 +00:00
session := &sessions.SessionState{AccessToken: "imaginary_access_token"}
2016-02-17 12:19:52 +00:00
email, err := p.GetEmailAddress(session)
assert.Equal(t, nil, err)
assert.Equal(t, "michael.bland@gsa.gov", email)
}
// Note that trying to trigger the "failed building request" case is not
// practical, since the only way it can fail is if the URL fails to parse.
func TestGitLabProviderGetEmailAddressFailedRequest(t *testing.T) {
b := testGitLabBackend("unused payload")
defer b.Close()
2018-11-29 14:26:41 +00:00
bURL, _ := url.Parse(b.URL)
p := testGitLabProvider(bURL.Host)
2016-02-17 12:19:52 +00:00
// We'll trigger a request failure by using an unexpected access
// token. Alternatively, we could allow the parsing of the payload as
// JSON to fail.
2019-05-05 12:33:13 +00:00
session := &sessions.SessionState{AccessToken: "unexpected_access_token"}
2016-02-17 12:19:52 +00:00
email, err := p.GetEmailAddress(session)
assert.NotEqual(t, nil, err)
assert.Equal(t, "", email)
}
func TestGitLabProviderGetEmailAddressEmailNotPresentInPayload(t *testing.T) {
b := testGitLabBackend("{\"foo\": \"bar\"}")
defer b.Close()
2018-11-29 14:26:41 +00:00
bURL, _ := url.Parse(b.URL)
p := testGitLabProvider(bURL.Host)
2016-02-17 12:19:52 +00:00
2019-05-05 12:33:13 +00:00
session := &sessions.SessionState{AccessToken: "imaginary_access_token"}
2016-02-17 12:19:52 +00:00
email, err := p.GetEmailAddress(session)
assert.NotEqual(t, nil, err)
assert.Equal(t, "", email)
}