|
| 1 | +import * as ADCSDK from '@api7/adc-sdk'; |
| 2 | +import { gte, lt } from 'semver'; |
| 3 | + |
| 4 | +import { BackendAPISIX } from '../../src'; |
| 5 | +import { server, token } from '../support/constants'; |
| 6 | +import { conditionalDescribe, semverCondition } from '../support/utils'; |
| 7 | +import { |
| 8 | + createEvent, |
| 9 | + deleteEvent, |
| 10 | + dumpConfiguration, |
| 11 | + syncEvents, |
| 12 | + updateEvent, |
| 13 | +} from '../support/utils'; |
| 14 | + |
| 15 | +describe('Consumer E2E', () => { |
| 16 | + let backend: BackendAPISIX; |
| 17 | + |
| 18 | + beforeAll(() => { |
| 19 | + backend = new BackendAPISIX({ |
| 20 | + server, |
| 21 | + token, |
| 22 | + tlsSkipVerify: true, |
| 23 | + }); |
| 24 | + }); |
| 25 | + |
| 26 | + conditionalDescribe(semverCondition(gte, '3.11.0'))( |
| 27 | + 'Sync and dump consumers (with credential support)', |
| 28 | + () => { |
| 29 | + const consumer1Name = 'consumer1'; |
| 30 | + const consumer1Key = 'consumer1-key'; |
| 31 | + const consumer1Cred = { |
| 32 | + name: consumer1Key, |
| 33 | + type: 'key-auth', |
| 34 | + config: { key: consumer1Key }, |
| 35 | + }; |
| 36 | + const consumer1 = { |
| 37 | + username: consumer1Name, |
| 38 | + credentials: [consumer1Cred], |
| 39 | + } as ADCSDK.Consumer; |
| 40 | + |
| 41 | + it('Create consumers', async () => |
| 42 | + syncEvents(backend, [ |
| 43 | + createEvent(ADCSDK.ResourceType.CONSUMER, consumer1Name, consumer1), |
| 44 | + createEvent( |
| 45 | + ADCSDK.ResourceType.CONSUMER_CREDENTIAL, |
| 46 | + consumer1Key, |
| 47 | + consumer1Cred, |
| 48 | + consumer1Name, |
| 49 | + ), |
| 50 | + ])); |
| 51 | + |
| 52 | + it('Dump', async () => { |
| 53 | + const result = (await dumpConfiguration( |
| 54 | + backend, |
| 55 | + )) as ADCSDK.Configuration; |
| 56 | + expect(result.consumers).toHaveLength(1); |
| 57 | + expect(result.consumers[0]).toMatchObject(consumer1); |
| 58 | + expect(result.consumers[0].credentials).toMatchObject( |
| 59 | + consumer1.credentials, |
| 60 | + ); |
| 61 | + }); |
| 62 | + |
| 63 | + it('Update consumer credential', async () => { |
| 64 | + consumer1.credentials[0].config.key = 'new-key'; |
| 65 | + await syncEvents(backend, [ |
| 66 | + updateEvent( |
| 67 | + ADCSDK.ResourceType.CONSUMER_CREDENTIAL, |
| 68 | + consumer1Key, |
| 69 | + consumer1Cred, |
| 70 | + consumer1Name, |
| 71 | + ), |
| 72 | + ]); |
| 73 | + }); |
| 74 | + |
| 75 | + it('Dump again (consumer credential updated)', async () => { |
| 76 | + const result = (await dumpConfiguration( |
| 77 | + backend, |
| 78 | + )) as ADCSDK.Configuration; |
| 79 | + expect(result.consumers[0]).toMatchObject(consumer1); |
| 80 | + expect(result.consumers[0].credentials[0].config.key).toEqual( |
| 81 | + 'new-key', |
| 82 | + ); |
| 83 | + }); |
| 84 | + |
| 85 | + it('Delete consumer credential', async () => |
| 86 | + syncEvents(backend, [ |
| 87 | + deleteEvent( |
| 88 | + ADCSDK.ResourceType.CONSUMER_CREDENTIAL, |
| 89 | + consumer1Key, |
| 90 | + consumer1Name, |
| 91 | + ), |
| 92 | + ])); |
| 93 | + |
| 94 | + it('Dump again (consumer credential should not exist)', async () => { |
| 95 | + const result = (await dumpConfiguration( |
| 96 | + backend, |
| 97 | + )) as ADCSDK.Configuration; |
| 98 | + expect(result.consumers).toHaveLength(1); |
| 99 | + console.log(result.consumers[0]); |
| 100 | + expect(result.consumers[0].credentials).toBeUndefined(); |
| 101 | + }); |
| 102 | + |
| 103 | + it('Delete consumer', async () => |
| 104 | + syncEvents(backend, [ |
| 105 | + deleteEvent(ADCSDK.ResourceType.CONSUMER, consumer1Name), |
| 106 | + ])); |
| 107 | + |
| 108 | + it('Dump again (consumer should not exist)', async () => { |
| 109 | + const result = (await dumpConfiguration( |
| 110 | + backend, |
| 111 | + )) as ADCSDK.Configuration; |
| 112 | + expect(result.consumers).toHaveLength(0); |
| 113 | + }); |
| 114 | + }, |
| 115 | + ); |
| 116 | +}); |
0 commit comments