|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const chai = require('chai'); |
| 4 | +const expect = chai.expect; |
| 5 | +const kongContext = require('../lib/kong/context'); |
| 6 | +const kongApi = require('../lib/kong/index'); |
| 7 | +const localKongHost = 'http://127.0.0.1:8001'; |
| 8 | + |
| 9 | +describe('Kong API Object Specification', () => { |
| 10 | + it('must be able to create a plugin', async () => { |
| 11 | + const context = kongContext('', '', localKongHost); |
| 12 | + const kong = kongApi(context); |
| 13 | + await kong.plugins.createOrUpdatePlugin({ |
| 14 | + name: 'correlation-id', |
| 15 | + enabled: true, |
| 16 | + config: { |
| 17 | + header_name: 'X-Request-ID', |
| 18 | + echo_downstream: true, |
| 19 | + generator: 'uuid' |
| 20 | + } |
| 21 | + }); |
| 22 | + |
| 23 | + const plugins = await kong.plugins.allPlugins(); |
| 24 | + expect(plugins).to.be.lengthOf(1); |
| 25 | + const firstPlugin = plugins[0]; |
| 26 | + expect(firstPlugin.name).to.be.equal('correlation-id'); |
| 27 | + expect(firstPlugin.config.header_name).to.be.equal('X-Request-ID'); |
| 28 | + }); |
| 29 | + |
| 30 | + it('must be able to remove a plugin', async () => { |
| 31 | + const context = kongContext('', '', localKongHost); |
| 32 | + const kong = kongApi(context); |
| 33 | + await kong.plugins.createOrUpdatePlugin({ |
| 34 | + name: "ip-restriction", |
| 35 | + enabled: true, |
| 36 | + config: { |
| 37 | + whitelist: ["127.0.0.1"] |
| 38 | + } |
| 39 | + }); |
| 40 | + |
| 41 | + const plugins = await kong.plugins.allPlugins(); |
| 42 | + expect(plugins).to.be.lengthOf(1); |
| 43 | + const pluginId = plugins[0].id; |
| 44 | + await kong.plugins.removePlugin(pluginId); |
| 45 | + const pluginsAgain = await kong.plugins.allPlugins(); |
| 46 | + expect(pluginsAgain).to.be.lengthOf(0); |
| 47 | + }); |
| 48 | + |
| 49 | + afterEach(async () => { |
| 50 | + // clean up all entities |
| 51 | + const context = kongContext('', '', localKongHost); |
| 52 | + const kong = kongApi(context); |
| 53 | + const plugins = await kong.plugins.allPlugins(); |
| 54 | + const pluginIds = plugins.map(eachPlugin => eachPlugin.id); |
| 55 | + pluginIds.map(async id => await kong.plugins.removePlugin(id).catch(err => console.log(err.message))); |
| 56 | + }); |
| 57 | +}); |
| 58 | + |
0 commit comments