From 0bd5893ea159f988a15a26123bfbfbfc88e32529 Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Sun, 9 Jun 2024 20:46:50 -0700 Subject: [PATCH] Fixes permission cache refresh issue when removing component ALL --- package.json | 16 ++++++++-------- src/helpers/cache.js | 4 ++-- tests/unit-test/cache.test.js | 30 ++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 8599a54..1d21a37 100644 --- a/package.json +++ b/package.json @@ -41,8 +41,8 @@ "cors": "^2.8.5", "express": "^4.19.2", "express-basic-auth": "^1.2.1", - "express-rate-limit": "^7.2.0", - "express-validator": "^7.0.1", + "express-rate-limit": "^7.3.1", + "express-validator": "^7.1.0", "graphql": "^16.8.1", "graphql-http": "^1.22.1", "graphql-tag": "^2.12.6", @@ -50,20 +50,20 @@ "jsonwebtoken": "^9.0.2", "moment": "^2.30.1", "mongodb": "^6.7.0", - "mongoose": "^8.4.0", + "mongoose": "^8.4.1", "pino": "^9.1.0", - "pino-pretty": "^11.1.0", - "swagger-ui-express": "^5.0.0", - "switcher-client": "^4.1.0", + "pino-pretty": "^11.2.0", + "swagger-ui-express": "^5.0.1", + "switcher-client": "^4.1.1", "validator": "^13.12.0" }, "devDependencies": { "env-cmd": "^10.1.0", - "eslint": "^9.3.0", + "eslint": "^9.4.0", "jest": "^29.7.0", "jest-sonar-reporter": "^2.0.0", "node-notifier": "^10.0.1", - "nodemon": "^3.1.2", + "nodemon": "^3.1.3", "sinon": "^18.0.0", "supertest": "^7.0.0" }, diff --git a/src/helpers/cache.js b/src/helpers/cache.js index ee9d585..d4832d6 100644 --- a/src/helpers/cache.js +++ b/src/helpers/cache.js @@ -1,4 +1,4 @@ -import { ActionTypes } from '../models/permission.js'; +import { ActionTypes, RouterTypes } from '../models/permission.js'; class Cache { constructor() { @@ -50,7 +50,7 @@ class Cache { return parsedKey.domainId === String(domainId) && (parentId === undefined || parsedKey.parentId === String(parentId)) && (action === ActionTypes.ALL || parsedKey.actions.includes(action)) && - parsedKey.router === router; + (router === RouterTypes.ALL || parsedKey.router === router); } _isPermissionCacheActivated() { diff --git a/tests/unit-test/cache.test.js b/tests/unit-test/cache.test.js index 9999ccb..9b96057 100644 --- a/tests/unit-test/cache.test.js +++ b/tests/unit-test/cache.test.js @@ -94,4 +94,34 @@ describe('Test permissionCache', () => { expect(result2).toBeUndefined(); }); + it('UNIT_CAHCE - Should reload cache - requested router ALL to be removed', () => { + const cacheKey = permissionCache.permissionKey( + 'adminId', + 'domainId', + 'parentId', + [ActionTypes.READ], + RouterTypes.GROUP + ); + + permissionCache.set(cacheKey, 'value'); + permissionCache.permissionReset('domainId', ActionTypes.ALL, RouterTypes.ALL); + const result = permissionCache.get(cacheKey); + expect(result).toBeUndefined(); + }); + + it('UNIT_CAHCE - Should NOT reload cache - requested different action for router ALL', () => { + const cacheKey = permissionCache.permissionKey( + 'adminId', + 'domainId', + 'parentId', + [ActionTypes.READ], + RouterTypes.GROUP + ); + + permissionCache.set(cacheKey, 'value'); + permissionCache.permissionReset('domainId', ActionTypes.UPDATE, RouterTypes.ALL); + const result = permissionCache.get(cacheKey); + expect(result).toEqual('value'); + }); + }); \ No newline at end of file