forked from Shared/esg
Implement normalized error handling with Persian translations across all exception types, replace legacy NestJS exceptions with AppException, and add unit, integration, and smoke tests. - Add error catalog with gateway-owned codes and Persian messages - Introduce AppException wrapping normalized error envelopes - Add translateError helper for automatic messageFa population - Remove claims module and update provider error normalization - Add unit tests for error contracts and helper functions - Add integration tests for admin and inquiry endpoints - Add smoke tests for real provider connectivity - Add test support utilities (auth mocks, assertions, app factory)
64 lines
1.9 KiB
TypeScript
64 lines
1.9 KiB
TypeScript
import { CanActivate, ExecutionContext } from '@nestjs/common';
|
|
import { AuthenticatedUser } from '../../src/auth/interfaces/authenticated-user.interface';
|
|
import { ClientType } from '../../src/common/enums/client-type.enum';
|
|
import { InquiryType } from '../../src/common/enums/inquiry-type.enum';
|
|
import { Role } from '../../src/common/enums/role.enum';
|
|
import { AppException } from '../../src/common/exceptions/app-exception';
|
|
|
|
export function makeAuthenticatedUser(
|
|
overrides: Partial<AuthenticatedUser> = {},
|
|
): AuthenticatedUser {
|
|
return {
|
|
id: 'user-1',
|
|
username: 'admin',
|
|
email: 'admin@example.com',
|
|
role: Role.SUPER_ADMIN,
|
|
clientType: ClientType.INTERNAL,
|
|
appName: 'test-admin',
|
|
allowedInquiries: Object.values(InquiryType),
|
|
requestLimitPerMinute: 100,
|
|
requestLimitPerDay: 10000,
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
export function authenticatedGuard(user = makeAuthenticatedUser()): CanActivate {
|
|
return {
|
|
canActivate(context: ExecutionContext): boolean {
|
|
const request = context.switchToHttp().getRequest<{ user?: AuthenticatedUser }>();
|
|
request.user = user;
|
|
return true;
|
|
},
|
|
};
|
|
}
|
|
|
|
export function bearerTokenGuard(user = makeAuthenticatedUser()): CanActivate {
|
|
return {
|
|
canActivate(context: ExecutionContext): boolean {
|
|
const request = context
|
|
.switchToHttp()
|
|
.getRequest<{ headers: Record<string, string | undefined>; user?: AuthenticatedUser }>();
|
|
const authorization = request.headers.authorization;
|
|
|
|
if (!authorization) {
|
|
throw new AppException('UNAUTHORIZED');
|
|
}
|
|
|
|
if (authorization === 'Bearer expired') {
|
|
throw new AppException('UNAUTHORIZED', { message: 'jwt expired' });
|
|
}
|
|
|
|
request.user = user;
|
|
return true;
|
|
},
|
|
};
|
|
}
|
|
|
|
export function passGuard(): CanActivate {
|
|
return {
|
|
canActivate(): boolean {
|
|
return true;
|
|
},
|
|
};
|
|
}
|