UNPKG

@unito/integration-cli

Version:

Integration CLI

256 lines (255 loc) 14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const test_1 = require("@oclif/test"); const sinon_1 = tslib_1.__importDefault(require("sinon")); const child_process_1 = tslib_1.__importDefault(require("child_process")); const IntegrationsPlatform = tslib_1.__importStar(require("../../src/services/integrationsPlatform")); const IntegrationsPlatformResource = tslib_1.__importStar(require("../../src/resources/integrationsPlatform")); const ConfigurationResource = tslib_1.__importStar(require("../../src/resources/configuration")); const IntegrationResource = tslib_1.__importStar(require("../../src/resources/integrations")); const CredentialResource = tslib_1.__importStar(require("../../src/resources/credentials")); const DecryptionResource = tslib_1.__importStar(require("../../src/resources/decryption")); const errors_1 = require("../../src/errors"); const styles_1 = require("../helpers/styles"); describe('Test', () => { const cliConfiguration = { name: 'a', baseUrl: 'b', secrets: { secret: 'encryptedSecret', }, environmentVariables: { success: '', failure: '', }, testAccounts: { compliance: { accessToken: 'token', encryptedToken: `${ConfigurationResource.ENCRYPTION_PREFIX}decrypt-me`, }, development: { accessToken: 'developmentToken', }, }, }; let execSyncStub; let spawnStub; beforeEach(() => { process.env.NODE_MODULES_FOLDER = '/path/to/node_modules'; sinon_1.default.stub(IntegrationsPlatform, 'decryptData').resolves({ decryptedData: 'decrypted-me' }); sinon_1.default.stub(IntegrationsPlatformResource, 'validateAuthenticated'); execSyncStub = sinon_1.default.stub(child_process_1.default, 'execSync'); spawnStub = sinon_1.default.stub(child_process_1.default, 'spawn').returns({ on: (..._args) => { } }); }); afterEach(() => { process.env.NODE_MODULES_FOLDER = undefined; sinon_1.default.restore(); }); test_1.test .stdout() .stub(ConfigurationResource, 'getConfiguration', stub => stub.returns(cliConfiguration)) .stub(IntegrationResource, 'validateIsIntegrationDirectory', stub => stub.returns(true)) .command(['test']) .it('launches the debugger', () => { (0, test_1.expect)(execSyncStub.getCall(0).args.at(0).split(' ').slice(0, 2)).to.deep.equal(['npm', 'install']); (0, test_1.expect)(spawnStub.getCall(0).args.at(0).split(' ').slice(0, 2)).to.deep.equal(['node']); }); test_1.test .stdout() .stderr() .stub(ConfigurationResource, 'getConfiguration', stub => stub.resolves(cliConfiguration)) .stub(IntegrationResource, 'validateIsIntegrationDirectory', stub => stub.throws(new Error('boom!'))) .command(['test']) .catch(ctx => { (0, test_1.expect)(ctx.message).to.equal('boom!'); }) .it('unhandled exception'); test_1.test .stdout() .stub(ConfigurationResource, 'getConfiguration', stub => stub.resolves(cliConfiguration)) .stub(IntegrationResource, 'validateIsIntegrationDirectory', stub => stub.throws(new errors_1.NoIntegrationFoundError())) .command(['test']) .exit(-1) .it('handled exception'); test_1.test .stdout() .stub(ConfigurationResource, 'getConfiguration', stub => stub.resolves(cliConfiguration)) .stub(IntegrationResource, 'validateIsIntegrationDirectory', stub => stub.returns(true)) .command(['test', '--verbose']) .it('verbose mode', () => { (0, test_1.expect)(spawnStub.getCall(0).args.at(1)).to.include('--verbose'); }); test_1.test .stdout() .stub(ConfigurationResource, 'getConfiguration', stub => stub.resolves(cliConfiguration)) .stub(IntegrationResource, 'validateIsIntegrationDirectory', stub => stub.returns(true)) .command(['test', '--credential-payload', '{"foo": "bar"}']) .it('uses provided credentials', () => { (0, test_1.expect)(spawnStub.getCall(0).args.at(1)).to.include('--credential-payload={"foo": "bar"}'); }); test_1.test .stdout() .stub(ConfigurationResource, 'getConfiguration', stub => stub.resolves(cliConfiguration)) .stub(IntegrationResource, 'validateIsIntegrationDirectory', stub => stub.returns(true)) .command(['test']) .it('defaults to test account when no flag is provided', () => { (0, test_1.expect)(spawnStub.getCall(0).args.at(1)).to.include('--credential-payload={"accessToken":"developmentToken","unitoCredentialId":"development","unitoUserId":"development"}'); }); test_1.test .stdout() .stub(ConfigurationResource, 'getConfiguration', stub => stub.resolves(cliConfiguration)) .stub(IntegrationResource, 'validateIsIntegrationDirectory', stub => stub.returns(true)) .command(['test', '--test-account', 'compliance']) .it('uses compliance credentials when a flag is provided', () => { (0, test_1.expect)(spawnStub.getCall(0).args.at(1)).to.include('--credential-payload={"accessToken":"token","encryptedToken":"decrypted-me","unitoCredentialId":"compliance","unitoUserId":"compliance"}'); }); test_1.test .stdout() .stub(ConfigurationResource, 'getConfiguration', stub => stub.resolves(cliConfiguration)) .stub(IntegrationResource, 'validateIsIntegrationDirectory', stub => stub.returns(true)) .command(['test']) .it('decrypt secrets correctly', () => { (0, test_1.expect)(spawnStub.getCall(0).args.at(1)).to.include('--secrets-payload={"secret":"encryptedSecret"}'); }); test_1.test .stdout() .stub(ConfigurationResource, 'getConfiguration', stub => stub.resolves(cliConfiguration)) .stub(IntegrationResource, 'validateIsIntegrationDirectory', stub => stub.returns(true)) .command(['test', '--output-path', 'foo.json']) .it('output path', () => { (0, test_1.expect)(spawnStub.getCall(0).args.at(1)).to.include('--output-path=foo.json'); }); test_1.test .stdout() .stub(ConfigurationResource, 'getConfiguration', stub => stub.resolves(cliConfiguration)) .stub(IntegrationResource, 'validateIsIntegrationDirectory', stub => stub.returns(true)) .command(['test', '--crawlMode=sample']) .it('sampling crawlMode', () => { (0, test_1.expect)(spawnStub.getCall(0).args.at(1)).to.include('--operation-collection-items-per-page=5'); (0, test_1.expect)(spawnStub.getCall(0).args.at(1)).to.include('--operation-collection-follow-next-pages=false'); }); test_1.test .stdout() .stub(ConfigurationResource, 'getConfiguration', stub => stub.resolves(cliConfiguration)) .stub(IntegrationResource, 'validateIsIntegrationDirectory', stub => stub.returns(true)) .command(['test', '--crawlMode=single']) .it('single crawlMode', () => { (0, test_1.expect)(spawnStub.getCall(0).args.at(1)).to.include('--operation-collection-items-per-page=1'); (0, test_1.expect)(spawnStub.getCall(0).args.at(1)).to.include('--operation-collection-follow-next-pages=false'); }); test_1.test .stdout() .stub(ConfigurationResource, 'getConfiguration', stub => stub.resolves(cliConfiguration)) .stub(IntegrationResource, 'validateIsIntegrationDirectory', stub => stub.returns(true)) .command(['test', '--checks', 'foo,bar']) .it('checks', () => { (0, test_1.expect)(spawnStub.getCall(0).args.at(1)).to.include('--checks=foo,bar'); }); test_1.test .stdout() .stub(ConfigurationResource, 'getConfiguration', stub => stub.returns({ ...cliConfiguration, graphRelativeUrl: 'foo' })) .stub(IntegrationResource, 'validateIsIntegrationDirectory', stub => stub.returns(true)) .command(['test']) .it('overrides --graph-relative-url', () => { (0, test_1.expect)(spawnStub.getCall(0).args.at(1)).to.include('--graph-relative-url=foo'); }); test_1.test .stdout() .stub(ConfigurationResource, 'getConfiguration', stub => stub.returns({ ...cliConfiguration, credentialAccountRelativeUrl: 'foo', })) .stub(IntegrationResource, 'validateIsIntegrationDirectory', stub => stub.returns(true)) .command(['test']) .it('overrides --credential-account-relative-url', () => { (0, test_1.expect)(spawnStub.getCall(0).args.at(1)).to.include('--credential-account-relative-url=foo'); }); test_1.test .stdout() .stub(ConfigurationResource, 'getConfiguration', stub => stub.returns({ ...cliConfiguration, webhookParsingRelativeUrl: 'foo' })) .stub(IntegrationResource, 'validateIsIntegrationDirectory', stub => stub.returns(true)) .command(['test']) .it('overrides --webhook-parsing-relative-url', () => { (0, test_1.expect)(spawnStub.getCall(0).args.at(1)).to.include('--webhook-parsing-relative-url=foo'); }); test_1.test .stdout() .stub(ConfigurationResource, 'getConfiguration', stub => stub.returns({ ...cliConfiguration, webhookSubscriptionsRelativeUrl: 'foo', })) .stub(IntegrationResource, 'validateIsIntegrationDirectory', stub => stub.returns(true)) .command(['test']) .it('overrides --webhook-subscriptions-relative-url', () => { (0, test_1.expect)(spawnStub.getCall(0).args.at(1)).to.include('--webhook-subscriptions-relative-url=foo'); }); test_1.test .stdout() .stub(ConfigurationResource, 'getConfiguration', stub => stub.returns({ ...cliConfiguration, webhookAcknowledgeRelativeUrl: 'bar', })) .stub(IntegrationResource, 'validateIsIntegrationDirectory', stub => stub.returns(true)) .command(['test']) .it('overrides --webhook-acknowledge-relative-url', () => { (0, test_1.expect)(spawnStub.getCall(0).args.at(1)).to.include('--webhook-acknowledge-relative-url=bar'); }); test_1.test .stdout() .stub(ConfigurationResource, 'getConfiguration', stub => stub.resolves(cliConfiguration)) .stub(IntegrationResource, 'validateIsIntegrationDirectory', stub => stub.returns(true)) .stub(CredentialResource, 'fetchCredential', stub => stub.resolves({ payload: { from: 'credential' }, id: '123', unitoUserId: '456' })) .command(['test', '--credential-id=123']) .it('credential-id', () => { (0, test_1.expect)(spawnStub.getCall(0).args.at(1)).to.include('--credential-payload={"from":"credential","unitoCredentialId":"123","unitoUserId":"456"}'); (0, test_1.expect)(spawnStub.getCall(0).args.at(1)).to.include('--read-only'); }); test_1.test .stdout() .stub(ConfigurationResource, 'getConfiguration', stub => stub.resolves(cliConfiguration)) .stub(IntegrationResource, 'validateIsIntegrationDirectory', stub => stub.returns(true)) .stub(CredentialResource, 'fetchCredential', stub => stub.resolves({ payload: { from: 'credential' }, id: '123', unitoUserId: '456' })) .command(['test', '--credential-id=123', '--no-read-only']) .it('credential-id && read-only', () => { (0, test_1.expect)(spawnStub.getCall(0).args.at(1)).to.include('--credential-payload={"from":"credential","unitoCredentialId":"123","unitoUserId":"456"}'); (0, test_1.expect)(spawnStub.getCall(0).args.at(1)).to.not.include('--read-only'); }); test_1.test .stdout() .stub(ConfigurationResource, 'getConfiguration', stub => stub.resolves(cliConfiguration)) .stub(IntegrationResource, 'validateIsIntegrationDirectory', stub => stub.returns(true)) .stub(CredentialResource, 'fetchCredential', stub => stub.resolves({ payload: { from: 'credential' }, id: '123', unitoUserId: '456' })) .stub(DecryptionResource, 'decryptEntries', stub => stub .onFirstCall() .resolves({ successful: {}, failed: [] }) .onSecondCall() .resolves({ successful: {}, failed: [] }) .onThirdCall() .resolves({ successful: { success: 'a' }, failed: ['failure'] })) .command(['test']) .it('environment variables', ctx => { (0, test_1.expect)(ctx.stdout).to.contain((0, styles_1.uncolorize)('Could not decrypt the environment variable failure.')); (0, test_1.expect)(spawnStub.getCall(0).args.at(2).env.success).to.equal('a'); (0, test_1.expect)(spawnStub.getCall(0).args.at(2).env.failure).to.be.undefined; }); test_1.test .stdout() .stub(ConfigurationResource, 'getConfiguration', stub => stub.resolves(cliConfiguration)) .stub(IntegrationResource, 'validateIsIntegrationDirectory', stub => stub.returns(true)) .stub(CredentialResource, 'fetchCredential', stub => stub.resolves({ payload: { from: 'credential' }, id: '123', unitoUserId: '456' })) .stub(DecryptionResource, 'decryptEntries', stub => stub .onFirstCall() .resolves({ successful: {}, failed: [] }) .onSecondCall() .resolves({ successful: {}, failed: [] }) .onThirdCall() .resolves({ successful: { success: 'a' }, failed: ['failure'] })) .env({ failure: 'b' }) .command(['test']) .it('override environment variables', ctx => { (0, test_1.expect)(ctx.stdout).to.not.contain((0, styles_1.uncolorize)('Could not decrypt the environment variable failure.')); (0, test_1.expect)(spawnStub.getCall(0).args.at(2).env.success).to.equal('a'); (0, test_1.expect)(spawnStub.getCall(0).args.at(2).env.failure).to.equal('b'); }); });