1
0
mirror of https://github.com/volatiletech/authboss.git synced 2025-01-24 05:17:10 +02:00
authboss/oauth2/providers_test.go

74 lines
1.6 KiB
Go
Raw Normal View History

package oauth2
import (
2017-02-20 15:56:26 -08:00
"context"
"io/ioutil"
"net/http"
"strings"
"testing"
"time"
2017-02-20 15:56:26 -08:00
"golang.org/x/oauth2"
)
func init() {
// This has an extra parameter that the Google client wouldn't normally get, but it'll safely be
// ignored.
clientGet = func(_ *http.Client, url string) (*http.Response, error) {
return &http.Response{
Body: ioutil.NopCloser(strings.NewReader(`{"id":"id", "email":"email", "name": "name"}`)),
}, nil
}
}
func TestGoogle(t *testing.T) {
t.Parallel()
cfg := *testProviders["google"].OAuth2Config
tok := &oauth2.Token{
AccessToken: "token",
TokenType: "Bearer",
RefreshToken: "refresh",
Expiry: time.Now().Add(60 * time.Minute),
}
details, err := GoogleUserDetails(context.Background(), cfg, tok)
if err != nil {
t.Error(err)
}
if uid, ok := details[OAuth2UID]; !ok || uid != "id" {
t.Error("UID wrong:", uid)
}
if email, ok := details[OAuth2Email]; !ok || email != "email" {
t.Error("Email wrong:", email)
}
}
2015-08-03 22:35:43 +02:00
func TestFacebook(t *testing.T) {
t.Parallel()
2015-08-03 22:35:43 +02:00
cfg := *testProviders["facebook"].OAuth2Config
tok := &oauth2.Token{
AccessToken: "token",
TokenType: "Bearer",
RefreshToken: "refresh",
Expiry: time.Now().Add(60 * time.Minute),
}
details, err := FacebookUserDetails(context.Background(), cfg, tok)
2015-08-03 22:35:43 +02:00
if err != nil {
t.Error(err)
}
if uid, ok := details[OAuth2UID]; !ok || uid != "id" {
2015-08-03 22:35:43 +02:00
t.Error("UID wrong:", uid)
}
if email, ok := details[OAuth2Email]; !ok || email != "email" {
2015-08-03 22:35:43 +02:00
t.Error("Email wrong:", email)
}
if name, ok := details[OAuth2Name]; !ok || name != "name" {
2015-08-03 22:35:43 +02:00
t.Error("Name wrong:", name)
}
}