Skip to content

Commit aa70442

Browse files
committed
fixes after review
1 parent 3ad88ae commit aa70442

4 files changed

Lines changed: 52 additions & 3 deletions

File tree

src/main.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,12 @@ async function bootstrap() {
188188
logger.log(`Transaction pool enabled: ${apiConfigService.isTransactionPoolEnabled()}`);
189189
logger.log(`Transaction pool cache warmer enabled: ${apiConfigService.isTransactionPoolCacheWarmerEnabled()}`);
190190

191-
logger.log('Restricted routes enabled: ' + apiConfigService.isRestrictedRoutesEnabled());
191+
logger.log(`Restricted routes enabled: ${apiConfigService.isRestrictedRoutesEnabled()}`);
192192
}
193193

194194
async function configurePublicApp(publicApp: NestExpressApplication, apiConfigService: ApiConfigService) {
195195
if (apiConfigService.isRestrictedRoutesEnabled()) {
196-
const restrictedRoutesMiddleware = new RestrictedRoutesMiddleware(apiConfigService);
196+
const restrictedRoutesMiddleware = publicApp.get(RestrictedRoutesMiddleware);
197197
publicApp.use(restrictedRoutesMiddleware.use.bind(restrictedRoutesMiddleware));
198198
}
199199

src/public.app.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { GuestCacheService } from '@multiversx/sdk-nestjs-cache';
99
import { LoggingModule } from '@multiversx/sdk-nestjs-common';
1010
import { DynamicModuleUtils } from './utils/dynamic.module.utils';
1111
import { LocalCacheController } from './endpoints/caching/local.cache.controller';
12+
import { RestrictedRoutesMiddleware } from './utils/restricted.routes.middleware';
1213

1314
@Module({
1415
imports: [
@@ -23,6 +24,7 @@ import { LocalCacheController } from './endpoints/caching/local.cache.controller
2324
providers: [
2425
DynamicModuleUtils.getNestJsApiConfigService(),
2526
GuestCacheService,
27+
RestrictedRoutesMiddleware,
2628
],
2729
exports: [
2830
EndpointsServicesModule,
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { NotFoundException } from '@nestjs/common';
2+
import { Request, Response } from 'express';
3+
import { ApiConfigService } from 'src/common/api-config/api.config.service';
4+
import { RestrictedRoutesMiddleware } from 'src/utils/restricted.routes.middleware';
5+
6+
describe('RestrictedRoutesMiddleware', () => {
7+
let middleware: RestrictedRoutesMiddleware;
8+
let apiConfigService: jest.Mocked<ApiConfigService>;
9+
10+
beforeEach(() => {
11+
apiConfigService = {
12+
getRestrictedRoutes: jest.fn(),
13+
} as unknown as jest.Mocked<ApiConfigService>;
14+
15+
middleware = new RestrictedRoutesMiddleware(apiConfigService);
16+
});
17+
18+
it('should throw NotFoundException when route is restricted', () => {
19+
apiConfigService.getRestrictedRoutes.mockReturnValue(['/blocked']);
20+
21+
const req = {
22+
path: '/blocked',
23+
} as Request;
24+
const res = {} as Response;
25+
const next = jest.fn();
26+
27+
expect(() => middleware.use(req, res, next)).toThrow(NotFoundException);
28+
expect(() => middleware.use(req, res, next)).toThrow('Cannot GET /blocked');
29+
expect(next).not.toHaveBeenCalled();
30+
});
31+
32+
it('should call next when route is not restricted', () => {
33+
apiConfigService.getRestrictedRoutes.mockReturnValue(['/blocked']);
34+
35+
const req = {
36+
path: '/allowed',
37+
} as Request;
38+
const res = {} as Response;
39+
const next = jest.fn();
40+
41+
middleware.use(req, res, next);
42+
43+
expect(next).toHaveBeenCalledTimes(1);
44+
});
45+
});

src/utils/restricted.routes.middleware.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import { NestMiddleware, NotFoundException } from '@nestjs/common';
1+
import { Injectable, NestMiddleware, NotFoundException } from '@nestjs/common';
22
import { Request, Response, NextFunction } from 'express';
33
import { ApiConfigService } from 'src/common/api-config/api.config.service';
44

5+
@Injectable()
56
export class RestrictedRoutesMiddleware implements NestMiddleware {
67
constructor(
78
private readonly apiConfigService: ApiConfigService,
89
) { }
10+
911
use(req: Request, _res: Response, next: NextFunction) {
1012
const restrictedRoutes = this.apiConfigService.getRestrictedRoutes();
1113
if (restrictedRoutes.includes(req.path)) {

0 commit comments

Comments
 (0)