Skip to content

Commit 4d28889

Browse files
authored
Merge pull request #1806 from contentstack/test1/DX-1303
Test1/dx 1303
2 parents 0aed4f3 + 5afa771 commit 4d28889

File tree

3 files changed

+153
-1
lines changed

3 files changed

+153
-1
lines changed

packages/contentstack-audit/src/modules/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ import GlobalField from './global-fields';
33
import ContentType from './content-types';
44
import Workflows from './workflows';
55
import Extensions from './extensions';
6+
import CustomRoles from './custom-roles';
67
import Assets from './assets';
7-
export { Entries, GlobalField, ContentType, Workflows, Extensions, Assets };
8+
export { Entries, GlobalField, ContentType, Workflows, Extensions, Assets, CustomRoles };
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
{
2+
"uidcs1123": {
3+
"urlPath": "/roles/uid123",
4+
"stackHeaders": { "api_key": "uid123", "branch": "main" },
5+
"name": "Test role 1",
6+
"description": "",
7+
"rules": [
8+
{ "module": "environment", "environments": ["uidenv123"], "acl": { "read": true } },
9+
{ "module": "locale", "locales": ["uidloc123"], "acl": { "read": true } },
10+
{ "module": "content_type", "content_types": ["$all"], "acl": { "read": true, "sub_acl": { "read": true } } },
11+
{ "module": "entry", "content_type": "about", "entries": ["uident123"], "acl": { "read": true } },
12+
{ "module": "branch", "branches": ["main"], "acl": { "read": true } }
13+
],
14+
"users": [],
15+
"uid": "uid123",
16+
"owner": "vikram@contentstack.com",
17+
"stack": {
18+
"created_at": "2024-11-14T23:18:46.087Z",
19+
"updated_at": "2025-02-28T00:38:09.298Z",
20+
"uid": "uid123",
21+
"name": "Datasync-manager",
22+
"org_uid": "uid123",
23+
"api_key": "uid123",
24+
"master_locale": "en-us",
25+
"is_asset_download_public": true,
26+
"owner_uid": "uid123",
27+
"user_uids": ["uid123", "uid123"],
28+
"master_key": "uid123"
29+
},
30+
"permissions": {
31+
"content_types": [
32+
{ "uid": "about", "SYS_ACL": { "read": true } },
33+
{ "uid": "about", "SYS_ACL": { "read": true } },
34+
{ "uid": "header", "SYS_ACL": { "read": true, "sub_acl": { "read": true } } },
35+
{ "uid": "header", "SYS_ACL": { "read": true, "sub_acl": { "read": true } } }
36+
],
37+
"environments": [{ "uid": "uid123", "SYS_ACL": { "read": true } }],
38+
"locales": [{ "uid": "uid123", "SYS_ACL": { "read": true } }]
39+
},
40+
"SYS_ACL": {}
41+
},
42+
"uidcs2123": {
43+
"urlPath": "/roles/uid123",
44+
"stackHeaders": { "api_key": "uid123", "branch": "main" },
45+
"name": "Role 2",
46+
"description": "",
47+
"rules": [
48+
{ "module": "environment", "environments": ["uid123"], "acl": { "read": true } },
49+
{ "module": "locale", "locales": ["uid123"], "acl": { "read": true } },
50+
{ "module": "content_type", "content_types": ["$all"], "acl": { "read": true, "sub_acl": { "read": true } } },
51+
{ "module": "entry", "content_type": "header", "entries": ["uid123"], "acl": { "read": true } },
52+
{ "module": "branch", "branches": ["dev"], "acl": { "read": true } }
53+
],
54+
"users": [],
55+
"uid": "uid123",
56+
"created_at": "2025-03-02T16:22:56.080Z",
57+
"updated_at": "2025-03-02T16:22:56.080Z",
58+
"owner": "vikram@contentstack.com",
59+
"stack": {
60+
"created_at": "2024-11-14T23:18:46.087Z",
61+
"updated_at": "2025-02-28T00:38:09.298Z",
62+
"uid": "uid123",
63+
"name": "Datasync-manager",
64+
"org_uid": "uid123",
65+
"api_key": "uid123",
66+
"master_locale": "en-us",
67+
"is_asset_download_public": true,
68+
"owner_uid": "uid123",
69+
"user_uids": ["uid123", "uid123"],
70+
"master_key": "uid123"
71+
},
72+
"permissions": {
73+
"content_types": [
74+
{ "uid": "about", "SYS_ACL": { "read": true, "sub_acl": { "read": true } } },
75+
{ "uid": "about", "SYS_ACL": { "read": true, "sub_acl": { "read": true } } },
76+
{ "uid": "header", "SYS_ACL": { "read": true } },
77+
{ "uid": "header", "SYS_ACL": { "read": true } }
78+
],
79+
"environments": [{ "uid": "uid123", "SYS_ACL": { "read": true } }],
80+
"locales": [{ "uid": "uid123", "SYS_ACL": { "read": true } }]
81+
},
82+
"SYS_ACL": {}
83+
}
84+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { resolve } from 'path';
2+
import { expect } from 'chai';
3+
import cloneDeep from 'lodash/cloneDeep';
4+
import fancy from 'fancy-test';
5+
import Sinon from 'sinon';
6+
import config from '../../../src/config';
7+
import { CustomRoles } from '../../../src/modules';
8+
import { CtConstructorParam, ModuleConstructorParam } from '../../../src/types';
9+
10+
describe('Custom roles module', () => {
11+
let constructorParam: ModuleConstructorParam & Pick<CtConstructorParam, 'ctSchema'>;
12+
13+
beforeEach(() => {
14+
constructorParam = {
15+
log: () => {},
16+
moduleName: 'custom-roles',
17+
config: Object.assign(config, { basePath: resolve(__dirname, '..', 'mock', 'contents'), flags: {} }),
18+
ctSchema: cloneDeep(require('../mock/contents/content_types/schema.json')),
19+
};
20+
});
21+
22+
describe('run method', () => {
23+
fancy
24+
.stdout({ print: process.env.PRINT === 'true' || false })
25+
.it('should have missingFieldsInCustomRoles length equals to 2', async () => {
26+
const customRoleInstance = new CustomRoles({
27+
...constructorParam,
28+
config: { ...constructorParam.config, branch: 'test' },
29+
});
30+
await customRoleInstance.run();
31+
expect(customRoleInstance.missingFieldsInCustomRoles).length(2);
32+
expect(JSON.stringify(customRoleInstance.missingFieldsInCustomRoles)).includes('"branches":["main"]');
33+
});
34+
35+
fancy
36+
.stdout({ print: process.env.PRINT === 'true' || false })
37+
.stub(CustomRoles.prototype, 'fixCustomRoleSchema', async () => {})
38+
.it('should call fixCustomRoleSchema', async () => {
39+
const customRoleInstance = new CustomRoles({
40+
...constructorParam,
41+
config: { ...constructorParam.config, branch: 'test' },
42+
fix: true,
43+
});
44+
const logSpy = Sinon.spy(customRoleInstance, 'fixCustomRoleSchema');
45+
await customRoleInstance.run();
46+
expect(logSpy.callCount).to.be.equals(1);
47+
});
48+
49+
fancy
50+
.stdout({ print: process.env.PRINT === 'true' || false })
51+
.stub(CustomRoles.prototype, 'writeFixContent', async () => {})
52+
.it('should call writeFixContent', async () => {
53+
const customRoleInstance = new CustomRoles({
54+
...constructorParam,
55+
config: { ...constructorParam.config, branch: 'test' },
56+
fix: true,
57+
});
58+
const logSpy = Sinon.spy(customRoleInstance, 'writeFixContent');
59+
await customRoleInstance.run();
60+
expect(logSpy.callCount).to.be.equals(1);
61+
});
62+
});
63+
64+
after(() => {
65+
Sinon.restore(); // Clears Sinon spies/stubs/mocks
66+
});
67+
});

0 commit comments

Comments
 (0)