Skip to content

Commit

Permalink
refactor(permissions-formatter): add a new service that handle permis…
Browse files Browse the repository at this point in the history
…sions formatting
  • Loading branch information
Thenkei committed Jun 16, 2021
1 parent 0db0260 commit e2bc2f2
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 77 deletions.
2 changes: 2 additions & 0 deletions src/context/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const baseFilterParser = require('../services/base-filters-parser');
const ConfigStore = require('../services/config-store');
const PermissionsChecker = require('../services/permissions-checker');
const PermissionsGetter = require('../services/permissions-getter');
const permissionsFormatter = require('../services/permissions-formatter');
const SchemaFileUpdater = require('../services/schema-file-updater');
const SmartActionHook = require('../services/smart-action-hook');
const schemasGenerator = require('../generators/schemas');
Expand Down Expand Up @@ -133,6 +134,7 @@ function initServices(context) {
context.addInstance('forestServerRequester', forestServerRequester);
context.addInstance('schemasGenerator', schemasGenerator);
context.addInstance('baseFilterParser', baseFilterParser);
context.addInstance('permissionsFormatter', permissionsFormatter);
context.addClass(ProjectDirectoryFinder);
context.addClass(ConfigStore);
context.addClass(PermissionsGetter);
Expand Down
43 changes: 43 additions & 0 deletions src/services/permissions-formatter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

const _transformActionsPermissionsFromOldToNewFormat = (smartActionsPermissions) => {
const newSmartActionsPermissions = {};
Object.keys(smartActionsPermissions).forEach((actionName) => {
const action = smartActionsPermissions[actionName];
newSmartActionsPermissions[actionName] = {
triggerEnabled: action.users ? action.allowed && action.users : action.allowed,
};
});
return newSmartActionsPermissions;
};

const transformPermissionsFromOldToNewFormat = (permissions) => {
const newPermissions = {};

Object.keys(permissions).forEach((modelName) => {
const modelPermissions = permissions[modelName];
const { collection } = modelPermissions;

newPermissions[modelName] = {
collection: {
browseEnabled: collection.list || collection.searchToEdit,
readEnabled: collection.show,
editEnabled: collection.update,
addEnabled: collection.create,
deleteEnabled: collection.delete,
exportEnabled: collection.export,
},
scope: modelPermissions.scope,
};

if (modelPermissions.actions) {
newPermissions[modelName]
.actions = _transformActionsPermissionsFromOldToNewFormat(modelPermissions.actions);
}
});

return newPermissions;
};

module.exports = {
transformPermissionsFromOldToNewFormat,
};
44 changes: 3 additions & 41 deletions src/services/permissions-getter.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
class PermissionsGetter {
constructor({
configStore, env, forestServerRequester, moment, VError,
configStore, env, permissionsFormatter, forestServerRequester, moment, VError,
}) {
this.configStore = configStore;
this.forestServerRequester = forestServerRequester;
this.permissionsFormatter = permissionsFormatter;
this.moment = moment;
this.VError = VError;

Expand Down Expand Up @@ -92,45 +93,6 @@ class PermissionsGetter {
};
}

static _transformActionsPermissionsFromOldToNewFormat(smartActionsPermissions) {
const newSmartActionsPermissions = {};
Object.keys(smartActionsPermissions).forEach((actionName) => {
const action = smartActionsPermissions[actionName];
newSmartActionsPermissions[actionName] = {
triggerEnabled: action.users ? action.allowed && action.users : action.allowed,
};
});
return newSmartActionsPermissions;
}

static _transformPermissionsFromOldToNewFormat(permissions) {
const newPermissions = {};

Object.keys(permissions).forEach((modelName) => {
const modelPermissions = permissions[modelName];
const { collection } = modelPermissions;

newPermissions[modelName] = {
collection: {
browseEnabled: collection.list || collection.searchToEdit,
readEnabled: collection.show,
editEnabled: collection.update,
addEnabled: collection.create,
deleteEnabled: collection.delete,
exportEnabled: collection.export,
},
scope: modelPermissions.scope,
};

if (modelPermissions.actions) {
newPermissions[modelName].actions = PermissionsGetter
._transformActionsPermissionsFromOldToNewFormat(modelPermissions.actions);
}
});

return newPermissions;
}

_setRenderingPermissions(renderingId, permissions, { environmentId } = {}) {
const options = { environmentId, initIfNotExisting: true };
if (!this._getPermissions(options).renderings) {
Expand Down Expand Up @@ -166,7 +128,7 @@ class PermissionsGetter {
this._setRolesACLPermissions(renderingId, permissions, { environmentId });
} else {
const newFormatPermissions = permissions
? PermissionsGetter._transformPermissionsFromOldToNewFormat(permissions)
? this.permissionsFormatter.transformPermissionsFromOldToNewFormat(permissions)
: null;
this._setRenderingPermissions(renderingId, newFormatPermissions, { environmentId });
}
Expand Down
100 changes: 75 additions & 25 deletions test/services/permissions-getter.unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ describe('services > PermissionsGetter', () => {
env: {},
configStore: {},
forestServerRequester: {},
permissionsFormatter: {},
moment,
VError,
};
Expand Down Expand Up @@ -169,19 +170,24 @@ describe('services > PermissionsGetter', () => {
},
};

const permissionsGetter = new PermissionsGetter(defaultDependencies);
const mockTransformPermissionsFromOldToNewFormat = jest.fn((p) => p);
const permissionsGetter = new PermissionsGetter({
...defaultDependencies,
permissionsFormatter: {
transformPermissionsFromOldToNewFormat: mockTransformPermissionsFromOldToNewFormat,
},
});

permissionsGetter.isRolesACLActivated = true;

jest.spyOn(permissionsGetter, '_setRolesACLPermissions');
jest.spyOn(PermissionsGetter, '_transformPermissionsFromOldToNewFormat');

permissionsGetter._setPermissions(1, permissions);
expect(permissionsGetter._setRolesACLPermissions).toHaveBeenCalledTimes(1);
expect(permissionsGetter._setRolesACLPermissions)
.toHaveBeenCalledWith(1, permissions, { environmentId: undefined });
expect(PermissionsGetter._transformPermissionsFromOldToNewFormat).not.toHaveBeenCalled();

jest.restoreAllMocks();
expect(mockTransformPermissionsFromOldToNewFormat)
.not.toHaveBeenCalled();
});

describe('with environmentId', () => {
Expand All @@ -196,17 +202,24 @@ describe('services > PermissionsGetter', () => {
},
};

const permissionsGetter = new PermissionsGetter(defaultDependencies);
const mockTransformPermissionsFromOldToNewFormat = jest.fn((p) => p);
const permissionsGetter = new PermissionsGetter({
...defaultDependencies,
permissionsFormatter: {
transformPermissionsFromOldToNewFormat: mockTransformPermissionsFromOldToNewFormat,
},
});

permissionsGetter.isRolesACLActivated = true;

jest.spyOn(permissionsGetter, '_setRolesACLPermissions');
jest.spyOn(PermissionsGetter, '_transformPermissionsFromOldToNewFormat');

permissionsGetter._setPermissions(1, permissions, { environmentId });
expect(permissionsGetter._setRolesACLPermissions).toHaveBeenCalledTimes(1);
expect(permissionsGetter._setRolesACLPermissions)
.toHaveBeenCalledWith(1, permissions, { environmentId });
expect(PermissionsGetter._transformPermissionsFromOldToNewFormat).not.toHaveBeenCalled();
expect(mockTransformPermissionsFromOldToNewFormat)
.not.toHaveBeenCalled();

jest.restoreAllMocks();
});
Expand All @@ -227,17 +240,24 @@ describe('services > PermissionsGetter', () => {
queries: ['someQuery'],
};

const permissionsGetter = new PermissionsGetter(defaultDependencies);
const mockTransformPermissionsFromOldToNewFormat = jest.fn((p) => p);
const permissionsGetter = new PermissionsGetter({
...defaultDependencies,
permissionsFormatter: {
transformPermissionsFromOldToNewFormat: mockTransformPermissionsFromOldToNewFormat,
},
});

permissionsGetter.isRolesACLActivated = true;

jest.spyOn(permissionsGetter, '_setRolesACLPermissions');
jest.spyOn(PermissionsGetter, '_transformPermissionsFromOldToNewFormat');

permissionsGetter._setPermissions(1, permissions, { environmentId }, stats);
expect(permissionsGetter._setRolesACLPermissions).toHaveBeenCalledTimes(1);
expect(permissionsGetter._setRolesACLPermissions)
.toHaveBeenCalledWith(1, permissions, { environmentId });
expect(PermissionsGetter._transformPermissionsFromOldToNewFormat).not.toHaveBeenCalled();
expect(mockTransformPermissionsFromOldToNewFormat)
.not.toHaveBeenCalled();

const renderingPermissions = permissionsGetter
._getPermissionsInRendering(1, { environmentId }).data;
Expand Down Expand Up @@ -295,20 +315,27 @@ describe('services > PermissionsGetter', () => {

const permissions = {};

const permissionsGetter = new PermissionsGetter(defaultDependencies);
const mockTransformPermissionsFromOldToNewFormat = jest.fn((p) => p);
const permissionsGetter = new PermissionsGetter({
...defaultDependencies,
permissionsFormatter: {
transformPermissionsFromOldToNewFormat: mockTransformPermissionsFromOldToNewFormat,
},
});

permissionsGetter.isRolesACLActivated = false;
jest.spyOn(permissionsGetter, '_setRenderingPermissions');
jest.spyOn(permissionsGetter, '_setCollectionsPermissions');
jest.spyOn(PermissionsGetter, '_transformPermissionsFromOldToNewFormat');

permissionsGetter._setPermissions(1, permissions);

expect(permissionsGetter._setRenderingPermissions).toHaveBeenCalledTimes(1);
expect(permissionsGetter._setRenderingPermissions)
.toHaveBeenCalledWith(1, permissions, { environmentId: undefined });
expect(permissionsGetter._setCollectionsPermissions).not.toHaveBeenCalled();
expect(PermissionsGetter._transformPermissionsFromOldToNewFormat).toHaveBeenCalledTimes(1);
expect(PermissionsGetter._transformPermissionsFromOldToNewFormat)
expect(mockTransformPermissionsFromOldToNewFormat)
.toHaveBeenCalledTimes(1);
expect(mockTransformPermissionsFromOldToNewFormat)
.toHaveBeenCalledWith(permissions);

jest.restoreAllMocks();
Expand All @@ -322,20 +349,27 @@ describe('services > PermissionsGetter', () => {
const environmentId = 100;
const permissions = {};

const permissionsGetter = new PermissionsGetter(defaultDependencies);
const mockTransformPermissionsFromOldToNewFormat = jest.fn((p) => p);
const permissionsGetter = new PermissionsGetter({
...defaultDependencies,
permissionsFormatter: {
transformPermissionsFromOldToNewFormat: mockTransformPermissionsFromOldToNewFormat,
},
});

permissionsGetter.isRolesACLActivated = false;
jest.spyOn(permissionsGetter, '_setRenderingPermissions');
jest.spyOn(permissionsGetter, '_setCollectionsPermissions');
jest.spyOn(PermissionsGetter, '_transformPermissionsFromOldToNewFormat');

permissionsGetter._setPermissions(1, permissions, { environmentId });

expect(permissionsGetter._setRenderingPermissions).toHaveBeenCalledTimes(1);
expect(permissionsGetter._setRenderingPermissions)
.toHaveBeenCalledWith(1, permissions, { environmentId });
expect(permissionsGetter._setCollectionsPermissions).not.toHaveBeenCalled();
expect(PermissionsGetter._transformPermissionsFromOldToNewFormat).toHaveBeenCalledTimes(1);
expect(PermissionsGetter._transformPermissionsFromOldToNewFormat)
expect(mockTransformPermissionsFromOldToNewFormat)
.toHaveBeenCalledTimes(1);
expect(mockTransformPermissionsFromOldToNewFormat)
.toHaveBeenCalledWith(permissions);

jest.restoreAllMocks();
Expand All @@ -352,19 +386,24 @@ describe('services > PermissionsGetter', () => {
queries: ['someQuery'],
};

const permissionsGetter = new PermissionsGetter(defaultDependencies);
const mockTransformPermissionsFromOldToNewFormat = jest.fn((p) => p);
const permissionsGetter = new PermissionsGetter({
...defaultDependencies,
permissionsFormatter: {
transformPermissionsFromOldToNewFormat: mockTransformPermissionsFromOldToNewFormat,
},
});

permissionsGetter.isRolesACLActivated = false;
jest.spyOn(permissionsGetter, '_setRenderingPermissions');
jest.spyOn(permissionsGetter, '_setCollectionsPermissions');
jest.spyOn(PermissionsGetter, '_transformPermissionsFromOldToNewFormat');

permissionsGetter._setPermissions(1, permissions, { environmentId }, stats);

expect(permissionsGetter._setRenderingPermissions).toHaveBeenCalledTimes(1);
expect(permissionsGetter._setCollectionsPermissions).not.toHaveBeenCalled();
expect(PermissionsGetter._transformPermissionsFromOldToNewFormat).toHaveBeenCalledTimes(1);
expect(PermissionsGetter._transformPermissionsFromOldToNewFormat)
.toHaveBeenCalledWith(permissions);
expect(mockTransformPermissionsFromOldToNewFormat).toHaveBeenCalledTimes(1);
expect(mockTransformPermissionsFromOldToNewFormat).toHaveBeenCalledWith(permissions);

const renderingPermissions = permissionsGetter
._getPermissionsInRendering(1, { environmentId }).data;
Expand All @@ -386,7 +425,13 @@ describe('services > PermissionsGetter', () => {
queries: ['someOtherQuery'],
};

const permissionsGetter = new PermissionsGetter(defaultDependencies);
const mockTransformPermissionsFromOldToNewFormat = jest.fn((p) => p);
const permissionsGetter = new PermissionsGetter({
...defaultDependencies,
permissionsFormatter: {
transformPermissionsFromOldToNewFormat: mockTransformPermissionsFromOldToNewFormat,
},
});
permissionsGetter.isRolesACLActivated = false;


Expand Down Expand Up @@ -679,6 +724,8 @@ describe('services > PermissionsGetter', () => {
data: {},
meta: {},
};

const mockTransformPermissionsFromOldToNewFormat = jest.fn((p) => p);
const mockForestServerRequesterPerform = jest.fn(async () => fakeResponse);
const permissionsGetter = new PermissionsGetter({
...defaultDependencies,
Expand All @@ -690,6 +737,9 @@ describe('services > PermissionsGetter', () => {
forestServerRequester: {
perform: mockForestServerRequesterPerform,
},
permissionsFormatter: {
transformPermissionsFromOldToNewFormat: mockTransformPermissionsFromOldToNewFormat,
},
});

await permissionsGetter._retrievePermissions(1);
Expand Down
Loading

0 comments on commit e2bc2f2

Please sign in to comment.