Skip to content

Commit

Permalink
unit test case for audit workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
cs-raj committed Feb 27, 2024
1 parent 9498841 commit 25990f0
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/contentstack-audit/src/audit-base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export abstract class AuditBaseCommand extends BaseCommand<typeof AuditBaseComma
!isEmpty(missingCtRefs) ||
!isEmpty(missingGfRefs) ||
!isEmpty(missingEntryRefs) ||
isEmpty(missingCtRefsInWorkflow)
!isEmpty(missingCtRefsInWorkflow)
) {
if (this.currentCommand === 'cm:stacks:audit') {
this.log(this.$t(auditMsg.FINAL_REPORT_PATH, { path: this.sharedConfig.reportPath }), 'warn');
Expand Down
5 changes: 1 addition & 4 deletions packages/contentstack-audit/src/modules/workflows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ export default class Workflows {
this.workflowSchema = existsSync(this.workflowPath)
? values(JSON.parse(readFileSync(this.workflowPath, 'utf8')) as Workflow[])
: [];
this.ctSchema = [];
this.ctSchema.forEach((ct) => this.ctUidSet.add(ct.uid));

this.workflowSchema.forEach((workflow: Workflow) => {
let ctNotPresent: string[] = [];
workflow.content_types.forEach((ct) => {
Expand Down Expand Up @@ -119,8 +117,7 @@ export default class Workflows {
}
}
}

this.writeFixContent(newWorkflowSchema);
await this.writeFixContent(newWorkflowSchema);
}

async writeFixContent(newWorkflowSchema: Record<string, Workflow>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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([]);
Expand All @@ -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'] });
})
Expand All @@ -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', () => {})
Expand Down
148 changes: 148 additions & 0 deletions packages/contentstack-audit/test/unit/modules/workflow.test.ts
Original file line number Diff line number Diff line change
@@ -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<string, Workflow>;
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<string, Workflow>) {
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,
},
});
});
});
});

0 comments on commit 25990f0

Please sign in to comment.