-
Notifications
You must be signed in to change notification settings - Fork 1
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 #6 from BrewInteractive/feature/create-GetAuthoriz…
…ation-decorator Feature/create get authorization decorator
- Loading branch information
Showing
6 changed files
with
90 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { Faker } from 'mockingbird'; | ||
import { AuthorizationToken } from './authorization-token.decorator'; | ||
import { ROUTE_ARGS_METADATA } from '@nestjs/common/constants'; | ||
|
||
function getParamDecoratorFactory() { | ||
class TestDecorator { | ||
public getDecoratorValue(@AuthorizationToken() value) { | ||
console.log(value); | ||
} | ||
} | ||
|
||
const args = Reflect.getMetadata( | ||
ROUTE_ARGS_METADATA, | ||
TestDecorator, | ||
'getDecoratorValue', | ||
); | ||
return args[Object.keys(args)[0]].factory; | ||
} | ||
|
||
describe('AuthorizationToken Decorator', () => { | ||
it('should return the authorization header', () => { | ||
const token = Faker.datatype.string(); | ||
const mockRequest = { | ||
headers: { | ||
authorization: `Bearer ${token}`, | ||
}, | ||
}; | ||
|
||
const mockContext = { | ||
switchToHttp: jest.fn().mockReturnValue({ | ||
getRequest: jest.fn().mockReturnValue(mockRequest), | ||
}), | ||
}; | ||
|
||
const factory = getParamDecoratorFactory(); | ||
const authorization = factory('authorization', mockContext); | ||
|
||
expect(authorization).toBe(token); | ||
}); | ||
|
||
it('should return undefined if the token type is not bearer ', () => { | ||
const mockRequest = { | ||
headers: { | ||
authorization: 'test mockToken', | ||
}, | ||
}; | ||
|
||
const mockContext = { | ||
switchToHttp: jest.fn().mockReturnValue({ | ||
getRequest: jest.fn().mockReturnValue(mockRequest), | ||
}), | ||
}; | ||
|
||
const factory = getParamDecoratorFactory(); | ||
const authorization = factory('authorization', mockContext); | ||
|
||
expect(authorization).toBeUndefined(); | ||
}); | ||
|
||
it('should return undefined if no authorization header is present', () => { | ||
const mockRequest = { | ||
headers: {}, | ||
}; | ||
|
||
const mockContext = { | ||
switchToHttp: jest.fn().mockReturnValue({ | ||
getRequest: jest.fn().mockReturnValue(mockRequest), | ||
}), | ||
}; | ||
|
||
const factory = getParamDecoratorFactory(); | ||
const authorization = factory('authorization', mockContext); | ||
|
||
expect(authorization).toBeUndefined(); | ||
}); | ||
}); |
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,9 @@ | ||
import { ExecutionContext, createParamDecorator } from '@nestjs/common'; | ||
|
||
export const AuthorizationToken = createParamDecorator( | ||
(_: unknown, context: ExecutionContext): string => { | ||
const request = context.switchToHttp().getRequest(); | ||
const [type, token] = request.headers['authorization']?.split(' ') ?? []; | ||
return type === 'Bearer' ? token : undefined; | ||
}, | ||
); |
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 @@ | ||
export * from './authorization-token.decorator'; |
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