diff --git a/src/helpers/cache.js b/src/helpers/cache.js index 3fbb562..7ea2fce 100644 --- a/src/helpers/cache.js +++ b/src/helpers/cache.js @@ -6,7 +6,7 @@ class Cache { } set(key, value) { - if (!this.isPermissionCacheActivated()) { + if (!this._isPermissionCacheActivated()) { return; } @@ -17,13 +17,18 @@ class Cache { return this.cache.get(key); } - permissionKey(adminId, domainId, parentId, actions, router) { + has(key) { + return this.cache.has(key); + } + + permissionKey(adminId, domainId, parentId, actions, router, environment) { return JSON.stringify({ adminId: String(adminId), domainId: String(domainId), parentId: String(parentId), actions, - router + router, + environment }); } @@ -35,26 +40,22 @@ class Cache { const keys = this.cache.keys(); for (const key of keys) { const parsedKey = JSON.parse(key); - if (this.matchesKey(parsedKey, domainId, action, router, parentId)) { + if (this._matchesKey(parsedKey, domainId, action, router, parentId)) { this.cache.delete(key); } } } - matchesKey(parsedKey, domainId, action, router, parentId) { + _matchesKey(parsedKey, domainId, action, router, parentId) { return parsedKey.domainId === String(domainId) && (parentId === undefined || parsedKey.parentId === String(parentId)) && (action === ActionTypes.ALL || parsedKey.actions.includes(action)) && parsedKey.router === router; } - isPermissionCacheActivated() { + _isPermissionCacheActivated() { return process.env.PERMISSION_CACHE_ACTIVATED === 'true'; } - - has(key) { - return this.cache.has(key); - } } const permissionCache = new Cache(); diff --git a/src/routers/admin.js b/src/routers/admin.js index 944ee25..c401a36 100644 --- a/src/routers/admin.js +++ b/src/routers/admin.js @@ -112,7 +112,9 @@ router.post('/admin/collaboration/permission', auth, [ }; const parentId = element._id || element.key || element.name || element.strategy; - const cacheKey = permissionCache.permissionKey(req.admin._id, req.body.domain, parentId, req.body.action, req.body.router); + const cacheKey = permissionCache.permissionKey(req.admin._id, req.body.domain, parentId, + req.body.action, req.body.router, req.body.environment); + if (permissionCache.has(cacheKey)) { return res.send(permissionCache.get(cacheKey)); } diff --git a/tests/unit-test/cache.test.js b/tests/unit-test/cache.test.js index 9896415..7358176 100644 --- a/tests/unit-test/cache.test.js +++ b/tests/unit-test/cache.test.js @@ -1,8 +1,13 @@ import { permissionCache } from "../../src/helpers/cache"; +import { EnvType } from "../../src/models/environment"; import { ActionTypes, RouterTypes } from "../../src/models/permission"; describe("Test permissionCache", () => { + beforeEach(() => { + permissionCache.cache.clear(); + }); + it("UNIT_CACHE - Should set and get cache", () => { const cacheKey = permissionCache.permissionKey( 'adminId', @@ -33,7 +38,7 @@ describe("Test permissionCache", () => { expect(result).toBeUndefined(); }); - it("UNIT_CACHE - Should not reload cache", () => { + it("UNIT_CACHE - Should NOT reload cache", () => { const cacheKey = permissionCache.permissionKey( 'adminId', 'domainId', @@ -48,7 +53,7 @@ describe("Test permissionCache", () => { expect(result).toEqual('value'); }); - it("UNIT_CACHE - Should not reload cache - empty router/action", () => { + it("UNIT_CACHE - Should NOT reload cache - empty router/action", () => { const cacheKey = permissionCache.permissionKey( 'adminId', 'domainId', @@ -63,4 +68,30 @@ describe("Test permissionCache", () => { expect(result).toEqual('value'); }); + it("UNIT_CACHE - Should NOT get from cache - different environment", () => { + const cacheKey = permissionCache.permissionKey( + 'adminId', + 'domainId', + 'parentId', + [ActionTypes.UPDATE, ActionTypes.READ], + RouterTypes.GROUP, + EnvType.DEFAULT + ); + + permissionCache.set(cacheKey, 'value1'); + const result = permissionCache.get(cacheKey); + expect(result).toEqual('value1'); + + const cacheKey2 = permissionCache.permissionKey( + 'adminId', + 'domainId', + 'parentId', + [ActionTypes.UPDATE, ActionTypes.READ], + RouterTypes.GROUP + ); + + const result2 = permissionCache.get(cacheKey2); + expect(result2).toBeUndefined(); + }); + }); \ No newline at end of file