|
| 1 | +var request = require('supertest'); |
| 2 | +require('should'); |
| 3 | +var testUtils = require("../testUtils"); |
| 4 | +request = request(testUtils.url); |
| 5 | + |
| 6 | +var API_KEY_ADMIN = ""; |
| 7 | +var APP_KEY = ""; |
| 8 | +var APP_ID = ""; |
| 9 | +var DEVICE_1 = "appuser_query_test_1"; |
| 10 | +var DEVICE_2 = "appuser_query_test_2"; |
| 11 | + |
| 12 | +/** |
| 13 | + * Happy-path coverage for the /i/app_users write endpoints that validate a |
| 14 | + * user-supplied Mongo query via common.parseUserQuery. A dedicated app is |
| 15 | + * created (and deleted at the end) so the seeded users do not pollute the |
| 16 | + * shared test app used by the ordered, count-based write suites. Two users are |
| 17 | + * seeded so update/delete match exactly one user (a valid, non-empty, |
| 18 | + * non-near-total query) and return 200. |
| 19 | + */ |
| 20 | +describe('Testing query-validated app_users write endpoints (happy path)', function() { |
| 21 | + describe('Create dedicated app', function() { |
| 22 | + it('should create app', function(done) { |
| 23 | + API_KEY_ADMIN = testUtils.get("API_KEY_ADMIN"); |
| 24 | + var params = {name: "Test App AppUsers Query"}; |
| 25 | + request |
| 26 | + .get('/i/apps/create?api_key=' + API_KEY_ADMIN + "&args=" + JSON.stringify(params)) |
| 27 | + .expect(200) |
| 28 | + .end(function(err, res) { |
| 29 | + if (err) { |
| 30 | + return done(err); |
| 31 | + } |
| 32 | + var ob = JSON.parse(res.text); |
| 33 | + ob.should.have.property('_id'); |
| 34 | + ob.should.have.property('key'); |
| 35 | + APP_ID = ob._id; |
| 36 | + APP_KEY = ob.key; |
| 37 | + setTimeout(done, 1000 * testUtils.testScalingFactor); |
| 38 | + }); |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + describe('Seed dedicated users', function() { |
| 43 | + it('should create first user', function(done) { |
| 44 | + var metrics = {"_os": "Android", "_os_version": "9", "_device": "TestDevice"}; |
| 45 | + request |
| 46 | + .get('/i?device_id=' + DEVICE_1 + '&app_key=' + APP_KEY + '&begin_session=1&metrics=' + JSON.stringify(metrics)) |
| 47 | + .expect(200) |
| 48 | + .end(function(err, res) { |
| 49 | + if (err) { |
| 50 | + return done(err); |
| 51 | + } |
| 52 | + var ob = JSON.parse(res.text); |
| 53 | + ob.should.have.property('result', 'Success'); |
| 54 | + setTimeout(done, 1000 * testUtils.testScalingFactor); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should create second user', function(done) { |
| 59 | + var metrics = {"_os": "Android", "_os_version": "9", "_device": "TestDevice"}; |
| 60 | + request |
| 61 | + .get('/i?device_id=' + DEVICE_2 + '&app_key=' + APP_KEY + '&begin_session=1&metrics=' + JSON.stringify(metrics)) |
| 62 | + .expect(200) |
| 63 | + .end(function(err, res) { |
| 64 | + if (err) { |
| 65 | + return done(err); |
| 66 | + } |
| 67 | + var ob = JSON.parse(res.text); |
| 68 | + ob.should.have.property('result', 'Success'); |
| 69 | + setTimeout(done, 1000 * testUtils.testScalingFactor); |
| 70 | + }); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + describe('GET /i/app_users/update', function() { |
| 75 | + it('should update the matching user with a valid query', function(done) { |
| 76 | + var query = encodeURIComponent(JSON.stringify({did: DEVICE_1})); |
| 77 | + var update = encodeURIComponent(JSON.stringify({"custom.qtestflag": "1"})); |
| 78 | + request |
| 79 | + .get('/i/app_users/update?api_key=' + API_KEY_ADMIN + '&app_id=' + APP_ID + '&query=' + query + '&update=' + update) |
| 80 | + .expect(200) |
| 81 | + .end(function(err, res) { |
| 82 | + if (err) { |
| 83 | + return done(err); |
| 84 | + } |
| 85 | + var ob = JSON.parse(res.text); |
| 86 | + ob.should.have.property('result', 'User Updated'); |
| 87 | + done(); |
| 88 | + }); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + describe('GET /i/app_users/export', function() { |
| 93 | + it('should export the matching user with a valid query', function(done) { |
| 94 | + var query = encodeURIComponent(JSON.stringify({did: DEVICE_1})); |
| 95 | + request |
| 96 | + .get('/i/app_users/export?api_key=' + API_KEY_ADMIN + '&app_id=' + APP_ID + '&query=' + query) |
| 97 | + .expect(200) |
| 98 | + .end(function(err, res) { |
| 99 | + if (err) { |
| 100 | + return done(err); |
| 101 | + } |
| 102 | + done(); |
| 103 | + }); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + describe('GET /i/app_users/delete', function() { |
| 108 | + it('should delete the matching user with a valid query', function(done) { |
| 109 | + var query = encodeURIComponent(JSON.stringify({did: DEVICE_2})); |
| 110 | + request |
| 111 | + .get('/i/app_users/delete?api_key=' + API_KEY_ADMIN + '&app_id=' + APP_ID + '&query=' + query) |
| 112 | + .expect(200) |
| 113 | + .end(function(err, res) { |
| 114 | + if (err) { |
| 115 | + return done(err); |
| 116 | + } |
| 117 | + var ob = JSON.parse(res.text); |
| 118 | + ob.should.have.property('result', 'User deleted'); |
| 119 | + done(); |
| 120 | + }); |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + describe('Delete dedicated app', function() { |
| 125 | + it('should delete app', function(done) { |
| 126 | + request |
| 127 | + .get('/i/apps/delete?api_key=' + API_KEY_ADMIN + "&args=" + JSON.stringify({app_id: APP_ID})) |
| 128 | + .expect(200) |
| 129 | + .end(function(err, res) { |
| 130 | + if (err) { |
| 131 | + return done(err); |
| 132 | + } |
| 133 | + var ob = JSON.parse(res.text); |
| 134 | + ob.should.have.property('result', 'Success'); |
| 135 | + done(); |
| 136 | + }); |
| 137 | + }); |
| 138 | + }); |
| 139 | +}); |
0 commit comments