From a48da85ebe2adb9d86f2f8acf82aa5734e478a0b Mon Sep 17 00:00:00 2001 From: vjrj Date: Thu, 14 Jun 2018 20:35:41 +0200 Subject: [PATCH] More REST calls --- imports/api/Rest/Rest.js | 43 ++++++++++++++++ test/rest.test.js | 108 +++++++++++++++++++++++++++++++++------ 2 files changed, 134 insertions(+), 17 deletions(-) diff --git a/imports/api/Rest/Rest.js b/imports/api/Rest/Rest.js index eaf3764..9ee1256 100644 --- a/imports/api/Rest/Rest.js +++ b/imports/api/Rest/Rest.js @@ -321,3 +321,46 @@ apiV1.addRoute('mobile/subscriptions', { authRequired: false }, { return jsend.success({}); } }); + +apiV1.addRoute('mobile/subscriptions/all', { authRequired: false }, { + get: function get() { + const { token, mobileToken } = this.bodyParams; + try { + check(token, String); + check(mobileToken, String); + } catch (e) { + return defaultFailParams(e); + } + + const failed = checkAuthToken(token); + if (failed) return failed; + + const user = Meteor.users.findOne({ fireBaseToken: mobileToken }); + if (!user) return failed; + + const result = Subscriptions.find({ owner: user._id }); + + return jsend.success({ subscriptions: result.fetch(), count: result.count() }); + }, + delete: function delAll() { + const { token, mobileToken } = this.bodyParams; + try { + check(token, String); + check(mobileToken, String); + } catch (e) { + return defaultFailParams(e); + } + + const failed = checkAuthToken(token); + if (failed) return failed; + + if (Meteor.users.find({ fireBaseToken: mobileToken }).count() !== 1) return failed; + + const user = Meteor.users.findOne({ fireBaseToken: mobileToken }); + if (!user) return failed; + const toRemove = Subscriptions.find({ owner: user._id }).count(); + Subscriptions.remove({ owner: user._id }); + + return jsend.success({ count: toRemove }); + } +}); diff --git a/test/rest.test.js b/test/rest.test.js index 316d324..6b48274 100644 --- a/test/rest.test.js +++ b/test/rest.test.js @@ -142,7 +142,7 @@ describe('basic api v1 returns', () => { let subsId; - it('should create mobile user subscriptions', async (done) => { + function addSubs(done) { // this are removed in the test database but we are testing in dev/ci database // Remove previous subs of this user @@ -164,8 +164,48 @@ describe('basic api v1 returns', () => { subsId = jsendResult.data.subsId; done(); }); + } + + it('should create mobile user subscriptions', async (done) => { + addSubs(done); }); + it('should not create mobile user subscriptions with wrong token', async (done) => { + HTTP.post(url('api/v1/mobile/subscriptions'), { + data: { + token: 'wrongOne', + mobileToken, + lat: 3.106111, + lon: 53.5775, + distance: 100 + } + }, (error, result) => { + chai.expect(error, null); + chai.expect(result.statusCode).equal(401); + done(); + }); + }); + + it('should get all mobile user subscriptions', async (done) => { + HTTP.get(url('api/v1/mobile/subscriptions/all'), { + data: { + token, + mobileToken + } + }, (error, result) => { + chai.expect(error, null); + chai.expect(result.statusCode).equal(200); + const jsendResult = result.data; + chai.expect(jsendResult.status).equal('success'); + chai.expect(typeof jsendResult.data.subscriptions).to.equal('object'); + chai.expect(typeof jsendResult.data.count).to.equal('number'); + chai.expect(jsendResult.data.count).to.equal(1); + // console.log(JSON.stringify(jsendResult.data.subscriptions)); + done(); + }); + }); + + it('should remove mobile user subscriptions', async (done) => { // this are removed in the test database but we are testing in dev/ci database @@ -184,21 +224,6 @@ describe('basic api v1 returns', () => { }); }); - it('should not create mobile user subscriptions with wrong token', async (done) => { - HTTP.post(url('api/v1/mobile/subscriptions'), { - data: { - token: 'wrongOne', - mobileToken, - lat: 3.106111, - lon: 53.5775, - distance: 100 - } - }, (error, result) => { - chai.expect(error, null); - chai.expect(result.statusCode).equal(401); - done(); - }); - }); it('should not remove mobile user subscriptions with wrong token', async (done) => { HTTP.del(url('api/v1/mobile/subscriptions'), { @@ -214,7 +239,56 @@ describe('basic api v1 returns', () => { }); }); - // TODO list all subs + it('should not get mobile user subscriptions with wrong token', async (done) => { + HTTP.get(url('api/v1/mobile/subscriptions/all'), { + data: { + token: 'wrongOne', + mobileToken + } + }, (error, result) => { + chai.expect(error, null); + chai.expect(result.statusCode).equal(401); + done(); + }); + }); + + it('should get all mobile user subscriptions', async (done) => { + HTTP.get(url('api/v1/mobile/subscriptions/all'), { + data: { + token, + mobileToken + } + }, (error, result) => { + chai.expect(error, null); + chai.expect(result.statusCode).equal(200); + const jsendResult = result.data; + chai.expect(jsendResult.status).equal('success'); + chai.expect(typeof jsendResult.data.subscriptions).to.equal('object'); + chai.expect(typeof jsendResult.data.count).to.equal('number'); + chai.expect(jsendResult.data.count).to.equal(0); + done(); + }); + }); + + it('should del all mobile user subscriptions', async (done) => { + // Add two + addSubs(() => {}); + addSubs(() => {}); + HTTP.del(url('api/v1/mobile/subscriptions/all'), { + data: { + token, + mobileToken + } + }, (error, result) => { + chai.expect(error, null); + chai.expect(result.statusCode).equal(200); + const jsendResult = result.data; + chai.expect(jsendResult.status).equal('success'); + chai.expect(typeof jsendResult.data.count).to.equal('number'); + chai.expect(jsendResult.data.count).to.equal(2); + done(); + }); + }); // TODO remove all subs });