Skip to content

Commit 9c87d3f

Browse files
add tests for the github provider
1 parent 2cdf568 commit 9c87d3f

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

oauth2/oauth2_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/aarondl/authboss/v3/mocks"
1414
"golang.org/x/oauth2"
1515
"golang.org/x/oauth2/facebook"
16+
"golang.org/x/oauth2/github"
1617
"golang.org/x/oauth2/google"
1718
)
1819

@@ -46,6 +47,20 @@ var testProviders = map[string]authboss.OAuth2Provider{
4647
},
4748
FindUserDetails: FacebookUserDetails,
4849
},
50+
51+
"github": {
52+
53+
OAuth2Config: &oauth2.Config{
54+
ClientID: `jazz`,
55+
ClientSecret: `hands`,
56+
Scopes: []string{`email`},
57+
Endpoint: github.Endpoint,
58+
// This is typically set by Init() but some tests rely on it's existence
59+
RedirectURL: "https://www.example.com/auth/oauth2/callback/github",
60+
},
61+
62+
FindUserDetails: GithubUserDetails,
63+
},
4964
}
5065

5166
var testToken = &oauth2.Token{

oauth2/providers_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,36 @@ func TestFacebook(t *testing.T) {
7171
t.Error("Name wrong:", name)
7272
}
7373
}
74+
75+
func TestGithub(t *testing.T) {
76+
77+
t.Parallel()
78+
79+
cfg := *testProviders["github"].OAuth2Config
80+
81+
tok := &oauth2.Token{
82+
83+
AccessToken: "token",
84+
TokenType: "Bearer",
85+
RefreshToken: "refresh",
86+
Expiry: time.Now().Add(60 * time.Minute),
87+
}
88+
89+
details, err := GithubUserDetails(context.Background(), cfg, tok)
90+
91+
if err != nil {
92+
93+
t.Error(err)
94+
}
95+
96+
if uid, ok := details[OAuth2UID]; !ok || uid != "id" {
97+
t.Error("UID wrong:", uid)
98+
}
99+
if email, ok := details[OAuth2Email]; !ok || email != "email" {
100+
t.Error("Email wrong:", email)
101+
}
102+
if name, ok := details[OAuth2Name]; !ok || name != "name" {
103+
t.Error("Name wrong:", name)
104+
}
105+
106+
}

0 commit comments

Comments
 (0)