Skip to content

Commit a68c228

Browse files
committed
fix: update docs for context definition on RequireFlagsEnabledProps. add tests with targeting rules defined for the InMemoryProvider
Signed-off-by: Kaushal Kapasi <[email protected]>
1 parent 0118d87 commit a68c228

File tree

4 files changed

+22
-16
lines changed

4 files changed

+22
-16
lines changed

packages/nest/src/require-flags-enabled.decorator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ interface RequireFlagsEnabledProps {
3333
domain?: string;
3434

3535
/**
36-
* Global {@link EvaluationContext} for OpenFeature.
36+
* The {@link EvaluationContext} for evaluating the feature flag.
3737
* @see {@link OpenFeature#setContext}
3838
*/
3939
context?: EvaluationContext;

packages/nest/test/fixtures.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { InMemoryProvider } from '@openfeature/server-sdk';
2+
import type { EvaluationContext } from '@openfeature/server-sdk';
23
import type { ExecutionContext } from '@nestjs/common';
34
import { OpenFeatureModule } from '../src';
45

@@ -27,6 +28,12 @@ export const defaultProvider = new InMemoryProvider({
2728
defaultVariant: 'default',
2829
variants: { default: false, enabled: true },
2930
disabled: false,
31+
contextEvaluator: (ctx: EvaluationContext) => {
32+
if (ctx.targetingKey === '123') {
33+
return 'enabled';
34+
}
35+
return 'default';
36+
},
3037
},
3138
});
3239

packages/nest/test/open-feature-sdk.spec.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,6 @@ describe('OpenFeature SDK', () => {
149149
});
150150

151151
it('should throw a custom exception if the flag is disabled with context', async () => {
152-
jest.spyOn(defaultProvider, 'resolveBooleanEvaluation').mockResolvedValueOnce({
153-
value: false,
154-
reason: 'DISABLED',
155-
});
156152
await supertest(app.getHttpServer())
157153
.get('/flags-enabled-custom-exception-with-context')
158154
.set('x-user-id', '123')
@@ -161,21 +157,24 @@ describe('OpenFeature SDK', () => {
161157
});
162158

163159
describe('OpenFeatureControllerRequireFlagsEnabled', () => {
164-
it('should allow access to the RequireFlagsEnabled controller', async () => {
165-
// Only mock the first flag evaluation for Flag with key `testBooleanFlag2`, the second flag evaluation will use the default variation for flag with key `testBooleanFlag`
166-
jest.spyOn(defaultProvider, 'resolveBooleanEvaluation').mockResolvedValueOnce({
167-
value: true,
168-
reason: 'TARGETING_MATCH',
169-
});
170-
await supertest(app.getHttpServer()).get('/require-flags-enabled').expect(200).expect('Hello, world!');
160+
it('should allow access to the RequireFlagsEnabled controller with global context interceptor', async () => {
161+
await supertest(app.getHttpServer())
162+
.get('/require-flags-enabled')
163+
.set('x-user-id', '123')
164+
.expect(200)
165+
.expect('Hello, world!');
166+
});
167+
168+
it('should throw a 403 - Forbidden exception if user does not match targeting requirements', async () => {
169+
await supertest(app.getHttpServer()).get('/require-flags-enabled').set('x-user-id', 'not-123').expect(403);
171170
});
172171

173-
it('should throw a 403 - Forbidden exception if the flag is disabled', async () => {
172+
it('should throw a 403 - Forbidden exception if one of the flags is disabled', async () => {
174173
jest.spyOn(defaultProvider, 'resolveBooleanEvaluation').mockResolvedValueOnce({
175174
value: false,
176175
reason: 'DISABLED',
177176
});
178-
await supertest(app.getHttpServer()).get('/require-flags-enabled').expect(403);
177+
await supertest(app.getHttpServer()).get('/require-flags-enabled').set('x-user-id', '123').expect(403);
179178
});
180179
});
181180
});

packages/nest/test/test-app.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export class OpenFeatureController {
110110
}
111111

112112
@RequireFlagsEnabled({
113-
flags: [{ flagKey: 'testBooleanFlag' }],
113+
flags: [{ flagKey: 'testBooleanFlag2' }],
114114
exception: new ForbiddenException(),
115115
context: {
116116
targetingKey: 'user-id',
@@ -150,7 +150,7 @@ export class OpenFeatureContextScopedController {
150150

151151
@Controller('require-flags-enabled')
152152
@RequireFlagsEnabled({
153-
flags: [{ flagKey: 'testBooleanFlag2', defaultValue: true }, { flagKey: 'testBooleanFlag' }],
153+
flags: [{ flagKey: 'testBooleanFlag', defaultValue: false }, { flagKey: 'testBooleanFlag2' }],
154154
exception: new ForbiddenException(),
155155
})
156156
export class OpenFeatureRequireFlagsEnabledController {

0 commit comments

Comments
 (0)