11const spt = require ( 'supertest' ) ;
22const should = require ( 'should' ) ;
3- const testUtils = require ( '../../test/testUtils' ) ;
4- const plugins = require ( '../../plugins/pluginManager' ) ;
3+ const testUtils = require ( '../../../test/testUtils' ) ;
54
65const request = spt ( testUtils . url ) ;
76// change these in local testing directly or set env vars (also COUNTLY_CONFIG_HOSTNAME should be set with port)
87let API_KEY_ADMIN = testUtils . get ( "API_KEY_ADMIN" ) ;
98let APP_KEY = testUtils . get ( 'APP_KEY' ) ;
109let APP_ID = testUtils . get ( "APP_ID" ) ;
1110const config_ver = 2 ;
11+ var validOptions = {
12+ "tracking" : false ,
13+ "networking" : false ,
14+ "crt" : false ,
15+ "vt" : false ,
16+ "st" : false ,
17+ "cet" : false ,
18+ "ecz" : false ,
19+ "cr" : false ,
20+ "sui" : false ,
21+ "eqs" : false ,
22+ "rqs" : false ,
23+ "czi" : false ,
24+ "dort" : false ,
25+ "scui" : false ,
26+ "lkl" : false ,
27+ "lvs" : false ,
28+ "lsv" : false ,
29+ "lbc" : false ,
30+ "ltlpt" : false ,
31+ "ltl" : false ,
32+ "lt" : false ,
33+ "rcz" : false ,
34+ "bom" : false ,
35+ "bom_at" : false ,
36+ "bom_rqp" : false ,
37+ "bom_ra" : false ,
38+ "bom_d" : false
39+ } ;
1240
1341describe ( 'SDK Plugin' , function ( ) {
42+ beforeEach ( function ( done ) {
43+ request
44+ . post ( '/i/sdk-config/update-enforcement' )
45+ . query ( { api_key : API_KEY_ADMIN , app_id : APP_ID , enforcement : JSON . stringify ( validOptions ) } )
46+ . expect ( 200 )
47+ . end ( function ( err , res ) {
48+ should . not . exist ( err ) ;
49+ res . body . should . have . property ( 'result' , 'Success' ) ;
50+ done ( ) ;
51+ } ) ;
52+ } ) ;
53+
1454 //==================================================================================================================
1555 // method=sc tests
1656 //==================================================================================================================
@@ -145,7 +185,8 @@ describe('SDK Plugin', function() {
145185 } ) ;
146186 } ) ;
147187
148- it ( '8. should update correct config parameter' , function ( done ) {
188+ // modified after introducing enforcement
189+ it ( '8. should not return unenforced config parameter' , function ( done ) {
149190 request
150191 . get ( '/o' )
151192 . query ( { method : 'config-upload' , api_key : API_KEY_ADMIN , app_id : APP_ID , config : JSON . stringify ( { lt : 500 } ) } )
@@ -160,7 +201,7 @@ describe('SDK Plugin', function() {
160201 . end ( function ( err , res ) {
161202 should . not . exist ( err ) ;
162203 checkCommonConfigParam ( res ) ;
163- res . body . c . lt . should . be . exactly ( 500 ) ;
204+ res . body . c . should . not . have . property ( 'lt' ) ; // by default it is not enforced
164205 done ( ) ;
165206 } ) ;
166207 } ) ;
@@ -188,6 +229,101 @@ describe('SDK Plugin', function() {
188229 } ) ;
189230
190231 } ) ;
232+ describe ( 'POST /i/sdk-config/update-enforcement' , function ( ) {
233+ it ( '1. should return enforced config parameter' , function ( done ) {
234+ request
235+ . get ( '/o' )
236+ . query ( { method : 'config-upload' , api_key : API_KEY_ADMIN , app_id : APP_ID , config : JSON . stringify ( { lt : 500 } ) } )
237+ . expect ( 200 )
238+ . end ( function ( err , res ) {
239+ should . not . exist ( err ) ;
240+ res . body . should . have . property ( 'result' , 'Success' ) ;
241+ request
242+ . post ( '/i/sdk-config/update-enforcement' )
243+ . query ( { api_key : API_KEY_ADMIN , app_id : APP_ID , enforcement : JSON . stringify ( { lt : true } ) } )
244+ . expect ( 200 )
245+ . end ( function ( err , res ) {
246+ should . not . exist ( err ) ;
247+ res . body . should . have . property ( 'result' , 'Success' ) ;
248+ request
249+ . get ( '/o/sdk' )
250+ . query ( { method : 'sc' , app_key : APP_KEY , device_id : 'test' } )
251+ . expect ( 200 )
252+ . end ( function ( err , res ) {
253+ should . not . exist ( err ) ;
254+ checkCommonConfigParam ( res ) ;
255+ res . body . c . lt . should . be . exactly ( 500 ) ; // lt is enforced
256+ done ( ) ;
257+ } ) ;
258+ } ) ;
259+ } ) ;
260+ } ) ;
261+ checkBadCredentials ( '/i/sdk-config/update-enforcement' , 'sdk-config' , false , true ) ;
262+ it ( '7. should reject invalid enforcement format, string' , function ( done ) {
263+ request
264+ . post ( '/i/sdk-config/update-enforcement' )
265+ . query ( { api_key : API_KEY_ADMIN , app_id : APP_ID , enforcement : 'invalid json' } )
266+ . expect ( 400 )
267+ . end ( function ( err , res ) {
268+ should . not . exist ( err ) ;
269+ res . body . should . have . property ( 'result' , 'Error parsing enforcement' ) ;
270+ done ( ) ;
271+ } ) ;
272+ } ) ;
273+ it ( '8. should reject invalid enforcement format, array' , function ( done ) {
274+ request
275+ . post ( '/i/sdk-config/update-enforcement' )
276+ . query ( { api_key : API_KEY_ADMIN , app_id : APP_ID , enforcement : [ ] } )
277+ . expect ( 400 )
278+ . end ( function ( err , res ) {
279+ should . not . exist ( err ) ;
280+ res . body . should . have . property ( 'result' , 'Wrong enforcement format' ) ;
281+ done ( ) ;
282+ } ) ;
283+ } ) ;
284+ it ( '9. should remove unwanted keys' , function ( done ) {
285+ request
286+ . post ( '/i/sdk-config/update-enforcement' )
287+ . query ( { api_key : API_KEY_ADMIN , app_id : APP_ID , enforcement : JSON . stringify ( { ...validOptions , garbage : true } ) } )
288+ . expect ( 200 )
289+ . end ( function ( err , res ) {
290+ should . not . exist ( err ) ;
291+ res . body . should . have . property ( 'result' , 'Success' ) ;
292+ request
293+ . get ( '/o' )
294+ . query ( { method : 'sdk-enforcement' , api_key : API_KEY_ADMIN , app_id : APP_ID } )
295+ . expect ( 200 )
296+ . end ( function ( err , res ) {
297+ should . not . exist ( err ) ;
298+ for ( var key in validOptions ) {
299+ res . body . should . have . property ( key ) ;
300+ res . body [ key ] . should . be . a . Boolean ( ) ;
301+ res . body [ key ] . should . be . exactly ( validOptions [ key ] ) ;
302+ }
303+ res . body . should . not . have . property ( 'garbage' ) ; // garbage should be removed
304+ done ( ) ;
305+ } ) ;
306+ } ) ;
307+ } ) ;
308+ } ) ;
309+ describe ( 'GET /o?method=sdk-enforcement' , function ( ) {
310+ it ( '1. should return enforcement info for the app' , function ( done ) {
311+ request
312+ . get ( '/o' )
313+ . query ( { method : 'sdk-enforcement' , api_key : API_KEY_ADMIN , app_id : APP_ID } )
314+ . expect ( 200 )
315+ . end ( function ( err , res ) {
316+ should . not . exist ( err ) ;
317+ for ( var key in validOptions ) {
318+ res . body . should . have . property ( key ) ;
319+ res . body [ key ] . should . be . a . Boolean ( ) ;
320+ res . body [ key ] . should . be . exactly ( validOptions [ key ] ) ;
321+ }
322+ done ( ) ;
323+ } ) ;
324+ } ) ;
325+ checkBadCredentials ( '/o' , 'sdk-enforcement' ) ;
326+ } ) ;
191327} ) ;
192328
193329/**
@@ -225,9 +361,13 @@ function checkBadCredentials(endpoint, method, userType, usePost) {
225361 { method : method , app_id : APP_ID } ,
226362 { method : method , api_key : API_KEY_ADMIN , app_id : 'invalid_app_id' } ,
227363 { method : method , api_key : API_KEY_ADMIN } ,
228- { method : method }
364+ { method : method }
229365 ] ;
230366 var responses = [ 'User does not exist' , 'Missing parameter "api_key" or "auth_token"' , 'Invalid parameter "app_id"' , 'Missing parameter "app_id"' , 'Missing parameter "app_id"' ] ;
367+ if ( usePost ) {
368+ titles [ 4 ] = '6. should require api_key or auth_token' ;
369+ responses [ 4 ] = 'Missing parameter "api_key" or "auth_token"' ;
370+ }
231371
232372 // for app_key and device_id requiring endpoints
233373 if ( userType ) {
@@ -249,9 +389,6 @@ function checkBadCredentials(endpoint, method, userType, usePost) {
249389 . get ( endpoint )
250390 . query ( queries [ index ] ) ;
251391 if ( usePost ) {
252- if ( index === 3 || index === 4 ) {
253- return done ( ) ;
254- }
255392 req = request
256393 . post ( endpoint )
257394 . send ( queries [ index ] ) ;
0 commit comments