Skip to content

Commit ca13bbd

Browse files
authored
Merge pull request #164 from splitio/sdks-7670
[SDKS-7670] flag set input validation improvements
2 parents 1886909 + 4703df8 commit ca13bbd

8 files changed

Lines changed: 116 additions & 106 deletions

File tree

client/__tests__/treatmentsByFlagSets.test.js

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const request = require('supertest');
22
const app = require('../../app');
33
const { expectError, expectErrorContaining, expectOkMultipleResults, getLongKey } = require('../../utils/testWrapper');
44
const { NULL_FLAG_SETS, EMPTY_FLAG_SETS } = require('../../utils/constants');
5+
const { expectedGreenResults, expectedPurpleResults, expectedPinkResults } = require('../../utils/mocks');
56

67
jest.mock('node-fetch', () => {
78
return jest.fn().mockImplementation((url) => {
@@ -215,7 +216,7 @@ describe('get-treatments-by-sets', () => {
215216

216217
test('should be 400 if attributes is an invalid json (POST)', async (done) => {
217218
const response = await request(app)
218-
.post('/client/get-treatments-by-sets?key=test&set-name=my-experiment')
219+
.post('/client/get-treatments-by-sets?key=test&flag-sets=my-experiment')
219220
.set('Content-Type', 'application/json')
220221
// eslint-disable-next-line no-useless-escape
221222
.send('\|\\\"/regex/i') // Syntax error parsing the JSON.
@@ -225,50 +226,6 @@ describe('get-treatments-by-sets', () => {
225226
done();
226227
});
227228

228-
const expectedGreenResults = {
229-
'test_green': {
230-
treatment: 'on',
231-
},
232-
'test_color': {
233-
treatment: 'on',
234-
},
235-
'test_green_config': {
236-
treatment: 'on',
237-
config: undefined,
238-
},
239-
};
240-
const expectedPurpleResults = {
241-
'test_purple': {
242-
treatment: 'on',
243-
},
244-
'test_color': {
245-
treatment: 'on',
246-
},
247-
'test_purple_config': {
248-
treatment: 'on',
249-
config: undefined,
250-
},
251-
};
252-
const expectedPinkResults = {
253-
'test_purple': {
254-
treatment: 'on',
255-
},
256-
'test_green': {
257-
treatment: 'on',
258-
},
259-
'test_color': {
260-
treatment: 'on',
261-
},
262-
'test_purple_config': {
263-
treatment: 'on',
264-
config: undefined,
265-
},
266-
'test_green_config': {
267-
treatment: 'on',
268-
config: undefined,
269-
},
270-
};
271-
272229
test('should be 200 if is valid attributes (GET)', async (done) => {
273230
const response = await request(app)
274231
.get('/client/get-treatments-by-sets?key=key_green&flag-sets=set_green&attributes={"test":"test"}')

client/__tests__/treatmentsWithConfigByFlagSets.test.js

Lines changed: 9 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const request = require('supertest');
22
const app = require('../../app');
33
const { expectError, expectErrorContaining, expectOkMultipleResults, getLongKey } = require('../../utils/testWrapper');
44
const { NULL_FLAG_SETS, EMPTY_FLAG_SETS } = require('../../utils/constants');
5+
const { expectedGreenResultsWithConfig, expectedPurpleResultsWithConfig, expectedPinkResultsWithConfig } = require('../../utils/mocks');
56

67
jest.mock('node-fetch', () => {
78
return jest.fn().mockImplementation((url) => {
@@ -204,63 +205,19 @@ describe('get-treatments-with-config-by-sets', () => {
204205
done();
205206
});
206207

207-
const expectedGreenResults = {
208-
'test_green': {
209-
treatment: 'on',
210-
},
211-
'test_color': {
212-
treatment: 'on',
213-
},
214-
'test_green_config': {
215-
treatment: 'on',
216-
config: '{"color":"green"}',
217-
},
218-
};
219-
const expectedPurpleResults = {
220-
'test_purple': {
221-
treatment: 'on',
222-
},
223-
'test_color': {
224-
treatment: 'on',
225-
},
226-
'test_purple_config': {
227-
treatment: 'on',
228-
config: '{"color":"purple"}',
229-
},
230-
};
231-
const expectedPinkResults = {
232-
'test_purple': {
233-
treatment: 'on',
234-
},
235-
'test_green': {
236-
treatment: 'on',
237-
},
238-
'test_color': {
239-
treatment: 'on',
240-
},
241-
'test_purple_config': {
242-
treatment: 'on',
243-
config: '{"color":"purple"}',
244-
},
245-
'test_green_config': {
246-
treatment: 'on',
247-
config: '{"color":"green"}',
248-
},
249-
};
250-
251208
test('should be 200 if is valid attributes (GET)', async (done) => {
252209
const response = await request(app)
253210
.get('/client/get-treatments-with-config-by-sets?key=key_green&flag-sets=set_green&attributes={"test":"test"}')
254211
.set('Authorization', 'key_green');
255-
expectOkMultipleResults(response, 200, expectedGreenResults, 3);
212+
expectOkMultipleResults(response, 200, expectedGreenResultsWithConfig, 3);
256213
done();
257214
});
258215

259216
test('should be 200 when attributes is null (GET)', async (done) => {
260217
const response = await request(app)
261218
.get('/client/get-treatments-with-config-by-sets?key=key_green&flag-sets=set_green')
262219
.set('Authorization', 'key_green');
263-
expectOkMultipleResults(response, 200, expectedGreenResults, 3);
220+
expectOkMultipleResults(response, 200, expectedGreenResultsWithConfig, 3);
264221
done();
265222
});
266223

@@ -269,7 +226,7 @@ describe('get-treatments-with-config-by-sets', () => {
269226
.post('/client/get-treatments-with-config-by-sets?key=key_green&flag-sets=set_green')
270227
.send({attributes: { test:'test' }})
271228
.set('Authorization', 'key_green');
272-
expectOkMultipleResults(response, 200, expectedGreenResults, 3);
229+
expectOkMultipleResults(response, 200, expectedGreenResultsWithConfig, 3);
273230
done();
274231
});
275232

@@ -278,7 +235,7 @@ describe('get-treatments-with-config-by-sets', () => {
278235
.get('/client/get-treatments-with-config-by-sets?key=key_green&flag-sets=set_green&attributes={"test":"test"}')
279236
.send(JSON.stringify({attributes: { test:'test' }}))
280237
.set('Authorization', 'key_green');
281-
expectOkMultipleResults(response, 200, expectedGreenResults, 3);
238+
expectOkMultipleResults(response, 200, expectedGreenResultsWithConfig, 3);
282239
done();
283240
});
284241

@@ -289,31 +246,31 @@ describe('get-treatments-with-config-by-sets', () => {
289246
attributes: null,
290247
})
291248
.set('Authorization', 'key_green');
292-
expectOkMultipleResults(response, 200, expectedGreenResults, 3);
249+
expectOkMultipleResults(response, 200, expectedGreenResultsWithConfig, 3);
293250
done();
294251
});
295252

296253
test('should be 200 with multiple evaluation but evualuate configured flag sets', async (done) => {
297254
const response = await request(app)
298255
.get('/client/get-treatments-with-config-by-sets?key=key_green&flag-sets=set_green,set_purple,nonexistant-experiment')
299256
.set('Authorization', 'key_green');
300-
expectOkMultipleResults(response, 200, expectedGreenResults, 3);
257+
expectOkMultipleResults(response, 200, expectedGreenResultsWithConfig, 3);
301258
done();
302259
});
303260

304261
test('should be 200 with multiple evaluation but evualuate configured flag sets', async (done) => {
305262
const response = await request(app)
306263
.get('/client/get-treatments-with-config-by-sets?key=key_purple&flag-sets=set_green,set_purple,nonexistant-experiment')
307264
.set('Authorization', 'key_purple');
308-
expectOkMultipleResults(response, 200, expectedPurpleResults, 3);
265+
expectOkMultipleResults(response, 200, expectedPurpleResultsWithConfig, 3);
309266
done();
310267
});
311268

312269
test('should be 200 with multiple evaluation but evualuate configured flag sets', async (done) => {
313270
const response = await request(app)
314271
.get('/client/get-treatments-with-config-by-sets?key=key_purple&flag-sets=set_green,set_purple,nonexistant-experiment')
315272
.set('Authorization', 'key_pink');
316-
expectOkMultipleResults(response, 200, expectedPinkResults, 5);
273+
expectOkMultipleResults(response, 200, expectedPinkResultsWithConfig, 5);
317274
done();
318275
});
319276
});

client/client.router.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const express = require('express');
22
const router = express.Router();
33
const keyValidator = require('../utils/inputValidation/key');
44
const splitValidator = require('../utils/inputValidation/split');
5-
const flagSetValidator = require('../utils/inputValidation/flagSet');
5+
const flagSetsValidator = require('../utils/inputValidation/flagSets');
66
const splitsValidator = require('../utils/inputValidation/splits');
77
const attributesValidator = require('../utils/inputValidation/attributes');
88
const trafficTypeValidator = require('../utils/inputValidation/trafficType');
@@ -86,7 +86,7 @@ const treatmentsValidation = (req, res, next) => {
8686
const flagSetsValidation = (req, res, next) => {
8787
const matchingKeyValidation = keyValidator(req.query.key, 'key');
8888
const bucketingKeyValidation = req.query['bucketing-key'] !== undefined ? keyValidator(req.query['bucketing-key'], 'bucketing-key') : null;
89-
const flagSetNameValidation = flagSetValidator(req.query['flag-sets']);
89+
const flagSetNameValidation = flagSetsValidator(req.query['flag-sets']);
9090
const attributesValidation = attributesValidator(req.query.attributes);
9191

9292
const error = parseValidators([matchingKeyValidation, bucketingKeyValidation, flagSetNameValidation, attributesValidation]);

utils/constants.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
const TRIMMABLE_SPACES_REGEX = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/;
2+
13
const EMPTY_FLAG_SETS = 'you passed an empty flag-sets, flag-sets must be a non-empty array.';
24
const NULL_FLAG_SETS = 'you passed a null or undefined flag-sets, flag-sets must be a non-empty array.';
35

46
module.exports = {
7+
TRIMMABLE_SPACES_REGEX,
58
EMPTY_FLAG_SETS,
69
NULL_FLAG_SETS,
710
};

utils/inputValidation/flagSet.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
const { NULL_FLAG_SETS, EMPTY_FLAG_SETS } = require('../constants');
21
const errorWrapper = require('./wrapper/error');
32
const okWrapper = require('./wrapper/ok');
4-
const TRIMMABLE_SPACES_REGEX = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/;
3+
const { NULL_FLAG_SET, EMPTY_FLAG_SET, TRIMMABLE_SPACES_REGEX } = require('../constants');
54

65
const validateFlagSet = (maybeFlagSet) => {
76
// eslint-disable-next-line
8-
if (maybeFlagSet == undefined) return errorWrapper(NULL_FLAG_SETS);
7+
if (maybeFlagSet == undefined) return errorWrapper(NULL_FLAG_SET);
98

109
if (TRIMMABLE_SPACES_REGEX.test(maybeFlagSet)) {
1110
console.log(`flag-sets "${maybeFlagSet}" has extra whitespace, trimming.`);
1211
maybeFlagSet = maybeFlagSet.trim();
1312
}
14-
if (maybeFlagSet.length === 0) return errorWrapper(EMPTY_FLAG_SETS);
13+
if (maybeFlagSet.length === 0) return errorWrapper(EMPTY_FLAG_SET);
1514

16-
return okWrapper(maybeFlagSet.split(','));
15+
return okWrapper(maybeFlagSet);
1716
};
1817

1918
module.exports = validateFlagSet;

utils/inputValidation/flagSets.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const errorWrapper = require('./wrapper/error');
2+
const okWrapper = require('./wrapper/ok');
3+
const lang = require('../lang');
4+
const validateFlagSet = require('./flagSet');
5+
const { EMPTY_FLAG_SETS, NULL_FLAG_SETS } = require('../constants');
6+
7+
const validateFlagSets = (maybeFlagSets) => {
8+
// eslint-disable-next-line eqeqeq
9+
if (maybeFlagSets == undefined) return errorWrapper(NULL_FLAG_SETS);
10+
11+
maybeFlagSets = maybeFlagSets.split(',');
12+
13+
if (maybeFlagSets.length > 0) {
14+
let validatedArray = [];
15+
// Remove invalid values
16+
maybeFlagSets.forEach(maybeFlagSet => {
17+
const flagSetValidation = validateFlagSet(maybeFlagSet);
18+
if (flagSetValidation.valid) validatedArray.push(flagSetValidation.value);
19+
});
20+
21+
// Strip off duplicated values if we have valid flag sets then return
22+
if (validatedArray.length) return okWrapper(lang.uniq(validatedArray));
23+
}
24+
25+
return errorWrapper(EMPTY_FLAG_SETS);
26+
};
27+
28+
module.exports = validateFlagSets;

utils/inputValidation/split.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const errorWrapper = require('./wrapper/error');
22
const okWrapper = require('./wrapper/ok');
3-
const TRIMMABLE_SPACES_REGEX = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/;
3+
const { TRIMMABLE_SPACES_REGEX } = require('../constants');
44

55
const validateSplit = (maybeSplit) => {
66
// eslint-disable-next-line eqeqeq

utils/mocks/index.js

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,70 @@ const integrations = [{
8282
type: 'GOOGLE_ANALYTICS_TO_SPLIT',
8383
}];
8484

85-
module.exports = { core, scheduler, urls, startup, storage, sync, integrations, apiKeyMocksMap };
85+
const expectedGreenResults = {
86+
'test_green': {
87+
treatment: 'on',
88+
},
89+
'test_color': {
90+
treatment: 'on',
91+
},
92+
'test_green_config': {
93+
treatment: 'on',
94+
},
95+
};
96+
const expectedPurpleResults = {
97+
'test_purple': {
98+
treatment: 'on',
99+
},
100+
'test_color': {
101+
treatment: 'on',
102+
},
103+
'test_purple_config': {
104+
treatment: 'on',
105+
},
106+
};
107+
const expectedPinkResults = {
108+
...expectedGreenResults,
109+
...expectedPurpleResults,
110+
};
111+
112+
const expectedGreenResultsWithConfig = {
113+
'test_green': {
114+
treatment: 'on',
115+
},
116+
'test_color': {
117+
treatment: 'on',
118+
},
119+
'test_green_config': {
120+
treatment: 'on',
121+
config: '{"color":"green"}',
122+
},
123+
};
124+
125+
const expectedPurpleResultsWithConfig = {
126+
'test_purple': {
127+
treatment: 'on',
128+
},
129+
'test_color': {
130+
treatment: 'on',
131+
},
132+
'test_purple_config': {
133+
treatment: 'on',
134+
config: '{"color":"purple"}',
135+
},
136+
};
137+
138+
const expectedPinkResultsWithConfig = {
139+
...expectedGreenResultsWithConfig,
140+
...expectedPurpleResultsWithConfig,
141+
};
142+
143+
module.exports = {
144+
core, scheduler, urls, startup, storage, sync, integrations, apiKeyMocksMap,
145+
expectedGreenResults,
146+
expectedPurpleResults,
147+
expectedPinkResults,
148+
expectedGreenResultsWithConfig,
149+
expectedPurpleResultsWithConfig,
150+
expectedPinkResultsWithConfig,
151+
};

0 commit comments

Comments
 (0)