4
4
const requestWrapper = require ( '../../lib/requestwrapper' ) ;
5
5
requestWrapper . sendRequest = jest . fn ( ) ;
6
6
7
+ const jwt = require ( 'jsonwebtoken' ) ;
8
+ jwt . decode = jest . fn ( ( ) => {
9
+ return { exp : 100 , iat : 100 } ;
10
+ } ) ;
11
+
7
12
const IamTokenManagerV1 = require ( '../../iam-token-manager/v1' ) . IamTokenManagerV1 ;
8
13
9
14
const CLIENT_ID_SECRET_WARNING =
@@ -22,19 +27,16 @@ describe('iam_token_manager_v1', function() {
22
27
const userManagedToken = 'abcd-1234' ;
23
28
const instance = new IamTokenManagerV1 ( { iamAccessToken : userManagedToken } ) ;
24
29
const requestMock = jest . spyOn ( instance , 'requestToken' ) ;
25
- const refreshMock = jest . spyOn ( instance , 'refreshToken' ) ;
26
30
27
31
instance . getToken ( function ( err , token ) {
28
32
expect ( token ) . toBe ( userManagedToken ) ;
29
33
expect ( requestMock ) . not . toHaveBeenCalled ( ) ;
30
- expect ( refreshMock ) . not . toHaveBeenCalled ( ) ;
31
34
done ( ) ;
32
35
} ) ;
33
36
} ) ;
34
37
35
38
it ( 'should turn an iam apikey into an access token' , function ( done ) {
36
39
const instance = new IamTokenManagerV1 ( { iamApikey : 'abcd-1234' } ) ;
37
- const refreshMock = jest . spyOn ( instance , 'refreshToken' ) ;
38
40
39
41
const accessToken = '9012' ;
40
42
const iamResponse = {
@@ -51,7 +53,6 @@ describe('iam_token_manager_v1', function() {
51
53
52
54
instance . getToken ( function ( err , token ) {
53
55
expect ( token ) . toBe ( accessToken ) ;
54
- expect ( refreshMock ) . not . toHaveBeenCalled ( ) ;
55
56
done ( ) ;
56
57
} ) ;
57
58
} ) ;
@@ -85,15 +86,14 @@ describe('iam_token_manager_v1', function() {
85
86
86
87
instance . getToken ( function ( err , token ) {
87
88
expect ( token ) . toBe ( accessToken ) ;
88
- expect ( requestMock ) . not . toHaveBeenCalled ( ) ;
89
+ expect ( requestMock ) . toHaveBeenCalled ( ) ;
89
90
done ( ) ;
90
91
} ) ;
91
92
} ) ;
92
93
93
94
it ( 'should use a valid access token if one is stored' , function ( done ) {
94
95
const instance = new IamTokenManagerV1 ( { iamApikey : 'abcd-1234' } ) ;
95
96
const requestMock = jest . spyOn ( instance , 'requestToken' ) ;
96
- const refreshMock = jest . spyOn ( instance , 'refreshToken' ) ;
97
97
98
98
const accessToken = '1234' ;
99
99
const currentTokenInfo = {
@@ -105,10 +105,11 @@ describe('iam_token_manager_v1', function() {
105
105
} ;
106
106
107
107
instance . tokenInfo = currentTokenInfo ;
108
+ instance . timeToLive = currentTokenInfo . expires_in ;
109
+ instance . expireTime = currentTokenInfo . expiration ;
108
110
109
111
instance . getToken ( function ( err , token ) {
110
112
expect ( token ) . toBe ( accessToken ) ;
111
- expect ( refreshMock ) . not . toHaveBeenCalled ( ) ;
112
113
expect ( requestMock ) . not . toHaveBeenCalled ( ) ;
113
114
done ( ) ;
114
115
} ) ;
@@ -117,7 +118,6 @@ describe('iam_token_manager_v1', function() {
117
118
it ( 'should return a user-managed access token if one is set post-construction' , function ( done ) {
118
119
const instance = new IamTokenManagerV1 ( { iamApikey : 'abcd-1234' } ) ;
119
120
const requestMock = jest . spyOn ( instance , 'requestToken' ) ;
120
- const refreshMock = jest . spyOn ( instance , 'refreshToken' ) ;
121
121
122
122
const accessToken = '9012' ;
123
123
const currentTokenInfo = {
@@ -133,7 +133,6 @@ describe('iam_token_manager_v1', function() {
133
133
134
134
instance . getToken ( function ( err , token ) {
135
135
expect ( token ) . toBe ( accessToken ) ;
136
- expect ( refreshMock ) . not . toHaveBeenCalled ( ) ;
137
136
expect ( requestMock ) . not . toHaveBeenCalled ( ) ;
138
137
done ( ) ;
139
138
} ) ;
@@ -167,14 +166,13 @@ describe('iam_token_manager_v1', function() {
167
166
168
167
instance . getToken ( function ( err , token ) {
169
168
expect ( token ) . toBe ( accessToken ) ;
170
- expect ( requestMock ) . not . toHaveBeenCalled ( ) ;
169
+ expect ( requestMock ) . toHaveBeenCalled ( ) ;
171
170
done ( ) ;
172
171
} ) ;
173
172
} ) ;
174
173
175
174
it ( 'should request a new token when refresh token does not have expiration field' , function ( done ) {
176
175
const instance = new IamTokenManagerV1 ( { iamApikey : 'abcd-1234' } ) ;
177
- const refreshMock = jest . spyOn ( instance , 'refreshToken' ) ;
178
176
179
177
const currentTokenInfo = {
180
178
access_token : '1234' ,
@@ -199,7 +197,6 @@ describe('iam_token_manager_v1', function() {
199
197
200
198
instance . getToken ( function ( err , token ) {
201
199
expect ( token ) . toBe ( accessToken ) ;
202
- expect ( refreshMock ) . not . toHaveBeenCalled ( ) ;
203
200
done ( ) ;
204
201
} ) ;
205
202
} ) ;
@@ -208,7 +205,7 @@ describe('iam_token_manager_v1', function() {
208
205
const instance = new IamTokenManagerV1 ( { iamApikey : 'abcd-1234' } ) ;
209
206
210
207
requestWrapper . sendRequest . mockImplementation ( ( parameters , _callback ) => {
211
- _callback ( ) ;
208
+ _callback ( null , { access_token : 'abcd' } ) ;
212
209
} ) ;
213
210
214
211
instance . getToken ( function ( ) {
@@ -227,7 +224,7 @@ describe('iam_token_manager_v1', function() {
227
224
} ) ;
228
225
229
226
requestWrapper . sendRequest . mockImplementation ( ( parameters , _callback ) => {
230
- _callback ( ) ;
227
+ _callback ( null , { access_token : 'abcd' } ) ;
231
228
} ) ;
232
229
233
230
instance . getToken ( function ( ) {
@@ -252,7 +249,7 @@ describe('iam_token_manager_v1', function() {
252
249
console . log . mockRestore ( ) ;
253
250
254
251
requestWrapper . sendRequest . mockImplementation ( ( parameters , _callback ) => {
255
- _callback ( ) ;
252
+ _callback ( null , { access_token : 'abcd' } ) ;
256
253
} ) ;
257
254
258
255
instance . getToken ( function ( ) {
@@ -276,7 +273,7 @@ describe('iam_token_manager_v1', function() {
276
273
console . log . mockRestore ( ) ;
277
274
278
275
requestWrapper . sendRequest . mockImplementation ( ( parameters , _callback ) => {
279
- _callback ( ) ;
276
+ _callback ( null , { access_token : 'abcd' } ) ;
280
277
} ) ;
281
278
282
279
instance . getToken ( function ( ) {
@@ -295,7 +292,7 @@ describe('iam_token_manager_v1', function() {
295
292
instance . setIamAuthorizationInfo ( 'foo' , 'bar' ) ;
296
293
297
294
requestWrapper . sendRequest . mockImplementation ( ( parameters , _callback ) => {
298
- _callback ( ) ;
295
+ _callback ( null , { access_token : 'abcd' } ) ;
299
296
} ) ;
300
297
301
298
instance . getToken ( function ( ) {
@@ -321,7 +318,7 @@ describe('iam_token_manager_v1', function() {
321
318
console . log . mockRestore ( ) ;
322
319
323
320
requestWrapper . sendRequest . mockImplementation ( ( parameters , _callback ) => {
324
- _callback ( ) ;
321
+ _callback ( null , { access_token : 'abcd' } ) ;
325
322
} ) ;
326
323
327
324
instance . getToken ( function ( ) {
@@ -347,7 +344,7 @@ describe('iam_token_manager_v1', function() {
347
344
console . log . mockRestore ( ) ;
348
345
349
346
requestWrapper . sendRequest . mockImplementation ( ( parameters , _callback ) => {
350
- _callback ( ) ;
347
+ _callback ( null , { access_token : 'abcd' } ) ;
351
348
} ) ;
352
349
353
350
instance . getToken ( function ( ) {
@@ -366,7 +363,7 @@ describe('iam_token_manager_v1', function() {
366
363
instance . setIamAuthorizationInfo ( null , null ) ;
367
364
368
365
requestWrapper . sendRequest . mockImplementation ( ( parameters , _callback ) => {
369
- _callback ( ) ;
366
+ _callback ( null , { access_token : 'abcd' } ) ;
370
367
} ) ;
371
368
372
369
instance . getToken ( function ( ) {
0 commit comments