Skip to content

Commit 6d408c3

Browse files
authored
Merge pull request #6568 from Countly/sdk-bs-tests
[sdk]bs tests
2 parents 3fadfe3 + 44e6b71 commit 6d408c3

File tree

6 files changed

+196
-42
lines changed

6 files changed

+196
-42
lines changed

plugins/sdk/api/api.js

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,35 @@ var countlyModel = require('../../../api/lib/countly.model.js');
55
var {validateRead, validateUpdate} = require('../../../api/utils/rights.js');
66

77
const FEATURE_NAME = 'sdk';
8+
const validOptions = [
9+
"tracking",
10+
"networking",
11+
"crt",
12+
"vt",
13+
"st",
14+
"cet",
15+
"ecz",
16+
"cr",
17+
"sui",
18+
"eqs",
19+
"rqs",
20+
"czi",
21+
"dort",
22+
"scui",
23+
"lkl",
24+
"lvs",
25+
"lsv",
26+
"lbc",
27+
"ltlpt",
28+
"ltl",
29+
"lt",
30+
"rcz",
31+
"bom",
32+
"bom_at",
33+
"bom_rqp",
34+
"bom_ra",
35+
"bom_d"
36+
];
837

938
plugins.register("/permissions/features", function(ob) {
1039
ob.features.push(FEATURE_NAME);
@@ -109,7 +138,7 @@ plugins.register("/permissions/features", function(ob) {
109138
plugins.register("/o", function(ob) {
110139
var params = ob.params;
111140

112-
// returns server config for the given app
141+
// returns server config for the given app (regardless of enforcement, can be useful)
113142
if (params.qstring.method === "sdk-config") {
114143
validateRead(params, FEATURE_NAME, function() {
115144
getSDKConfig(params).then(function(res) {
@@ -145,35 +174,6 @@ plugins.register("/permissions/features", function(ob) {
145174
}
146175

147176
var configToSave = uploadConfig.c || uploadConfig; // incase they provide the config object directly
148-
var validOptions = [
149-
"tracking",
150-
"networking",
151-
"crt",
152-
"vt",
153-
"st",
154-
"cet",
155-
"ecz",
156-
"cr",
157-
"sui",
158-
"eqs",
159-
"rqs",
160-
"czi",
161-
"dort",
162-
"scui",
163-
"lkl",
164-
"lvs",
165-
"lsv",
166-
"lbc",
167-
"ltlpt",
168-
"ltl",
169-
"lt",
170-
"rcz",
171-
"bom",
172-
"bom_at",
173-
"bom_rqp",
174-
"bom_ra",
175-
"bom_d"
176-
];
177177
for (var key in configToSave) {
178178
if (validOptions.indexOf(key) === -1) {
179179
delete configToSave[key];
@@ -197,6 +197,8 @@ plugins.register("/permissions/features", function(ob) {
197197
});
198198
});
199199
}
200+
201+
// returns enforcement info for the given app
200202
if (params.qstring.method === "sdk-enforcement") {
201203
validateRead(params, FEATURE_NAME, function() {
202204
getEnforcement(params).then(function(res) {
@@ -221,6 +223,9 @@ plugins.register("/permissions/features", function(ob) {
221223
break;
222224
case 'update-enforcement':
223225
validateUpdate(params, FEATURE_NAME, function() {
226+
if (!params.app_id) {
227+
return common.returnMessage(params, 400, 'Missing parameter "app_id"');
228+
}
224229
var enforcement = params.qstring.enforcement;
225230
if (typeof enforcement === "string") {
226231
try {
@@ -230,6 +235,15 @@ plugins.register("/permissions/features", function(ob) {
230235
return common.returnMessage(params, 400, 'Error parsing enforcement');
231236
}
232237
}
238+
if (typeof enforcement !== "object") {
239+
return common.returnMessage(params, 400, 'Wrong enforcement format');
240+
}
241+
// check remove enforcement options those are not in validOptions
242+
for (var key in enforcement) {
243+
if (validOptions.indexOf(key) === -1) {
244+
delete enforcement[key];
245+
}
246+
}
233247
common.outDb.collection('sdk_enforcement').updateOne(
234248
{ _id: params.app_id + "" },
235249
{ $set: { enforcement: enforcement } },
@@ -532,6 +546,9 @@ plugins.register("/permissions/features", function(ob) {
532546
return common.returnMessage(params, 400, 'Error parsing parameter');
533547
}
534548
}
549+
if (!params.app_id) {
550+
return common.returnMessage(params, 400, 'Missing parameter "app_id"');
551+
}
535552
common.outDb.collection('sdk_configs').updateOne({_id: params.app_id + ""}, {$set: {config: parameter} }, {upsert: true}, function() {
536553
common.returnOutput(params, {result: 'Success'});
537554
});

plugins/sdk/tests.js renamed to plugins/sdk/tests/tests.js

Lines changed: 145 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,56 @@
11
const spt = require('supertest');
22
const should = require('should');
3-
const testUtils = require('../../test/testUtils');
4-
const plugins = require('../../plugins/pluginManager');
3+
const testUtils = require('../../../test/testUtils');
54

65
const request = spt(testUtils.url);
76
// change these in local testing directly or set env vars (also COUNTLY_CONFIG_HOSTNAME should be set with port)
87
let API_KEY_ADMIN = testUtils.get("API_KEY_ADMIN");
98
let APP_KEY = testUtils.get('APP_KEY');
109
let APP_ID = testUtils.get("APP_ID");
1110
const 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

1341
describe('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]);

ui-tests/cypress/e2e/sdk/tool_04.cy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ describe('4.Mixed tooltip (old Android SDK version)', () => {
1515
it('4.3-Test', function() {
1616
goToConfigTab(true);
1717
checkTooltipAppears('success', 2, true);
18-
checkTooltipAppears('warning', 20, true);
18+
checkTooltipAppears('warning', 25, true);
1919
});
2020
});

ui-tests/cypress/e2e/sdk/tool_05.cy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ describe('5.Mixed tooltip (old iOS SDK version)', () => {
1515
it('5.3-Test', function() {
1616
goToConfigTab(true);
1717
checkTooltipAppears('success', 2, true);
18-
checkTooltipAppears('warning', 20, true);
18+
checkTooltipAppears('warning', 25, true);
1919
});
2020
});

ui-tests/cypress/e2e/sdk/tool_09.cy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ describe('9.Mixed tooltip (multiple SDK versions)', () => {
1818
it('9.3-Test', function() {
1919
goToConfigTab(true);
2020
checkTooltipAppears('success', 2, true);
21-
checkTooltipAppears('warning', 20, true);
21+
checkTooltipAppears('warning', 25, true);
2222
});
2323
});

ui-tests/cypress/lib/sdk/setup.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const createRequest = (sdkName, sdkVersion) => {
6868
};
6969

7070
const checkTooltipAppears = (tooltip, count, early) => {
71-
cy.get('.cly-vue-tooltip-icon.ion.ion-help-circled.tooltip-' + tooltip).should('have.length', count ? count : 22);
71+
cy.get('.cly-vue-tooltip-icon.ion.ion-help-circled.tooltip-' + tooltip).should('have.length', count ? count : 27);
7272

7373
if (early) {
7474
return;

0 commit comments

Comments
 (0)