Skip to content

Commit

Permalink
build: Add unit tests for app-import command
Browse files Browse the repository at this point in the history
...
  • Loading branch information
Göran Sander committed Nov 24, 2023
1 parent 42fd155 commit 9b79bf1
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 4 deletions.
1 change: 0 additions & 1 deletion src/__tests__/app_export_cert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
81 changes: 81 additions & 0 deletions src/__tests__/app_import_cert.test.js
Original file line number Diff line number Diff line change
@@ -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;
}
});
});
82 changes: 82 additions & 0 deletions src/__tests__/app_import_jwt.test.js
Original file line number Diff line number Diff line change
@@ -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;
}
});
});
5 changes: 2 additions & 3 deletions src/lib/app/class_allapps.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/lib/cmd/importapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ const importAppFromFile = async (options) => {
return false;
} catch (err) {
logger.error(`IMPORT APP: ${err.stack}`);
return false;
}
};

Expand Down

0 comments on commit 9b79bf1

Please sign in to comment.