1
0
mirror of https://github.com/volatiletech/authboss.git synced 2024-11-28 08:58:38 +02:00
authboss/oauth2/providers_test.go
2017-07-30 19:42:05 -07:00

83 lines
1.8 KiB
Go

package oauth2
import (
"context"
"io/ioutil"
"net/http"
"strings"
"testing"
"time"
"github.com/volatiletech/authboss"
"golang.org/x/oauth2"
)
func TestGoogle(t *testing.T) {
saveClientGet := clientGet
defer func() {
clientGet = saveClientGet
}()
clientGet = func(_ *http.Client, url string) (*http.Response, error) {
return &http.Response{
Body: ioutil.NopCloser(strings.NewReader(`{"id":"id", "email":"email"}`)),
}, nil
}
cfg := *testProviders["google"].OAuth2Config
tok := &oauth2.Token{
AccessToken: "token",
TokenType: "Bearer",
RefreshToken: "refresh",
Expiry: time.Now().Add(60 * time.Minute),
}
user, err := Google(context.TODO(), cfg, tok)
if err != nil {
t.Error(err)
}
if uid, ok := user[authboss.StoreOAuth2UID]; !ok || uid != "id" {
t.Error("UID wrong:", uid)
}
if email, ok := user[authboss.StoreEmail]; !ok || email != "email" {
t.Error("Email wrong:", email)
}
}
func TestFacebook(t *testing.T) {
saveClientGet := clientGet
defer func() {
clientGet = saveClientGet
}()
clientGet = func(_ *http.Client, url string) (*http.Response, error) {
return &http.Response{
Body: ioutil.NopCloser(strings.NewReader(`{"id":"id", "email":"email", "name":"name"}`)),
}, nil
}
cfg := *testProviders["facebook"].OAuth2Config
tok := &oauth2.Token{
AccessToken: "token",
TokenType: "Bearer",
RefreshToken: "refresh",
Expiry: time.Now().Add(60 * time.Minute),
}
user, err := Facebook(context.TODO(), cfg, tok)
if err != nil {
t.Error(err)
}
if uid, ok := user[authboss.StoreOAuth2UID]; !ok || uid != "id" {
t.Error("UID wrong:", uid)
}
if email, ok := user[authboss.StoreEmail]; !ok || email != "email" {
t.Error("Email wrong:", email)
}
if name, ok := user["name"]; !ok || name != "name" {
t.Error("Name wrong:", name)
}
}