github.com/volatiletech/authboss@v2.4.1+incompatible/oauth2/providers_test.go (about) 1 package oauth2 2 3 import ( 4 "context" 5 "io/ioutil" 6 "net/http" 7 "strings" 8 "testing" 9 "time" 10 11 "golang.org/x/oauth2" 12 ) 13 14 func init() { 15 // This has an extra parameter that the Google client wouldn't normally 16 // get, but it'll safely be ignored. 17 clientGet = func(_ *http.Client, url string) (*http.Response, error) { 18 return &http.Response{ 19 Body: ioutil.NopCloser(strings.NewReader(`{"id":"id", "email":"email", "name": "name"}`)), 20 }, nil 21 } 22 } 23 24 func TestGoogle(t *testing.T) { 25 t.Parallel() 26 27 cfg := *testProviders["google"].OAuth2Config 28 tok := &oauth2.Token{ 29 AccessToken: "token", 30 TokenType: "Bearer", 31 RefreshToken: "refresh", 32 Expiry: time.Now().Add(60 * time.Minute), 33 } 34 35 details, err := GoogleUserDetails(context.Background(), cfg, tok) 36 if err != nil { 37 t.Error(err) 38 } 39 40 if uid, ok := details[OAuth2UID]; !ok || uid != "id" { 41 t.Error("UID wrong:", uid) 42 } 43 if email, ok := details[OAuth2Email]; !ok || email != "email" { 44 t.Error("Email wrong:", email) 45 } 46 } 47 48 func TestFacebook(t *testing.T) { 49 t.Parallel() 50 51 cfg := *testProviders["facebook"].OAuth2Config 52 tok := &oauth2.Token{ 53 AccessToken: "token", 54 TokenType: "Bearer", 55 RefreshToken: "refresh", 56 Expiry: time.Now().Add(60 * time.Minute), 57 } 58 59 details, err := FacebookUserDetails(context.Background(), cfg, tok) 60 if err != nil { 61 t.Error(err) 62 } 63 64 if uid, ok := details[OAuth2UID]; !ok || uid != "id" { 65 t.Error("UID wrong:", uid) 66 } 67 if email, ok := details[OAuth2Email]; !ok || email != "email" { 68 t.Error("Email wrong:", email) 69 } 70 if name, ok := details[OAuth2Name]; !ok || name != "name" { 71 t.Error("Name wrong:", name) 72 } 73 }