Skip to content

Commit

Permalink
test: move consumer e2e to sperated spec file
Browse files Browse the repository at this point in the history
  • Loading branch information
bzp2010 committed Sep 15, 2024
1 parent 9700a71 commit 1300521
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 145 deletions.
195 changes: 195 additions & 0 deletions libs/backend-api7/e2e/resources/consumer.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
import * as ADCSDK from '@api7/adc-sdk';
import { gte } from 'lodash';
import { lt } from 'semver';

import { BackendAPI7 } from '../../src';
import { conditionalDescribe, semverCondition } from '../support/utils';
import {
createEvent,
deleteEvent,
dumpConfiguration,
syncEvents,
updateEvent,
} from '../support/utils';

describe('Consumer E2E', () => {
let backend: BackendAPI7;

beforeAll(() => {
backend = new BackendAPI7({
server: process.env.SERVER,
token: process.env.TOKEN,
tlsSkipVerify: true,
gatewayGroup: 'default',
});
});

conditionalDescribe(semverCondition(lt, '3.2.15'))(
'Sync and dump consumers (without credentials support)',
() => {
const consumer1Name = 'consumer1';
const consumer1 = {
username: consumer1Name,
plugins: {
'key-auth': {
key: consumer1Name,
},
},
} as ADCSDK.Consumer;
const consumer2Name = 'consumer2';
const consumer2 = {
username: consumer2Name,
plugins: {
'key-auth': {
key: consumer2Name,
},
},
} as ADCSDK.Consumer;

it('Create consumers', async () =>
syncEvents(backend, [
createEvent(ADCSDK.ResourceType.CONSUMER, consumer1Name, consumer1),
createEvent(ADCSDK.ResourceType.CONSUMER, consumer2Name, consumer2),
]));

it('Dump', async () => {
const result = (await dumpConfiguration(
backend,
)) as ADCSDK.Configuration;
expect(result.consumers).toHaveLength(2);
expect(result.consumers[0]).toMatchObject(consumer2);
expect(result.consumers[1]).toMatchObject(consumer1);
});

it('Update consumer1', async () => {
consumer1.description = 'desc';
await syncEvents(backend, [
updateEvent(ADCSDK.ResourceType.CONSUMER, consumer1Name, consumer1),
]);
});

it('Dump again (consumer1 updated)', async () => {
const result = (await dumpConfiguration(
backend,
)) as ADCSDK.Configuration;
expect(result.consumers[0]).toMatchObject(consumer1);
});

it('Delete consumer1', async () =>
syncEvents(backend, [
deleteEvent(ADCSDK.ResourceType.CONSUMER, consumer1Name),
]));

it('Dump again (consumer1 should not exist)', async () => {
const result = (await dumpConfiguration(
backend,
)) as ADCSDK.Configuration;
expect(result.consumers).toHaveLength(1);
expect(result.consumers[0]).toMatchObject(consumer2);
});

it('Delete consumer2', async () =>
syncEvents(backend, [
deleteEvent(ADCSDK.ResourceType.CONSUMER, consumer2Name),
]));

it('Dump again (consumer2 should not exist)', async () => {
const result = (await dumpConfiguration(
backend,
)) as ADCSDK.Configuration;
expect(result.consumers).toHaveLength(0);
});
},
);

conditionalDescribe(semverCondition(gte, '3.2.15'))(
'Sync and dump consumers (with credentials support)',
() => {
const consumer1Name = 'consumer1';
const consumer1Key = 'consumer1-key';
const consumer1Cred = {
name: consumer1Key,
type: 'key-auth',
config: { key: consumer1Key },
};
const consumer1 = {
username: consumer1Name,
credentials: [consumer1Cred],
} as ADCSDK.Consumer;

it('Create consumers', async () =>
syncEvents(backend, [
createEvent(ADCSDK.ResourceType.CONSUMER, consumer1Name, consumer1),
createEvent(
ADCSDK.ResourceType.CONSUMER_CREDENTIAL,
consumer1Key,
consumer1Cred,
consumer1Name,
),
]));

it('Dump', async () => {
const result = (await dumpConfiguration(
backend,
)) as ADCSDK.Configuration;
expect(result.consumers).toHaveLength(1);
expect(result.consumers[0]).toMatchObject(consumer1);
expect(result.consumers[0].credentials).toMatchObject(
consumer1.credentials,
);
});

it('Update consumer credential', async () => {
consumer1.credentials[0].config.key = 'new-key';
await syncEvents(backend, [
updateEvent(
ADCSDK.ResourceType.CONSUMER_CREDENTIAL,
consumer1Key,
consumer1,
consumer1Name,
),
]);
});

it('Dump again (consumer credential updated)', async () => {
const result = (await dumpConfiguration(
backend,
)) as ADCSDK.Configuration;
expect(result.consumers[0]).toMatchObject(consumer1);
expect(result.consumers[0].credentials[0].config.key).toEqual(
'new-key',
);
});

it('Delete consumer credential', async () =>
syncEvents(backend, [
deleteEvent(
ADCSDK.ResourceType.CONSUMER_CREDENTIAL,
consumer1Key,
consumer1Name,
),
]));

it('Dump again (consumer credential should not exist)', async () => {
const result = (await dumpConfiguration(
backend,
)) as ADCSDK.Configuration;
expect(result.consumers).toHaveLength(1);
expect(result.consumers[0]).toMatchObject(consumer1);
expect(result.consumers[0].credentials).toHaveLength(0);
});

it('Delete consumer', async () =>
syncEvents(backend, [
deleteEvent(ADCSDK.ResourceType.CONSUMER, consumer1Name),
]));

it('Dump again (consumer should not exist)', async () => {
const result = (await dumpConfiguration(
backend,
)) as ADCSDK.Configuration;
expect(result.consumers).toHaveLength(0);
});
},
);
});
13 changes: 13 additions & 0 deletions libs/backend-api7/e2e/support/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,16 @@ export const deleteEvent = (
),
parentId: parentName ? ADCSDK.utils.generateId(parentName) : undefined,
});

