mirror of
https://github.com/volatiletech/authboss.git
synced 2024-12-12 10:45:11 +02:00
8213e87e83
- Fix #183
74 lines
1.6 KiB
Go
74 lines
1.6 KiB
Go
package oauth2
|
|
|
|
import (
|
|
"context"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"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)
|
|
}
|
|
}
|
|
|
|
func TestFacebook(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
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)
|
|
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)
|
|
}
|
|
if name, ok := details[OAuth2Name]; !ok || name != "name" {
|
|
t.Error("Name wrong:", name)
|
|
}
|
|
}
|