From 4c0973dfd40e27ca441dca725bdf810e65f665d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20Rodr=C3=ADguez?= <127134616+armando-rodriguez-cko@users.noreply.github.com> Date: Wed, 2 Oct 2024 10:46:51 +0200 Subject: [PATCH] Knet Updates. Workflow updates --- src/api/workflows/workflows.js | 118 +++++ test/workflows/workflows.js | 917 ++++++++++++++++++++++++++------- 2 files changed, 854 insertions(+), 181 deletions(-) diff --git a/src/api/workflows/workflows.js b/src/api/workflows/workflows.js index a93ebab..3ac40ef 100644 --- a/src/api/workflows/workflows.js +++ b/src/api/workflows/workflows.js @@ -125,6 +125,30 @@ export default class Workflows { } } + /** + * Adds a workflow action. Actions determine what the workflow will do when it is triggered. + * + * @memberof Workflows + * @param {string} id Workflow Id. + * @param {Object} body Workflows request body. + * @return {Promise} A promise to the Workflows response. + */ + async addAction(id, body) { + try { + const response = await post( + this.config.httpClient, + `${this.config.host}/workflows/${id}/actions/`, + this.config, + this.config.sk, + body + ); + return await response.json; + } catch (err) { + const error = await determineError(err); + throw error; + } + } + /** * Update a workflow action. * @@ -150,6 +174,53 @@ export default class Workflows { } } + /** + * Removes a workflow action. Actions determine what the workflow will do when it is triggered. + * + * @memberof Workflows + * @param {string} workflowId Workflow id. + * @param {string} workflowActionId Workflow action Id. + * @return {Promise} A promise to the Workflows response. + */ + async removeAction(workflowId, workflowActionId) { + try { + const response = await _delete( + this.config.httpClient, + `${this.config.host}/workflows/${workflowId}/actions/${workflowActionId}`, + this.config, + this.config.sk + ); + return await response.json; + } catch (err) { + const error = await determineError(err); + throw error; + } + } + + /** + * Adds a workflow condition. Conditions determine when the workflow will trigger. + * + * @memberof Workflows + * @param {string} id Workflow Id. + * @param {Object} body Workflows request body. + * @return {Promise} A promise to the Workflows response. + */ + async addCondition(id, body) { + try { + const response = await post( + this.config.httpClient, + `${this.config.host}/workflows/${id}/conditions/`, + this.config, + this.config.sk, + body + ); + return await response.json; + } catch (err) { + const error = await determineError(err); + throw error; + } + } + /** * Update a workflow condition. * @@ -175,6 +246,53 @@ export default class Workflows { } } + /** + * Removes a workflow condition. Conditions determine when the workflow will trigger. + * + * @memberof Workflows + * @param {string} workflowId Workflow id. + * @param {string} workflowConditionId Workflow condition Id. + * @return {Promise} A promise to the Workflows response. + */ + async removeCondition(workflowId, workflowConditionId) { + try { + const response = await _delete( + this.config.httpClient, + `${this.config.host}/workflows/${workflowId}/conditions/${workflowConditionId}`, + this.config, + this.config.sk + ); + return await response.json; + } catch (err) { + const error = await determineError(err); + throw error; + } + } + + /** + * Validate a workflow in our Sandbox environment. + * + * @memberof Workflows + * @param {string} id Workflow id. + * @param {Object} body Event types for which the workflow will execute. + * @return {Promise} A promise to the Workflows response. + */ + async test(id, body) { + try { + const response = await post( + this.config.httpClient, + `${this.config.host}/workflows/${id}/test`, + this.config, + this.config.sk, + body + ); + return await response.json; + } catch (err) { + const error = await determineError(err); + throw error; + } + } + /** * Get a list of sources and their events for building new workflows * diff --git a/test/workflows/workflows.js b/test/workflows/workflows.js index fa5bc9e..acfa5f1 100644 --- a/test/workflows/workflows.js +++ b/test/workflows/workflows.js @@ -1,41 +1,52 @@ -import { AuthenticationError, } from '../../src/services/errors'; +import { AuthenticationError, NotFoundError, } from '../../src/services/errors'; import { Checkout } from '../../src/index'; import { expect } from 'chai'; import nock from 'nock'; describe('Workflows', () => { + it('should get all workflows', async () => { nock('https://access.sandbox.checkout.com').post('/connect/token').reply(201, { access_token: '1234', expires_in: 3600, token_type: 'Bearer', - scope: 'flow', + scope: ['flow', 'flow:workflows'], }); nock('https://api.sandbox.checkout.com') .get('/workflows') - .reply(201, { + .reply(200, { data: [ { - id: 'wf_ufvajwyu2gyubkkge6y7c3oui4', + id: 'wf_wlu3wxc26jounofs5iez75qaqa', name: 'SITE4', - _links: [Object], + active: true, + _links: { + self: { + href: 'https://api.checkout.com/workflows/wf_wlu3wxc26jounofs5iez75qaqa', + }, + }, }, { id: 'wf_vsmdljusa3felalfww7tegllyi', name: 'test', - _links: [Object], + active: false, + _links: { + self: { + href: 'https://api.checkout.com/workflows/wf_wlu3wxc26jounofs5iez75qaqa', + }, + }, }, ], }); - let cko = new Checkout( + const cko = new Checkout( '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', { client: 'ack_vvzhoai466su3j3vbxb47ts5oe', - scope: ['flow'], + scope: ['flow', 'flow:workflows'], environment: 'sandbox', } ); - let workflows = await cko.workflows.getAll(); + const workflows = await cko.workflows.getAll(); expect(workflows.data).to.exist; }); @@ -44,19 +55,19 @@ describe('Workflows', () => { access_token: '1234', expires_in: 3600, token_type: 'Bearer', - scope: 'flow', + scope: ['flow', 'flow:workflows'], }); nock('https://api.sandbox.checkout.com').get('/workflows').reply(401); - let cko = new Checkout( + const cko = new Checkout( '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', { client: 'ack_vvzhoai466su3j3vbxb47ts5oe', - scope: ['flow'], + scope: ['flow', 'flow:workflows'], environment: 'sandbox', } ); try { - let workflows = await cko.workflows.getAll(); + await cko.workflows.getAll(); } catch (err) { expect(err).to.be.instanceOf(AuthenticationError); } @@ -67,27 +78,27 @@ describe('Workflows', () => { access_token: '1234', expires_in: 3600, token_type: 'Bearer', - scope: 'flow', + scope: ['flow', 'flow:workflows'], }); nock('https://api.sandbox.checkout.com') .post('/workflows') .reply(201, { - id: 'wf_cllqo2rsrp2evenxnzbl23n4mm', + id: 'wf_wlu3wxc26jounofs5iez75qaqa', _links: { self: { - href: 'https://api.sandbox.checkout.com/workflows/wf_cllqo2rsrp2evenxnzbl23n4mm', + href: 'https://api.sandbox.checkout.com/workflows/wf_wlu3wxc26jounofs5iez75qaqa', }, }, }); - let cko = new Checkout( + const cko = new Checkout( '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', { client: 'ack_vvzhoai466su3j3vbxb47ts5oe', - scope: ['flow'], + scope: ['flow', 'flow:workflows'], environment: 'sandbox', } ); - let workflows = await cko.workflows.add({ + const workflows = await cko.workflows.add({ name: 'Webhooks workflow', conditions: [ { @@ -127,19 +138,19 @@ describe('Workflows', () => { access_token: '1234', expires_in: 3600, token_type: 'Bearer', - scope: 'flow', + scope: ['flow', 'flow:workflows'], }); nock('https://api.sandbox.checkout.com').post('/workflows').reply(401); - let cko = new Checkout( + const cko = new Checkout( '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', { client: 'ack_vvzhoai466su3j3vbxb47ts5oe', - scope: ['flow'], + scope: ['flow', 'flow:workflows'], environment: 'sandbox', } ); try { - let workflows = await cko.workflows.add({ + await cko.workflows.add({ name: 'Webhooks workflow', conditions: [ { @@ -181,12 +192,12 @@ describe('Workflows', () => { access_token: '1234', expires_in: 3600, token_type: 'Bearer', - scope: 'flow', + scope: ['flow', 'flow:workflows'], }); nock('https://api.sandbox.checkout.com') - .get('/workflows/wf_5zm7uccsc6bencaujumvutvfem') - .reply(201, { - id: 'wf_5zm7uccsc6bencaujumvutvfem', + .get('/workflows/wf_34pacj7ae6wexju4avpecxvp6e') + .reply(200, { + id: 'wf_34pacj7ae6wexju4avpecxvp6e', name: 'Webhooks workflow', conditions: [ { @@ -216,19 +227,19 @@ describe('Workflows', () => { ], _links: { self: { - href: 'https://api.sandbox.checkout.com/workflows/wf_5zm7uccsc6bencaujumvutvfem', + href: 'https://api.sandbox.checkout.com/workflows/wf_34pacj7ae6wexju4avpecxvp6e', }, }, }); - let cko = new Checkout( + const cko = new Checkout( '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', { client: 'ack_vvzhoai466su3j3vbxb47ts5oe', - scope: ['flow'], + scope: ['flow', 'flow:workflows'], environment: 'sandbox', } ); - let workflows = await cko.workflows.get('wf_5zm7uccsc6bencaujumvutvfem'); + const workflows = await cko.workflows.get('wf_34pacj7ae6wexju4avpecxvp6e'); expect(workflows.id).to.exist; }); @@ -237,21 +248,21 @@ describe('Workflows', () => { access_token: '1234', expires_in: 3600, token_type: 'Bearer', - scope: 'flow', + scope: ['flow', 'flow:workflows'], }); nock('https://api.sandbox.checkout.com') - .get('/workflows/wf_5zm7uccsc6bencaujumvutvfem') + .get('/workflows/wf_34pacj7ae6wexju4avpecxvp6e') .reply(401); - let cko = new Checkout( + const cko = new Checkout( '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', { client: 'ack_vvzhoai466su3j3vbxb47ts5oe', - scope: ['flow'], + scope: ['flow', 'flow:workflows'], environment: 'sandbox', } ); try { - let workflows = await cko.workflows.get('wf_5zm7uccsc6bencaujumvutvfem'); + await cko.workflows.get('wf_34pacj7ae6wexju4avpecxvp6e'); } catch (err) { expect(err).to.be.instanceOf(AuthenticationError); } @@ -262,20 +273,21 @@ describe('Workflows', () => { access_token: '1234', expires_in: 3600, token_type: 'Bearer', - scope: 'flow', + scope: ['flow', 'flow:workflows'], }); nock('https://api.sandbox.checkout.com') - .delete('/workflows/wf_rou7m32mhmyeblg4xebx5pueoi') - .reply(201, {}); - let cko = new Checkout( + .delete('/workflows/wf_34pacj7ae6wexju4avpecxvp6e') + .reply(204); + const cko = new Checkout( '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', { client: 'ack_vvzhoai466su3j3vbxb47ts5oe', - scope: ['flow'], + scope: ['flow', 'flow:workflows'], environment: 'sandbox', } ); - let workflows = await cko.workflows.remove('wf_rou7m32mhmyeblg4xebx5pueoi'); + const workflows = await cko.workflows.remove('wf_34pacj7ae6wexju4avpecxvp6e'); + expect(workflows).to.deep.equal({}); }); it('should throw AuthenticationError when removing a workflow', async () => { @@ -283,21 +295,21 @@ describe('Workflows', () => { access_token: '1234', expires_in: 3600, token_type: 'Bearer', - scope: 'flow', + scope: ['flow', 'flow:workflows'], }); nock('https://api.sandbox.checkout.com') - .delete('/workflows/wf_rou7m32mhmyeblg4xebx5pueoi') + .delete('/workflows/wf_34pacj7ae6wexju4avpecxvp6e') .reply(401); - let cko = new Checkout( + const cko = new Checkout( '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', { client: 'ack_vvzhoai466su3j3vbxb47ts5oe', - scope: ['flow'], + scope: ['flow', 'flow:workflows'], environment: 'sandbox', } ); try { - let workflows = await cko.workflows.remove('wf_rou7m32mhmyeblg4xebx5pueoi'); + await cko.workflows.remove('wf_34pacj7ae6wexju4avpecxvp6e'); } catch (err) { expect(err).to.be.instanceOf(AuthenticationError); } @@ -308,20 +320,20 @@ describe('Workflows', () => { access_token: '1234', expires_in: 3600, token_type: 'Bearer', - scope: 'flow', + scope: ['flow', 'flow:workflows'], }); nock('https://api.sandbox.checkout.com') - .patch('/workflows/wf_2i7z3lwdoe5uzmomm7yzrytqdy') + .patch('/workflows/wf_c7svxlvo2bbuva4f6s3xu4f7wm') .reply(201, { name: 'Webhooks workflow updated' }); - let cko = new Checkout( + const cko = new Checkout( '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', { client: 'ack_vvzhoai466su3j3vbxb47ts5oe', - scope: ['flow'], + scope: ['flow', 'flow:workflows'], environment: 'sandbox', } ); - let workflows = await cko.workflows.patch('wf_2i7z3lwdoe5uzmomm7yzrytqdy', { + const workflows = await cko.workflows.patch('wf_c7svxlvo2bbuva4f6s3xu4f7wm', { name: 'Webhooks workflow updated', }); expect(workflows.name).to.equal('Webhooks workflow updated'); @@ -332,47 +344,172 @@ describe('Workflows', () => { access_token: '1234', expires_in: 3600, token_type: 'Bearer', - scope: 'flow', + scope: ['flow', 'flow:workflows'], }); nock('https://api.sandbox.checkout.com') - .patch('/workflows/wf_2i7z3lwdoe5uzmomm7yzrytqdy') + .patch('/workflows/wf_c7svxlvo2bbuva4f6s3xu4f7wm') .reply(401); - let cko = new Checkout( + const cko = new Checkout( '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', { client: 'ack_vvzhoai466su3j3vbxb47ts5oe', - scope: ['flow'], + scope: ['flow', 'flow:workflows'], environment: 'sandbox', } ); try { - let workflows = await cko.workflows.patch('wf_2i7z3lwdoe5uzmomm7yzrytqdy', { + await cko.workflows.patch('wf_c7svxlvo2bbuva4f6s3xu4f7wm', { name: 'Webhooks workflow updated', }); } catch (err) { expect(err).to.be.instanceOf(AuthenticationError); } }); +}); + +describe('Workflow Actions', () => { + + it('should add a workflow action', async () => { + nock('https://access.sandbox.checkout.com').post('/connect/token').reply(201, { + access_token: '1234', + expires_in: 3600, + token_type: 'Bearer', + scope: ['flow', 'flow:workflows'], + }); + nock('https://api.sandbox.checkout.com') + .post('/workflows/wf_c7svxlvo2bbuva4f6s3xu4f7wm/actions/') + .reply(201, { + id: 'wfa_wlu3wxc26jounofs5iez75qaqa', + _links: { + self: { + href: 'https://api.checkout.com/workflows/wf_c7svxlvo2bbuva4f6s3xu4f7wm/actions/wfa_wlu3wxc26jounofs5iez75qaqa', + }, + }, + }); + const cko = new Checkout( + '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', + { + client: 'ack_vvzhoai466su3j3vbxb47ts5oe', + scope: ['flow', 'flow:workflows'], + environment: 'sandbox', + } + ); + const action = await cko.workflows.addAction('wf_c7svxlvo2bbuva4f6s3xu4f7wm', { + type: 'webhook', + url: 'https://example.com/webhooks', + }); + expect(action.id).to.equal('wfa_wlu3wxc26jounofs5iez75qaqa'); + }); + + it('should throw AuthenticationError when adding an action with invalid credentials', async () => { + nock('https://access.sandbox.checkout.com').post('/connect/token').reply(201, { + access_token: '1234', + expires_in: 3600, + token_type: 'Bearer', + scope: ['flow', 'flow:workflows'], + }); + nock('https://api.sandbox.checkout.com') + .post('/workflows/wf_c7svxlvo2bbuva4f6s3xu4f7wm/actions/') + .reply(401); + + const cko = new Checkout( + '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', + { + client: 'ack_vvzhoai466su3j3vbxb47ts5oe', + scope: ['flow', 'flow:workflows'], + environment: 'sandbox', + } + ); + + try { + await cko.workflows.addAction('wf_c7svxlvo2bbuva4f6s3xu4f7wm', { + type: 'webhook', + url: 'https://example.com/webhooks', + }); + } catch (err) { + expect(err).to.be.instanceOf(AuthenticationError); + } + }); + + it('should throw NotFoundError when adding an action to a non-existent workflow', async () => { + nock('https://access.sandbox.checkout.com').post('/connect/token').reply(201, { + access_token: '1234', + expires_in: 3600, + token_type: 'Bearer', + scope: ['flow', 'flow:workflows'], + }); + nock('https://api.sandbox.checkout.com') + .post('/workflows/wf_nonexistent/actions/') + .reply(404); + + const cko = new Checkout( + '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', + { + client: 'ack_vvzhoai466su3j3vbxb47ts5oe', + scope: ['flow', 'flow:workflows'], + environment: 'sandbox', + } + ); + + try { + await cko.workflows.addAction('wf_nonexistent', { + type: 'webhook', + url: 'https://example.com/webhooks', + }); + } catch (err) { + expect(err).to.be.instanceOf(NotFoundError); + } + }); + + it('should throw an error for server issues when adding an action', async () => { + nock('https://access.sandbox.checkout.com').post('/connect/token').reply(201, { + access_token: '1234', + expires_in: 3600, + token_type: 'Bearer', + scope: ['flow', 'flow:workflows'], + }); + nock('https://api.sandbox.checkout.com') + .post('/workflows/wfa_wlu3wxc26jounofs5iez75qaqa/actions/') + .reply(500); + + const cko = new Checkout( + '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', + { + client: 'ack_vvzhoai466su3j3vbxb47ts5oe', + scope: ['flow', 'flow:workflows'], + environment: 'sandbox', + } + ); + + try { + await cko.workflows.addAction('wfa_wlu3wxc26jounofs5iez75qaqa', { + type: 'webhook', + url: 'https://example.com/webhooks', + }); + } catch (err) { + expect(err.http_code).to.equal(500); + } + }); it('should update a workflow action', async () => { nock('https://access.sandbox.checkout.com').post('/connect/token').reply(201, { access_token: '1234', expires_in: 3600, token_type: 'Bearer', - scope: 'flow', + scope: ['flow', 'flow:workflows'], }); nock('https://api.sandbox.checkout.com') .put('/workflows/wf_2i7z3lwdoe5uzmomm7yzrytqdy/actions/wfa_5qxwp7stgcqufj63mkr42xyeqi') - .reply(201, {}); - let cko = new Checkout( + .reply(200); + const cko = new Checkout( '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', { client: 'ack_vvzhoai466su3j3vbxb47ts5oe', - scope: ['flow'], + scope: ['flow', 'flow:workflows'], environment: 'sandbox', } ); - let workflows = await cko.workflows.updateAction( + const workflows = await cko.workflows.updateAction( 'wf_2i7z3lwdoe5uzmomm7yzrytqdy', 'wfa_5qxwp7stgcqufj63mkr42xyeqi', { @@ -380,6 +517,7 @@ describe('Workflows', () => { url: 'https://example.com/updated', } ); + expect(workflows).to.deep.equal({}); }); it('should throw AuthenticationError when updating a workflow action', async () => { @@ -387,21 +525,21 @@ describe('Workflows', () => { access_token: '1234', expires_in: 3600, token_type: 'Bearer', - scope: 'flow', + scope: ['flow', 'flow:workflows'], }); nock('https://api.sandbox.checkout.com') .put('/workflows/wf_2i7z3lwdoe5uzmomm7yzrytqdy/actions/wfa_5qxwp7stgcqufj63mkr42xyeqi') .reply(401); - let cko = new Checkout( + const cko = new Checkout( '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', { client: 'ack_vvzhoai466su3j3vbxb47ts5oe', - scope: ['flow'], + scope: ['flow', 'flow:workflows'], environment: 'sandbox', } ); try { - let workflows = await cko.workflows.updateAction( + await cko.workflows.updateAction( 'wf_2i7z3lwdoe5uzmomm7yzrytqdy', 'wfa_5qxwp7stgcqufj63mkr42xyeqi', { @@ -414,29 +552,217 @@ describe('Workflows', () => { } }); + it('should remove a workflow action', async () => { + nock('https://access.sandbox.checkout.com').post('/connect/token').reply(201, { + access_token: '1234', + expires_in: 3600, + token_type: 'Bearer', + scope: ['flow', 'flow:workflows'], + }); + nock('https://api.sandbox.checkout.com') + .delete('/workflows/wf_rou7m32mhmyeblg4xebx5pueoi/actions/wfa_c7svxlvo2bbuva4f6s3xu4f7wm') + .reply(204); + const cko = new Checkout( + '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', + { + client: 'ack_vvzhoai466su3j3vbxb47ts5oe', + scope: ['flow', 'flow:workflows'], + environment: 'sandbox', + } + ); + const workflows = await cko.workflows.removeAction( + 'wf_rou7m32mhmyeblg4xebx5pueoi', + 'wfa_c7svxlvo2bbuva4f6s3xu4f7wm' + ); + expect(workflows).to.deep.equal({}); + }); + + it('should throw AuthenticationError when removing a workflow action', async () => { + nock('https://access.sandbox.checkout.com').post('/connect/token').reply(201, { + access_token: '1234', + expires_in: 3600, + token_type: 'Bearer', + scope: ['flow', 'flow:workflows'], + }); + nock('https://api.sandbox.checkout.com') + .delete('/workflows/wf_rou7m32mhmyeblg4xebx5pueoi/actions/wfa_c7svxlvo2bbuva4f6s3xu4f7wm') + .reply(401); + const cko = new Checkout( + '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', + { + client: 'ack_vvzhoai466su3j3vbxb47ts5oe', + scope: ['flow', 'flow:workflows'], + environment: 'sandbox', + } + ); + try { + await cko.workflows.removeAction( + 'wf_rou7m32mhmyeblg4xebx5pueoi', + 'wfa_c7svxlvo2bbuva4f6s3xu4f7wm' + ); + } catch (err) { + expect(err).to.be.instanceOf(AuthenticationError); + } + }); + +}); + +describe('Workflow Conditions', () => { + + it('should add a workflow condition', async () => { + nock('https://access.sandbox.checkout.com').post('/connect/token').reply(201, { + access_token: '1234', + expires_in: 3600, + token_type: 'Bearer', + scope: ['flow', 'flow:workflows'], + }); + nock('https://api.sandbox.checkout.com') + .post('/workflows/wf_2i7z3lwdoe5uzmomm7yzrytqdy/conditions/') + .reply(201, { + id: 'wfc_wlu3wxc26jounofs5iez75qaqa', + _links: { + self: { + href: 'https://api.checkout.com/workflows/wf_2i7z3lwdoe5uzmomm7yzrytqdy/conditions/wfc_wlu3wxc26jounofs5iez75qaqa', + }, + }, + }); + + const cko = new Checkout( + '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', + { + client: 'ack_vvzhoai466su3j3vbxb47ts5oe', + scope: ['flow', 'flow:workflows'], + environment: 'sandbox', + } + ); + const condition = await cko.workflows.addCondition('wf_2i7z3lwdoe5uzmomm7yzrytqdy', { + type: 'event', + events: { + gateway: ['payment_approved', 'payment_declined'], + }, + }); + expect(condition.id).to.equal('wfc_wlu3wxc26jounofs5iez75qaqa'); + }); + + it('should throw AuthenticationError when adding a condition with invalid credentials', async () => { + nock('https://access.sandbox.checkout.com').post('/connect/token').reply(201, { + access_token: '1234', + expires_in: 3600, + token_type: 'Bearer', + scope: ['flow', 'flow:workflows'], + }); + nock('https://api.sandbox.checkout.com') + .post('/workflows/wf_2i7z3lwdoe5uzmomm7yzrytqdy/conditions/') + .reply(401); + + const cko = new Checkout( + '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', + { + client: 'ack_vvzhoai466su3j3vbxb47ts5oe', + scope: ['flow', 'flow:workflows'], + environment: 'sandbox', + } + ); + + try { + await cko.workflows.addCondition('wf_2i7z3lwdoe5uzmomm7yzrytqdy', { + type: 'event', + events: { + gateway: ['payment_approved', 'payment_declined'], + }, + }); + } catch (err) { + expect(err).to.be.instanceOf(AuthenticationError); + } + }); + + it('should throw NotFoundError when adding a condition to a non-existent workflow', async () => { + nock('https://access.sandbox.checkout.com').post('/connect/token').reply(201, { + access_token: '1234', + expires_in: 3600, + token_type: 'Bearer', + scope: ['flow', 'flow:workflows'], + }); + nock('https://api.sandbox.checkout.com') + .post('/workflows/wf_nonexistent/conditions/') + .reply(404); + + const cko = new Checkout( + '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', + { + client: 'ack_vvzhoai466su3j3vbxb47ts5oe', + scope: ['flow', 'flow:workflows'], + environment: 'sandbox', + } + ); + + try { + await cko.workflows.addCondition('wf_nonexistent', { + type: 'event', + events: { + gateway: ['payment_approved', 'payment_declined'], + }, + }); + } catch (err) { + expect(err).to.be.instanceOf(NotFoundError); + } + }); + + it('should throw an error for server issues when adding a condition', async () => { + nock('https://access.sandbox.checkout.com').post('/connect/token').reply(201, { + access_token: '1234', + expires_in: 3600, + token_type: 'Bearer', + scope: ['flow', 'flow:workflows'], + }); + nock('https://api.sandbox.checkout.com') + .post('/workflows/wf_2i7z3lwdoe5uzmomm7yzrytqdy/conditions/') + .reply(500); + + const cko = new Checkout( + '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', + { + client: 'ack_vvzhoai466su3j3vbxb47ts5oe', + scope: ['flow', 'flow:workflows'], + environment: 'sandbox', + } + ); + + try { + await cko.workflows.addCondition('wf_2i7z3lwdoe5uzmomm7yzrytqdy', { + type: 'event', + events: { + gateway: ['payment_approved', 'payment_declined'], + }, + }); + } catch (err) { + expect(err.http_code).to.equal(500); + } + }); + it('should update a workflow condition', async () => { nock('https://access.sandbox.checkout.com').post('/connect/token').reply(201, { access_token: '1234', expires_in: 3600, token_type: 'Bearer', - scope: 'flow', + scope: ['flow', 'flow:workflows'], }); nock('https://api.sandbox.checkout.com') .put( - '/workflows/wf_2i7z3lwdoe5uzmomm7yzrytqdy/conditions/wfc_ybu4t6aruwju5l6ymlc67ya5ne' + '/workflows/wf_c7svxlvo2bbuva4f6s3xu4f7wm/conditions/wfc_d5estuyxzshubhly2wu3hloehi' ) - .reply(201, {}); - let cko = new Checkout( + .reply(200, {}); + const cko = new Checkout( '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', { client: 'ack_vvzhoai466su3j3vbxb47ts5oe', - scope: ['flow'], + scope: ['flow', 'flow:workflows'], environment: 'sandbox', } ); - let workflows = await cko.workflows.updateCondition( - 'wf_2i7z3lwdoe5uzmomm7yzrytqdy', - 'wfc_ybu4t6aruwju5l6ymlc67ya5ne', + const workflows = await cko.workflows.updateCondition( + 'wf_c7svxlvo2bbuva4f6s3xu4f7wm', + 'wfc_d5estuyxzshubhly2wu3hloehi', { type: 'event', events: { @@ -444,32 +770,33 @@ describe('Workflows', () => { }, } ); + expect(workflows).to.deep.equal({}); }); - it('should throw AuthenticationError when XXXXXX', async () => { + it('should throw AuthenticationError when updating a workflow condition', async () => { nock('https://access.sandbox.checkout.com').post('/connect/token').reply(201, { access_token: '1234', expires_in: 3600, token_type: 'Bearer', - scope: 'flow', + scope: ['flow', 'flow:workflows'], }); nock('https://api.sandbox.checkout.com') .put( - '/workflows/wf_2i7z3lwdoe5uzmomm7yzrytqdy/conditions/wfc_ybu4t6aruwju5l6ymlc67ya5ne' + '/workflows/wf_c7svxlvo2bbuva4f6s3xu4f7wm/conditions/wfc_d5estuyxzshubhly2wu3hloehi' ) .reply(401); - let cko = new Checkout( + const cko = new Checkout( '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', { client: 'ack_vvzhoai466su3j3vbxb47ts5oe', - scope: ['flow'], + scope: ['flow', 'flow:workflows'], environment: 'sandbox', } ); try { - let workflows = await cko.workflows.updateCondition( - 'wf_2i7z3lwdoe5uzmomm7yzrytqdy', - 'wfc_ybu4t6aruwju5l6ymlc67ya5ne', + await cko.workflows.updateCondition( + 'wf_c7svxlvo2bbuva4f6s3xu4f7wm', + 'wfc_d5estuyxzshubhly2wu3hloehi', { type: 'event', events: { @@ -486,16 +813,214 @@ describe('Workflows', () => { } }); + it('should remove a workflow condition', async () => { + nock('https://access.sandbox.checkout.com').post('/connect/token') + .reply(201, { + access_token: '1234', + expires_in: 3600, + token_type: 'Bearer', + scope: ['flow', 'flow:workflows'], + }); + nock('https://api.sandbox.checkout.com') + .delete('/workflows/wf_c7svxlvo2bbuva4f6s3xu4f7wm/conditions/wfc_c7svxlvo2bbuva4f6s3xu4f7wm') + .reply(204); + const cko = new Checkout( + '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', + { + client: 'ack_vvzhoai466su3j3vbxb47ts5oe', + scope: ['flow', 'flow:workflows'], + environment: 'sandbox', + } + ); + const workflows = await cko.workflows.removeCondition( + 'wf_c7svxlvo2bbuva4f6s3xu4f7wm', + 'wfc_c7svxlvo2bbuva4f6s3xu4f7wm' + ); + expect(workflows).to.deep.equal({}); + }); + + it('should throw AuthenticationError when removing a workflow condition', async () => { + nock('https://access.sandbox.checkout.com').post('/connect/token').reply(201, { + access_token: '1234', + expires_in: 3600, + token_type: 'Bearer', + scope: ['flow', 'flow:workflows'], + }); + nock('https://api.sandbox.checkout.com') + .delete('/workflows/wf_c7svxlvo2bbuva4f6s3xu4f7wm/conditions/wfc_c7svxlvo2bbuva4f6s3xu4f7wm') + .reply(401); + const cko = new Checkout( + '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', + { + client: 'ack_vvzhoai466su3j3vbxb47ts5oe', + scope: ['flow', 'flow:workflows'], + environment: 'sandbox', + } + ); + try { + await cko.workflows.removeCondition( + 'wf_c7svxlvo2bbuva4f6s3xu4f7wm', + 'wfc_c7svxlvo2bbuva4f6s3xu4f7wm' + ); + } catch (err) { + expect(err).to.be.instanceOf(AuthenticationError); + } + }); + +}); + +describe('Workflow Test', () => { + + it('should test a workflow', async () => { + nock('https://access.sandbox.checkout.com').post('/connect/token').reply(201, { + access_token: '1234', + expires_in: 3600, + token_type: 'Bearer', + scope: ['flow', 'flow:workflows'], + }); + nock('https://api.sandbox.checkout.com') + .post( + '/workflows/wf_2i7z3lwdoe5uzmomm7yzrytqdy/test' + ) + .reply(204); + const cko = new Checkout( + '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', + { + client: 'ack_vvzhoai466su3j3vbxb47ts5oe', + scope: ['flow', 'flow:workflows'], + environment: 'sandbox', + } + ); + const workflows = await cko.workflows.test( + 'wf_2i7z3lwdoe5uzmomm7yzrytqdy', + { + "event_types": [ + "payment_approved", + "payment_declined", + "card_verification_declined", + "card_verified", + "payment_authorization_incremented", + "payment_authorization_increment_declined", + "payment_capture_declined", + "payment_captured", + "payment_refund_declined", + "payment_refunded", + "payment_void_declined", + "payment_voided", + "dispute_canceled", + "dispute_evidence_required", + "dispute_expired", + "dispute_lost", + "dispute_resolved", + "dispute_won" + ] + } + ); + + expect(workflows).to.deep.equal({}); + }); + + it('should throw AuthenticationError when testing a workflow with invalid credentials', async () => { + nock('https://access.sandbox.checkout.com').post('/connect/token').reply(201, { + access_token: '1234', + expires_in: 3600, + token_type: 'Bearer', + scope: ['flow', 'flow:workflows'], + }); + nock('https://api.sandbox.checkout.com') + .post('/workflows/wf_2i7z3lwdoe5uzmomm7yzrytqdy/test') + .reply(401); + + const cko = new Checkout( + '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', + { + client: 'ack_vvzhoai466su3j3vbxb47ts5oe', + scope: ['flow', 'flow:workflows'], + environment: 'sandbox', + } + ); + + try { + await cko.workflows.test('wf_2i7z3lwdoe5uzmomm7yzrytqdy', { + "event_types": ["payment_approved", "payment_declined"] + }); + } catch (err) { + expect(err).to.be.instanceOf(AuthenticationError); + } + }); + + it('should throw NotFoundError when testing a non-existent workflow', async () => { + nock('https://access.sandbox.checkout.com').post('/connect/token').reply(201, { + access_token: '1234', + expires_in: 3600, + token_type: 'Bearer', + scope: ['flow', 'flow:workflows'], + }); + nock('https://api.sandbox.checkout.com') + .post('/workflows/wf_nonexistent/test') + .reply(404); + + const cko = new Checkout( + '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', + { + client: 'ack_vvzhoai466su3j3vbxb47ts5oe', + scope: ['flow', 'flow:workflows'], + environment: 'sandbox', + } + ); + + try { + await cko.workflows.test('wf_nonexistent', { + "event_types": ["payment_approved", "payment_declined"] + }); + } catch (err) { + expect(err).to.be.instanceOf(NotFoundError); + } + }); + + it('should throw an error for server issues when testing a workflow', async () => { + nock('https://access.sandbox.checkout.com').post('/connect/token').reply(201, { + access_token: '1234', + expires_in: 3600, + token_type: 'Bearer', + scope: ['flow', 'flow:workflows'], + }); + nock('https://api.sandbox.checkout.com') + .post('/workflows/wf_2i7z3lwdoe5uzmomm7yzrytqdy/test') + .reply(500); + + const cko = new Checkout( + '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', + { + client: 'ack_vvzhoai466su3j3vbxb47ts5oe', + scope: ['flow', 'flow:workflows'], + environment: 'sandbox', + } + ); + + try { + await cko.workflows.test('wf_2i7z3lwdoe5uzmomm7yzrytqdy', { + "event_types": ["payment_approved", "payment_declined"] + }); + } catch (err) { + expect(err.http_code).to.equal(500); + } + }); + +}); + +describe('Workflow Events', () => { + it('should get event types', async () => { nock('https://access.sandbox.checkout.com').post('/connect/token').reply(201, { access_token: '1234', expires_in: 3600, token_type: 'Bearer', - scope: 'flow', + scope: ['flow', 'flow:workflows'], }); nock('https://api.sandbox.checkout.com') .get('/workflows/event-types') - .reply(201, [ + .reply(200, [ { id: 'dispute', display_name: 'Disputes', @@ -503,36 +1028,36 @@ describe('Workflows', () => { events: [[Object]], }, ]); - let cko = new Checkout( + const cko = new Checkout( '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', { client: 'ack_vvzhoai466su3j3vbxb47ts5oe', - scope: ['flow'], + scope: ['flow', 'flow:workflows'], environment: 'sandbox', } ); - let workflows = await cko.workflows.getEventTypes(); + const workflows = await cko.workflows.getEventTypes(); expect(workflows[0].id).to.equal('dispute'); }); - it('should throw AuthenticationError when XXXXXX', async () => { + it('should throw AuthenticationError when get a workflow event', async () => { nock('https://access.sandbox.checkout.com').post('/connect/token').reply(201, { access_token: '1234', expires_in: 3600, token_type: 'Bearer', - scope: 'flow', + scope: ['flow', 'flow:workflows'], }); nock('https://api.sandbox.checkout.com').get('/workflows/event-types').reply(401); - let cko = new Checkout( + const cko = new Checkout( '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', { client: 'ack_vvzhoai466su3j3vbxb47ts5oe', - scope: ['flow'], + scope: ['flow', 'flow:workflows'], environment: 'sandbox', } ); try { - let workflows = await cko.workflows.getEventTypes(); + await cko.workflows.getEventTypes(); } catch (err) { expect(err).to.be.instanceOf(AuthenticationError); } @@ -543,63 +1068,67 @@ describe('Workflows', () => { access_token: '1234', expires_in: 3600, token_type: 'Bearer', - scope: 'flow', + scope: ['flow', 'flow:workflows'], }); nock('https://api.sandbox.checkout.com') .get('/workflows/events/evt_hsfxtjwidv6ulah5gdbiqwqnka') - .reply(201, { + .reply(200, { id: 'evt_hsfxtjwidv6ulah5gdbiqwqnka', source: 'gateway', type: 'payment_approved', }); - let cko = new Checkout( + const cko = new Checkout( '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', { client: 'ack_vvzhoai466su3j3vbxb47ts5oe', - scope: ['flow'], + scope: ['flow', 'flow:workflows'], environment: 'sandbox', } ); - let workflows = await cko.workflows.getEvent('evt_hsfxtjwidv6ulah5gdbiqwqnka'); + const workflows = await cko.workflows.getEvent('evt_hsfxtjwidv6ulah5gdbiqwqnka'); expect(workflows.id).to.equal('evt_hsfxtjwidv6ulah5gdbiqwqnka'); }); - it('should throw AuthenticationError when XXXXXX', async () => { + it('should throw AuthenticationError when get a workflow event', async () => { nock('https://access.sandbox.checkout.com').post('/connect/token').reply(201, { access_token: '1234', expires_in: 3600, token_type: 'Bearer', - scope: 'flow', + scope: ['flow', 'flow:workflows'], }); nock('https://api.sandbox.checkout.com') .get('/workflows/events/evt_hsfxtjwidv6ulah5gdbiqwqnka') .reply(401); - let cko = new Checkout( + const cko = new Checkout( '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', { client: 'ack_vvzhoai466su3j3vbxb47ts5oe', - scope: ['flow'], + scope: ['flow', 'flow:workflows'], environment: 'sandbox', } ); try { - let workflows = await cko.workflows.getEvent('evt_hsfxtjwidv6ulah5gdbiqwqnka'); + await cko.workflows.getEvent('evt_hsfxtjwidv6ulah5gdbiqwqnka'); } catch (err) { expect(err).to.be.instanceOf(AuthenticationError); } }); +}); + +describe('Workflow Action Invocations', () => { + it('should get action invocations', async () => { nock('https://access.sandbox.checkout.com').post('/connect/token').reply(201, { access_token: '1234', expires_in: 3600, token_type: 'Bearer', - scope: 'flow', + scope: ['flow', 'flow:workflows'], }); nock('https://api.sandbox.checkout.com') .get('/workflows/events/evt_az5sblvku4ge3dwpztvyizgcau/actions/wfa_uzkxpffkvpiu5fe3h5ira7sqpa') - .reply(201, { + .reply(200, { workflow_id: 'wf_c7svxlvo2bbuva4f6s3xu4f7wm', event_id: 'evt_az5sblvku4ge3dwpztvyizgcau', workflow_action_id: 'wfa_uzkxpffkvpiu5fe3h5ira7sqpa', @@ -634,65 +1163,71 @@ describe('Workflows', () => { }, ], }); - let cko = new Checkout( + const cko = new Checkout( '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', { client: 'ack_vvzhoai466su3j3vbxb47ts5oe', - scope: ['flow'], + scope: ['flow', 'flow:workflows'], environment: 'sandbox', } ); - let workflows = await cko.workflows.getActionInvocations('evt_az5sblvku4ge3dwpztvyizgcau', 'wfa_uzkxpffkvpiu5fe3h5ira7sqpa'); + const workflows = await cko.workflows.getActionInvocations('evt_az5sblvku4ge3dwpztvyizgcau', 'wfa_uzkxpffkvpiu5fe3h5ira7sqpa'); expect(workflows.workflow_id).to.equal('wf_c7svxlvo2bbuva4f6s3xu4f7wm'); expect(workflows.event_id).to.equal('evt_az5sblvku4ge3dwpztvyizgcau'); expect(workflows.workflow_action_id).to.equal('wfa_uzkxpffkvpiu5fe3h5ira7sqpa'); }); - it('should throw AuthenticationError when XXXXXX', async () => { + it('should throw AuthenticationError when get an action invocation', async () => { nock('https://access.sandbox.checkout.com').post('/connect/token').reply(201, { access_token: '1234', expires_in: 3600, token_type: 'Bearer', - scope: 'flow', + scope: ['flow', 'flow:workflows'], }); nock('https://api.sandbox.checkout.com') .get('/workflows/events/evt_az5sblvku4ge3dwpztvyizgcau/actions/wfa_uzkxpffkvpiu5fe3h5ira7sqpa') .reply(401); - let cko = new Checkout( + const cko = new Checkout( '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', { client: 'ack_vvzhoai466su3j3vbxb47ts5oe', - scope: ['flow'], + scope: ['flow', 'flow:workflows'], environment: 'sandbox', } ); try { - let workflows = await cko.workflows.getActionInvocations('evt_az5sblvku4ge3dwpztvyizgcau', 'wfa_uzkxpffkvpiu5fe3h5ira7sqpa'); + await cko.workflows.getActionInvocations('evt_az5sblvku4ge3dwpztvyizgcau', 'wfa_uzkxpffkvpiu5fe3h5ira7sqpa'); } catch (err) { expect(err).to.be.instanceOf(AuthenticationError); } }); +}); + +describe('Workflow Reflows', () => { + it('should reflow by event', async () => { nock('https://access.sandbox.checkout.com').post('/connect/token').reply(201, { access_token: '1234', expires_in: 3600, token_type: 'Bearer', - scope: 'flow', + scope: ['flow', 'flow:workflows'], }); nock('https://api.sandbox.checkout.com') .post('/workflows/events/evt_hsfxtjwidv6ulah5gdbiqwqnka/reflow') - .reply(201, {}); - let cko = new Checkout( + .reply(202); + const cko = new Checkout( '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', { client: 'ack_vvzhoai466su3j3vbxb47ts5oe', - scope: ['flow'], + scope: ['flow', 'flow:workflows'], environment: 'sandbox', } ); - let workflows = await cko.workflows.reflowByEvent('evt_hsfxtjwidv6ulah5gdbiqwqnka'); + const workflows = await cko.workflows.reflowByEvent('evt_hsfxtjwidv6ulah5gdbiqwqnka'); + + expect(workflows).to.deep.equal({}); }); it('should throw AuthenticationError when reflowing by event id', async () => { @@ -700,21 +1235,21 @@ describe('Workflows', () => { access_token: '1234', expires_in: 3600, token_type: 'Bearer', - scope: 'flow', + scope: ['flow', 'flow:workflows'], }); nock('https://api.sandbox.checkout.com') .post('/workflows/events/evt_hsfxtjwidv6ulah5gdbiqwqnka/reflow') .reply(401); - let cko = new Checkout( + const cko = new Checkout( '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', { client: 'ack_vvzhoai466su3j3vbxb47ts5oe', - scope: ['flow'], + scope: ['flow', 'flow:workflows'], environment: 'sandbox', } ); try { - let workflows = await cko.workflows.reflowByEvent('evt_hsfxtjwidv6ulah5gdbiqwqnka'); + await cko.workflows.reflowByEvent('evt_hsfxtjwidv6ulah5gdbiqwqnka'); } catch (err) { expect(err).to.be.instanceOf(AuthenticationError); } @@ -725,50 +1260,53 @@ describe('Workflows', () => { access_token: '1234', expires_in: 3600, token_type: 'Bearer', - scope: 'flow', + scope: ['flow', 'flow:workflows'], }); nock('https://api.sandbox.checkout.com') .post( '/workflows/events/evt_hsfxtjwidv6ulah5gdbiqwqnka/workflow/wf_6p73pesh75vu7fqo6p6exhpe54/reflow' ) - .reply(201, {}); + .reply(202); - let cko = new Checkout( + const cko = new Checkout( '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', { client: 'ack_vvzhoai466su3j3vbxb47ts5oe', - scope: ['flow'], + scope: ['flow', 'flow:workflows'], environment: 'sandbox', } ); - let workflows = await cko.workflows.reflowByEventAndWorkflow( + const workflows = await cko.workflows.reflowByEventAndWorkflow( 'evt_hsfxtjwidv6ulah5gdbiqwqnka', 'wf_6p73pesh75vu7fqo6p6exhpe54' ); + + expect(workflows).to.deep.equal({}); }); it('should throw AuthenticationError when reflowing by event and workflow', async () => { - nock('https://access.sandbox.checkout.com').post('/connect/token').reply(201, { + nock('https://access.sandbox.checkout.com').post('/connect/token') + .reply(201, { access_token: '1234', expires_in: 3600, token_type: 'Bearer', - scope: 'flow', + scope: ['flow', 'flow:workflows'], }); nock('https://api.sandbox.checkout.com') .post( '/workflows/events/evt_hsfxtjwidv6ulah5gdbiqwqnka/workflow/wf_6p73pesh75vu7fqo6p6exhpe54/reflow' ) .reply(401); - let cko = new Checkout( + const cko = new Checkout( '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', { client: 'ack_vvzhoai466su3j3vbxb47ts5oe', - scope: ['flow'], + scope: ['flow', 'flow:workflows'], environment: 'sandbox', } ); try { - let workflows = await cko.workflows.reflowByEventAndWorkflow( + await cko.workflows.reflowByEventAndWorkflow( 'evt_hsfxtjwidv6ulah5gdbiqwqnka', 'wf_6p73pesh75vu7fqo6p6exhpe54' ); @@ -778,45 +1316,51 @@ describe('Workflows', () => { }); it('should reflow events by event and workflow ids', async () => { - nock('https://access.sandbox.checkout.com').post('/connect/token').reply(201, { + nock('https://access.sandbox.checkout.com').post('/connect/token') + .reply(201, { access_token: '1234', expires_in: 3600, token_type: 'Bearer', - scope: 'flow', + scope: ['flow', 'flow:workflows'], }); - nock('https://api.sandbox.checkout.com').post('/workflows/events/reflow').reply(201, {}); - let cko = new Checkout( + nock('https://api.sandbox.checkout.com').post('/workflows/events/reflow') + .reply(202); + const cko = new Checkout( '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', { client: 'ack_vvzhoai466su3j3vbxb47ts5oe', - scope: ['flow'], + scope: ['flow', 'flow:workflows'], environment: 'sandbox', } ); - let workflows = await cko.workflows.reflowEventsByEventAndWorkflowIds( + const workflows = await cko.workflows.reflowEventsByEventAndWorkflowIds( ['evt_hsfxtjwidv6ulah5gdbiqwqnka'], ['wf_6p73pesh75vu7fqo6p6exhpe54'] ); + + expect(workflows).to.deep.equal({}); }); it('should throw AuthenticationError when reflowing events by event and workflow ids', async () => { - nock('https://access.sandbox.checkout.com').post('/connect/token').reply(201, { + nock('https://access.sandbox.checkout.com').post('/connect/token') + .reply(201, { access_token: '1234', expires_in: 3600, token_type: 'Bearer', - scope: 'flow', + scope: ['flow', 'flow:workflows'], }); - nock('https://api.sandbox.checkout.com').post('/workflows/events/reflow').reply(401); - let cko = new Checkout( + nock('https://api.sandbox.checkout.com').post('/workflows/events/reflow') + .reply(401); + const cko = new Checkout( '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', { client: 'ack_vvzhoai466su3j3vbxb47ts5oe', - scope: ['flow'], + scope: ['flow', 'flow:workflows'], environment: 'sandbox', } ); try { - let workflows = await cko.workflows.reflowEventsByEventAndWorkflowIds( + await cko.workflows.reflowEventsByEventAndWorkflowIds( ['evt_hsfxtjwidv6ulah5gdbiqwqnka'], ['wf_6p73pesh75vu7fqo6p6exhpe54'] ); @@ -826,25 +1370,29 @@ describe('Workflows', () => { }); it('should reflow events by subject and workflow ids', async () => { - nock('https://access.sandbox.checkout.com').post('/connect/token').reply(201, { + nock('https://access.sandbox.checkout.com').post('/connect/token') + .reply(201, { access_token: '1234', expires_in: 3600, token_type: 'Bearer', - scope: 'flow', + scope: ['flow', 'flow:workflows'], }); - nock('https://api.sandbox.checkout.com').post('/workflows/events/reflow').reply(201, {}); - let cko = new Checkout( + nock('https://api.sandbox.checkout.com').post('/workflows/events/reflow') + .reply(202); + const cko = new Checkout( '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', { client: 'ack_vvzhoai466su3j3vbxb47ts5oe', - scope: ['flow'], + scope: ['flow', 'flow:workflows'], environment: 'sandbox', } ); - let workflows = await cko.workflows.reflowEventsBySubjectAndWorkflowIds( + const workflows = await cko.workflows.reflowEventsBySubjectAndWorkflowIds( ['pay_ymhp72mhubcejmjjwcupzalm5e'], ['wf_6p73pesh75vu7fqo6p6exhpe54'] ); + + expect(workflows).to.deep.equal({}); }); it('should throw AuthenticationError when reflowing events by subject and workflow ids', async () => { @@ -852,19 +1400,20 @@ describe('Workflows', () => { access_token: '1234', expires_in: 3600, token_type: 'Bearer', - scope: 'flow', + scope: ['flow', 'flow:workflows'], }); - nock('https://api.sandbox.checkout.com').post('/workflows/events/reflow').reply(401); - let cko = new Checkout( + nock('https://api.sandbox.checkout.com').post('/workflows/events/reflow') + .reply(401); + const cko = new Checkout( '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', { client: 'ack_vvzhoai466su3j3vbxb47ts5oe', - scope: ['flow'], + scope: ['flow', 'flow:workflows'], environment: 'sandbox', } ); try { - let workflows = await cko.workflows.reflowEventsBySubjectAndWorkflowIds( + await cko.workflows.reflowEventsBySubjectAndWorkflowIds( ['pay_ymhp72mhubcejmjjwcupzalm5e'], ['wf_6p73pesh75vu7fqo6p6exhpe54'] ); @@ -873,16 +1422,20 @@ describe('Workflows', () => { } }); +}); + +describe('Workflow Subjects', () => { + it('should get subject events', async () => { nock('https://access.sandbox.checkout.com').post('/connect/token').reply(201, { access_token: '1234', expires_in: 3600, token_type: 'Bearer', - scope: 'flow', + scope: ['flow', 'flow:workflows'], }); nock('https://api.sandbox.checkout.com') .get('/workflows/events/subject/pay_ymhp72mhubcejmjjwcupzalm5e') - .reply(201, { + .reply(200, { data: [ { id: 'evt_hsfxtjwidv6ulah5gdbiqwqnka', @@ -892,15 +1445,15 @@ describe('Workflows', () => { }, ], }); - let cko = new Checkout( + const cko = new Checkout( '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', { client: 'ack_vvzhoai466su3j3vbxb47ts5oe', - scope: ['flow'], + scope: ['flow', 'flow:workflows'], environment: 'sandbox', } ); - let workflows = await cko.workflows.getSubjectEvents('pay_ymhp72mhubcejmjjwcupzalm5e'); + const workflows = await cko.workflows.getSubjectEvents('pay_ymhp72mhubcejmjjwcupzalm5e'); expect(workflows.data[0].type).to.equal('payment_approved'); }); @@ -909,21 +1462,21 @@ describe('Workflows', () => { access_token: '1234', expires_in: 3600, token_type: 'Bearer', - scope: 'flow', + scope: ['flow', 'flow:workflows'], }); nock('https://api.sandbox.checkout.com') .get('/workflows/events/subject/pay_ymhp72mhubcejmjjwcupzalm5e') .reply(401); - let cko = new Checkout( + const cko = new Checkout( '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', { client: 'ack_vvzhoai466su3j3vbxb47ts5oe', - scope: ['flow'], + scope: ['flow', 'flow:workflows'], environment: 'sandbox', } ); try { - let workflows = await cko.workflows.getSubjectEvents('pay_ymhp72mhubcejmjjwcupzalm5e'); + await cko.workflows.getSubjectEvents('pay_ymhp72mhubcejmjjwcupzalm5e'); } catch (err) { expect(err).to.be.instanceOf(AuthenticationError); } @@ -934,20 +1487,21 @@ describe('Workflows', () => { access_token: '1234', expires_in: 3600, token_type: 'Bearer', - scope: 'flow', + scope: ['flow', 'flow:workflows'], }); nock('https://api.sandbox.checkout.com') .post('/workflows/events/subject/pay_ymhp72mhubcejmjjwcupzalm5e/reflow') - .reply(201, {}); - let cko = new Checkout( + .reply(202); + const cko = new Checkout( '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', { client: 'ack_vvzhoai466su3j3vbxb47ts5oe', - scope: ['flow'], + scope: ['flow', 'flow:workflows'], environment: 'sandbox', } ); - let workflows = await cko.workflows.reflowBySubject('pay_ymhp72mhubcejmjjwcupzalm5e'); + const workflows = await cko.workflows.reflowBySubject('pay_ymhp72mhubcejmjjwcupzalm5e'); + expect(workflows).to.deep.equal({}); }); it('should throw AuthenticationError when reflowing by subject', async () => { @@ -955,21 +1509,21 @@ describe('Workflows', () => { access_token: '1234', expires_in: 3600, token_type: 'Bearer', - scope: 'flow', + scope: ['flow', 'flow:workflows'], }); nock('https://api.sandbox.checkout.com') .post('/workflows/events/subject/pay_ymhp72mhubcejmjjwcupzalm5e/reflow') .reply(401); - let cko = new Checkout( + const cko = new Checkout( '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', { client: 'ack_vvzhoai466su3j3vbxb47ts5oe', - scope: ['flow'], + scope: ['flow', 'flow:workflows'], environment: 'sandbox', } ); try { - let workflows = await cko.workflows.reflowBySubject('pay_ymhp72mhubcejmjjwcupzalm5e'); + await cko.workflows.reflowBySubject('pay_ymhp72mhubcejmjjwcupzalm5e'); } catch (err) { expect(err).to.be.instanceOf(AuthenticationError); } @@ -980,25 +1534,26 @@ describe('Workflows', () => { access_token: '1234', expires_in: 3600, token_type: 'Bearer', - scope: 'flow', + scope: ['flow', 'flow:workflows'], }); nock('https://api.sandbox.checkout.com') .post( '/workflows/events/subject/pay_ymhp72mhubcejmjjwcupzalm5e/workflow/wf_6p73pesh75vu7fqo6p6exhpe54/reflow' ) - .reply(201, {}); - let cko = new Checkout( + .reply(202); + const cko = new Checkout( '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', { client: 'ack_vvzhoai466su3j3vbxb47ts5oe', - scope: ['flow'], + scope: ['flow', 'flow:workflows'], environment: 'sandbox', } ); - let workflows = await cko.workflows.reflowBySubjectAndWorkflow( + const workflows = await cko.workflows.reflowBySubjectAndWorkflow( 'pay_ymhp72mhubcejmjjwcupzalm5e', 'wf_6p73pesh75vu7fqo6p6exhpe54' ); + expect(workflows).to.deep.equal({}); }); it('should throw AuthenticationError when reflowing by subject and workflow', async () => { @@ -1006,23 +1561,23 @@ describe('Workflows', () => { access_token: '1234', expires_in: 3600, token_type: 'Bearer', - scope: 'flow', + scope: ['flow', 'flow:workflows'], }); nock('https://api.sandbox.checkout.com') .post( '/workflows/events/subject/pay_ymhp72mhubcejmjjwcupzalm5e/workflow/wf_6p73pesh75vu7fqo6p6exhpe54/reflow' ) .reply(401); - let cko = new Checkout( + const cko = new Checkout( '2p7YQ37fHiRr8O6lQAikl8enICesB1dvAJrpmE2nZfEOpxzE-J_Gho7wDy0HY9951RfdUr0vSaQCzRKP0-o5Xg', { client: 'ack_vvzhoai466su3j3vbxb47ts5oe', - scope: ['flow'], + scope: ['flow', 'flow:workflows'], environment: 'sandbox', } ); try { - let workflows = await cko.workflows.reflowBySubjectAndWorkflow( + await cko.workflows.reflowBySubjectAndWorkflow( 'pay_ymhp72mhubcejmjjwcupzalm5e', 'wf_6p73pesh75vu7fqo6p6exhpe54' );