From 3028e243f890fab0a581cc3eb45ab2835de60f3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Berkefeld?= Date: Wed, 18 Jan 2023 16:27:10 +0100 Subject: [PATCH] #638: ensure token requests are not counted --- test/dataExtension.test.js | 20 ++++++++++---------- test/query.test.js | 16 ++++++++-------- test/transactionalEmail.test.js | 12 ++++++------ test/transactionalPush.test.js | 14 ++++++++------ test/transactionalSMS.test.js | 12 ++++++------ test/utils.js | 24 ++++++++++++++++++++++++ 6 files changed, 62 insertions(+), 36 deletions(-) diff --git a/test/dataExtension.test.js b/test/dataExtension.test.js index 6e3c1320d..6900de377 100644 --- a/test/dataExtension.test.js +++ b/test/dataExtension.test.js @@ -29,9 +29,9 @@ describe('dataExtension', () => { 'returned metadata was not equal expected' ); assert.equal( - Object.values(testUtils.getAPIHistory()).flat().length, - 6, - 'Unexpected number of requests made' + testUtils.getAPIHistoryLength(), + 5, + 'Unexpected number of requests made. Run testUtils.getAPIHistoryDebug() to see the requests' ); return; }); @@ -65,9 +65,9 @@ describe('dataExtension', () => { 'returned metadata was not equal expected for update' ); assert.equal( - Object.values(testUtils.getAPIHistory()).flat().length, - 12, - 'Unexpected number of requests made' + testUtils.getAPIHistoryLength(), + 11, + 'Unexpected number of requests made. Run testUtils.getAPIHistoryDebug() to see the requests' ); return; }); @@ -112,9 +112,9 @@ describe('dataExtension', () => { 'returned deployment file was not equal expected' ); assert.equal( - Object.values(testUtils.getAPIHistory()).flat().length, + testUtils.getAPIHistoryLength(), 5, - 'Unexpected number of requests made' + 'Unexpected number of requests made. Run testUtils.getAPIHistoryDebug() to see the requests' ); return; }); @@ -159,9 +159,9 @@ describe('dataExtension', () => { 'returned deployment file was not equal expected' ); assert.equal( - Object.values(testUtils.getAPIHistory()).flat().length, + testUtils.getAPIHistoryLength(), 5, - 'Unexpected number of requests made' + 'Unexpected number of requests made. Run testUtils.getAPIHistoryDebug() to see the requests' ); return; }); diff --git a/test/query.test.js b/test/query.test.js index 76f4682fa..9b35ae11b 100644 --- a/test/query.test.js +++ b/test/query.test.js @@ -38,9 +38,9 @@ describe('query', () => { file(testUtils.getExpectedFile('9999999', 'query', 'get', 'sql')) ); assert.equal( - Object.values(testUtils.getAPIHistory()).flat().length, + testUtils.getAPIHistoryLength(), 6, - 'Unexpected number of requests made' + 'Unexpected number of requests made. Run testUtils.getAPIHistoryDebug() to see the requests' ); return; }); @@ -80,9 +80,9 @@ describe('query', () => { ); // check number of API calls assert.equal( - Object.values(testUtils.getAPIHistory()).flat().length, + testUtils.getAPIHistoryLength(), 8, - 'Unexpected number of requests made' + 'Unexpected number of requests made. Run testUtils.getAPIHistoryDebug() to see the requests' ); return; }); @@ -126,9 +126,9 @@ describe('query', () => { file(testUtils.getActualDeployFile('testExistingQuery', 'query', 'sql')) ).to.equal(file(testUtils.getExpectedFile('9999999', 'query', 'build', 'sql'))); assert.equal( - Object.values(testUtils.getAPIHistory()).flat().length, + testUtils.getAPIHistoryLength(), 6, - 'Unexpected number of requests made' + 'Unexpected number of requests made. Run testUtils.getAPIHistoryDebug() to see the requests' ); return; }); @@ -173,9 +173,9 @@ describe('query', () => { ).to.equal(file(testUtils.getExpectedFile('9999999', 'query', 'build', 'sql'))); assert.equal( - Object.values(testUtils.getAPIHistory()).flat().length, + testUtils.getAPIHistoryLength(), 6, - 'Unexpected number of requests made' + 'Unexpected number of requests made. Run testUtils.getAPIHistoryDebug() to see the requests' ); return; }); diff --git a/test/transactionalEmail.test.js b/test/transactionalEmail.test.js index 489535791..0598a4067 100644 --- a/test/transactionalEmail.test.js +++ b/test/transactionalEmail.test.js @@ -32,9 +32,9 @@ describe('transactionalEmail', () => { 'returned JSON was not equal expected' ); assert.equal( - Object.values(testUtils.getAPIHistory()).flat().length, + testUtils.getAPIHistoryLength(), 12, - 'Unexpected number of requests made' + 'Unexpected number of requests made. Run testUtils.getAPIHistoryDebug() to see the requests' ); return; }); @@ -68,9 +68,9 @@ describe('transactionalEmail', () => { ); // check number of API calls assert.equal( - Object.values(testUtils.getAPIHistory()).flat().length, + testUtils.getAPIHistoryLength(), 13, - 'Unexpected number of requests made' + 'Unexpected number of requests made. Run testUtils.getAPIHistoryDebug() to see the requests' ); return; }); @@ -110,9 +110,9 @@ describe('transactionalEmail', () => { 'returned deployment JSON was not equal expected' ); assert.equal( - Object.values(testUtils.getAPIHistory()).flat().length, + testUtils.getAPIHistoryLength(), 12, - 'Unexpected number of requests made' + 'Unexpected number of requests made. Run testUtils.getAPIHistoryDebug() to see the requests' ); return; }); diff --git a/test/transactionalPush.test.js b/test/transactionalPush.test.js index 4fa770568..49d008a09 100644 --- a/test/transactionalPush.test.js +++ b/test/transactionalPush.test.js @@ -32,9 +32,9 @@ describe('transactionalPush', () => { 'returned JSON was not equal expected' ); assert.equal( - Object.values(testUtils.getAPIHistory()).flat().length, + testUtils.getAPIHistoryLength(), 3, - 'Unexpected number of requests made' + 'Unexpected number of requests made. Run testUtils.getAPIHistoryDebug() to see the requests' ); return; }); @@ -68,9 +68,10 @@ describe('transactionalPush', () => { ); // check number of API calls assert.equal( - Object.values(testUtils.getAPIHistory()).flat().length, + testUtils.getAPIHistoryLength(), 4, - 'Unexpected number of requests made' + 'Unexpected number of requests made. Run testUtils.getAPIHistoryDebug() to see the requests', + testUtils.getAPIHistoryDebug() ); return; }); @@ -110,9 +111,10 @@ describe('transactionalPush', () => { 'returned deployment JSON was not equal expected' ); assert.equal( - Object.values(testUtils.getAPIHistory()).flat().length, + testUtils.getAPIHistoryLength(), 3, - 'Unexpected number of requests made' + 'Unexpected number of requests made. Run testUtils.getAPIHistoryDebug() to see the requests', + testUtils.getAPIHistoryDebug() ); return; }); diff --git a/test/transactionalSMS.test.js b/test/transactionalSMS.test.js index 79a6d28f4..7c6c48847 100644 --- a/test/transactionalSMS.test.js +++ b/test/transactionalSMS.test.js @@ -39,9 +39,9 @@ describe('transactionalSMS', () => { file(testUtils.getExpectedFile('9999999', 'transactionalSMS', 'get', 'amp')) ); assert.equal( - Object.values(testUtils.getAPIHistory()).flat().length, + testUtils.getAPIHistoryLength(), 4, - 'Unexpected number of requests made' + 'Unexpected number of requests made. Run testUtils.getAPIHistoryDebug() to see the requests' ); return; }); @@ -85,9 +85,9 @@ describe('transactionalSMS', () => { ); // check number of API calls assert.equal( - Object.values(testUtils.getAPIHistory()).flat().length, + testUtils.getAPIHistoryLength(), 5, - 'Unexpected number of requests made' + 'Unexpected number of requests made. Run testUtils.getAPIHistoryDebug() to see the requests' ); return; }); @@ -139,9 +139,9 @@ describe('transactionalSMS', () => { file(testUtils.getExpectedFile('9999999', 'transactionalSMS', 'build', 'amp')) ); assert.equal( - Object.values(testUtils.getAPIHistory()).flat().length, + testUtils.getAPIHistoryLength(), 4, - 'Unexpected number of requests made' + 'Unexpected number of requests made. Run testUtils.getAPIHistoryDebug() to see the requests' ); return; }); diff --git a/test/utils.js b/test/utils.js index d2d1ab5c6..86b2c73e3 100644 --- a/test/utils.js +++ b/test/utils.js @@ -138,12 +138,36 @@ exports.mockReset = () => { fsmock.restore(); apimock.restore(); }; +/** + * helper to return amount of api callouts + * + * @param {boolean} [includeToken] if true, will include token calls in count + * @returns {object} of API history + */ +exports.getAPIHistoryLength = (includeToken) => { + const historyArr = Object.values(apimock.history).flat(); + if (includeToken) { + return historyArr.length; + } + return historyArr.filter((item) => item.url !== '/v2/token').length; +}; /** * helper to return api history * * @returns {object} of API history */ exports.getAPIHistory = () => apimock.history; +/** + * helper to return most important fields for each api call + * + * @returns {object} of API history + */ +exports.getAPIHistoryDebug = () => { + const historyArr = Object.values(apimock.history) + .flat() + .map((item) => ({ url: item.url, data: item.data })); + return historyArr; +}; /** * escapes string for regex