1
1
import {
2
2
mockServiceToken ,
3
- providerUserPayload ,
4
3
providerUserToken ,
5
- signedJwt ,
6
- signedJwtForVerification ,
7
- testTenants
4
+ signedJwt
8
5
} from '../../../../../test-resources/test/test-util' ;
9
- import * as tokenAccessor from '../token-accessor ' ;
10
- import { decodeJwt } from '../jwt ' ;
6
+ import * as xsuaaService from '../xsuaa-service ' ;
7
+ import { clientCredentialsTokenCache } from '../client-credentials-token-cache ' ;
11
8
import { getDestination } from './destination-accessor' ;
12
9
import { getDestinationFromServiceBinding } from './destination-from-vcap' ;
13
- import { destinationCache } from './destination-cache' ;
14
10
import SpyInstance = jest . SpyInstance ;
15
11
import type { Service } from '../environment-accessor' ;
16
12
17
13
describe ( 'vcap-service-destination' , ( ) => {
18
- const serviceTokenSpy = jest . spyOn ( tokenAccessor , 'serviceToken' ) ;
19
-
20
- beforeAll ( ( ) => {
21
- mockServiceToken ( ) ;
22
- } ) ;
23
-
24
14
beforeEach ( ( ) => {
25
15
mockServiceBindings ( ) ;
26
16
} ) ;
27
17
28
- afterEach ( async ( ) => {
18
+ afterEach ( ( ) => {
29
19
delete process . env . VCAP_SERVICES ;
30
- jest . clearAllMocks ( ) ;
31
- await destinationCache . clear ( ) ;
20
+ clientCredentialsTokenCache . clear ( ) ;
21
+ jest . restoreAllMocks ( ) ;
32
22
} ) ;
33
23
34
24
function getActualClientId ( spyInstance : SpyInstance ) : string {
35
25
return spyInstance . mock . calls [ 0 ] [ 0 ] [ 'credentials' ] [ 'clientid' ] ;
36
26
}
37
27
38
28
it ( 'creates a destination for the aicore service' , async ( ) => {
29
+ const serviceTokenSpy = mockServiceToken ( ) ;
39
30
await expect (
40
31
getDestinationFromServiceBinding ( {
41
32
destinationName : 'my-aicore' ,
@@ -52,6 +43,7 @@ describe('vcap-service-destination', () => {
52
43
} ) ;
53
44
54
45
it ( 'creates a destination for the business logging service' , async ( ) => {
46
+ const serviceTokenSpy = mockServiceToken ( ) ;
55
47
await expect (
56
48
getDestinationFromServiceBinding ( {
57
49
destinationName : 'my-business-logging' ,
@@ -68,6 +60,7 @@ describe('vcap-service-destination', () => {
68
60
} ) ;
69
61
70
62
it ( 'creates ad destination for the xsuaa service' , async ( ) => {
63
+ const serviceTokenSpy = mockServiceToken ( ) ;
71
64
await expect (
72
65
getDestinationFromServiceBinding ( {
73
66
destinationName : 'my-xsuaa' ,
@@ -84,6 +77,7 @@ describe('vcap-service-destination', () => {
84
77
} ) ;
85
78
86
79
it ( 'creates a destination for the service manager service' , async ( ) => {
80
+ const serviceTokenSpy = mockServiceToken ( ) ;
87
81
await expect (
88
82
getDestinationFromServiceBinding ( {
89
83
destinationName : 'my-service-manager' ,
@@ -100,6 +94,7 @@ describe('vcap-service-destination', () => {
100
94
} ) ;
101
95
102
96
it ( 'creates a destination for the destination service' , async ( ) => {
97
+ const serviceTokenSpy = mockServiceToken ( ) ;
103
98
await expect (
104
99
getDestinationFromServiceBinding ( {
105
100
destinationName : 'my-destination-service' ,
@@ -115,6 +110,7 @@ describe('vcap-service-destination', () => {
115
110
} ) ;
116
111
117
112
it ( 'creates a destination for the saas registry' , async ( ) => {
113
+ const serviceTokenSpy = mockServiceToken ( ) ;
118
114
await expect (
119
115
getDestinationFromServiceBinding ( {
120
116
destinationName : 'my-saas-registry' ,
@@ -130,6 +126,7 @@ describe('vcap-service-destination', () => {
130
126
} ) ;
131
127
132
128
it ( 'creates a destination for the workflow' , async ( ) => {
129
+ const serviceTokenSpy = mockServiceToken ( ) ;
133
130
await expect (
134
131
getDestinationFromServiceBinding ( {
135
132
destinationName : 'my-workflow' ,
@@ -157,6 +154,16 @@ describe('vcap-service-destination', () => {
157
154
} ) ;
158
155
159
156
it ( 'uses the cache if enabled' , async ( ) => {
157
+ const getClientCredentialsTokenSpy = jest
158
+ . spyOn ( xsuaaService , 'getClientCredentialsToken' )
159
+ . mockResolvedValue ( {
160
+ access_token : providerUserToken ,
161
+ token_type : 'bearer' ,
162
+ expires_in : 1000 ,
163
+ scope : 'some scope' ,
164
+ jti : 'some jti'
165
+ } ) ;
166
+
160
167
await getDestinationFromServiceBinding ( {
161
168
destinationName : 'my-destination-service' ,
162
169
useCache : true ,
@@ -167,35 +174,7 @@ describe('vcap-service-destination', () => {
167
174
useCache : true ,
168
175
jwt : providerUserToken
169
176
} ) ;
170
- expect ( serviceTokenSpy ) . toBeCalledTimes ( 1 ) ;
171
- expect (
172
- destinationCache
173
- . getCacheInstance ( )
174
- . get ( `${ providerUserPayload . zid } ::my-destination` )
175
- ) . toBeDefined ( ) ;
176
- } ) ;
177
-
178
- it ( 'returns undefined if cached destination JWT has expired' , async ( ) => {
179
- const jwtPayload = {
180
- iat : 1692273899 ,
181
- exp : 1692317098 ,
182
- zid : testTenants . provider ,
183
- user_id : 'user-prov' ,
184
- ext_attr : { enhancer : 'XSUAA' }
185
- } ;
186
- const jwt = signedJwtForVerification ( jwtPayload ) ;
187
- jest . useFakeTimers ( ) ;
188
- await getDestinationFromServiceBinding ( {
189
- destinationName : 'my-destination-service' ,
190
- useCache : true ,
191
- jwt
192
- } ) ;
193
- jest . advanceTimersByTime ( 720 * 60 * 1000 + 1 ) ;
194
- await expect (
195
- destinationCache
196
- . getCacheInstance ( )
197
- . get ( `${ jwtPayload . zid } ::my-destination-service` )
198
- ) . resolves . toBeUndefined ( ) ;
177
+ expect ( getClientCredentialsTokenSpy ) . toHaveBeenCalledTimes ( 1 ) ;
199
178
} ) ;
200
179
201
180
it ( 'creates a destination using a custom transformation function' , async ( ) => {
@@ -274,7 +253,7 @@ describe('vcap-service-destination', () => {
274
253
expect ( serviceBindingTransformFn ) . toBeCalledTimes ( 1 ) ;
275
254
} ) ;
276
255
277
- it ( 'sets forwarded auth token if needed (uncached) ' , async ( ) => {
256
+ it ( 'sets forwarded auth token if needed' , async ( ) => {
278
257
const jwt = signedJwt ( { } ) ;
279
258
280
259
const destination = await getDestinationFromServiceBinding ( {
@@ -288,51 +267,6 @@ describe('vcap-service-destination', () => {
288
267
289
268
expect ( destination ?. authTokens ?. [ 0 ] ) . toMatchObject ( { value : jwt } ) ;
290
269
} ) ;
291
-
292
- it ( 'sets forwarded auth token if needed (cached)' , async ( ) => {
293
- const jwt = signedJwt ( { zid : 'tenant' } ) ;
294
-
295
- destinationCache . cacheRetrievedDestination (
296
- decodeJwt ( jwt ) ,
297
- {
298
- name : 'my-custom-service' ,
299
- forwardAuthToken : true
300
- } ,
301
- 'tenant'
302
- ) ;
303
-
304
- const destination = await getDestinationFromServiceBinding ( {
305
- destinationName : 'my-custom-service' ,
306
- useCache : true ,
307
- jwt
308
- } ) ;
309
-
310
- expect ( destination ?. authTokens ?. [ 0 ] ) . toMatchObject ( { value : jwt } ) ;
311
- } ) ;
312
-
313
- // TODO: fix in #1123
314
- it . skip ( 'does not cache the forwarded token' , async ( ) => {
315
- const jwt = signedJwt ( { zid : 'tenant' } ) ;
316
-
317
- await getDestinationFromServiceBinding ( {
318
- destinationName : 'my-custom-service' ,
319
- useCache : true ,
320
- jwt,
321
- serviceBindingTransformFn : async ( { name } ) => ( {
322
- forwardAuthToken : true ,
323
- name
324
- } )
325
- } ) ;
326
-
327
- const destination = await destinationCache . retrieveDestinationFromCache (
328
- decodeJwt ( jwt ) ,
329
- 'my-custom-service' ,
330
- 'tenant'
331
- ) ;
332
-
333
- expect ( destination ) . toBeDefined ( ) ;
334
- expect ( destination ?. authTokens ) . toBeUndefined ( ) ;
335
- } ) ;
336
270
} ) ;
337
271
338
272
function mockServiceBindings ( ) {
0 commit comments