From 32ac0b4c910eaeeb27415b47d7cd4071637d4d8f Mon Sep 17 00:00:00 2001 From: tickBit Date: Mon, 7 Sep 2026 11:59:58 +0300 Subject: [PATCH] current linter & Prettier changes only --- .../authorizationInterceptor.array.test.ts | 77 ++++++++++++++---- .../authorization.interceptor.ts | 80 +++++++++---------- src/box/auth/boxTestingSession.guard.ts | 11 +-- src/box/box.controller.ts | 5 +- 4 files changed, 106 insertions(+), 67 deletions(-) diff --git a/src/__tests__/authorization/authorizationInterceptor.array.test.ts b/src/__tests__/authorization/authorizationInterceptor.array.test.ts index cc5153a64..c97eab71e 100644 --- a/src/__tests__/authorization/authorizationInterceptor.array.test.ts +++ b/src/__tests__/authorization/authorizationInterceptor.array.test.ts @@ -42,7 +42,11 @@ describe('AuthorizationInterceptor - Array Payload Handling', () => { it('should process single object payloads correctly', async () => { const requestBody = { _id: 'room_1', name: 'Living Room' }; - const mockUser = new User('mock_profile_id', 'mock_player_id', 'mock_clan_id'); + const mockUser = new User( + 'mock_profile_id', + 'mock_player_id', + 'mock_clan_id', + ); const mockExecutionContext = { switchToHttp: () => ({ @@ -51,9 +55,14 @@ describe('AuthorizationInterceptor - Array Payload Handling', () => { getHandler: () => ({}), } as unknown as ExecutionContext; - const mockCallHandler = { handle: jest.fn().mockReturnValue(of({ data: 'ok' })) }; + const mockCallHandler = { + handle: jest.fn().mockReturnValue(of({ data: 'ok' })), + }; - const result$ = await interceptor.intercept(mockExecutionContext, mockCallHandler); + const result$ = await interceptor.intercept( + mockExecutionContext, + mockCallHandler, + ); result$.subscribe({ next: () => { @@ -70,7 +79,11 @@ describe('AuthorizationInterceptor - Array Payload Handling', () => { { _id: 'room_2', name: 'Bedroom' }, ]; - const mockUser = new User('mock_profile_id', 'mock_player_id', 'mock_clan_id'); + const mockUser = new User( + 'mock_profile_id', + 'mock_player_id', + 'mock_clan_id', + ); const mockExecutionContext = { switchToHttp: () => ({ @@ -79,7 +92,9 @@ describe('AuthorizationInterceptor - Array Payload Handling', () => { getHandler: () => ({}), } as unknown as ExecutionContext; - const mockCallHandler = { handle: jest.fn().mockReturnValue(of({ data: 'ok' })) }; + const mockCallHandler = { + handle: jest.fn().mockReturnValue(of({ data: 'ok' })), + }; const result$ = await interceptor.intercept( mockExecutionContext, @@ -90,7 +105,7 @@ describe('AuthorizationInterceptor - Array Payload Handling', () => { next: () => { const request = mockExecutionContext.switchToHttp().getRequest(); expect(Array.isArray(request.body)).toBe(true); - expect(request.body.length).toBe(2); + expect(request.body).toHaveLength(2); expect(request.body[0]._id).toBe('room_1'); expect(request.body[1]._id).toBe('room_2'); }, @@ -104,7 +119,11 @@ describe('AuthorizationInterceptor - Array Payload Handling', () => { mockCaslFactory.createForUser.mockResolvedValueOnce(restrictAbility); const requestBody = [{ _id: 'room_1', name: 'Living Room' }]; - const mockUser = new User('mock_profile_id', 'mock_player_id', 'mock_clan_id'); + const mockUser = new User( + 'mock_profile_id', + 'mock_player_id', + 'mock_clan_id', + ); const mockExecutionContext = { switchToHttp: () => ({ @@ -127,7 +146,11 @@ describe('AuthorizationInterceptor - Array Payload Handling', () => { mockCaslFactory.createForUser.mockResolvedValueOnce(restrictAbility); const requestBody = { _id: 'room_1', name: 'Living Room' }; - const mockUser = new User('mock_profile_id', 'mock_player_id', 'mock_clan_id'); + const mockUser = new User( + 'mock_profile_id', + 'mock_player_id', + 'mock_clan_id', + ); const mockExecutionContext = { switchToHttp: () => ({ @@ -149,7 +172,11 @@ describe('AuthorizationInterceptor - Array Payload Handling', () => { subject: TestDto, }); - const mockUser = new User('mock_profile_id', 'mock_player_id', 'mock_clan_id'); + const mockUser = new User( + 'mock_profile_id', + 'mock_player_id', + 'mock_clan_id', + ); const mockExecutionContext = { switchToHttp: () => ({ @@ -158,9 +185,14 @@ describe('AuthorizationInterceptor - Array Payload Handling', () => { getHandler: () => ({}), } as unknown as ExecutionContext; - const mockCallHandler = { handle: jest.fn().mockReturnValue(of({ _id: 'room_1', name: 'Test' })) }; + const mockCallHandler = { + handle: jest.fn().mockReturnValue(of({ _id: 'room_1', name: 'Test' })), + }; - const result$ = await interceptor.intercept(mockExecutionContext, mockCallHandler); + const result$ = await interceptor.intercept( + mockExecutionContext, + mockCallHandler, + ); result$.subscribe({ next: (res) => { @@ -170,7 +202,11 @@ describe('AuthorizationInterceptor - Array Payload Handling', () => { }); it('should process response mapping logic for array responses', async () => { - const mockUser = new User('mock_profile_id', 'mock_player_id', 'mock_clan_id'); + const mockUser = new User( + 'mock_profile_id', + 'mock_player_id', + 'mock_clan_id', + ); const mockExecutionContext = { switchToHttp: () => ({ @@ -184,9 +220,14 @@ describe('AuthorizationInterceptor - Array Payload Handling', () => { { _id: 'room_2', name: 'Bedroom' }, ]; - const mockCallHandler = { handle: jest.fn().mockReturnValue(of(responseData)) }; + const mockCallHandler = { + handle: jest.fn().mockReturnValue(of(responseData)), + }; - const result$ = await interceptor.intercept(mockExecutionContext, mockCallHandler); + const result$ = await interceptor.intercept( + mockExecutionContext, + mockCallHandler, + ); result$.subscribe({ next: (data) => { @@ -198,7 +239,11 @@ describe('AuthorizationInterceptor - Array Payload Handling', () => { it('should throw InternalServerErrorException if no permission metadata is defined on route', async () => { mockReflector.get.mockReturnValueOnce(undefined); - const mockUser = new User('mock_profile_id', 'mock_player_id', 'mock_clan_id'); + const mockUser = new User( + 'mock_profile_id', + 'mock_player_id', + 'mock_clan_id', + ); const mockExecutionContext = { switchToHttp: () => ({ @@ -213,4 +258,4 @@ describe('AuthorizationInterceptor - Array Payload Handling', () => { interceptor.intercept(mockExecutionContext, mockCallHandler), ).rejects.toThrow(InternalServerErrorException); }); -}); \ No newline at end of file +}); diff --git a/src/authorization/authorization.interceptor.ts b/src/authorization/authorization.interceptor.ts index c4ac17571..cb22c808f 100644 --- a/src/authorization/authorization.interceptor.ts +++ b/src/authorization/authorization.interceptor.ts @@ -159,53 +159,53 @@ export class AuthorizationInterceptor implements NestInterceptor { //Filter out all fields that logged user can not update //Basically create a new request body if (action === Action.update) { - if (Array.isArray(request.body)) { - // 1. Process Array Payloads - const items = request.body.map((item) => { - const dataClass = plainToInstance(subject, item); - - if (!userAbility.can(requestAction, dataClass)) { - throw requestForbiddenError; - } + if (Array.isArray(request.body)) { + // 1. Process Array Payloads + const items = request.body.map((item) => { + const dataClass = plainToInstance(subject, item); - const allowedFields = this.getAllowedFields( - userAbility, - requestAction, - dataClass, - subject, - ); - allowedFields.push('_id'); + if (!userAbility.can(requestAction, dataClass)) { + throw requestForbiddenError; + } - if (!allowedFields || allowedFields.length === 0) { - throw requestForbiddenError; - } + const allowedFields = this.getAllowedFields( + userAbility, + requestAction, + dataClass, + subject, + ); + allowedFields.push('_id'); - return pick(dataClass, allowedFields); - }); + if (!allowedFields || allowedFields.length === 0) { + throw requestForbiddenError; + } - request.body = items; - } else { - // 2. Process Single Object Payloads (Original Logic) - const dataClass = plainToInstance(subject, request.body); - if (!userAbility.can(requestAction, dataClass)) { - throw requestForbiddenError; - } + return pick(dataClass, allowedFields); + }); - const allowedFields = this.getAllowedFields( - userAbility, - requestAction, - dataClass, - subject, - ); - allowedFields.push('_id'); + request.body = items; + } else { + // 2. Process Single Object Payloads (Original Logic) + const dataClass = plainToInstance(subject, request.body); + if (!userAbility.can(requestAction, dataClass)) { + throw requestForbiddenError; + } - if (!allowedFields || allowedFields.length === 0) { - throw requestForbiddenError; - } + const allowedFields = this.getAllowedFields( + userAbility, + requestAction, + dataClass, + subject, + ); + allowedFields.push('_id'); - request.body = pick(dataClass, allowedFields); - } - } + if (!allowedFields || allowedFields.length === 0) { + throw requestForbiddenError; + } + + request.body = pick(dataClass, allowedFields); + } + } return next.handle().pipe( map(async (data: any) => { diff --git a/src/box/auth/boxTestingSession.guard.ts b/src/box/auth/boxTestingSession.guard.ts index 74ffa7288..5ee9ceb54 100644 --- a/src/box/auth/boxTestingSession.guard.ts +++ b/src/box/auth/boxTestingSession.guard.ts @@ -1,15 +1,11 @@ -import { - CanActivate, - ForbiddenException, - Injectable, -} from '@nestjs/common'; +import { CanActivate, ForbiddenException, Injectable } from '@nestjs/common'; import { envVars } from '../../common/service/envHandler/envVars'; import { APIErrorReason } from '../../common/controller/APIErrorReason'; import { APIError } from '../../common/controller/APIError'; /** * Guard for testing sessions. - * + * * Prohibits access to endpoints used in Box related Controllers outside testing. */ @Injectable() @@ -20,7 +16,8 @@ export class BoxTestingSessionGuard implements CanActivate { throw new ForbiddenException({ statusCode: 403, error: 'Forbidden', - message: 'Controller is only available when ENVIRONMENT is TESTING_SESSION', + message: + 'Controller is only available when ENVIRONMENT is TESTING_SESSION', errors: [ new APIError({ reason: APIErrorReason.NOT_ALLOWED, diff --git a/src/box/box.controller.ts b/src/box/box.controller.ts index 6ad6ffe08..9f8796fc3 100644 --- a/src/box/box.controller.ts +++ b/src/box/box.controller.ts @@ -41,10 +41,7 @@ import { Action } from '../authorization/enum/action.enum'; import { BoxTestingSessionGuard } from './auth/boxTestingSession.guard'; @Controller('box') -@UseGuards( - BoxAuthGuard, - BoxTestingSessionGuard -) +@UseGuards(BoxAuthGuard, BoxTestingSessionGuard) export class BoxController { public constructor( @InjectModel(GroupAdmin.name) public readonly groupModel: Model,