From 9b79bf1b24139a402202e9e5ffb23eb4e59eaf37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=B6ran=20Sander?= Date: Fri, 24 Nov 2023 21:29:18 +0000 Subject: [PATCH] build: Add unit tests for app-import command ... --- src/__tests__/app_export_cert.test.js | 1 - src/__tests__/app_import_cert.test.js | 81 ++++++++++++++++++++++++++ src/__tests__/app_import_jwt.test.js | 82 +++++++++++++++++++++++++++ src/lib/app/class_allapps.js | 5 +- src/lib/cmd/importapp.js | 1 + 5 files changed, 166 insertions(+), 4 deletions(-) create mode 100644 src/__tests__/app_import_cert.test.js create mode 100644 src/__tests__/app_import_jwt.test.js diff --git a/src/__tests__/app_export_cert.test.js b/src/__tests__/app_export_cert.test.js index aa35aa6..9bd2f0a 100644 --- a/src/__tests__/app_export_cert.test.js +++ b/src/__tests__/app_export_cert.test.js @@ -24,7 +24,6 @@ const options = { }; const defaultTestTimeout = process.env.CTRL_Q_TEST_TIMEOUT || 600000; // 10 minute default timeout -console.log(`Jest timeout: ${defaultTestTimeout}`); jest.setTimeout(defaultTestTimeout); // Test suite for app export diff --git a/src/__tests__/app_import_cert.test.js b/src/__tests__/app_import_cert.test.js new file mode 100644 index 0000000..f26fd6e --- /dev/null +++ b/src/__tests__/app_import_cert.test.js @@ -0,0 +1,81 @@ +/* eslint-disable no-console */ +const { test, expect, describe } = require('@jest/globals'); + +const { importAppFromFile } = require('../lib/cmd/importapp'); +const { appExistById, deleteAppById } = require('../lib/util/app'); + +const options = { + logLevel: process.env.CTRL_Q_LOG_LEVEL || 'info', + authType: process.env.CTRL_Q_AUTH_TYPE || 'cert', + authCertFile: process.env.CTRL_Q_AUTH_CERT_FILE || './cert/client.pem', + authCertKeyFile: process.env.CTRL_Q_AUTH_CERT_KEY_FILE || './cert/client_key.pem', + host: process.env.CTRL_Q_HOST || '', + port: process.env.CTRL_Q_PORT || '4242', + schemaVersion: process.env.CTRL_Q_SCHEMA_VERSION || '12.612.0', + virtualProxy: process.env.CTRL_Q_VIRTUAL_PROXY || '', + secure: process.env.CTRL_Q_SECURE || true, + authUserDir: process.env.CTRL_Q_AUTH_USER_DIR || '', + authUserId: process.env.CTRL_Q_AUTH_USER_ID || '', + + sleepAppUpload: process.env.CTRL_Q_SLEEP_APP_UPLOAD || '500', + limitExportCount: process.env.CTRL_Q_LIMIT_EXPORT_COUNT || '0', +}; + +const defaultTestTimeout = process.env.CTRL_Q_TEST_TIMEOUT || 600000; // 10 minute default timeout +jest.setTimeout(defaultTestTimeout); + +options.fileType = 'excel'; +options.fileName = `./testdata/tasks.xlsx`; +options.sheetName = 'App import'; +options.port = '4242'; + +// Test suite for app export +describe('impor apps from QVF files (cert auth)', () => { + test('get tasks (verify parameters)', async () => { + expect(options.authCertFile).not.toHaveLength(0); + expect(options.authCertKeyFile).not.toHaveLength(0); + expect(options.host).not.toHaveLength(0); + expect(options.authUserDir).not.toHaveLength(0); + expect(options.authUserId).not.toHaveLength(0); + }); + + /** + * One tag, overwrite + * + * --file-name ./testdata/tasks.xlsx + * --sheet-name 'App import' + */ + test('import apps', async () => { + const result = await importAppFromFile(options); + // console.log(result); + + // Output should be an object + expect(typeof result).toBe('object'); + + // Verify that output contains at least one app + expect(result.appList.length).toBeGreaterThan(0); + + // Delete all created apps + for (let i = 0; i < result.appList.length; ) { + // id of uploaded app + const appId = result.appList[i].appComplete.createdAppId; + // console.log(`App ID: ${appId}`); + + // Check if app exists + // eslint-disable-next-line no-await-in-loop + const appExists = await appExistById(appId, options); + + if (appExists) { + // Delete app + // eslint-disable-next-line no-await-in-loop + const resultDelete = await deleteAppById(appId, options); + expect(resultDelete).toBe(true); + } else { + // App does not exist + expect(appExists).toBe(false); + // console.log(`App ${appId} does not exist in Sense. Skipping delete.`); + } + i += 1; + } + }); +}); diff --git a/src/__tests__/app_import_jwt.test.js b/src/__tests__/app_import_jwt.test.js new file mode 100644 index 0000000..b0a5b22 --- /dev/null +++ b/src/__tests__/app_import_jwt.test.js @@ -0,0 +1,82 @@ +/* eslint-disable no-console */ +const { test, expect, describe } = require('@jest/globals'); + +const { importAppFromFile } = require('../lib/cmd/importapp'); + +const options = { + logLevel: process.env.CTRL_Q_LOG_LEVEL || 'info', + authType: process.env.CTRL_Q_AUTH_TYPE || 'cert', + authCertFile: process.env.CTRL_Q_AUTH_CERT_FILE || './cert/client.pem', + authCertKeyFile: process.env.CTRL_Q_AUTH_CERT_KEY_FILE || './cert/client_key.pem', + host: process.env.CTRL_Q_HOST || '', + port: process.env.CTRL_Q_PORT || '4242', + schemaVersion: process.env.CTRL_Q_SCHEMA_VERSION || '12.612.0', + virtualProxy: process.env.CTRL_Q_VIRTUAL_PROXY || '', + secure: process.env.CTRL_Q_SECURE || true, + authUserDir: process.env.CTRL_Q_AUTH_USER_DIR || '', + authUserId: process.env.CTRL_Q_AUTH_USER_ID || '', + + sleepAppUpload: process.env.CTRL_Q_SLEEP_APP_UPLOAD || '500', + limitExportCount: process.env.CTRL_Q_LIMIT_EXPORT_COUNT || '0', + + authJwt: process.env.CTRL_Q_AUTH_JWT || '', +}; + +const defaultTestTimeout = process.env.CTRL_Q_TEST_TIMEOUT || 600000; // 10 minute default timeout +jest.setTimeout(defaultTestTimeout); + +options.fileType = 'excel'; +options.fileName = `./testdata/tasks.xlsx`; +options.sheetName = 'App import'; +options.authType = 'jwt'; +options.port = '443'; +options.virtualProxy = 'jwt'; + +// Test suite for app export +describe('impor apps from QVF files (cert auth)', () => { + test('get tasks (verify parameters)', async () => { + expect(options.host).not.toHaveLength(0); + expect(options.authUserDir).not.toHaveLength(0); + expect(options.authUserId).not.toHaveLength(0); + }); + + /** + * One tag, overwrite + * + * --file-name ./testdata/tasks.xlsx + * --sheet-name 'App import' + */ + test('import apps', async () => { + const result = await importAppFromFile(options); + // console.log(result); + + // Output should be an object + expect(typeof result).toBe('object'); + + // Verify that output contains at least one app + expect(result.appList.length).toBeGreaterThan(0); + + // Delete all created apps + for (let i = 0; i < result.appList.length; ) { + // id of uploaded app + const appId = result.appList[i].appComplete.createdAppId; + // console.log(`App ID: ${appId}`); + + // Check if app exists + // eslint-disable-next-line no-await-in-loop + const appExists = await appExistById(appId, options); + + if (appExists) { + // Delete app + // eslint-disable-next-line no-await-in-loop + const resultDelete = await deleteAppById(appId, options); + expect(resultDelete).toBe(true); + } else { + // App does not exist + expect(appExists).toBe(false); + // console.log(`App ${appId} does not exist in Sense. Skipping delete.`); + } + i += 1; + } + }); +}); diff --git a/src/lib/app/class_allapps.js b/src/lib/app/class_allapps.js index 548ccf5..0f611ed 100644 --- a/src/lib/app/class_allapps.js +++ b/src/lib/app/class_allapps.js @@ -646,7 +646,7 @@ class QlikSenseApps { // Pause for a while to let Sense repository catch up await sleep(1000); - // console.log(this.options) + // Should cerrificates be used for authentication? let axiosConfig2; if (this.options.authType === 'cert') { @@ -665,9 +665,8 @@ class QlikSenseApps { body: app, }); } - // console.log(axiosConfig2) const result2 = await axios.request(axiosConfig2); - // console.log('b1') + if (result2.status === 200) { logger.debug(`Update of imported app wrt tags, custom properties and owner was successful.`); return true; diff --git a/src/lib/cmd/importapp.js b/src/lib/cmd/importapp.js index f2093de..adb7348 100644 --- a/src/lib/cmd/importapp.js +++ b/src/lib/cmd/importapp.js @@ -83,6 +83,7 @@ const importAppFromFile = async (options) => { return false; } catch (err) { logger.error(`IMPORT APP: ${err.stack}`); + return false; } };