From 25990f0b85e01af3c2c83141635a5860f0b090d0 Mon Sep 17 00:00:00 2001 From: raj pandey Date: Tue, 27 Feb 2024 13:19:17 +0530 Subject: [PATCH] unit test case for audit workflow --- .../src/audit-base-command.ts | 2 +- .../src/modules/workflows.ts | 5 +- .../test/unit/audit-base-command.test.ts | 5 +- .../test/unit/modules/workflow.test.ts | 148 ++++++++++++++++++ 4 files changed, 154 insertions(+), 6 deletions(-) create mode 100644 packages/contentstack-audit/test/unit/modules/workflow.test.ts diff --git a/packages/contentstack-audit/src/audit-base-command.ts b/packages/contentstack-audit/src/audit-base-command.ts index 98f0fbbf69..44662e44ed 100644 --- a/packages/contentstack-audit/src/audit-base-command.ts +++ b/packages/contentstack-audit/src/audit-base-command.ts @@ -56,7 +56,7 @@ export abstract class AuditBaseCommand extends BaseCommand this.ctUidSet.add(ct.uid)); - this.workflowSchema.forEach((workflow: Workflow) => { let ctNotPresent: string[] = []; workflow.content_types.forEach((ct) => { @@ -119,8 +117,7 @@ export default class Workflows { } } } - - this.writeFixContent(newWorkflowSchema); + await this.writeFixContent(newWorkflowSchema); } async writeFixContent(newWorkflowSchema: Record) { diff --git a/packages/contentstack-audit/test/unit/audit-base-command.test.ts b/packages/contentstack-audit/test/unit/audit-base-command.test.ts index 81301e8997..fc3a665d74 100644 --- a/packages/contentstack-audit/test/unit/audit-base-command.test.ts +++ b/packages/contentstack-audit/test/unit/audit-base-command.test.ts @@ -7,7 +7,7 @@ import { expect } from '@oclif/test'; import { ux, cliux } from '@contentstack/cli-utilities'; import { AuditBaseCommand } from '../../src/audit-base-command'; -import { ContentType, Entries, GlobalField } from '../../src/modules'; +import { ContentType, Entries, GlobalField, Workflows } from '../../src/modules'; import { FileTransportInstance } from 'winston/lib/winston/transports'; import { $t, auditMsg } from '../../src/messages'; @@ -61,6 +61,7 @@ describe('AuditBaseCommand class', () => { .stub(Entries.prototype, 'run', () => ({ entry_1: {} })) .stub(ContentType.prototype, 'run', () => ({ ct_1: {} })) .stub(GlobalField.prototype, 'run', () => ({ gf_1: {} })) + .stub(Workflows.prototype, 'run', () => ({ wf_1: {} })) .stub(fs, 'createWriteStream', () => new PassThrough()) .it('should print info of no ref found', async (ctx) => { await AuditCMD.run([]); @@ -75,6 +76,7 @@ describe('AuditBaseCommand class', () => { .stub(winston, 'createLogger', () => ({ log: console.log, error: console.error })) .stub(fs, 'mkdirSync', () => {}) .stub(fs, 'writeFileSync', () => {}) + .stub(AuditBaseCommand.prototype, 'showOutputOnScreenWorkflowsAndExtension', () => {}) .stub(ux, 'table', (...args: any) => { args[1].missingRefs.get({ missingRefs: ['gf_0'] }); }) @@ -91,6 +93,7 @@ describe('AuditBaseCommand class', () => { })) .stub(ContentType.prototype, 'run', () => ({ ct_1: {} })) .stub(GlobalField.prototype, 'run', () => ({ gf_1: {} })) + .stub(Workflows.prototype, 'run', () => ({ wf_1: {} })) .stub(fs, 'createBackUp', () => {}) .stub(fs, 'createWriteStream', () => new PassThrough()) .stub(AuditBaseCommand.prototype, 'createBackUp', () => {}) diff --git a/packages/contentstack-audit/test/unit/modules/workflow.test.ts b/packages/contentstack-audit/test/unit/modules/workflow.test.ts new file mode 100644 index 0000000000..cb382323e7 --- /dev/null +++ b/packages/contentstack-audit/test/unit/modules/workflow.test.ts @@ -0,0 +1,148 @@ +import fs from 'fs'; +import { resolve } from 'path'; +import { fancy } from 'fancy-test'; +import { expect } from '@oclif/test'; +import cloneDeep from 'lodash/cloneDeep'; +import { ux } from '@contentstack/cli-utilities'; + +import config from '../../../src/config'; +import { Workflows } from '../../../src/modules'; +import { $t, auditMsg } from '../../../src/messages'; +import { values } from 'lodash'; +import { Workflow } from '../../../src/types'; +import { join } from 'path'; + +describe('Workflows', () => { + describe('run method with invalid path for workflows', () => { + const wf = new Workflows({ + log: () => {}, + moduleName: 'workflows', + ctSchema: cloneDeep(require('./../mock/contents/workflows/ctSchema.json')), + config: Object.assign(config, { basePath: resolve(__dirname, '..', 'mock', 'workflows'), flags: {} }), + }); + fancy + .stdout({ print: process.env.PRINT === 'true' || false }) + .stub(ux, 'confirm', async () => true) + .it('Should Validate the base path for workflows', async () => { + try { + await wf.run(); + } catch (error: any) { + expect(error).to.be.instanceOf(Error); + expect(error.message).to.eql($t(auditMsg.NOT_VALID_PATH, { path: wf.folderPath })); + } + }); + }); + describe('run method with valid path for workflows and ctSchema', () => { + const wf = new Workflows({ + log: () => {}, + moduleName: 'workflows', + ctSchema: cloneDeep(require('./../mock/contents/workflows/ctSchema.json')), + config: Object.assign(config, { + basePath: resolve(`./test/unit/mock/contents/`), + flags: {}, + }), + }); + + fancy + .stdout({ print: process.env.PRINT === 'true' || true }) + .stub(ux, 'confirm', async () => true) + .it( + 'should expect missingRefs equal to empty array, expect entire workflow schema and empty missingCts', + async () => { + const missingRefs = await wf.run(); + expect(wf.workflowSchema).eql(values(JSON.parse(fs.readFileSync(wf.workflowPath, 'utf8')))); + expect(missingRefs).eql([]); + expect(wf.missingCts).eql(new Set([])); + }, + ); + }); + + describe('run method with valid path and empty ctSchema to check the missing references', () => { + const wf = new Workflows({ + log: () => {}, + moduleName: 'workflows', + ctSchema: [], + config: Object.assign(config, { + basePath: resolve(`./test/unit/mock/contents/`), + flags: {}, + }), + }); + + fancy + .stdout({ print: process.env.PRINT === 'true' || true }) + .stub(ux, 'confirm', async () => true) + .it('should expect missingRefs equal to all workflows', async () => { + const missingRefs = await wf.run(); + expect(missingRefs).eql(wf.workflowSchema); + }); + }); + + describe('run method with audit fix for workflows with valid path and empty ctSchema', () => { + const wf = new Workflows({ + log: () => {}, + moduleName: 'workflows', + ctSchema: [], + config: Object.assign(config, { + basePath: resolve(`./test/unit/mock/contents/`), + flags: {}, + }), + fix: true, + }); + + fancy + .stdout({ print: process.env.PRINT === 'true' || true }) + .stub(wf, 'fixWorkflowSchema', async () => {}) + .stub(wf, 'writeFixContent', async () => {}) + .stub(ux, 'confirm', async () => true) + .it('the run function should run and flow should go till fixWorkflowSchema', async () => { + await wf.run(); + }); + + fancy + .stdout({ print: process.env.PRINT === 'true' || true }) + .stub(wf, 'writeFixContent', () => {}) + .stub(ux, 'confirm', async () => true) + .it('should expect fixWorkflow schema to run', async () => { + const wfInstance = new (class Class extends Workflows { + public newWorkflowSchema!: Record; + constructor() { + super({ + log: () => {}, + moduleName: 'workflows', + ctSchema: [], + config: Object.assign(config, { + basePath: resolve(`./test/unit/mock/contents/`), + flags: {}, + }), + fix: true, + }); + this.workflowPath = join(this.folderPath, this.fileName); + } + async writeFixContent(WorkflowSchema: Record) { + this.newWorkflowSchema = WorkflowSchema; + } + })(); + // console.log(JSON.stringify(wfInstance)); + await wfInstance.fixWorkflowSchema(); + expect(wfInstance.newWorkflowSchema).eql(JSON.parse(fs.readFileSync(wfInstance.workflowPath, 'utf8'))); + }); + + fancy + .stdout({ print: process.env.PRINT === 'true' || true }) + .stub(wf, 'writeFileSync', () => {}) + .stub(ux, 'confirm', async () => true) + .it('expect the format for the fixed Workflow schema that only contains the ct that is present', async () => { + return await wf.writeFixContent({ + id: { + name: 'wf3', + uid: 'uid', + org_uid: 'org1', + api_key: 'apikey', + content_types: ['ct2'], + enabled: false, + deleted_at: false, + }, + }); + }); + }); +});