-
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.
chore: added main, bridge, auth guard and gateway guard
- Loading branch information
Showing
6 changed files
with
140 additions
and
4 deletions.
There are no files selected for viewing
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,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); | ||
} | ||
} |
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,69 @@ | ||
import { ValidateUUIDPipe } from './validate-uuid.pipe'; | ||
import { BadRequestException, ArgumentMetadata } from '@nestjs/common'; | ||
|
||
describe('ValidateUUIDPipe', () => { | ||
let pipe: ValidateUUIDPipe; | ||
|
||
beforeEach(() => { | ||
pipe = new ValidateUUIDPipe(); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(pipe).toBeDefined(); | ||
}); | ||
|
||
it('When uuid given is valid, then it should return the same value', () => { | ||
const validUUID = '123e4567-e89b-12d3-a456-426614174000'; | ||
const metadata: ArgumentMetadata = { | ||
type: 'body', | ||
metatype: null, | ||
data: 'testUUID', | ||
}; | ||
expect(pipe.transform(validUUID, metadata)).toEqual(validUUID); | ||
}); | ||
|
||
it('When UUID is invalid, then it should throw', () => { | ||
const invalidUUID = 'invalid-uuid'; | ||
const metadata: ArgumentMetadata = { | ||
type: 'body', | ||
metatype: null, | ||
data: 'testUUID', | ||
}; | ||
expect(() => pipe.transform(invalidUUID, metadata)).toThrow( | ||
BadRequestException, | ||
); | ||
expect(() => pipe.transform(invalidUUID, metadata)).toThrow( | ||
`Value of 'testUUID' is not a valid UUID.`, | ||
); | ||
}); | ||
|
||
it('When UUID is null, then it should throw', () => { | ||
const nullUUID = null; | ||
const metadata: ArgumentMetadata = { | ||
type: 'body', | ||
metatype: null, | ||
data: 'testUUID', | ||
}; | ||
expect(() => pipe.transform(nullUUID, metadata)).toThrow( | ||
BadRequestException, | ||
); | ||
expect(() => pipe.transform(nullUUID, metadata)).toThrow( | ||
`Value of 'testUUID' is not a valid UUID.`, | ||
); | ||
}); | ||
|
||
it('When UUID is undefined, then it should throw', () => { | ||
const undefinedUUID = undefined; | ||
const metadata: ArgumentMetadata = { | ||
type: 'body', | ||
metatype: null, | ||
data: 'testUUID', | ||
}; | ||
expect(() => pipe.transform(undefinedUUID, metadata)).toThrow( | ||
BadRequestException, | ||
); | ||
expect(() => pipe.transform(undefinedUUID, metadata)).toThrow( | ||
`Value of 'testUUID' is not a valid UUID.`, | ||
); | ||
}); | ||
}); |
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,18 @@ | ||
import { | ||
PipeTransform, | ||
Injectable, | ||
ArgumentMetadata, | ||
BadRequestException, | ||
} from '@nestjs/common'; | ||
import { isUUID } from 'class-validator'; | ||
|
||
@Injectable() | ||
export class ValidateUUIDPipe implements PipeTransform { | ||
transform(value: any, metadata: ArgumentMetadata) { | ||
if (!value || !isUUID(value)) | ||
throw new BadRequestException( | ||
`Value of '${metadata.data}' is not a valid UUID.`, | ||
); | ||
return value; | ||
} | ||
} |