-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(firebase): enhance Firebase integration with improved decorators…
… and guards
- Loading branch information
Showing
9 changed files
with
179 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
export const FIREBASE_ADMIN_MODULE_OPTIONS = 'FIREBASE_ADMIN_MODULE_OPTIONS'; | ||
export const FIREBASE_ADMIN_NAME = 'FIREBASE_ADMIN_NAME'; | ||
export const FIREBASE_ADMIN_INJECT = 'FIREBASE_ADMIN_INJECT'; | ||
export const FIREBASE_ADMIN_CONFIG = 'FIREBASE_ADMIN_CONFIG'; | ||
export const FIREBASE_ADMIN_AUTH_STRATEGY = 'FIREBASE_ADMIN_AUTH_STRATEGY'; | ||
|
||
export const FIREBASE_USER_METADATA = 'FIREBASE_USER_METADATA'; | ||
export const FIREBASE_TOKEN_USER_METADATA = 'FIREBASE_USER_METADATA'; | ||
export const FIREBASE_CLAIMS_USER_METADATA = 'FIREBASE_CLAIMS_METADATA'; | ||
export const FIREBASE_APP_ROLES_DECORATOR = 'ROLES'; |
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,42 @@ | ||
import { ExecutionContext } from '@nestjs/common'; | ||
import { ClaimsFactory } from './claims.decorator'; | ||
import { FIREBASE_CLAIMS_USER_METADATA } from '../constant/firebase.constant'; | ||
|
||
const mockExecutionContext: ExecutionContext = { | ||
getType: jest.fn(), | ||
getArgByIndex: jest.fn(), | ||
getArgs: jest.fn(), | ||
getClass: jest.fn(), | ||
getHandler: jest.fn(), | ||
switchToHttp: jest.fn(), | ||
switchToRpc: jest.fn(), | ||
switchToWs: jest.fn(), | ||
}; | ||
|
||
describe('Firebase Claims Decorator - Unit Test', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should extract the claims from the request context in an HTTP context', () => { | ||
const mockClaims = { | ||
claims: 'test_claims', | ||
}; | ||
|
||
jest.spyOn(mockExecutionContext, 'getType').mockReturnValue('http'); | ||
|
||
const mockRequest = { | ||
metadata: { | ||
[FIREBASE_CLAIMS_USER_METADATA]: mockClaims, | ||
}, | ||
}; | ||
|
||
jest.spyOn(mockExecutionContext, 'switchToHttp').mockReturnValue({ | ||
getRequest: () => mockRequest, | ||
} as any); | ||
|
||
const result = ClaimsFactory(null, mockExecutionContext); | ||
|
||
expect(result).toEqual(mockClaims); | ||
}); | ||
}); |
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,10 @@ | ||
import { createParamDecorator, ExecutionContext } from '@nestjs/common'; | ||
import { FIREBASE_CLAIMS_USER_METADATA } from '../constant/firebase.constant'; | ||
|
||
export const ClaimsFactory = (data: unknown, ctx: ExecutionContext) => { | ||
const context = ctx.switchToHttp(); | ||
const request = context.getRequest(); | ||
return request.metadata?.[FIREBASE_CLAIMS_USER_METADATA as string]; | ||
}; | ||
|
||
export const FirebaseUserClaims = createParamDecorator(ClaimsFactory); |
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,42 @@ | ||
import { ExecutionContext } from '@nestjs/common'; | ||
import { FIREBASE_TOKEN_USER_METADATA } from '../constant/firebase.constant'; | ||
import { UserFactory } from './user.decorator'; | ||
|
||
const mockExecutionContext: ExecutionContext = { | ||
getType: jest.fn(), | ||
getArgByIndex: jest.fn(), | ||
getArgs: jest.fn(), | ||
getClass: jest.fn(), | ||
getHandler: jest.fn(), | ||
switchToHttp: jest.fn(), | ||
switchToRpc: jest.fn(), | ||
switchToWs: jest.fn(), | ||
}; | ||
|
||
describe('Firebase User Decorator - Unit Test', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should extract the user from the request context in an HTTP context', () => { | ||
const mockClaims = { | ||
user: 'test_user', | ||
}; | ||
|
||
jest.spyOn(mockExecutionContext, 'getType').mockReturnValue('http'); | ||
|
||
const mockRequest = { | ||
metadata: { | ||
[FIREBASE_TOKEN_USER_METADATA]: mockClaims, | ||
}, | ||
}; | ||
|
||
jest.spyOn(mockExecutionContext, 'switchToHttp').mockReturnValue({ | ||
getRequest: () => mockRequest, | ||
} as any); | ||
|
||
const result = UserFactory(null, mockExecutionContext); | ||
|
||
expect(result).toEqual(mockClaims); | ||
}); | ||
}); |
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,10 @@ | ||
import { createParamDecorator, ExecutionContext } from '@nestjs/common'; | ||
import { FIREBASE_TOKEN_USER_METADATA } from '../constant/firebase.constant'; | ||
|
||
export const UserFactory = (data: unknown, ctx: ExecutionContext) => { | ||
const context = ctx.switchToHttp(); | ||
const request = context.getRequest(); | ||
return request.metadata[FIREBASE_TOKEN_USER_METADATA as string]; | ||
}; | ||
|
||
export const FirebaseUser = createParamDecorator(UserFactory); |
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