|
| 1 | +const { expect } = require('chai'); |
| 2 | +const { startApiBuilder, stopApiBuilder, requestAsync, sendToElasticsearch, getRandomInt } = require('../../../_base'); |
| 3 | +const path = require('path'); |
| 4 | +const fs = require('fs'); |
| 5 | +const nock = require('nock'); |
| 6 | +const envLoader = require('dotenv'); |
| 7 | + |
| 8 | +describe('Endpoints', function () { |
| 9 | + this.timeout(30000); |
| 10 | + let server; |
| 11 | + let auth; |
| 12 | + const indexName = `apigw-traffic-summary-search_custom_restricted_perm_test_${getRandomInt(9999)}`; |
| 13 | + |
| 14 | + /** |
| 15 | + * Start API Builder. |
| 16 | + */ |
| 17 | + before(() => { |
| 18 | + return new Promise(function(resolve, reject){ |
| 19 | + const envFilePath = path.join(__dirname, '../../../.env'); |
| 20 | + if (fs.existsSync(envFilePath)) { |
| 21 | + envLoader.config({ path: envFilePath }); |
| 22 | + } |
| 23 | + // Special permissions configured required to get unrestricted traffic access |
| 24 | + process.env.UNRESTRICTED_PERMISSIONS = "logs,mgmt"; |
| 25 | + server = startApiBuilder(); |
| 26 | + server.apibuilder.config.testElasticIndex = indexName; |
| 27 | + elasticConfig = server.apibuilder.config.pluginConfig['@axway-api-builder-ext/api-builder-plugin-fn-elasticsearch'].elastic; |
| 28 | + server.started |
| 29 | + .then(() => { |
| 30 | + const entryset = require('../../../documents/http/search_count_documents'); |
| 31 | + sendToElasticsearch(elasticConfig, indexName, 'traffic-summary/index_template.json', entryset) |
| 32 | + .then(() => { |
| 33 | + resolve(); |
| 34 | + }) |
| 35 | + .catch(err => reject(err)); |
| 36 | + }); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + /** |
| 41 | + * Stop API Builder after the tests. |
| 42 | + */ |
| 43 | + after(() => { |
| 44 | + stopApiBuilder(server); |
| 45 | + delete process.env.UNRESTRICTED_PERMISSIONS; |
| 46 | + }); |
| 47 | + |
| 48 | + afterEach(async () => { |
| 49 | + nock.cleanAll(); |
| 50 | + }); |
| 51 | + |
| 52 | + describe('Search', async () => { |
| 53 | + it('[RestrictedPermissions-0002] Custom restriction permission configuration - User has with monitoring permission a restricted access', async () => { |
| 54 | + // Configured permissions: mgmt,logs - Not enough for user rene only having permission monitoring |
| 55 | + nock('https://mocked-api-gateway:8090').get('/api/rbac/currentuser').reply(200, { "result": "rene" }); |
| 56 | + nock('https://mocked-api-gateway:8090').get('/api/topology').reply(200, { result: {} }); |
| 57 | + nock('https://mocked-api-gateway:8090').get('/api/rbac/permissions/currentuser').replyWithFile(200, './test/mockedReplies/apigateway/permissionMonitoring.json'); |
| 58 | + nock('https://mocked-api-gateway:8075').get(`/api/portal/v1.3/users?field=loginName&op=eq&value=rene&field=enabled&op=eq&value=enabled`).replyWithFile(200, './test/mockedReplies/apimanager/apiManagerUserRene.json'); |
| 59 | + nock('https://mocked-api-gateway:8075').get(`/api/portal/v1.3/organizations/2bfaa1c2-49ab-4059-832d-MAX`).replyWithFile(200, './test/mockedReplies/apimanager/organizationMax.json'); |
| 60 | + return await requestAsync({ |
| 61 | + method: 'GET', |
| 62 | + uri: `http://localhost:${server.apibuilder.port}/api/elk/v1/api/router/service/instance-1/ops/search?count=20`, |
| 63 | + headers: { |
| 64 | + 'cookie': 'VIDUSR=Search-Count-0001-DAVID-1597468226-Z+qdRW4rGZnwzQ==', |
| 65 | + 'csrf-token': '04F9F07E59F588CDE469FC367A12ED3A4B845FDA9A9AE2D9A77686823067CDDC' |
| 66 | + }, |
| 67 | + json: true |
| 68 | + }).then(({ response, body }) => { |
| 69 | + expect(response.statusCode).to.equal(200); |
| 70 | + expect(body).to.be.an('Object'); |
| 71 | + expect(body).to.have.property('data'); |
| 72 | + expect(body.data).to.have.lengthOf(2); // Expect only two results as the user is restricted |
| 73 | + }); |
| 74 | + }); |
| 75 | + }); |
| 76 | +}); |
0 commit comments