type cond = boolean | (() => boolean);

export const conditionalDescribe = (cond: cond) =>
cond ? describe : describe.skip;

export const conditionalIt = (cond: cond) => (cond ? it : it.skip);

export const semverCondition = (
op: (v1: string, v2: string) => boolean,
target: string,
base = process.env.BACKEND_API7_VERSION ?? '0.0.0',
) => op(target, base);
145 changes: 0 additions & 145 deletions libs/backend-api7/e2e/sync-and-dump-1.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,73 +247,6 @@ describe('Sync and Dump - 1', () => {
});
});

describe('Sync and dump consumers', () => {
const consumer1Name = 'consumer1';
const consumer1 = {
username: consumer1Name,
plugins: {
'key-auth': {
key: consumer1Name,
},
},
} as ADCSDK.Consumer;
const consumer2Name = 'consumer2';
const consumer2 = {
username: consumer2Name,
plugins: {
'key-auth': {
key: consumer2Name,
},
},
} as ADCSDK.Consumer;

it('Create consumers', async () =>
syncEvents(backend, [
createEvent(ADCSDK.ResourceType.CONSUMER, consumer1Name, consumer1),
createEvent(ADCSDK.ResourceType.CONSUMER, consumer2Name, consumer2),
]));

it('Dump', async () => {
const result = (await dumpConfiguration(backend)) as ADCSDK.Configuration;
expect(result.consumers).toHaveLength(2);
expect(result.consumers[0]).toMatchObject(consumer2);
expect(result.consumers[1]).toMatchObject(consumer1);
});

it('Update consumer1', async () => {
consumer1.description = 'desc';
await syncEvents(backend, [
updateEvent(ADCSDK.ResourceType.CONSUMER, consumer1Name, consumer1),
]);
});

it('Dump again (consumer1 updated)', async () => {
const result = (await dumpConfiguration(backend)) as ADCSDK.Configuration;
expect(result.consumers[0]).toMatchObject(consumer1);
});

it('Delete consumer1', async () =>
syncEvents(backend, [
deleteEvent(ADCSDK.ResourceType.CONSUMER, consumer1Name),
]));

it('Dump again (consumer1 should not exist)', async () => {
const result = (await dumpConfiguration(backend)) as ADCSDK.Configuration;
expect(result.consumers).toHaveLength(1);
expect(result.consumers[0]).toMatchObject(consumer2);
});

it('Delete consumer2', async () =>
syncEvents(backend, [
deleteEvent(ADCSDK.ResourceType.CONSUMER, consumer2Name),
]));

it('Dump again (consumer2 should not exist)', async () => {
const result = (await dumpConfiguration(backend)) as ADCSDK.Configuration;
expect(result.consumers).toHaveLength(0);
});
});

describe('Sync and dump ssls', () => {
const certificates = [
{
Expand Down Expand Up @@ -514,82 +447,4 @@ describe('Sync and Dump - 1', () => {
expect(Object.keys(result.plugin_metadata)).toHaveLength(0);
});
});

describe('Sync and dump consumer credentials', () => {
const consumer1Name = 'consumer1';
const consumer1Key = 'consumer1-key';
const consumer1Cred = {
name: consumer1Key,
type: 'key-auth',
config: { key: consumer1Key },
};
const consumer1 = {
username: consumer1Name,
credentials: [consumer1Cred],
} as ADCSDK.Consumer;

it('Create consumers', async () =>
syncEvents(backend, [
createEvent(ADCSDK.ResourceType.CONSUMER, consumer1Name, consumer1),
createEvent(
ADCSDK.ResourceType.CONSUMER_CREDENTIAL,
consumer1Key,
consumer1Cred,
consumer1Name,
),
]));

it('Dump', async () => {
const result = (await dumpConfiguration(backend)) as ADCSDK.Configuration;
expect(result.consumers).toHaveLength(1);
expect(result.consumers[0]).toMatchObject(consumer1);
expect(result.consumers[0].credentials).toMatchObject(
consumer1.credentials,
);
});

it('Update consumer credential', async () => {
consumer1.credentials[0].config.key = 'new-key';
await syncEvents(backend, [
updateEvent(
ADCSDK.ResourceType.CONSUMER_CREDENTIAL,
consumer1Key,
consumer1,
consumer1Name,
),
]);
});

it('Dump again (consumer credential updated)', async () => {
const result = (await dumpConfiguration(backend)) as ADCSDK.Configuration;
expect(result.consumers[0]).toMatchObject(consumer1);
expect(result.consumers[0].credentials[0].config.key).toEqual('new-key');
});

it('Delete consumer credential', async () =>
syncEvents(backend, [
deleteEvent(
ADCSDK.ResourceType.CONSUMER_CREDENTIAL,
consumer1Key,
consumer1Name,
),
]));

it('Dump again (consumer credential should not exist)', async () => {
const result = (await dumpConfiguration(backend)) as ADCSDK.Configuration;
expect(result.consumers).toHaveLength(1);
expect(result.consumers[0]).toMatchObject(consumer1);
expect(result.consumers[0].credentials).toHaveLength(0);
});

it('Delete consumer', async () =>
syncEvents(backend, [
deleteEvent(ADCSDK.ResourceType.CONSUMER, consumer1Name),
]));

it('Dump again (consumer should not exist)', async () => {
const result = (await dumpConfiguration(backend)) as ADCSDK.Configuration;
expect(result.consumers).toHaveLength(0);
});
});
});

0 comments on commit 1300521

Please sign in to comment.