Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
243 changes: 129 additions & 114 deletions examples/pnpm-lock.yaml

Large diffs are not rendered by default.

41 changes: 7 additions & 34 deletions lib/src/wso2/wso2-api/handler/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import nock from 'nock';
import { mockClient } from 'aws-sdk-client-mock';
import { GetSecretValueCommand, SecretsManagerClient } from '@aws-sdk/client-secrets-manager';

import { petstoreOpenapi } from '../__tests__/petstore';
import { ApiFromListV1, PublisherPortalAPIv1, Wso2ApiDefinitionV1 } from '../v1/types';
import { Wso2ApiCustomResourceProperties } from '../types';
import { nockBasicWso2SDK } from '../../wso2-utils.test';

import { Wso2ApiCustomResourceEvent, handler } from './index';

Expand Down Expand Up @@ -44,7 +43,7 @@ describe('wso2 custom resource lambda', () => {
});

it('basic wso2 api create', async () => {
nockBasicWso2SDK();
nockBasicWso2SDK(baseWso2Url);

// api list mock
nock(baseWso2Url)
Expand Down Expand Up @@ -88,7 +87,7 @@ describe('wso2 custom resource lambda', () => {
});

it('basic wso2 api update', async () => {
nockBasicWso2SDK();
nockBasicWso2SDK(baseWso2Url);

// api list mock
const testDefs: Wso2ApiDefinitionV1 = {
Expand Down Expand Up @@ -136,7 +135,7 @@ describe('wso2 custom resource lambda', () => {
});

it('basic wso2 api change on UPDATE operation', async () => {
nockBasicWso2SDK();
nockBasicWso2SDK(baseWso2Url);

// api list mock
const testDefs: Wso2ApiDefinitionV1 = {
Expand Down Expand Up @@ -181,7 +180,7 @@ describe('wso2 custom resource lambda', () => {
});

it('should pass with success if wso2 answers properly after a few retries', async () => {
nockBasicWso2SDK();
nockBasicWso2SDK(baseWso2Url);

// api list mock
const testDefs: Wso2ApiDefinitionV1 = {
Expand Down Expand Up @@ -257,7 +256,7 @@ describe('wso2 custom resource lambda', () => {
});

it('should fail after retrying checking WSO2 api for a few times', async () => {
nockBasicWso2SDK();
nockBasicWso2SDK(baseWso2Url);

// api list mock
const testDefs: Wso2ApiDefinitionV1 = {
Expand Down Expand Up @@ -317,7 +316,7 @@ describe('wso2 custom resource lambda', () => {
});

it('basic wso2 api delete on DELETE operation', async () => {
nockBasicWso2SDK();
nockBasicWso2SDK(baseWso2Url);

// api update mock
nock(baseWso2Url)
Expand Down Expand Up @@ -415,32 +414,6 @@ describe('wso2 custom resource lambda', () => {
retryOptions: testRetryOptions,
};

const nockBasicWso2SDK = (): void => {
const secretMock = mockClient(SecretsManagerClient);
secretMock.on(GetSecretValueCommand).resolves({
SecretBinary: Buffer.from(JSON.stringify({ user: 'user1', pwd: 'pwd1' })),
});

// register client mock
nock(baseWso2Url).post('/client-registration/v0.17/register').reply(200, {
clientId: 'clientId1',
clientSecret: 'clientSecret1',
});

// get token mock
nock(baseWso2Url).post('/oauth2/token').reply(200, {
access_token: '1111-1111-1111',
});

// mock server check
nock(baseWso2Url)
.get('/services/Version')
.reply(
200,
'<ns:getVersionResponse xmlns:ns="http://version.services.core.carbon.wso2.org"><return>WSO2 API Manager-3.2.0</return></ns:getVersionResponse>',
);
};

const nockAfterUpdateCreate = (testDefs: Wso2ApiDefinitionV1): void => {
// api openapi update mock
nock(baseWso2Url)
Expand Down
35 changes: 4 additions & 31 deletions lib/src/wso2/wso2-application/handler/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import nock from 'nock';
import { mockClient } from 'aws-sdk-client-mock';
import { GetSecretValueCommand, SecretsManagerClient } from '@aws-sdk/client-secrets-manager';

import { Wso2ApplicationDefinition } from '../v1/types';
import { Wso2ApplicationCustomResourceProperties } from '../types';
import { nockBasicWso2SDK } from '../../wso2-utils.test';

import { Wso2ApplicationCustomResourceEvent, handler } from './index';

Expand Down Expand Up @@ -43,7 +42,7 @@ describe('wso2 application custom resource lambda', () => {
});

it('wso2 application delete', async () => {
nockBasicWso2SDK();
nockBasicWso2SDK(baseWso2Url);

// application get mock
nock(baseWso2Url)
Expand All @@ -56,7 +55,7 @@ describe('wso2 application custom resource lambda', () => {
});

it('basic wso2 application update', async () => {
nockBasicWso2SDK();
nockBasicWso2SDK(baseWso2Url);

const testDefs: Wso2ApplicationDefinition = testApplicationDefs();

Expand Down Expand Up @@ -89,7 +88,7 @@ describe('wso2 application custom resource lambda', () => {
});

it('basic wso2 application create', async () => {
nockBasicWso2SDK();
nockBasicWso2SDK(baseWso2Url);

// api list mock
nock(baseWso2Url)
Expand Down Expand Up @@ -183,30 +182,4 @@ describe('wso2 application custom resource lambda', () => {
applicationDefinition: testApplicationDefs(),
retryOptions: testRetryOptions,
};

const nockBasicWso2SDK = (): void => {
const secretMock = mockClient(SecretsManagerClient);
secretMock.on(GetSecretValueCommand).resolves({
SecretBinary: Buffer.from(JSON.stringify({ user: 'user1', pwd: 'pwd1' })),
});

// register client mock
nock(baseWso2Url).post('/client-registration/v0.17/register').reply(200, {
clientId: 'clientId1',
clientSecret: 'clientSecret1',
});

// get token mock
nock(baseWso2Url).post('/oauth2/token').reply(200, {
access_token: '1111-1111-1111',
});

// mock server check
nock(baseWso2Url)
.get('/services/Version')
.reply(
200,
'<ns:getVersionResponse xmlns:ns="http://version.services.core.carbon.wso2.org"><return>WSO2 API Manager-3.2.0</return></ns:getVersionResponse>',
);
};
});
182 changes: 182 additions & 0 deletions lib/src/wso2/wso2-subscription/handler/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
/* eslint-disable no-console */
/* eslint-disable camelcase */
/* eslint-disable @typescript-eslint/no-explicit-any */

import nock from 'nock';

import { Wso2SubscriptionDefinition } from '../v1/types';
import { Wso2SubscriptionCustomResourceProperties } from '../types';
import { nockBasicWso2SDK } from '../../wso2-utils.test';

import { handler, Wso2SubscriptionCustomResourceEvent } from './index';

const baseWso2Url = 'https://mywso2.com';

const testRetryOptions = {
checkRetries: {
startingDelay: 100,
delayFirstAttempt: true,
maxDelay: 100,
numOfAttempts: 0,
timeMultiple: 1.1,
},
mutationRetries: {
startingDelay: 100,
delayFirstAttempt: true,
maxDelay: 100,
numOfAttempts: 0,
timeMultiple: 1.1,
},
};

const originalConsoleLog = console.log;

describe('wso2 subscription custom resource lambda', () => {
beforeEach(() => {
nock.cleanAll();
// silence verbose console logs. comment this for debugging
// console.log = (): void => {};
});
afterEach(() => {
console.log = originalConsoleLog;
});

it('wso2 subscription delete', async () => {
nockBasicWso2SDK(baseWso2Url);

// application get mock
nock(baseWso2Url)
.delete(/.*\/store\/v1\/subscriptions\/[^\\/]+$/)
.times(1) // check if was created
.reply(200);

const eres = await handler(testCFNEventDelete(testEvent, '123-456'));
expect(eres.Status).toBe('SUCCESS');
});

it('basic wso2 subscription update', async () => {
nockBasicWso2SDK(baseWso2Url);

const testDefs: Wso2SubscriptionDefinition = testSubscriptionDefs();

// subscriptions list mock
nock(baseWso2Url)
.get(/.*\/store\/v1\/subscriptions.*/)
.query(true)
.times(1) // check create or update
.reply(200, { list: [{ ...testDefs, subscriptionId: '123-456' }] });

// subscription update mock
nock(baseWso2Url)
.put(/.*\/store\/v1\/subscriptions\/[^\\/]+$/)
.times(1)
.reply(200);

// subscription get mock
nock(baseWso2Url)
.get(/.*\/store\/v1\/subscriptions\/[^\\/]+$/)
.times(1) // check if was created
.reply(200, { ...testDefs });

const eres = await handler(
testCFNEventCreate({
...testEvent,
}),
);
expect(eres.PhysicalResourceId).toBe('123-456');
expect(eres.Status).toBe('SUCCESS');
});

it('basic wso2 subscription create', async () => {
nockBasicWso2SDK(baseWso2Url);

// subscriptions list mock
nock(baseWso2Url)
.get(/.*\/store\/v1\/subscriptions.*/)
.query(true)
.times(1) // check create or update
.reply(200, { list: [] });

const testDefs: Wso2SubscriptionDefinition = testSubscriptionDefs();

// subscription create mock
nock(baseWso2Url)
.post(/.*\/store\/v1\/subscriptions$/)
.reply(201, { ...testDefs, subscriptionId: '123-456' });

// subscription get mock
nock(baseWso2Url)
.get(/.*\/store\/v1\/subscriptions\/[^\\/]+$/)
.times(1) // check if was created
.reply(200, { ...testDefs });

const eres = await handler(
testCFNEventCreate({
...testEvent,
}),
);
expect(eres.PhysicalResourceId).toBe('123-456');
expect(eres.Status).toBe('SUCCESS');
});

const testSubscriptionDefs = (): Wso2SubscriptionDefinition => {
return {
apiId: '111-222',
applicationId: '333-444',
throttlingPolicy: 'Unlimited',
};
};

const commonEvt = {
StackId: 'test-stack',
RequestId: '123-123123',
LogicalResourceId: 'abc abc',
ServiceToken: 'arn:somelambdatest',
ResponseURL: 's3bucketxxx',
ResourceType: 'wso2subscription',
};

const testCFNEventCreate = (
baseProperties: Wso2SubscriptionCustomResourceProperties,
): Wso2SubscriptionCustomResourceEvent => {
return {
...commonEvt,
RequestType: 'Create',
ResourceProperties: { ...baseProperties, ServiceToken: 'arn:somelambdatest' },
};
};
const testCFNEventDelete = (
baseProperties: Wso2SubscriptionCustomResourceProperties,
PhysicalResourceId: string,
): Wso2SubscriptionCustomResourceEvent => {
return {
...commonEvt,
RequestType: 'Delete',
ResourceProperties: { ...baseProperties, ServiceToken: 'arn:somelambdatest' },
PhysicalResourceId,
};
};
// const testCFNEventUpdate = (
// baseProperties: Wso2SubscriptionCustomResourceProperties,
// PhysicalResourceId: string,
// oldResourceProperties: Record<string, string>,
// ): Wso2SubscriptionCustomResourceEvent => {
// return {
// ...commonEvt,
// RequestType: 'Update',
// ResourceProperties: { ...baseProperties, ServiceToken: 'arn:somelambdatest' },
// PhysicalResourceId,
// OldResourceProperties: oldResourceProperties,
// };
// };

const testEvent: Wso2SubscriptionCustomResourceProperties = {
wso2Config: {
baseApiUrl: baseWso2Url,
credentialsSecretId: 'arn:aws:secretsmanager:us-east-1:123123123:secret:MySecret',
apiVersion: 'v1',
},
subscriptionDefinition: testSubscriptionDefs(),
retryOptions: testRetryOptions,
};
});
Loading