2015-03-30 19:30:27 +00:00
|
|
|
package providers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
2015-05-21 03:23:48 +00:00
|
|
|
"encoding/json"
|
2015-03-30 19:30:27 +00:00
|
|
|
"github.com/bmizerany/assert"
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newGoogleProvider() *GoogleProvider {
|
|
|
|
return NewGoogleProvider(
|
|
|
|
&ProviderData{
|
2015-03-31 16:59:07 +00:00
|
|
|
ProviderName: "",
|
|
|
|
LoginUrl: &url.URL{},
|
|
|
|
RedeemUrl: &url.URL{},
|
|
|
|
ProfileUrl: &url.URL{},
|
2015-05-08 21:13:35 +00:00
|
|
|
ValidateUrl: &url.URL{},
|
2015-03-31 16:59:07 +00:00
|
|
|
Scope: ""})
|
2015-03-30 19:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGoogleProviderDefaults(t *testing.T) {
|
|
|
|
p := newGoogleProvider()
|
|
|
|
assert.NotEqual(t, nil, p)
|
2015-03-31 16:59:07 +00:00
|
|
|
assert.Equal(t, "Google", p.Data().ProviderName)
|
2015-06-23 17:23:47 +00:00
|
|
|
assert.Equal(t, "https://accounts.google.com/o/oauth2/auth?access_type=offline",
|
2015-03-30 19:30:27 +00:00
|
|
|
p.Data().LoginUrl.String())
|
2015-06-23 17:23:47 +00:00
|
|
|
assert.Equal(t, "https://www.googleapis.com/oauth2/v3/token",
|
2015-03-30 19:30:27 +00:00
|
|
|
p.Data().RedeemUrl.String())
|
2015-05-08 21:13:35 +00:00
|
|
|
assert.Equal(t, "https://www.googleapis.com/oauth2/v1/tokeninfo",
|
|
|
|
p.Data().ValidateUrl.String())
|
2015-03-30 19:30:27 +00:00
|
|
|
assert.Equal(t, "", p.Data().ProfileUrl.String())
|
|
|
|
assert.Equal(t, "profile email", p.Data().Scope)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGoogleProviderOverrides(t *testing.T) {
|
|
|
|
p := NewGoogleProvider(
|
|
|
|
&ProviderData{
|
|
|
|
LoginUrl: &url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Host: "example.com",
|
|
|
|
Path: "/oauth/auth"},
|
|
|
|
RedeemUrl: &url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Host: "example.com",
|
|
|
|
Path: "/oauth/token"},
|
|
|
|
ProfileUrl: &url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Host: "example.com",
|
|
|
|
Path: "/oauth/profile"},
|
2015-05-08 21:13:35 +00:00
|
|
|
ValidateUrl: &url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Host: "example.com",
|
|
|
|
Path: "/oauth/tokeninfo"},
|
2015-03-30 19:30:27 +00:00
|
|
|
Scope: "profile"})
|
|
|
|
assert.NotEqual(t, nil, p)
|
2015-03-31 16:59:07 +00:00
|
|
|
assert.Equal(t, "Google", p.Data().ProviderName)
|
2015-03-30 19:30:27 +00:00
|
|
|
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/oauth/profile",
|
|
|
|
p.Data().ProfileUrl.String())
|
2015-05-08 21:13:35 +00:00
|
|
|
assert.Equal(t, "https://example.com/oauth/tokeninfo",
|
|
|
|
p.Data().ValidateUrl.String())
|
2015-03-30 19:30:27 +00:00
|
|
|
assert.Equal(t, "profile", p.Data().Scope)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGoogleProviderGetEmailAddress(t *testing.T) {
|
|
|
|
p := newGoogleProvider()
|
2015-05-21 03:23:48 +00:00
|
|
|
body, err := json.Marshal(
|
|
|
|
struct {
|
|
|
|
IdToken string `json:"id_token"`
|
|
|
|
}{
|
|
|
|
IdToken: "ignored prefix." + base64.URLEncoding.EncodeToString([]byte(`{"email": "michael.bland@gsa.gov"}`)),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert.Equal(t, nil, err)
|
|
|
|
email, err := p.GetEmailAddress(body, "ignored access_token")
|
2015-03-30 19:30:27 +00:00
|
|
|
assert.Equal(t, "michael.bland@gsa.gov", email)
|
|
|
|
assert.Equal(t, nil, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGoogleProviderGetEmailAddressInvalidEncoding(t *testing.T) {
|
|
|
|
p := newGoogleProvider()
|
2015-05-21 03:23:48 +00:00
|
|
|
body, err := json.Marshal(
|
|
|
|
struct {
|
|
|
|
IdToken string `json:"id_token"`
|
|
|
|
}{
|
|
|
|
IdToken: "ignored prefix." + `{"email": "michael.bland@gsa.gov"}`,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert.Equal(t, nil, err)
|
|
|
|
email, err := p.GetEmailAddress(body, "ignored access_token")
|
2015-03-30 19:30:27 +00:00
|
|
|
assert.Equal(t, "", email)
|
|
|
|
assert.NotEqual(t, nil, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGoogleProviderGetEmailAddressInvalidJson(t *testing.T) {
|
|
|
|
p := newGoogleProvider()
|
2015-05-21 03:23:48 +00:00
|
|
|
|
|
|
|
body, err := json.Marshal(
|
|
|
|
struct {
|
|
|
|
IdToken string `json:"id_token"`
|
|
|
|
}{
|
|
|
|
IdToken: "ignored prefix." + base64.URLEncoding.EncodeToString([]byte(`{"email": michael.bland@gsa.gov}`)),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert.Equal(t, nil, err)
|
|
|
|
email, err := p.GetEmailAddress(body, "ignored access_token")
|
2015-03-30 19:30:27 +00:00
|
|
|
assert.Equal(t, "", email)
|
|
|
|
assert.NotEqual(t, nil, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGoogleProviderGetEmailAddressEmailMissing(t *testing.T) {
|
|
|
|
p := newGoogleProvider()
|
2015-05-21 03:23:48 +00:00
|
|
|
body, err := json.Marshal(
|
|
|
|
struct {
|
|
|
|
IdToken string `json:"id_token"`
|
|
|
|
}{
|
|
|
|
IdToken: "ignored prefix." + base64.URLEncoding.EncodeToString([]byte(`{"not_email": "missing"}`)),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert.Equal(t, nil, err)
|
|
|
|
email, err := p.GetEmailAddress(body, "ignored access_token")
|
2015-03-30 19:30:27 +00:00
|
|
|
assert.Equal(t, "", email)
|
|
|
|
assert.NotEqual(t, nil, err)
|
|
|
|
}
|