-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #357 from internxt/chore/workspaces-gateway-and-mi…
…scelaneous-files [_] chore: added mailer service, files/folders/gateway/workspaces dtos
- Loading branch information
Showing
31 changed files
with
851 additions
and
4 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
src/common/http-exception-filter-extended.exception.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { ArgumentsHost, HttpException, Logger } from '@nestjs/common'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { BaseExceptionFilter } from '@nestjs/core'; | ||
import { newUser } from '../../test/fixtures'; | ||
import { ExtendedHttpExceptionFilter } from './http-exception-filter-extended.exception'; | ||
|
||
describe('ExtendedHttpExceptionFilter', () => { | ||
let filter: ExtendedHttpExceptionFilter; | ||
let loggerErrorSpy: jest.SpyInstance; | ||
let baseExceptionFilterCatchSpy: jest.SpyInstance; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ExtendedHttpExceptionFilter], | ||
}).compile(); | ||
|
||
filter = module.get<ExtendedHttpExceptionFilter>( | ||
ExtendedHttpExceptionFilter, | ||
); | ||
loggerErrorSpy = jest.spyOn(Logger.prototype, 'error').mockImplementation(); | ||
baseExceptionFilterCatchSpy = jest | ||
.spyOn(BaseExceptionFilter.prototype, 'catch') | ||
.mockImplementation(); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
const createMockArgumentsHost = (url: string, method: string, user: any) => | ||
({ | ||
switchToHttp: jest.fn().mockReturnValue({ | ||
getRequest: jest.fn().mockReturnValue({ | ||
url, | ||
method, | ||
user, | ||
}), | ||
getResponse: jest.fn().mockReturnValue({ | ||
statusCode: 500, | ||
headers: {}, | ||
getHeader: jest.fn(), | ||
setHeader: jest.fn(), | ||
isHeadersSent: false, | ||
}), | ||
getNext: jest.fn(), | ||
}), | ||
getArgs: jest.fn(), | ||
getArgByIndex: jest.fn(), | ||
switchToRpc: jest.fn(), | ||
switchToWs: jest.fn(), | ||
getType: jest.fn(), | ||
}) as unknown as ArgumentsHost; | ||
|
||
it('When non expected error are sent, then it should log details and call parent catch', () => { | ||
const mockException = new Error('Unexpected error'); | ||
const user = newUser(); | ||
const mockHost = createMockArgumentsHost('/my-endpoint', 'GET', user); | ||
|
||
filter.catch(mockException, mockHost); | ||
|
||
expect(loggerErrorSpy).toHaveBeenCalled(); | ||
expect(baseExceptionFilterCatchSpy).toHaveBeenCalledWith( | ||
mockException, | ||
mockHost, | ||
); | ||
}); | ||
|
||
it('When expected exception is sent, then it should not log detailss and call parent catch', () => { | ||
const mockException = new HttpException('This is an http error', 400); | ||
const user = newUser(); | ||
const mockHost = createMockArgumentsHost('/my-endpoint', 'GET', user); | ||
|
||
filter.catch(mockException, mockHost); | ||
|
||
expect(loggerErrorSpy).not.toHaveBeenCalled(); | ||
expect(baseExceptionFilterCatchSpy).toHaveBeenCalledWith( | ||
mockException, | ||
mockHost, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Catch, Logger, HttpException, ArgumentsHost } from '@nestjs/common'; | ||
import { BaseExceptionFilter } from '@nestjs/core'; | ||
|
||
@Catch() | ||
export class ExtendedHttpExceptionFilter extends BaseExceptionFilter { | ||
private readonly logger = new Logger(ExtendedHttpExceptionFilter.name); | ||
|
||
catch(exception: any, host: ArgumentsHost) { | ||
const ctx = host.switchToHttp(); | ||
|
||
const request = ctx.getRequest(); | ||
|
||
if (!(exception instanceof HttpException)) { | ||
const errorResponse = { | ||
timestamp: new Date().toISOString(), | ||
path: request.url, | ||
method: request.method, | ||
message: (exception as Error)?.message, | ||
stack: (exception as Error)?.stack, | ||
user: { email: request?.user?.email, uuid: request?.user?.uuid }, | ||
}; | ||
|
||
this.logger.error( | ||
`[UNEXPECTED_ERROR] - Details: ${JSON.stringify(errorResponse)}`, | ||
); | ||
} | ||
|
||
super.catch(exception, host); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import { SetMetadata } from '@nestjs/common'; | ||
export const DisableGlobalAuth = () => SetMetadata('disableGlobalAuth', true); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { PassportStrategy } from '@nestjs/passport'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { ExtractJwt, Strategy } from 'passport-jwt'; | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
const strategyId = 'gateway.jwt.rs256'; | ||
@Injectable() | ||
export class GatewayRS256JwtStrategy extends PassportStrategy( | ||
Strategy, | ||
strategyId, | ||
) { | ||
static id = strategyId; | ||
constructor(configService: ConfigService) { | ||
super({ | ||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), | ||
ignoreExpiration: false, | ||
secretOrKey: Buffer.from( | ||
configService.get('secrets.driveGateway') as string, | ||
'base64', | ||
).toString('utf8'), | ||
algorithms: ['RS256'], | ||
}); | ||
} | ||
|
||
async validate(): Promise<boolean> { | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { ExecutionContext, Injectable } from '@nestjs/common'; | ||
import { Reflector } from '@nestjs/core'; | ||
import { AuthGuard as PassportAuthGuard } from '@nestjs/passport'; | ||
import { GatewayRS256JwtStrategy } from './gateway-rs256jwt.strategy'; | ||
|
||
@Injectable() | ||
export class GatewayGuard extends PassportAuthGuard( | ||
GatewayRS256JwtStrategy.id, | ||
) { | ||
constructor(private readonly reflector: Reflector) { | ||
super(); | ||
} | ||
|
||
canActivate(context: ExecutionContext) { | ||
return super.canActivate(context); | ||
} | ||
} |
Oops, something went wrong.