diff --git a/libs/backend-api7/e2e/resources/consumer.e2e-spec.ts b/libs/backend-api7/e2e/resources/consumer.e2e-spec.ts new file mode 100644 index 0000000..28b9a38 --- /dev/null +++ b/libs/backend-api7/e2e/resources/consumer.e2e-spec.ts @@ -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); + }); + }, + ); +}); diff --git a/libs/backend-api7/e2e/support/utils.ts b/libs/backend-api7/e2e/support/utils.ts index 33f66da..50e0d80 100644 --- a/libs/backend-api7/e2e/support/utils.ts +++ b/libs/backend-api7/e2e/support/utils.ts @@ -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); diff --git a/libs/backend-api7/e2e/sync-and-dump-1.e2e-spec.ts b/libs/backend-api7/e2e/sync-and-dump-1.e2e-spec.ts index b8991b7..dfe8009 100644 --- a/libs/backend-api7/e2e/sync-and-dump-1.e2e-spec.ts +++ b/libs/backend-api7/e2e/sync-and-dump-1.e2e-spec.ts @@ -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 = [ { @@ -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); - }); - }); });