UNPKG

@unito/integration-cli

Version:

Integration CLI

373 lines (372 loc) 16.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const test_1 = require("@oclif/test"); const core_1 = require("@oclif/core"); const sinon = tslib_1.__importStar(require("sinon")); const inquirer_1 = tslib_1.__importDefault(require("inquirer")); const Configuration = tslib_1.__importStar(require("../../src/resources/configuration")); const Oauth2Resource = tslib_1.__importStar(require("../../src/resources/oauth2")); const IntegrationsPlatform = tslib_1.__importStar(require("../../src/services/integrationsPlatform")); const DecryptionResource = tslib_1.__importStar(require("../../src/resources/decryption")); const configurationTypes_1 = require("../../src/configurationTypes"); const styles_1 = require("../helpers/styles"); describe('oauth2', () => { let getConfigurationsStub; let writeTestAccountStub; let performOAuth2FlowStub; let updateTokenStub; const oauth2Information = { clientId: 'myClientId', clientSecret: 'myClientId', tokenUrl: 'myTokenurl.com', authorizationUrl: 'myAuthUrl.com', scopes: [{ name: '', description: '' }], requestContentType: configurationTypes_1.RequestContentType.URL_ENCODED, responseContentType: configurationTypes_1.RequestContentType.JSON, grantType: configurationTypes_1.GrantType.AUTHORIZATION_CODE, }; const baseConfiguration = { name: 'myintegration', authorizations: [ { name: 'MyAuthorization', method: configurationTypes_1.Method.OAUTH2, oauth2: oauth2Information, }, ], testAccounts: {}, }; const credentials = { accessToken: 'accessToken', refreshToken: 'refreshToken', }; beforeEach(() => { getConfigurationsStub = sinon.stub(Configuration, 'getConfiguration').resolves(baseConfiguration); sinon.spy(core_1.ux.action, 'start'); sinon.spy(core_1.ux.action, 'stop'); sinon.stub(inquirer_1.default, 'prompt').resolves({ oauth2Information: JSON.stringify(oauth2Information), }); writeTestAccountStub = sinon.stub(Configuration, 'writeTestAccount'); performOAuth2FlowStub = sinon.stub(Oauth2Resource, 'performOAuth2Flow').resolves(credentials); updateTokenStub = sinon.stub(Oauth2Resource, 'updateToken').resolves(credentials); }); afterEach(() => { sinon.restore(); }); test_1.test .stdout() .stderr() .do(() => getConfigurationsStub.returns({ ...baseConfiguration, authorizations: [], })) .command(['oauth2', '--test-account', 'development']) .exit(-1) .it("Errors out if no oauth2 authorization scheme is found in the integration's configuration"); test_1.test .stdout() .do(() => { getConfigurationsStub.returns({ ...baseConfiguration, authorizations: [ ...(baseConfiguration.authorizations ?? []), { name: 'Development Authorization', method: configurationTypes_1.Method.OAUTH2, oauth2: { ...oauth2Information, clientId: 'devClientID' }, development: true, }, ], }); sinon .stub(IntegrationsPlatform, 'encryptData') .onFirstCall() .resolves({ encryptedData: `${Configuration.ENCRYPTION_PREFIX}encryptedAccessToken` }) .onSecondCall() .resolves({ encryptedData: `${Configuration.ENCRYPTION_PREFIX}encryptedRefreshToken` }); }) .stub(DecryptionResource, 'decryptEntries', stub => stub.resolves({ successful: { ...oauth2Information, clientId: 'devClientID' }, failed: [] })) .command(['oauth2', '--test-account', 'development']) .it('prioritize development authorization', () => { (0, test_1.expect)(performOAuth2FlowStub.getCalls().length).to.equal(1); (0, test_1.expect)(performOAuth2FlowStub.getCall(0).args).to.deep.equal([ { ...oauth2Information, clientId: 'devClientID' }, 'production', undefined, ]); (0, test_1.expect)(updateTokenStub.getCalls().length).to.equal(0); (0, test_1.expect)(writeTestAccountStub.getCall(0).args).to.deep.equal([ { ...baseConfiguration, authorizations: [ ...(baseConfiguration.authorizations ?? []), { name: 'Development Authorization', method: configurationTypes_1.Method.OAUTH2, oauth2: { ...oauth2Information, clientId: 'devClientID' }, development: true, }, ], }, 'production', undefined, credentials, 'development', ]); }); test_1.test .stdout() .do(() => { getConfigurationsStub.returns({ ...baseConfiguration, testAccounts: { development: credentials, }, }); sinon .stub(IntegrationsPlatform, 'encryptData') .onFirstCall() .resolves({ encryptedData: `${Configuration.ENCRYPTION_PREFIX}encryptedAccessToken` }) .onSecondCall() .resolves({ encryptedData: `${Configuration.ENCRYPTION_PREFIX}encryptedRefreshToken` }); }) .stub(DecryptionResource, 'decryptEntries', stub => stub.resolves({ successful: { ...oauth2Information }, failed: [] })) .command(['oauth2', '--test-account', 'development', '--reauth']) .it('performs the oauth flow when there is a test accounts, the auth information are setup and --reauth flag is present', () => { (0, test_1.expect)(performOAuth2FlowStub.getCalls().length).to.equal(1); (0, test_1.expect)(updateTokenStub.getCalls().length).to.equal(0); (0, test_1.expect)(writeTestAccountStub.getCall(0).args).to.deep.equal([ { ...baseConfiguration, testAccounts: { development: credentials, }, }, 'production', undefined, credentials, 'development', ]); }); test_1.test .stdout() .do(() => { getConfigurationsStub.returns({ ...baseConfiguration, testAccounts: { development: { something: 'something' }, }, }); sinon .stub(IntegrationsPlatform, 'encryptData') .onFirstCall() .resolves({ encryptedData: `${Configuration.ENCRYPTION_PREFIX}encryptedAccessToken` }) .onSecondCall() .resolves({ encryptedData: `${Configuration.ENCRYPTION_PREFIX}encryptedRefreshToken` }); }) .stub(DecryptionResource, 'decryptEntries', stub => stub.resolves({ successful: { ...oauth2Information }, failed: [] })) .command(['oauth2', '--test-account', 'compliance']) .it('performs the oauth flow when the requested test account is not setup', () => { (0, test_1.expect)(performOAuth2FlowStub.getCalls().length).to.equal(1); (0, test_1.expect)(updateTokenStub.getCalls().length).to.equal(0); (0, test_1.expect)(writeTestAccountStub.getCall(0).args).to.deep.equal([ { ...baseConfiguration, testAccounts: { development: { something: 'something' }, }, }, 'production', undefined, credentials, 'compliance', ]); }); test_1.test .stdout() .do(() => { getConfigurationsStub.returns({ ...baseConfiguration, testAccounts: { development: credentials, }, }); sinon .stub(IntegrationsPlatform, 'encryptData') .onFirstCall() .resolves({ encryptedData: `${Configuration.ENCRYPTION_PREFIX}encryptedAccessToken` }) .onSecondCall() .resolves({ encryptedData: `${Configuration.ENCRYPTION_PREFIX}encryptedRefreshToken` }); }) .stub(DecryptionResource, 'decryptEntries', stub => stub.resolves({ successful: { ...oauth2Information }, failed: [] })) .command(['oauth2', '--test-account', 'development']) .it('refresh the token when there is an existing test accounts and the auth information are setup', () => { (0, test_1.expect)(performOAuth2FlowStub.getCalls().length).to.equal(0); (0, test_1.expect)(updateTokenStub.getCalls().length).to.equal(1); (0, test_1.expect)(writeTestAccountStub.getCall(0).args).to.deep.equal([ { ...baseConfiguration, testAccounts: { development: credentials, }, }, 'production', undefined, credentials, 'development', ]); }); test_1.test .stdout() .do(() => { getConfigurationsStub.returns({ ...baseConfiguration, authorizations: [ { name: 'Authorization', method: configurationTypes_1.Method.OAUTH2, oauth2: { ...oauth2Information, clientSecret: `${Configuration.ENCRYPTION_PREFIX}devClientID` }, }, ], testAccounts: { development: { accessToken: `${Configuration.ENCRYPTION_PREFIX}devAccessToken`, refreshToken: `${Configuration.ENCRYPTION_PREFIX}devRefreshToken`, }, }, }); sinon .stub(DecryptionResource, 'decryptEntries') .onFirstCall() .resolves({ successful: { ...oauth2Information, clientSecret: 'devClientID' }, failed: [] }) .onSecondCall() .resolves({ failed: [], successful: { accessToken: 'devAccessToken', refreshToken: 'devRefreshToken', }, }); sinon .stub(IntegrationsPlatform, 'encryptData') .onFirstCall() .resolves({ encryptedData: `${Configuration.ENCRYPTION_PREFIX}encryptedAccessToken` }) .onSecondCall() .resolves({ encryptedData: `${Configuration.ENCRYPTION_PREFIX}encryptedRefreshToken` }); }) .command(['oauth2', '--test-account', 'development']) .it('decrypt oauth2 authorization entries and test-account', () => { (0, test_1.expect)(performOAuth2FlowStub.getCalls().length).to.equal(0); (0, test_1.expect)(updateTokenStub.getCalls().length).to.equal(1); (0, test_1.expect)(writeTestAccountStub.getCall(0).args).to.deep.equal([ { ...baseConfiguration, authorizations: [ { name: 'Authorization', method: configurationTypes_1.Method.OAUTH2, oauth2: { ...oauth2Information, clientSecret: `${Configuration.ENCRYPTION_PREFIX}devClientID` }, }, ], testAccounts: { development: { accessToken: `${Configuration.ENCRYPTION_PREFIX}devAccessToken`, refreshToken: `${Configuration.ENCRYPTION_PREFIX}devRefreshToken`, }, }, }, 'production', undefined, { accessToken: `${Configuration.ENCRYPTION_PREFIX}encryptedAccessToken`, refreshToken: `${Configuration.ENCRYPTION_PREFIX}encryptedRefreshToken`, }, 'development', ]); }); test_1.test .stdout() .stderr() .do(() => getConfigurationsStub.returns({ ...baseConfiguration, testAccounts: { development: { something: 'something' }, }, })) .stub(DecryptionResource, 'decryptEntries', stub => stub.resolves({ successful: {}, failed: ['boom!'] })) .command(['oauth2']) .catch(ctx => { (0, test_1.expect)(ctx.message).to.equal('EEXIT: -1'); }) .it('must successfully decrypt all secrets', ctx => { (0, test_1.expect)(ctx.stderr).to.contain((0, styles_1.uncolorize)('The secret boom! could not be decrypted.')); }); test_1.test .stdout() .do(() => { getConfigurationsStub.returns({ ...baseConfiguration, testAccounts: { development: credentials, }, }); sinon .stub(IntegrationsPlatform, 'encryptData') .onFirstCall() .resolves({ encryptedData: `${Configuration.ENCRYPTION_PREFIX}encryptedAccessToken` }) .onSecondCall() .resolves({ encryptedData: `${Configuration.ENCRYPTION_PREFIX}encryptedRefreshToken` }); }) .stub(DecryptionResource, 'decryptEntries', stub => stub .onFirstCall() .resolves({ successful: { ...oauth2Information }, failed: [] }) .onSecondCall() .resolves({ successful: { refreshToken: 'test-refresh-token' }, failed: [] })) .command(['oauth2', '--test-account', 'development', '--environment', 'staging']) .it('passes the staging environment to updateToken when specified', () => { (0, test_1.expect)(performOAuth2FlowStub.getCalls().length).to.equal(0); (0, test_1.expect)(updateTokenStub.getCalls().length).to.equal(1); // Verify that updateToken was called with the correct environment parameter const updateTokenCall = updateTokenStub.getCall(0); (0, test_1.expect)(updateTokenCall.args).to.have.lengthOf(4); (0, test_1.expect)(updateTokenCall.args[0]).to.deep.equal(oauth2Information); // oauth2 config (0, test_1.expect)(updateTokenCall.args[1]).to.equal('test-refresh-token'); // refresh token (0, test_1.expect)(updateTokenCall.args[2]).to.deep.equal({ refreshToken: 'test-refresh-token' }); // credential payload (0, test_1.expect)(updateTokenCall.args[3]).to.equal('staging'); // environment }); test_1.test .stdout() .do(() => { getConfigurationsStub.returns({ ...baseConfiguration, testAccounts: { development: credentials, }, }); sinon .stub(IntegrationsPlatform, 'encryptData') .onFirstCall() .resolves({ encryptedData: `${Configuration.ENCRYPTION_PREFIX}encryptedAccessToken` }) .onSecondCall() .resolves({ encryptedData: `${Configuration.ENCRYPTION_PREFIX}encryptedRefreshToken` }); }) .stub(DecryptionResource, 'decryptEntries', stub => stub .onFirstCall() .resolves({ successful: { ...oauth2Information }, failed: [] }) .onSecondCall() .resolves({ successful: { refreshToken: 'test-refresh-token' }, failed: [] })) .command(['oauth2', '--test-account', 'development']) .it('defaults to production environment when no environment flag is specified', () => { (0, test_1.expect)(performOAuth2FlowStub.getCalls().length).to.equal(0); (0, test_1.expect)(updateTokenStub.getCalls().length).to.equal(1); // Verify that updateToken was called with the default production environment const updateTokenCall = updateTokenStub.getCall(0); (0, test_1.expect)(updateTokenCall.args).to.have.lengthOf(4); (0, test_1.expect)(updateTokenCall.args[0]).to.deep.equal(oauth2Information); // oauth2 config (0, test_1.expect)(updateTokenCall.args[1]).to.equal('test-refresh-token'); // refresh token (0, test_1.expect)(updateTokenCall.args[2]).to.deep.equal({ refreshToken: 'test-refresh-token' }); // credential payload (0, test_1.expect)(updateTokenCall.args[3]).to.equal('production'); // environment (default) }); });