1
0
mirror of https://github.com/volatiletech/authboss.git synced 2025-01-10 04:17:59 +02:00
authboss/oauth2/providers_test.go
Aaron L 1112987bce Rewrite oauth module
- Tried to be clear about OAuth2 vs OAuth in all places.
- Allow users to be locked from OAuth logins (if done manually for some
  reason other than failed logins)
- Cleaned up some docs and wording around the previously very confusing
  (now hopefully only somewhat confusing) oauth2 module.
2018-03-08 18:39:51 -08:00

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)
}
}