Skip to content

Commit

Permalink
chore: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhongpinWang committed Feb 24, 2025
1 parent e60f75b commit 4294f65
Showing 1 changed file with 109 additions and 0 deletions.
109 changes: 109 additions & 0 deletions packages/connectivity/src/scp-cf/destination/destination-cache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ import type {
Destination,
DestinationAuthToken
} from './destination-service-types';
import exp from 'constants';

Check failure on line 54 in packages/connectivity/src/scp-cf/destination/destination-cache.spec.ts

View workflow job for this annotation

GitHub Actions / checks

`constants` import should occur before import of `@sap-cloud-sdk/util`

Check failure on line 54 in packages/connectivity/src/scp-cf/destination/destination-cache.spec.ts

View workflow job for this annotation

GitHub Actions / checks

'exp' is defined but never used

Check failure on line 54 in packages/connectivity/src/scp-cf/destination/destination-cache.spec.ts

View workflow job for this annotation

GitHub Actions / checks

'exp' is defined but never used
import { JwtPayload } from 'jsonwebtoken';

Check failure on line 55 in packages/connectivity/src/scp-cf/destination/destination-cache.spec.ts

View workflow job for this annotation

GitHub Actions / checks

`jsonwebtoken` import should occur before import of `../jwt`

Check failure on line 55 in packages/connectivity/src/scp-cf/destination/destination-cache.spec.ts

View workflow job for this annotation

GitHub Actions / checks

'JwtPayload' is defined but never used

Check failure on line 55 in packages/connectivity/src/scp-cf/destination/destination-cache.spec.ts

View workflow job for this annotation

GitHub Actions / checks

'JwtPayload' is defined but never used
import { signedJwtForVerification } from '../../../../../test-resources/test/test-util';

Check failure on line 56 in packages/connectivity/src/scp-cf/destination/destination-cache.spec.ts

View workflow job for this annotation

GitHub Actions / checks

`../../../../../test-resources/test/test-util` import should occur before import of `./destination-service-cache`

const destinationOne: Destination = {
url: 'https://destination1.example',
Expand Down Expand Up @@ -806,6 +809,112 @@ describe('destination cache', () => {
});
});

it('should return undefined when Proxy-Authorization token expires first', async () => {
jest.useFakeTimers();
const twoMinutesTokenLifetime = 2 * 60;
const fourMinutesTokenLifetime = 4 * 60;
const sixMinutesTokenLifetime = 6 * 60;
const dummyJwt = { user_id: 'user', zid: 'tenant' };

const proxyAuthorizationToken = signedJwtForVerification({
...decodeJwt(providerServiceToken),
exp: twoMinutesTokenLifetime
});

const sapConnectivityAuthenticationToken = signedJwtForVerification({
...decodeJwt(providerUserToken),
exp: fourMinutesTokenLifetime
});

const destination = {
...destinationOne,
authTokens: [
{
expiresIn: sixMinutesTokenLifetime.toString()
} as DestinationAuthToken
],
proxyConfiguration: {
...connectivityProxyConfigMock,
headers: {
'Proxy-Authorization': `Bearer ${proxyAuthorizationToken}`,
'SAP-Connectivity-Authentication': `Bearer ${sapConnectivityAuthenticationToken}`
}
}
};

await destinationCache.cacheRetrievedDestination(
dummyJwt,
destination,
'tenant-user'
);

const retrieveDestination = () =>
destinationCache.retrieveDestinationFromCache(
dummyJwt,
destination.name!,
'tenant-user'
);

await expect(retrieveDestination()).resolves.toEqual(destination);

jest.advanceTimersByTime(twoMinutesTokenLifetime * 1000 + 1);

await expect(retrieveDestination()).resolves.toBeUndefined();
});

it('should return undefined when SAP-Connectivity-Authentication authorization token expires first', async () => {
jest.useFakeTimers();
const twoMinutesTokenLifetime = 2 * 60;
const fourMinutesTokenLifetime = 4 * 60;
const sixMinutesTokenLifetime = 6 * 60;
const dummyJwt = { user_id: 'user', zid: 'tenant' };

const proxyAuthorizationToken = signedJwtForVerification({
...decodeJwt(providerServiceToken),
exp: fourMinutesTokenLifetime
});

const sapConnectivityAuthenticationToken = signedJwtForVerification({
...decodeJwt(providerUserToken),
exp: twoMinutesTokenLifetime
});

const destination = {
...destinationOne,
authTokens: [
{
expiresIn: sixMinutesTokenLifetime.toString()
} as DestinationAuthToken
],
proxyConfiguration: {
...connectivityProxyConfigMock,
headers: {
'Proxy-Authorization': `Bearer ${proxyAuthorizationToken}`,
'SAP-Connectivity-Authentication': `Bearer ${sapConnectivityAuthenticationToken}`
}
}
};

await destinationCache.cacheRetrievedDestination(
dummyJwt,
destination,
'tenant-user'
);

const retrieveDestination = () =>
destinationCache.retrieveDestinationFromCache(
dummyJwt,
destination.name!,
'tenant-user'
);

await expect(retrieveDestination()).resolves.toEqual(destination);

jest.advanceTimersByTime(twoMinutesTokenLifetime * 1000 + 1);

await expect(retrieveDestination()).resolves.toBeUndefined();
});

describe('custom destination cache', () => {
// Cache with expiration time
const testCacheOne = new TestCache();
Expand Down

0 comments on commit 4294f65

Please sign in to comment.