From 64f356ed0987b8b11a828ffcab58f3861e05cc6d Mon Sep 17 00:00:00 2001 From: Vikram Kalta Date: Wed, 5 Mar 2025 10:12:05 +0000 Subject: [PATCH] test: added test cases for custom roles --- .../contentstack-audit/src/modules/index.ts | 3 +- .../contents/custom-roles/custom-roles.json | 84 +++++++++++++++++++ .../test/unit/modules/custom-roles.test.ts | 67 +++++++++++++++ 3 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 packages/contentstack-audit/test/unit/mock/contents/custom-roles/custom-roles.json create mode 100644 packages/contentstack-audit/test/unit/modules/custom-roles.test.ts diff --git a/packages/contentstack-audit/src/modules/index.ts b/packages/contentstack-audit/src/modules/index.ts index 35f598eacf..8df74f7cf2 100644 --- a/packages/contentstack-audit/src/modules/index.ts +++ b/packages/contentstack-audit/src/modules/index.ts @@ -3,4 +3,5 @@ import GlobalField from './global-fields'; import ContentType from './content-types'; import Workflows from './workflows'; import Extensions from './extensions'; -export { Entries, GlobalField, ContentType, Workflows, Extensions }; +import CustomRoles from './custom-roles'; +export { Entries, GlobalField, ContentType, Workflows, Extensions, CustomRoles }; diff --git a/packages/contentstack-audit/test/unit/mock/contents/custom-roles/custom-roles.json b/packages/contentstack-audit/test/unit/mock/contents/custom-roles/custom-roles.json new file mode 100644 index 0000000000..7d086f1adc --- /dev/null +++ b/packages/contentstack-audit/test/unit/mock/contents/custom-roles/custom-roles.json @@ -0,0 +1,84 @@ +{ + "uidcs1123": { + "urlPath": "/roles/uid123", + "stackHeaders": { "api_key": "uid123", "branch": "main" }, + "name": "Test role 1", + "description": "", + "rules": [ + { "module": "environment", "environments": ["uidenv123"], "acl": { "read": true } }, + { "module": "locale", "locales": ["uidloc123"], "acl": { "read": true } }, + { "module": "content_type", "content_types": ["$all"], "acl": { "read": true, "sub_acl": { "read": true } } }, + { "module": "entry", "content_type": "about", "entries": ["uident123"], "acl": { "read": true } }, + { "module": "branch", "branches": ["main"], "acl": { "read": true } } + ], + "users": [], + "uid": "uid123", + "owner": "vikram@contentstack.com", + "stack": { + "created_at": "2024-11-14T23:18:46.087Z", + "updated_at": "2025-02-28T00:38:09.298Z", + "uid": "uid123", + "name": "Datasync-manager", + "org_uid": "uid123", + "api_key": "uid123", + "master_locale": "en-us", + "is_asset_download_public": true, + "owner_uid": "uid123", + "user_uids": ["uid123", "uid123"], + "master_key": "uid123" + }, + "permissions": { + "content_types": [ + { "uid": "about", "SYS_ACL": { "read": true } }, + { "uid": "about", "SYS_ACL": { "read": true } }, + { "uid": "header", "SYS_ACL": { "read": true, "sub_acl": { "read": true } } }, + { "uid": "header", "SYS_ACL": { "read": true, "sub_acl": { "read": true } } } + ], + "environments": [{ "uid": "uid123", "SYS_ACL": { "read": true } }], + "locales": [{ "uid": "uid123", "SYS_ACL": { "read": true } }] + }, + "SYS_ACL": {} + }, + "uidcs2123": { + "urlPath": "/roles/uid123", + "stackHeaders": { "api_key": "uid123", "branch": "main" }, + "name": "Role 2", + "description": "", + "rules": [ + { "module": "environment", "environments": ["uid123"], "acl": { "read": true } }, + { "module": "locale", "locales": ["uid123"], "acl": { "read": true } }, + { "module": "content_type", "content_types": ["$all"], "acl": { "read": true, "sub_acl": { "read": true } } }, + { "module": "entry", "content_type": "header", "entries": ["uid123"], "acl": { "read": true } }, + { "module": "branch", "branches": ["dev"], "acl": { "read": true } } + ], + "users": [], + "uid": "uid123", + "created_at": "2025-03-02T16:22:56.080Z", + "updated_at": "2025-03-02T16:22:56.080Z", + "owner": "vikram@contentstack.com", + "stack": { + "created_at": "2024-11-14T23:18:46.087Z", + "updated_at": "2025-02-28T00:38:09.298Z", + "uid": "uid123", + "name": "Datasync-manager", + "org_uid": "uid123", + "api_key": "uid123", + "master_locale": "en-us", + "is_asset_download_public": true, + "owner_uid": "uid123", + "user_uids": ["uid123", "uid123"], + "master_key": "uid123" + }, + "permissions": { + "content_types": [ + { "uid": "about", "SYS_ACL": { "read": true, "sub_acl": { "read": true } } }, + { "uid": "about", "SYS_ACL": { "read": true, "sub_acl": { "read": true } } }, + { "uid": "header", "SYS_ACL": { "read": true } }, + { "uid": "header", "SYS_ACL": { "read": true } } + ], + "environments": [{ "uid": "uid123", "SYS_ACL": { "read": true } }], + "locales": [{ "uid": "uid123", "SYS_ACL": { "read": true } }] + }, + "SYS_ACL": {} + } +} diff --git a/packages/contentstack-audit/test/unit/modules/custom-roles.test.ts b/packages/contentstack-audit/test/unit/modules/custom-roles.test.ts new file mode 100644 index 0000000000..1702f3efc3 --- /dev/null +++ b/packages/contentstack-audit/test/unit/modules/custom-roles.test.ts @@ -0,0 +1,67 @@ +import { resolve } from 'path'; +import { expect } from 'chai'; +import cloneDeep from 'lodash/cloneDeep'; +import fancy from 'fancy-test'; +import Sinon from 'sinon'; +import config from '../../../src/config'; +import { CustomRoles } from '../../../src/modules'; +import { CtConstructorParam, ModuleConstructorParam } from '../../../src/types'; + +describe('Custom roles module', () => { + let constructorParam: ModuleConstructorParam & Pick; + + beforeEach(() => { + constructorParam = { + log: () => {}, + moduleName: 'custom-roles', + config: Object.assign(config, { basePath: resolve(__dirname, '..', 'mock', 'contents'), flags: {} }), + ctSchema: cloneDeep(require('../mock/contents/content_types/schema.json')), + }; + }); + + describe('run method', () => { + fancy + .stdout({ print: process.env.PRINT === 'true' || false }) + .it('should have missingFieldsInCustomRoles length equals to 2', async () => { + const customRoleInstance = new CustomRoles({ + ...constructorParam, + config: { ...constructorParam.config, branch: 'test' }, + }); + await customRoleInstance.run(); + expect(customRoleInstance.missingFieldsInCustomRoles).length(2); + expect(JSON.stringify(customRoleInstance.missingFieldsInCustomRoles)).includes('"branches":["main"]'); + }); + + fancy + .stdout({ print: process.env.PRINT === 'true' || false }) + .stub(CustomRoles.prototype, 'fixCustomRoleSchema', async () => {}) + .it('should call fixCustomRoleSchema', async () => { + const customRoleInstance = new CustomRoles({ + ...constructorParam, + config: { ...constructorParam.config, branch: 'test' }, + fix: true, + }); + const logSpy = Sinon.spy(customRoleInstance, 'fixCustomRoleSchema'); + await customRoleInstance.run(); + expect(logSpy.callCount).to.be.equals(1); + }); + + fancy + .stdout({ print: process.env.PRINT === 'true' || false }) + .stub(CustomRoles.prototype, 'writeFixContent', async () => {}) + .it('should call writeFixContent', async () => { + const customRoleInstance = new CustomRoles({ + ...constructorParam, + config: { ...constructorParam.config, branch: 'test' }, + fix: true, + }); + const logSpy = Sinon.spy(customRoleInstance, 'writeFixContent'); + await customRoleInstance.run(); + expect(logSpy.callCount).to.be.equals(1); + }); + }); + + after(() => { + Sinon.restore(); // Clears Sinon spies/stubs/mocks + }); +});