Skip to content

Commit

Permalink
chore: Add unit test for qscloud connection-test command
Browse files Browse the repository at this point in the history
  • Loading branch information
mountaindude committed Dec 15, 2024
1 parent e78fabc commit 5e53a96
Show file tree
Hide file tree
Showing 23 changed files with 97 additions and 32 deletions.
65 changes: 65 additions & 0 deletions src/__tests__/cmd/qscloud/connection_test_cloud.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { jest, test, expect, describe } from '@jest/globals';
import { qscloudTestConnection } from '../../../lib/cmd/qscloud/testconnection.js';

const options = {
logLevel: process.env.CTRL_Q_LOG_LEVEL || 'info',
tenantHost: process.env.CTRLQ_TENANT_HOST || '',
authType: process.env.CTRLQ_AUTH_TYPE || 'apikey',
apikey: process.env.CTRLQ_API_KEY || '',
};

const defaultTestTimeout = process.env.CTRL_Q_TEST_TIMEOUT || 120000; // 2 minute default timeout
console.log(`Jest timeout: ${defaultTestTimeout}`);
jest.setTimeout(defaultTestTimeout);

// Connection test using JWT auth
describe('connection test (JWT auth)', () => {
test('Verify parameters', async () => {
expect(options.logLevel).not.toHaveLength(0);
expect(options.tenantHost).not.toHaveLength(0);
expect(options.authType).toBe('apikey');
expect(options.apikey).not.toHaveLength(0);

// Check if the API key is a valid JWT
try {
const decoded = JSON.parse(Buffer.from(options.apikey.split('.')[1], 'base64').toString('utf8'));
expect(decoded).toBeInstanceOf(Object);

expect(decoded.aud).toBe('qlik.api');
expect(decoded.iss).toBe('qlik.api/api-keys');

// JTI should be a UUID
expect(decoded.jti).not.toHaveLength(0);
expect(decoded.jti).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/);

expect(decoded.sub).not.toHaveLength(0);
expect(decoded.subType).toBe('user');

expect(decoded.tenantId).not.toHaveLength(0);
} catch (error) {
expect(error).toBeInstanceOf(Error);
expect(error.message).toBe('Invalid API key');
}
});

/**
* Do connection test
* Should succeed
*/
test('do connection test', async () => {
const result = await qscloudTestConnection(options);

const decoded = JSON.parse(Buffer.from(options.apikey.split('.')[1], 'base64').toString('utf8'));

// Result should be a JSON object
expect(result).toBeInstanceOf(Object);
expect(result.tenantId).toBe(decoded.tenantId);

expect(result.id).not.toHaveLength(0);
expect(result.id).toBe(decoded.sub);

expect(result.name).not.toHaveLength(0);
expect(result.email).not.toHaveLength(0);
expect(result.status).not.toHaveLength(0);
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { jest, test, expect, describe } from '@jest/globals';

import { createUserActivityBucketsCustomProperty } from '../../lib/cmd/qseow/createuseractivitycp.js';
import { createUserActivityBucketsCustomProperty } from '../../../lib/cmd/qseow/createuseractivitycp.js';

const options = {
logLevel: process.env.CTRL_Q_LOG_LEVEL || 'info',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-console */
import { jest, test, expect, describe } from '@jest/globals';

import { getApps, getAppById } from '../../lib/util/qseow/app.js';
import { getApps, getAppById } from '../../../lib/util/qseow/app.js';

const options = {
logLevel: process.env.CTRL_Q_LOG_LEVEL || 'info',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { jest, test, expect, describe } from '@jest/globals';

import fs from 'node:fs';
import path from 'node:path';
import { exportAppToFile } from '../../lib/cmd/qseow/exportapp.js';
import { exportAppToFile } from '../../../lib/cmd/qseow/exportapp.js';

const options = {
logLevel: process.env.CTRL_Q_LOG_LEVEL || 'info',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { jest, test, expect, describe } from '@jest/globals';

import fs from 'node:fs';
import path from 'node:path';
import { exportAppToFile } from '../../lib/cmd/qseow/exportapp.js';
import { exportAppToFile } from '../../../lib/cmd/qseow/exportapp.js';

const options = {
logLevel: process.env.CTRL_Q_LOG_LEVEL || 'info',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable no-console */
import { jest, test, expect, describe } from '@jest/globals';

import { importAppFromFile } from '../../lib/cmd/qseow/importapp.js';
import { appExistById, deleteAppById } from '../../lib/util/qseow/app.js';
import { importAppFromFile } from '../../../lib/cmd/qseow/importapp.js';
import { appExistById, deleteAppById } from '../../../lib/util/qseow/app.js';

const options = {
logLevel: process.env.CTRL_Q_LOG_LEVEL || 'info',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable no-console */
import { jest, test, expect, describe } from '@jest/globals';

import { importAppFromFile } from '../../lib/cmd/qseow/importapp.js';
import { appExistById, deleteAppById } from '../../lib/util/qseow/app.js';
import { importAppFromFile } from '../../../lib/cmd/qseow/importapp.js';
import { appExistById, deleteAppById } from '../../../lib/util/qseow/app.js';

const options = {
logLevel: process.env.CTRL_Q_LOG_LEVEL || 'info',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint-disable no-console */
import { jest, test, expect, describe } from '@jest/globals';

import { getApps, getAppById, appExistById, deleteAppById } from '../../lib/util/qseow/app.js';
import { importAppFromFile } from '../../lib/cmd/qseow/importapp.js';
import { sleep } from '../../globals.js';
import { getApps, getAppById, appExistById, deleteAppById } from '../../../lib/util/qseow/app.js';
import { importAppFromFile } from '../../../lib/cmd/qseow/importapp.js';
import { sleep } from '../../../globals.js';

const options = {
logLevel: process.env.CTRL_Q_LOG_LEVEL || 'info',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { jest, test, expect, describe } from '@jest/globals';
import { validate as uuidValidate } from 'uuid';

import { scrambleField } from '../../lib/cmd/qseow/scramblefield.js';
import { scrambleField } from '../../../lib/cmd/qseow/scramblefield.js';

let options = {
logLevel: process.env.CTRL_Q_LOG_LEVEL || 'info',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-console */
import { jest, test, expect, describe } from '@jest/globals';

import { getBookmark } from '../../lib/cmd/qseow/getbookmark.js';
import { getBookmark } from '../../../lib/cmd/qseow/getbookmark.js';

const options = {
logLevel: process.env.CTRL_Q_LOG_LEVEL || 'info',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { jest, test, expect, describe } from '@jest/globals';

import { getBookmark } from '../../lib/cmd/qseow/getbookmark.js';
import { getBookmark } from '../../../lib/cmd/qseow/getbookmark.js';

const options = {
logLevel: process.env.CTRL_Q_LOG_LEVEL || 'info',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { jest, test, expect, describe } from '@jest/globals';

import { testConnection } from '../../lib/cmd/qseow/testconnection.js';
import { testConnection } from '../../../lib/cmd/qseow/testconnection.js';

const options = {
logLevel: process.env.CTRL_Q_LOG_LEVEL || 'info',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { jest, test, expect, describe } from '@jest/globals';

import { testConnection } from '../../lib/cmd/qseow/testconnection.js';
import { testConnection } from '../../../lib/cmd/qseow/testconnection.js';

const options = {
logLevel: process.env.CTRL_Q_LOG_LEVEL || 'info',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { jest, test, expect, describe } from '@jest/globals';

import { getScript } from '../../lib/cmd/qseow/getscript.js';
import { getScript } from '../../../lib/cmd/qseow/getscript.js';

const options = {
logLevel: process.env.CTRL_Q_LOG_LEVEL || 'info',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { jest, test, expect, describe } from '@jest/globals';

import { getScript } from '../../lib/cmd/qseow/getscript.js';
import { getScript } from '../../../lib/cmd/qseow/getscript.js';

const options = {
logLevel: process.env.CTRL_Q_LOG_LEVEL || 'info',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { jest, test, expect, describe } from '@jest/globals';

import { taskExistById, getTaskByName, getTaskById } from '../../lib/util/qseow/task.js';
import { taskExistById, getTaskByName, getTaskById } from '../../../lib/util/qseow/task.js';

const options = {
logLevel: process.env.CTRL_Q_LOG_LEVEL || 'info',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { jest, test, expect, describe } from '@jest/globals';

import { setTaskCustomProperty } from '../../lib/cmd/qseow/settaskcp.js';
import { getTaskById } from '../../lib/util/qseow/task.js';
import { setTaskCustomProperty } from '../../../lib/cmd/qseow/settaskcp.js';
import { getTaskById } from '../../../lib/util/qseow/task.js';

const options = {
logLevel: process.env.CTRL_Q_LOG_LEVEL || 'info',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { jest, test, expect, describe } from '@jest/globals';

import { setTaskCustomProperty } from '../../lib/cmd/qseow/settaskcp.js';
import { getTaskById } from '../../lib/util/qseow/task.js';
import { setTaskCustomProperty } from '../../../lib/cmd/qseow/settaskcp.js';
import { getTaskById } from '../../../lib/util/qseow/task.js';

const options = {
logLevel: process.env.CTRL_Q_LOG_LEVEL || 'info',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { jest, test, expect, describe } from '@jest/globals';

import fs from 'node:fs';
import path from 'node:path';
import { getTask } from '../../lib/cmd/qseow/gettask.js';
import { getTask } from '../../../lib/cmd/qseow/gettask.js';

const options = {
logLevel: process.env.CTRL_Q_LOG_LEVEL || 'info',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { jest, test, expect, describe } from '@jest/globals';

import fs from 'node:fs';
import path from 'node:path';
import { getTask } from '../../lib/cmd/qseow/gettask.js';
import { getTask } from '../../../lib/cmd/qseow/gettask.js';

const options = {
logLevel: process.env.CTRL_Q_LOG_LEVEL || 'info',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { jest, test, expect, describe } from '@jest/globals';

import { importTaskFromFile } from '../../lib/cmd/qseow/importtask.js';
import { getTaskById, deleteExternalProgramTaskById, deleteReloadTaskById } from '../../lib/util/qseow/task.js';
import { mapTaskType } from '../../lib/util/qseow/lookups.js';
import { importTaskFromFile } from '../../../lib/cmd/qseow/importtask.js';
import { getTaskById, deleteExternalProgramTaskById, deleteReloadTaskById } from '../../../lib/util/qseow/task.js';
import { mapTaskType } from '../../../lib/util/qseow/lookups.js';

const options = {
logLevel: process.env.CTRL_Q_LOG_LEVEL || 'info',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { jest, test, expect, describe } from '@jest/globals';

import { importTaskFromFile } from '../../lib/cmd/qseow/importtask.js';
import { getTaskById, deleteExternalProgramTaskById, deleteReloadTaskById } from '../../lib/util/qseow/task.js';
import { mapTaskType } from '../../lib/util/qseow/lookups.js';
import { importTaskFromFile } from '../../../lib/cmd/qseow/importtask.js';
import { getTaskById, deleteExternalProgramTaskById, deleteReloadTaskById } from '../../../lib/util/qseow/task.js';
import { mapTaskType } from '../../../lib/util/qseow/lookups.js';

const options = {
logLevel: process.env.CTRL_Q_LOG_LEVEL || 'info',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { jest, test, expect, describe } from '@jest/globals';

import { taskExistById, getTaskByName, getTaskById } from '../../lib/util/qseow/task.js';
import { taskExistById, getTaskByName, getTaskById } from '../../../lib/util/qseow/task.js';

const options = {
logLevel: process.env.CTRL_Q_LOG_LEVEL || 'info',
Expand Down

0 comments on commit 5e53a96

Please sign in to comment.