From 10e76206050b5ed771c924e748ac6fa74fc0b418 Mon Sep 17 00:00:00 2001 From: tada5hi Date: Wed, 28 Feb 2024 11:44:03 +0100 Subject: [PATCH] fix: assign all abilities to user in test mode --- .../server-core/src/http/middleware/authup.ts | 29 +++++++++++++------ .../server-core/src/http/middleware/error.ts | 1 - 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/packages/server-core/src/http/middleware/authup.ts b/packages/server-core/src/http/middleware/authup.ts index 726a30aa9..c4e810834 100644 --- a/packages/server-core/src/http/middleware/authup.ts +++ b/packages/server-core/src/http/middleware/authup.ts @@ -15,7 +15,7 @@ import { parseAuthorizationHeader } from 'hapic'; import { coreHandler, getRequestHeader } from 'routup'; import { useRequestCookie } from '@routup/basic/cookie'; import type { Request, Router } from 'routup'; -import { useEnv } from '../../config'; +import { EnvironmentName, useEnv } from '../../config'; import { hasAuthupClient, hasRedisClient, hasVaultClient, useAuthupClient, useRedisClient, } from '../../core'; @@ -31,6 +31,16 @@ TokenVerificationData, 'sub_name' >; +function generateAbilities() : Ability[] { + return Object.values(PermissionID).map((name) => ({ + name, + condition: null, + inverse: false, + power: 999, + target: null, + } satisfies Ability)); +} + function createFakeTokenVerificationData() : TokenVerificationDataMinimal { return { realm_id: 'd94b2f28-29e3-4ced-b8f1-6923a01dc1ee', @@ -40,13 +50,7 @@ function createFakeTokenVerificationData() : TokenVerificationDataMinimal { sub: 'd94b2f28-29e3-4ced-b8f1-6923a01dc1ee', sub_name: 'admin', - permissions: Object.values(PermissionID).map((name) => ({ - name, - condition: null, - inverse: false, - power: 999, - target: null, - } satisfies Ability)), + permissions: generateAbilities(), }; } @@ -54,7 +58,14 @@ function applyTokenVerificationData( req: Request, data: TokenVerificationDataMinimal, ) { - const ability = new AbilityManager(data.permissions); + let abilities : Ability[]; + if (useEnv('env') === EnvironmentName.TEST) { + abilities = generateAbilities(); + } else { + abilities = data.permissions; + } + + const ability = new AbilityManager(abilities); setRequestEnv(req, 'ability', ability); setRequestEnv(req, 'realmId', data.realm_id); diff --git a/packages/server-core/src/http/middleware/error.ts b/packages/server-core/src/http/middleware/error.ts index 3f1bf4c79..51c84026f 100644 --- a/packages/server-core/src/http/middleware/error.ts +++ b/packages/server-core/src/http/middleware/error.ts @@ -12,7 +12,6 @@ import { useLogger } from '../../config'; export function registerErrorHandler(router: Router) { router.use(errorHandler((error, req, res) => { - console.log(error.cause); // catch and decorate some db errors :) switch (error.code) { case 'ER_DUP_ENTRY':