Skip to content

Commit e6b2ac2

Browse files
committed
Refactor benchmark feature guard to use preHandler hook
Replace per-handler assertBenchmarkFeatureEnabled calls with a single app.addHook('preHandler', ...) registered at the plugin scope, following the established hook pattern used across the codebase. Made-with: Cursor
1 parent bd72a10 commit e6b2ac2

4 files changed

Lines changed: 31 additions & 65 deletions

File tree

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import {
2-
benchmarkFeatureGuard,
3-
BenchmarkFeatureGuard,
4-
} from './benchmark-feature-guard';
1+
import { preHandlerAsyncHookHandler } from 'fastify';
2+
import { assertBenchmarkFeatureEnabled } from './benchmark-feature-guard';
53

6-
export const getBenchmarkFeatureGuard = (): BenchmarkFeatureGuard => {
7-
return benchmarkFeatureGuard;
4+
export const getBenchmarkFeatureGuard = (): preHandlerAsyncHookHandler => {
5+
return assertBenchmarkFeatureEnabled;
86
};
Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,13 @@
1+
import { preHandlerAsyncHookHandler } from 'fastify';
12
import { AppSystemProp, logger, system } from '@openops/server-shared';
23
import { throwFeatureDisabledError } from './errors';
34

4-
export type BenchmarkFeatureGuard = {
5-
assertBenchmarkFeatureEnabled(
6-
projectId: string,
7-
organizationId: string,
8-
provider?: string,
9-
): Promise<void>;
10-
};
11-
12-
export const benchmarkFeatureGuard: BenchmarkFeatureGuard = {
13-
async assertBenchmarkFeatureEnabled(
14-
projectId: string,
15-
_organizationId: string,
16-
provider?: string,
17-
): Promise<void> {
18-
if (system.getBoolean(AppSystemProp.FINOPS_BENCHMARK_ENABLED) !== true) {
19-
logger.info(
20-
'Benchmark access denied: FINOPS_BENCHMARK_ENABLED flag is not enabled',
21-
{ provider, projectId },
22-
);
23-
throwFeatureDisabledError('Benchmark feature is not enabled');
24-
}
25-
},
5+
export const assertBenchmarkFeatureEnabled: preHandlerAsyncHookHandler = async (request) => {
6+
if (system.getBoolean(AppSystemProp.FINOPS_BENCHMARK_ENABLED) !== true) {
7+
logger.info(
8+
'Benchmark access denied: FINOPS_BENCHMARK_ENABLED flag is not enabled',
9+
{ projectId: request.principal.projectId },
10+
);
11+
throwFeatureDisabledError('Benchmark feature is not enabled');
12+
}
2613
};

packages/server/api/src/app/benchmark/benchmark.controller.ts

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,12 @@ import { createBenchmark } from './create-benchmark.service';
1919
import { resolveWizardNavigation } from './wizard.service';
2020

2121
export const benchmarkController: FastifyPluginAsyncTypebox = async (app) => {
22+
app.addHook('preHandler', getBenchmarkFeatureGuard());
23+
2224
app.post(
2325
'/:provider/wizard',
2426
WizardStepRequestOptions,
2527
async (request, reply) => {
26-
await getBenchmarkFeatureGuard().assertBenchmarkFeatureEnabled(
27-
request.principal.projectId,
28-
request.principal.organization.id,
29-
request.params.provider,
30-
);
31-
3228
const step = await resolveWizardNavigation(
3329
request.params.provider,
3430
{
@@ -45,12 +41,6 @@ export const benchmarkController: FastifyPluginAsyncTypebox = async (app) => {
4541
'/:provider',
4642
CreateBenchmarkRequestOptions,
4743
async (request, reply) => {
48-
await getBenchmarkFeatureGuard().assertBenchmarkFeatureEnabled(
49-
request.principal.projectId,
50-
request.principal.organization.id,
51-
request.params.provider,
52-
);
53-
5444
const result = await createBenchmark({
5545
provider: request.params.provider,
5646
projectId: request.principal.projectId,
@@ -61,11 +51,6 @@ export const benchmarkController: FastifyPluginAsyncTypebox = async (app) => {
6151
},
6252
);
6353
app.get('/', ListBenchmarksRequestOptions, async (request, reply) => {
64-
await getBenchmarkFeatureGuard().assertBenchmarkFeatureEnabled(
65-
request.principal.projectId,
66-
request.principal.organization.id,
67-
request.query.provider,
68-
);
6954
const items = await listBenchmarks({
7055
projectId: request.principal.projectId,
7156
provider: request.query.provider,
@@ -77,10 +62,6 @@ export const benchmarkController: FastifyPluginAsyncTypebox = async (app) => {
7762
'/:benchmarkId/status',
7863
BenchmarkStatusRequestOptions,
7964
async (request, reply) => {
80-
await getBenchmarkFeatureGuard().assertBenchmarkFeatureEnabled(
81-
request.principal.projectId,
82-
request.principal.organization.id,
83-
);
8465
const status = await getBenchmarkStatus({
8566
benchmarkId: request.params.benchmarkId,
8667
projectId: request.principal.projectId,

packages/server/api/test/unit/benchmark/benchmark-feature-guard.test.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,24 @@ jest.mock('@openops/server-shared', () => ({
1111
}));
1212

1313
import { logger, system } from '@openops/server-shared';
14+
import { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';
1415

1516
const mockSystem = system as jest.Mocked<typeof system>;
1617

17-
import { benchmarkFeatureGuard } from '../../../src/app/benchmark/benchmark-feature-guard';
18+
import { assertBenchmarkFeatureEnabled } from '../../../src/app/benchmark/benchmark-feature-guard';
1819

19-
describe('benchmarkFeatureGuard', () => {
20+
const mockFastifyInstance = {} as FastifyInstance;
21+
22+
const mockRequest = (projectId: string) =>
23+
({ principal: { projectId } }) as unknown as FastifyRequest;
24+
25+
const mockReply = {} as FastifyReply;
26+
27+
const callHook = (projectId: string) =>
28+
assertBenchmarkFeatureEnabled.call(mockFastifyInstance, mockRequest(projectId), mockReply);
29+
30+
describe('assertBenchmarkFeatureEnabled', () => {
2031
const projectId = 'project-id';
21-
const organizationId = 'org-id';
2232

2333
beforeEach(() => {
2434
jest.clearAllMocks();
@@ -27,31 +37,21 @@ describe('benchmarkFeatureGuard', () => {
2737
it('should throw error if FINOPS_BENCHMARK_ENABLED is not enabled', async () => {
2838
mockSystem.getBoolean.mockReturnValue(false);
2939

30-
await expect(
31-
benchmarkFeatureGuard.assertBenchmarkFeatureEnabled(
32-
projectId,
33-
organizationId,
34-
),
35-
).rejects.toThrow(
40+
await expect(callHook(projectId)).rejects.toThrow(
3641
expect.objectContaining({
3742
message: 'FEATURE_DISABLED: Benchmark feature is not enabled',
3843
}),
3944
);
4045

4146
expect(logger.info).toHaveBeenCalledWith(
4247
'Benchmark access denied: FINOPS_BENCHMARK_ENABLED flag is not enabled',
43-
{ provider: undefined, projectId },
48+
{ projectId },
4449
);
4550
});
4651

4752
it('should pass when FINOPS_BENCHMARK_ENABLED is true', async () => {
4853
mockSystem.getBoolean.mockReturnValue(true);
4954

50-
await expect(
51-
benchmarkFeatureGuard.assertBenchmarkFeatureEnabled(
52-
projectId,
53-
organizationId,
54-
),
55-
).resolves.toBeUndefined();
55+
await expect(callHook(projectId)).resolves.toBeUndefined();
5656
});
5757
});

0 commit comments

Comments
 (0)