@@ -31,9 +31,18 @@ function makeRequests (t, fastify) {
31
31
expires_in : '240000'
32
32
}
33
33
34
+ const RESPONSE_BODY_REFRESHED = {
35
+ access_token : 'my-access-token-refreshed' ,
36
+ refresh_token : 'my-refresh-token-refreshed' ,
37
+ token_type : 'bearer' ,
38
+ expires_in : '240000'
39
+ }
40
+
34
41
const githubScope = nock ( 'https://github.com' )
35
42
. post ( '/login/oauth/access_token' , 'code=my-code&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fcallback&grant_type=authorization_code&client_id=my-client-id&client_secret=my-secret' )
36
43
. reply ( 200 , RESPONSE_BODY )
44
+ . post ( '/login/oauth/access_token' , 'grant_type=refresh_token&refresh_token=my-refresh-token&client_id=my-client-id&client_secret=my-secret' )
45
+ . reply ( 200 , RESPONSE_BODY_REFRESHED )
37
46
38
47
fastify . inject ( {
39
48
method : 'GET' ,
@@ -42,7 +51,7 @@ function makeRequests (t, fastify) {
42
51
t . error ( err )
43
52
44
53
t . equal ( responseEnd . statusCode , 200 )
45
- t . strictSame ( JSON . parse ( responseEnd . payload ) , RESPONSE_BODY )
54
+ t . strictSame ( JSON . parse ( responseEnd . payload ) , RESPONSE_BODY_REFRESHED )
46
55
47
56
githubScope . done ( )
48
57
@@ -74,12 +83,18 @@ t.test('fastify-oauth2', t => {
74
83
this . getAccessTokenFromAuthorizationCodeFlow ( request , ( err , result ) => {
75
84
if ( err ) throw err
76
85
77
- const token = this . githubOAuth2 . accessToken . create ( result )
78
- reply . send ( {
79
- access_token : token . token . access_token ,
80
- refresh_token : token . token . refresh_token ,
81
- expires_in : token . token . expires_in ,
82
- token_type : token . token . token_type
86
+ // attempts to refresh the token
87
+ this . getNewAccessTokenWithRefreshToken ( result . refresh_token , undefined , ( err , result ) => {
88
+ if ( err ) throw err
89
+
90
+ const newToken = result
91
+
92
+ reply . send ( {
93
+ access_token : newToken . token . access_token ,
94
+ refresh_token : newToken . token . refresh_token ,
95
+ expires_in : newToken . token . expires_in ,
96
+ token_type : newToken . token . token_type
97
+ } )
83
98
} )
84
99
} )
85
100
} )
@@ -109,7 +124,10 @@ t.test('fastify-oauth2', t => {
109
124
fastify . get ( '/' , function ( request , reply ) {
110
125
return this . getAccessTokenFromAuthorizationCodeFlow ( request )
111
126
. then ( result => {
112
- const token = this . githubOAuth2 . accessToken . create ( result )
127
+ // attempts to refresh the token
128
+ return this . getNewAccessTokenWithRefreshToken ( result . refresh_token )
129
+ } )
130
+ . then ( token => {
113
131
return {
114
132
access_token : token . token . access_token ,
115
133
refresh_token : token . token . refresh_token ,
0 commit comments