@@ -2,7 +2,12 @@ import type { TestingModule } from '@nestjs/testing';
2
2
import { Test } from '@nestjs/testing' ;
3
3
import type { INestApplication } from '@nestjs/common' ;
4
4
import supertest from 'supertest' ;
5
- import { OpenFeatureController , OpenFeatureControllerContextScopedController , OpenFeatureTestService } from './test-app' ;
5
+ import {
6
+ OpenFeatureController ,
7
+ OpenFeatureContextScopedController ,
8
+ OpenFeatureRequireFlagsEnabledController ,
9
+ OpenFeatureTestService ,
10
+ } from './test-app' ;
6
11
import { exampleContextFactory , getOpenFeatureDefaultTestModule } from './fixtures' ;
7
12
import { OpenFeatureModule } from '../src' ;
8
13
import { defaultProvider , providers } from './fixtures' ;
@@ -14,11 +19,9 @@ describe('OpenFeature SDK', () => {
14
19
15
20
beforeAll ( async ( ) => {
16
21
moduleRef = await Test . createTestingModule ( {
17
- imports : [
18
- getOpenFeatureDefaultTestModule ( )
19
- ] ,
22
+ imports : [ getOpenFeatureDefaultTestModule ( ) ] ,
20
23
providers : [ OpenFeatureTestService ] ,
21
- controllers : [ OpenFeatureController ] ,
24
+ controllers : [ OpenFeatureController , OpenFeatureRequireFlagsEnabledController ] ,
22
25
} ) . compile ( ) ;
23
26
app = moduleRef . createNestApplication ( ) ;
24
27
app = await app . init ( ) ;
@@ -112,7 +115,7 @@ describe('OpenFeature SDK', () => {
112
115
} ) ;
113
116
114
117
describe ( 'evaluation context service should' , ( ) => {
115
- it ( 'inject the evaluation context from contex factory' , async function ( ) {
118
+ it ( 'inject the evaluation context from contex factory' , async function ( ) {
116
119
const evaluationSpy = jest . spyOn ( defaultProvider , 'resolveBooleanEvaluation' ) ;
117
120
await supertest ( app . getHttpServer ( ) )
118
121
. get ( '/dynamic-context-in-service' )
@@ -122,26 +125,62 @@ describe('OpenFeature SDK', () => {
122
125
expect ( evaluationSpy ) . toHaveBeenCalledWith ( 'testBooleanFlag' , false , { targetingKey : 'dynamic-user' } , { } ) ;
123
126
} ) ;
124
127
} ) ;
128
+
129
+ describe ( 'require flags enabled decorator' , ( ) => {
130
+ describe ( 'OpenFeatureController' , ( ) => {
131
+ it ( 'should sucessfully return the response if the flag is enabled' , async ( ) => {
132
+ await supertest ( app . getHttpServer ( ) ) . get ( '/flags-enabled' ) . expect ( 200 ) . expect ( 'Get Boolean Flag Success!' ) ;
133
+ } ) ;
134
+
135
+ it ( 'should throw an exception if the flag is disabled' , async ( ) => {
136
+ jest . spyOn ( defaultProvider , 'resolveBooleanEvaluation' ) . mockResolvedValueOnce ( {
137
+ value : false ,
138
+ reason : 'DISABLED' ,
139
+ } ) ;
140
+ await supertest ( app . getHttpServer ( ) ) . get ( '/flags-enabled' ) . expect ( 404 ) ;
141
+ } ) ;
142
+
143
+ it ( 'should throw a custom exception if the flag is disabled' , async ( ) => {
144
+ jest . spyOn ( defaultProvider , 'resolveBooleanEvaluation' ) . mockResolvedValueOnce ( {
145
+ value : false ,
146
+ reason : 'DISABLED' ,
147
+ } ) ;
148
+ await supertest ( app . getHttpServer ( ) ) . get ( '/flags-enabled-custom-exception' ) . expect ( 403 ) ;
149
+ } ) ;
150
+ } ) ;
151
+
152
+ describe ( 'OpenFeatureControllerRequireFlagsEnabled' , ( ) => {
153
+ it ( 'should allow access to the RequireFlagsEnabled controller' , async ( ) => {
154
+ await supertest ( app . getHttpServer ( ) ) . get ( '/require-flags-enabled' ) . expect ( 200 ) . expect ( 'Hello, world!' ) ;
155
+ } ) ;
156
+
157
+ it ( 'should throw a 403 - Forbidden exception if the flag is disabled' , async ( ) => {
158
+ jest . spyOn ( defaultProvider , 'resolveBooleanEvaluation' ) . mockResolvedValueOnce ( {
159
+ value : false ,
160
+ reason : 'DISABLED' ,
161
+ } ) ;
162
+ await supertest ( app . getHttpServer ( ) ) . get ( '/require-flags-enabled' ) . expect ( 403 ) ;
163
+ } ) ;
164
+ } ) ;
165
+ } ) ;
125
166
} ) ;
126
167
127
168
describe ( 'Without global context interceptor' , ( ) => {
128
-
129
169
let moduleRef : TestingModule ;
130
170
let app : INestApplication ;
131
171
132
172
beforeAll ( async ( ) => {
133
-
134
173
moduleRef = await Test . createTestingModule ( {
135
174
imports : [
136
175
OpenFeatureModule . forRoot ( {
137
176
contextFactory : exampleContextFactory ,
138
177
defaultProvider,
139
178
providers,
140
- useGlobalInterceptor : false
179
+ useGlobalInterceptor : false ,
141
180
} ) ,
142
181
] ,
143
182
providers : [ OpenFeatureTestService ] ,
144
- controllers : [ OpenFeatureController , OpenFeatureControllerContextScopedController ] ,
183
+ controllers : [ OpenFeatureController , OpenFeatureContextScopedController ] ,
145
184
} ) . compile ( ) ;
146
185
app = moduleRef . createNestApplication ( ) ;
147
186
app = await app . init ( ) ;
@@ -158,7 +197,7 @@ describe('OpenFeature SDK', () => {
158
197
} ) ;
159
198
160
199
describe ( 'evaluation context service should' , ( ) => {
161
- it ( 'inject empty context if no context interceptor is configured' , async function ( ) {
200
+ it ( 'inject empty context if no context interceptor is configured' , async function ( ) {
162
201
const evaluationSpy = jest . spyOn ( defaultProvider , 'resolveBooleanEvaluation' ) ;
163
202
await supertest ( app . getHttpServer ( ) )
164
203
. get ( '/dynamic-context-in-service' )
@@ -172,9 +211,26 @@ describe('OpenFeature SDK', () => {
172
211
describe ( 'With Controller bound Context interceptor' , ( ) => {
173
212
it ( 'should not use context if global context interceptor is not configured' , async ( ) => {
174
213
const evaluationSpy = jest . spyOn ( defaultProvider , 'resolveBooleanEvaluation' ) ;
175
- await supertest ( app . getHttpServer ( ) ) . get ( '/controller-context' ) . set ( 'x-user-id' , '123' ) . expect ( 200 ) . expect ( 'true' ) ;
214
+ await supertest ( app . getHttpServer ( ) )
215
+ . get ( '/controller-context' )
216
+ . set ( 'x-user-id' , '123' )
217
+ . expect ( 200 )
218
+ . expect ( 'true' ) ;
176
219
expect ( evaluationSpy ) . toHaveBeenCalledWith ( 'testBooleanFlag' , false , { targetingKey : '123' } , { } ) ;
177
220
} ) ;
178
221
} ) ;
222
+
223
+ describe ( 'require flags enabled decorator' , ( ) => {
224
+ it ( 'should return a 404 - Not Found exception if the flag is disabled' , async ( ) => {
225
+ jest . spyOn ( providers . domainScopedClient , 'resolveBooleanEvaluation' ) . mockResolvedValueOnce ( {
226
+ value : false ,
227
+ reason : 'DISABLED' ,
228
+ } ) ;
229
+ await supertest ( app . getHttpServer ( ) )
230
+ . get ( '/controller-context/flags-enabled' )
231
+ . set ( 'x-user-id' , '123' )
232
+ . expect ( 404 ) ;
233
+ } ) ;
234
+ } ) ;
179
235
} ) ;
180
236
} ) ;
0 commit comments