From a4807a8949b62d37b4b3705cefddb4673e64bb4d Mon Sep 17 00:00:00 2001 From: Valtteri Kantanen Date: Fri, 15 Nov 2024 13:03:39 +0200 Subject: [PATCH] Use a set for relevant IAMs --- src/auth/IAMConfig.ts | 37 ++++++++++++++++++++----------------- src/index.ts | 12 ++++-------- tests/chaos.spec.js | 3 ++- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/auth/IAMConfig.ts b/src/auth/IAMConfig.ts index be4b93a..389646b 100644 --- a/src/auth/IAMConfig.ts +++ b/src/auth/IAMConfig.ts @@ -511,23 +511,26 @@ const iamToFaculty = (iam) => { } } -const relevantIAMs = [] - .concat(Object.keys(joryMap)) - .concat(Object.keys(kojoMap)) - .concat(Object.values(kojoMap)) - .concat(Object.keys(kosuFacultyMap)) - .concat(Object.keys(dekaaniFacultyMap)) - .concat(opetusVaradekaani) - .concat(doctoralIams) - .concat(Object.keys(doctoralSchoolMap)) - .concat(universityWideGroups) - .concat(superAdminGroups) - .concat(adminGroups) - .concat(openUniGroups) - .concat(hyOneGroups) - .concat(employeeGroups) - .concat(oodikoneIams) - .concat(katselmusViewerIams) +const relevantIAMs = new Set( + [ + Object.keys(joryMap), + Object.keys(kojoMap), + Object.values(kojoMap), + Object.keys(kosuFacultyMap), + Object.keys(dekaaniFacultyMap), + opetusVaradekaani, + doctoralIams, + Object.keys(doctoralSchoolMap), + universityWideGroups, + superAdminGroups, + adminGroups, + openUniGroups, + hyOneGroups, + employeeGroups, + oodikoneIams, + katselmusViewerIams, + ].flat(), +) const RELEVANT_ORGANISATION_CODES = [ 'H930', // Avoin yliopisto diff --git a/src/index.ts b/src/index.ts index b4e866f..4d30796 100644 --- a/src/index.ts +++ b/src/index.ts @@ -39,9 +39,7 @@ app.get('/ping', (_req, res) => res.send('pong')) app.post('/', async (req, res) => { const { userId, iamGroups = [], getSisuAccess = false } = req.body - const relevantIamGroups = iamGroups.filter((iam) => - relevantIAMs.includes(iam), - ) + const relevantIamGroups = iamGroups.filter((iam) => relevantIAMs.has(iam)) const { access, specialGroup } = getIAMRights(relevantIamGroups) @@ -76,7 +74,7 @@ app.get('/all-access', async (_req, res) => { const users = await User.findAll() const usersWithAccess = users.map(({ dataValues: user }) => { - const iamGroups = user.iamGroups.filter((iam) => relevantIAMs.includes(iam)) + const iamGroups = user.iamGroups.filter((iam) => relevantIAMs.has(iam)) return { ...user, @@ -107,9 +105,7 @@ app.post('/access-and-special-groups', async (req, res) => { users.map(({ dataValues: user }) => user), 50, async (user) => { - const iamGroups = user.iamGroups.filter((iam) => - relevantIAMs.includes(iam), - ) + const iamGroups = user.iamGroups.filter((iam) => relevantIAMs.has(iam)) const { access, specialGroup } = getIAMRights(iamGroups) specialGroup.fullSisuAccess = await hasFullSisuAccess(user.id) @@ -153,7 +149,7 @@ app.get('/:id', async (req, res) => { if (!user) return res.sendStatus(404) - const iamGroups = user.iamGroups.filter((iam) => relevantIAMs.includes(iam)) + const iamGroups = user.iamGroups.filter((iam) => relevantIAMs.has(iam)) return res.send({ ...user.dataValues, diff --git a/tests/chaos.spec.js b/tests/chaos.spec.js index 596fde2..037240b 100644 --- a/tests/chaos.spec.js +++ b/tests/chaos.spec.js @@ -6,13 +6,14 @@ describe('Chaos test', () => { it('User IAM endpoint should not crash', async () => { const getRandomIams = () => { const randomIams = [] + const iamArray = [...relevantIAMs] for (let i = 0; i < 5 + Math.ceil(Math.random() * 10); i++) { // 50% are random strings if (Math.random() < 0.5) { randomIams.push(Math.random().toString(36).substring(2, 15)) } else { const randomIam = - relevantIAMs[Math.floor(Math.random() * relevantIAMs.length)] + iamArray[Math.floor(Math.random() * iamArray.length)] randomIams.push(randomIam) } }