Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions packages/server/api/src/app/benchmark/benchmark-feature-guard.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { AppSystemProp, logger, system } from '@openops/server-shared';
import { preHandlerAsyncHookHandler } from 'fastify';
import { throwFeatureDisabledError } from './errors';

export async function assertBenchmarkFeatureEnabled(
projectId: string,
provider?: string,
): Promise<void> {
export const assertBenchmarkFeatureEnabled: preHandlerAsyncHookHandler = async (
request,
) => {
if (system.getBoolean(AppSystemProp.FINOPS_BENCHMARK_ENABLED) !== true) {
logger.info(
'Benchmark access denied: FINOPS_BENCHMARK_ENABLED flag is not enabled',
{ provider, projectId },
{ projectId: request.principal.projectId },
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see no good reason to log the provider, as this guard checks only the value of system env variable

);
throwFeatureDisabledError('Benchmark feature is not enabled');
}
}
};
17 changes: 2 additions & 15 deletions packages/server/api/src/app/benchmark/benchmark.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,12 @@ import { createBenchmark } from './create-benchmark.service';
import { resolveWizardNavigation } from './wizard.service';

export const benchmarkController: FastifyPluginAsyncTypebox = async (app) => {
app.addHook('preHandler', assertBenchmarkFeatureEnabled);

app.post(
'/:provider/wizard',
WizardStepRequestOptions,
async (request, reply) => {
await assertBenchmarkFeatureEnabled(
request.principal.projectId,
request.params.provider,
);

const step = await resolveWizardNavigation(
request.params.provider,
{
Expand All @@ -44,11 +41,6 @@ export const benchmarkController: FastifyPluginAsyncTypebox = async (app) => {
'/:provider',
CreateBenchmarkRequestOptions,
async (request, reply) => {
await assertBenchmarkFeatureEnabled(
request.principal.projectId,
request.params.provider,
);

const result = await createBenchmark({
provider: request.params.provider,
projectId: request.principal.projectId,
Expand All @@ -59,10 +51,6 @@ export const benchmarkController: FastifyPluginAsyncTypebox = async (app) => {
},
);
app.get('/', ListBenchmarksRequestOptions, async (request, reply) => {
await assertBenchmarkFeatureEnabled(
request.principal.projectId,
request.query.provider,
);
const items = await listBenchmarks({
projectId: request.principal.projectId,
provider: request.query.provider,
Expand All @@ -74,7 +62,6 @@ export const benchmarkController: FastifyPluginAsyncTypebox = async (app) => {
'/:benchmarkId/status',
BenchmarkStatusRequestOptions,
async (request, reply) => {
await assertBenchmarkFeatureEnabled(request.principal.projectId);
const status = await getBenchmarkStatus({
benchmarkId: request.params.benchmarkId,
projectId: request.principal.projectId,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
jest.mock('@openops/server-shared', () => ({
system: {
getBoolean: jest.fn(),
},
AppSystemProp: {
FINOPS_BENCHMARK_ENABLED: 'FINOPS_BENCHMARK_ENABLED',
},
logger: {
info: jest.fn(),
},
}));

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

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

import { assertBenchmarkFeatureEnabled } from '../../../src/app/benchmark/benchmark-feature-guard';

const mockFastifyInstance = {} as FastifyInstance;

const mockRequest = (projectId: string) =>
({ principal: { projectId } } as unknown as FastifyRequest);

const mockReply = {} as FastifyReply;

const callHook = (projectId: string) =>
assertBenchmarkFeatureEnabled.call(
mockFastifyInstance,
mockRequest(projectId),
mockReply,
);

describe('assertBenchmarkFeatureEnabled', () => {
const projectId = 'project-id';

beforeEach(() => {
jest.clearAllMocks();
});

it('should throw error if FINOPS_BENCHMARK_ENABLED is not enabled', async () => {
mockSystem.getBoolean.mockReturnValue(false);

await expect(callHook(projectId)).rejects.toThrow(
expect.objectContaining({
message: 'FEATURE_DISABLED: Benchmark feature is not enabled',
}),
);

expect(logger.info).toHaveBeenCalledWith(
'Benchmark access denied: FINOPS_BENCHMARK_ENABLED flag is not enabled',
{ projectId },
);
});

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

await expect(callHook(projectId)).resolves.toBeUndefined();
});
});
Loading