-
Notifications
You must be signed in to change notification settings - Fork 5
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 #5 from rorteg/feat/decorator-guard
feat: added feature toggle guard
- Loading branch information
Showing
5 changed files
with
175 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { SetMetadata } from '@nestjs/common'; | ||
|
||
export const FEATURE_TOGGLE_DECORATOR_KEY = 'FEATURE_TOGGLE'; | ||
export const FeatureEnabled = (permissions: string) => | ||
SetMetadata(FEATURE_TOGGLE_DECORATOR_KEY, permissions); |
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,25 @@ | ||
import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common'; | ||
import { Reflector } from '@nestjs/core'; | ||
import { FEATURE_TOGGLE_DECORATOR_KEY } from '../decorators/feature-toggle.decorator'; | ||
import { FeatureToggleService } from '../feature-toggle.service'; | ||
|
||
@Injectable() | ||
export class FeatureToggleGuard implements CanActivate { | ||
constructor( | ||
private reflector: Reflector, | ||
private featureToggleService: FeatureToggleService | ||
) {} | ||
|
||
async canActivate( | ||
context: ExecutionContext | ||
): Promise<boolean> { | ||
const featureKey = this.reflector.get<string>( | ||
FEATURE_TOGGLE_DECORATOR_KEY, | ||
context.getHandler() | ||
); | ||
if (!featureKey) { | ||
return true; | ||
} | ||
return await this.featureToggleService.isFeatureEnabled(featureKey, 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
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,100 @@ | ||
import { ExecutionContext } from '@nestjs/common'; | ||
import { Reflector } from '@nestjs/core'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { FeatureToggleGuard, FeatureToggleService } from '..'; | ||
|
||
const createContextMock = (requestData = {}) => { | ||
const context: ExecutionContext = { | ||
switchToHttp: () => context, | ||
getRequest: () => { | ||
return requestData; | ||
}, | ||
getResponse: jest.fn(), | ||
getHandler: jest.fn(), | ||
getClass: jest.fn() | ||
} as unknown as ExecutionContext; | ||
return context; | ||
}; | ||
|
||
describe('FeatureToggleGuard', () => { | ||
let guard: FeatureToggleGuard; | ||
let reflector: Reflector; | ||
let featureToggleService: FeatureToggleService; | ||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
FeatureToggleGuard, | ||
{ | ||
provide: Reflector, | ||
useValue: { | ||
constructor: jest.fn(), | ||
get: jest.fn() | ||
} | ||
}, | ||
{ | ||
provide: FeatureToggleService, | ||
useValue: { | ||
constructor: jest.fn(), | ||
isFeatureEnabled: jest.fn() | ||
} | ||
} | ||
] | ||
}).compile(); | ||
|
||
guard = module.get<FeatureToggleGuard>(FeatureToggleGuard); | ||
featureToggleService = | ||
module.get<FeatureToggleService>(FeatureToggleService); | ||
reflector = module.get<Reflector>(Reflector); | ||
}); | ||
|
||
afterEach(async () => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(guard).toBeDefined(); | ||
}); | ||
|
||
it('should return true if the `Feature` decorator is not set', async () => { | ||
jest.spyOn(reflector, 'get').mockImplementation((a: any, b: any) => []); | ||
jest | ||
.spyOn(featureToggleService, 'isFeatureEnabled') | ||
.mockReturnValue(Promise.resolve(true)); | ||
|
||
const context = createContextMock({ headers: { TEST_FEATURE: '1' } }); | ||
const result = await guard.canActivate(context); | ||
|
||
expect(result).toBeTruthy(); | ||
expect(reflector.get).toBeCalledTimes(1); | ||
}); | ||
|
||
it('should return false if the `Feature` decorator is set but the feature is disable', async () => { | ||
jest.spyOn(reflector, 'get').mockReturnValue('TEST_FEATURE'); | ||
jest | ||
.spyOn(featureToggleService, 'isFeatureEnabled') | ||
.mockReturnValue(Promise.resolve(false)); | ||
|
||
const context = createContextMock({ | ||
headers: { TEST_FEATURE: '0' } | ||
}); | ||
|
||
const result = await guard.canActivate(context); | ||
expect(result).toBeFalsy(); | ||
expect(reflector.get).toBeCalledTimes(1); | ||
}); | ||
|
||
it('should return true if the `Feature` decorator is set but the feature is enable', async () => { | ||
jest.spyOn(reflector, 'get').mockReturnValue('TEST_FEATURE'); | ||
jest | ||
.spyOn(featureToggleService, 'isFeatureEnabled') | ||
.mockReturnValue(Promise.resolve(true)); | ||
|
||
const context = createContextMock({ | ||
headers: { TEST_FEATURE: '0' } | ||
}); | ||
|
||
const result = await guard.canActivate(context); | ||
expect(result).toBeTruthy(); | ||
expect(reflector.get).toBeCalledTimes(1); | ||
}); | ||
}); |