-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtreatmentWithConfig.test.js
More file actions
302 lines (272 loc) · 12.9 KB
/
treatmentWithConfig.test.js
File metadata and controls
302 lines (272 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
const request = require('supertest');
const app = require('../../app');
const { expectError, expectErrorContaining, expectOk, getLongKey } = require('../../utils/testWrapper');
describe('get-treatment-with-config', () => {
// Testing authorization
test('should be 401 if auth is not passed', async () => {
const response = await request(app)
.get('/client/get-treatment-with-config?key=test&split-name=my-experiment');
expectError(response, 401, 'Unauthorized');
});
test('should be 401 if auth does not match', async () => {
const response = await request(app)
.get('/client/get-treatment-with-config?key=test&split-name=my-experiment')
.set('Authorization', 'invalid');
expectError(response, 401, 'Unauthorized');
});
// Testing Input Validation.
// The following tests are going to check null parameters, wrong types or lengths.
test('should be 400 if key is not passed', async () => {
const expected = [
'you passed a null or undefined key, key must be a non-empty string.'
];
const response = await request(app)
.get('/client/get-treatment-with-config?split-name=test')
.set('Authorization', 'test');
expectErrorContaining(response, 400, expected);
});
test('should be 400 if key is empty', async () => {
const expected = [
'you passed an empty string, key must be a non-empty string.'
];
const response = await request(app)
.get('/client/get-treatment-with-config?key=&split-name=test')
.set('Authorization', 'test');
expectErrorContaining(response, 400, expected);
});
test('should be 400 if key is empty trimmed', async () => {
const expected = [
'you passed an empty string, key must be a non-empty string.'
];
const response = await request(app)
.get('/client/get-treatment-with-config?key= &split-name=test')
.set('Authorization', 'test');
expectErrorContaining(response, 400, expected);
});
test('should be 400 if key is too long', async () => {
const expected = [
'key too long, key must be 250 characters or less.'
];
const key = getLongKey();
const response = await request(app)
.get(`/client/get-treatment-with-config?key=${key}&split-name=test`)
.set('Authorization', 'test');
expectErrorContaining(response, 400, expected);
});
test('should be 400 if bucketing-key is empty', async () => {
const expected = [
'you passed an empty string, bucketing-key must be a non-empty string.'
];
const response = await request(app)
.get('/client/get-treatment-with-config?key=key&bucketing-key=&split-name=test')
.set('Authorization', 'test');
expectErrorContaining(response, 400, expected);
});
test('should be 400 if bucketing-key is empty trimmed', async () => {
const expected = [
'you passed an empty string, bucketing-key must be a non-empty string.'
];
const response = await request(app)
.get('/client/get-treatment-with-config?key=key&bucketing-key= &split-name=test')
.set('Authorization', 'test');
expectErrorContaining(response, 400, expected);
});
test('should be 400 if bucketing-key is too long', async () => {
const expected = [
'bucketing-key too long, bucketing-key must be 250 characters or less.'
];
const key = getLongKey();
const response = await request(app)
.get(`/client/get-treatment-with-config?key=key&bucketing-key=${key}&split-name=test`)
.set('Authorization', 'test');
expectErrorContaining(response, 400, expected);
});
test('should be 400 if split-name is not passed', async () => {
const expected = [
'you passed a null or undefined split-name, split-name must be a non-empty string.'
];
const response = await request(app)
.get('/client/get-treatment-with-config?key=test')
.set('Authorization', 'test');
expectErrorContaining(response, 400, expected);
});
test('should be 400 if split-name is empty', async () => {
const expected = [
'you passed an empty split-name, split-name must be a non-empty string.'
];
const response = await request(app)
.get('/client/get-treatment-with-config?key=test&split-name=')
.set('Authorization', 'test');
expectErrorContaining(response, 400, expected);
});
test('should be 400 if split-name is empty trimmed', async () => {
const expected = [
'you passed an empty split-name, split-name must be a non-empty string.'
];
const response = await request(app)
.get('/client/get-treatment-with-config?key=test&split-name= ')
.set('Authorization', 'test');
expectErrorContaining(response, 400, expected);
});
test('should be 400 if there are errors in key and split-name', async () => {
const expected = [
'you passed an empty string, key must be a non-empty string.',
'you passed an empty split-name, split-name must be a non-empty string.'
];
const response = await request(app)
.get('/client/get-treatment-with-config?key=&split-name= ')
.set('Authorization', 'test');
expectErrorContaining(response, 400, expected);
});
test('should be 400 if attributes is invalid', async () => {
const expected = [
'attributes must be a plain object.'
];
const response = await request(app)
.get('/client/get-treatment-with-config?key=test&split-name=my-experiment&attributes=lalala')
.set('Authorization', 'test');
expectErrorContaining(response, 400, expected);
});
test('should be 400 if there are multiple errors', async () => {
const expected = [
'you passed an empty string, key must be a non-empty string.',
'you passed an empty split-name, split-name must be a non-empty string.',
'attributes must be a plain object.'
];
const response = await request(app)
.get('/client/get-treatment-with-config?key= &split-name=&attributes="lalala"')
.set('Authorization', 'test');
expectErrorContaining(response, 400, expected);
});
test('should be 400 if there are multiple errors in every input', async () => {
const expected = [
'you passed an empty string, key must be a non-empty string.',
'you passed an empty split-name, split-name must be a non-empty string.',
'attributes must be a plain object.',
'bucketing-key too long, bucketing-key must be 250 characters or less.'
];
const key = getLongKey();
const response = await request(app)
.get(`/client/get-treatment-with-config?bucketing-key=${key}&key= &split-name=&attributes="lalala"`)
.set('Authorization', 'test');
expectErrorContaining(response, 400, expected);
});
test('should be 200 if is valid attributes (GET)', async () => {
const response = await request(app)
.get('/client/get-treatment-with-config?key=test&split-name=my-experiment&attributes={"test":"test"}')
.set('Authorization', 'test');
expectOk(response, 200, 'on', 'my-experiment', '{"desc" : "this applies only to ON treatment"}');
});
test('should be 200 when attributes is null (GET)', async () => {
const response = await request(app)
.get('/client/get-treatment-with-config?key=test&split-name=my-experiment')
.set('Authorization', 'test');
expectOk(response, 200, 'on', 'my-experiment', '{"desc" : "this applies only to ON treatment"}');
});
test('should be 200 if is valid attributes (POST)', async () => {
const response = await request(app)
.get('/client/get-treatment-with-config?key=test&split-name=my-experiment')
.send({attributes: { test:'test' }})
.set('Authorization', 'test');
expectOk(response, 200, 'on', 'my-experiment', '{"desc" : "this applies only to ON treatment"}');
});
test('should be 200 if is valid stringified attributes (POST)', async () => {
const response = await request(app)
.get('/client/get-treatment-with-config?key=test&split-name=my-experiment')
.send(JSON.stringify({attributes: { test:'test' }}))
.set('Authorization', 'test');
expectOk(response, 200, 'on', 'my-experiment', '{"desc" : "this applies only to ON treatment"}');
});
test('should be 200 when attributes is null (POST)', async () => {
const response = await request(app)
.get('/client/get-treatment-with-config?key=test&split-name=my-experiment')
.set('Authorization', 'test');
expectOk(response, 200, 'on', 'my-experiment', '{"desc" : "this applies only to ON treatment"}');
});
// Testing Multiple Experiments Regarding YAML
test('should be 200 with multiple experiments', async () => {
// Checking multiple experiments regarding yml passed
// With key=test
let response = await request(app)
.get('/client/get-treatment-with-config?key=test&split-name=my-experiment')
.set('Authorization', 'test');
expectOk(response, 200, 'on', 'my-experiment', '{"desc" : "this applies only to ON treatment"}');
// With key=only_test
response = await request(app)
.get('/client/get-treatment-with-config?key=only_test&split-name=my-experiment')
.set('Authorization', 'test');
expectOk(response, 200, 'off', 'my-experiment', '{"desc" : "this applies only to OFF and only for only_test. The rest will receive ON"}');
// With another split
response = await request(app)
.get('/client/get-treatment-with-config?key=test&split-name=other-experiment-3')
.set('Authorization', 'test');
expectOk(response, 200, 'off', 'other-experiment-3', null);
// With a non-existant split in yml
response = await request(app)
.get('/client/get-treatment-with-config?key=only_test&split-name=nonexistant-experiment')
.set('Authorization', 'test');
expectOk(response, 200, 'control', 'nonexistant-experiment', null);
});
test('should be 200 if properties is valid (GET)', async () => {
const response = await request(app)
.get('/client/get-treatment-with-config?key=test&split-name=my-experiment&properties={"package":"premium","admin":true,"discount":50}')
.set('Authorization', 'test');
expectOk(response, 200, 'on', 'my-experiment', '{"desc" : "this applies only to ON treatment"}');
});
test('should be 200 if properties is valid (POST)', async () => {
const response = await request(app)
.post('/client/get-treatment-with-config?key=test&split-name=my-experiment')
.send({
properties: { package: 'premium', admin: true, discount: 50 },
})
.set('Authorization', 'test');
expectOk(response, 200, 'on', 'my-experiment', '{"desc" : "this applies only to ON treatment"}');
});
test('should be 200 if properties is invalid (GET)', async () => {
const response = await request(app)
.get('/client/get-treatment-with-config?key=test&split-name=my-experiment&properties={"foo": {"bar": 1}}')
.set('Authorization', 'test');
expectOk(response, 200, 'on', 'my-experiment', '{"desc" : "this applies only to ON treatment"}');
});
test('should be 200 if properties is invalid (POST)', async () => {
const response = await request(app)
.post('/client/get-treatment-with-config?key=test&split-name=my-experiment')
.send({
properties: { foo: { bar: 1 } },
})
.set('Authorization', 'test');
expectOk(response, 200, 'on', 'my-experiment', '{"desc" : "this applies only to ON treatment"}');
});
test('should be 200 if impressionsDisabled is valid (GET)', async () => {
const response = await request(app)
.get('/client/get-treatment-with-config?key=test&split-name=my-experiment&properties={"package":"premium","admin":true,"discount":50}&impressions-disabled=true')
.set('Authorization', 'test');
expectOk(response, 200, 'on', 'my-experiment', '{"desc" : "this applies only to ON treatment"}');
});
test('should be 200 if impressionsDisabled is valid (POST)', async () => {
const response = await request(app)
.post('/client/get-treatment-with-config?key=test&split-name=my-experiment')
.send({
properties: { package: 'premium', admin: true, discount: 50 },
impressionsDisabled: true,
})
.set('Authorization', 'test');
expectOk(response, 200, 'on', 'my-experiment', '{"desc" : "this applies only to ON treatment"}');
});
test('should be 200 if impressionsDisabled is invalid (GET)', async () => {
const response = await request(app)
.get('/client/get-treatment-with-config?key=test&split-name=my-experiment&properties={"foo": {"bar": 1}}&impressions-disabled=lalala')
.set('Authorization', 'test');
expectOk(response, 200, 'on', 'my-experiment', '{"desc" : "this applies only to ON treatment"}');
});
test('should be 200 if impressionsDisabled is invalid (POST)', async () => {
const response = await request(app)
.post('/client/get-treatment-with-config?key=test&split-name=my-experiment')
.send({
properties: { foo: { bar: 1 } },
impressionsDisabled: 'lalala',
})
.set('Authorization', 'test');
expectOk(response, 200, 'on', 'my-experiment', '{"desc" : "this applies only to ON treatment"}');
});
});