Improved rest api calls
This commit is contained in:
parent
5b3d86e6a5
commit
5642b106d4
4 changed files with 45 additions and 8 deletions
|
|
@ -15,6 +15,7 @@ import Subscriptions from '/imports/api/Subscriptions/Subscriptions';
|
|||
import jsend from 'jsend';
|
||||
import { Random } from 'meteor/random';
|
||||
import { subscriptionsInsert } from '/imports/api/Subscriptions/methods.js';
|
||||
import { Mongo } from 'meteor/mongo';
|
||||
|
||||
const debug = false;
|
||||
|
||||
|
|
@ -275,18 +276,24 @@ if (!Meteor.settings.private.internalApiToken) {
|
|||
const {
|
||||
token,
|
||||
mobileToken,
|
||||
id,
|
||||
lat,
|
||||
lon,
|
||||
distance
|
||||
} = this.bodyParams;
|
||||
let oid;
|
||||
try {
|
||||
check(token, String);
|
||||
check(mobileToken, String);
|
||||
check(id, String);
|
||||
oid = new Mongo.ObjectID(id);
|
||||
check(oid, Meteor.Collection.ObjectID);
|
||||
check(lat, Number);
|
||||
check(lon, Number);
|
||||
check(distance, Number);
|
||||
checkLatLonDist(distance, lat, lon);
|
||||
} catch (e) {
|
||||
if (debug) console.log(e);
|
||||
return defaultFailParams(e);
|
||||
}
|
||||
|
||||
|
|
@ -297,6 +304,7 @@ if (!Meteor.settings.private.internalApiToken) {
|
|||
if (!user) return failMsg('User not found');
|
||||
|
||||
const newSubs = {};
|
||||
newSubs._id = new Meteor.Collection.ObjectID(id);
|
||||
newSubs.location = {};
|
||||
newSubs.location.lat = lat;
|
||||
newSubs.location.lon = lon;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
/* eslint-disable consistent-return */
|
||||
/* eslint-disable import/no-absolute-path */
|
||||
|
||||
import { Meteor } from 'meteor/meteor';
|
||||
import { Mongo } from 'meteor/mongo';
|
||||
import SimpleSchema from 'simpl-schema';
|
||||
import { defaultCreatedAt, defaultUpdateAt } from '/imports/api/Utility/Utils.js';
|
||||
|
|
@ -42,6 +43,7 @@ Subscriptions.deny({
|
|||
|
||||
|
||||
Subscriptions.schema = new SimpleSchema({
|
||||
_id: { type: Meteor.Collection.ObjectID, optional: true, blackbox: true },
|
||||
location: Object,
|
||||
'location.lat': Number,
|
||||
'location.lon': Number,
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ function geo(doc) {
|
|||
|
||||
export function subscriptionsInsert(doc, userId, type) {
|
||||
check(doc, {
|
||||
_id: Match.Maybe(Meteor.Collection.ObjectID),
|
||||
location: Match.ObjectIncluding({ lat: Number, lon: Number }),
|
||||
distance: Number
|
||||
});
|
||||
|
|
|
|||
|
|
@ -91,8 +91,9 @@ describe('basic api v1 returns', () => {
|
|||
}));
|
||||
|
||||
const mobileToken = 'user-mobile-firebase-token';
|
||||
|
||||
let testUserId;
|
||||
let returnedSubsId;
|
||||
const objId = new Meteor.Collection.ObjectID();
|
||||
|
||||
it('should create mobile users', async (done) => {
|
||||
// this are removed in the test database but we are testing in dev/ci database
|
||||
|
|
@ -139,11 +140,9 @@ describe('basic api v1 returns', () => {
|
|||
});
|
||||
});
|
||||
|
||||
let subsId;
|
||||
|
||||
function addSubs(done) {
|
||||
// this are removed in the test database but we are testing in dev/ci database
|
||||
|
||||
// Remove previous subs of this user
|
||||
// Subscriptions.remove({ owner: testUserId });
|
||||
// chai.expect(Subscriptions.find({ owner: testUserId }).count()).to.equal(0);
|
||||
|
|
@ -151,16 +150,19 @@ describe('basic api v1 returns', () => {
|
|||
data: {
|
||||
token,
|
||||
mobileToken,
|
||||
id: objId._str,
|
||||
lat: 3.106111,
|
||||
lon: 53.5775,
|
||||
distance: 100
|
||||
}
|
||||
}, (error, result) => {
|
||||
chai.expect(error, null);
|
||||
if (result.statusCode !== 200) console.log(result);
|
||||
chai.expect(result.statusCode).equal(200);
|
||||
const jsendResult = result.data;
|
||||
chai.expect(jsendResult.status).equal('success');
|
||||
subsId = jsendResult.data.subsId;
|
||||
returnedSubsId = jsendResult.data.subsId;
|
||||
chai.expect(returnedSubsId.toString()).equal(objId._str);
|
||||
done();
|
||||
});
|
||||
}
|
||||
|
|
@ -174,12 +176,14 @@ describe('basic api v1 returns', () => {
|
|||
data: {
|
||||
token: 'wrongOne',
|
||||
mobileToken,
|
||||
id: objId._str,
|
||||
lat: 3.106111,
|
||||
lon: 53.5775,
|
||||
distance: 100
|
||||
}
|
||||
}, (error, result) => {
|
||||
chai.expect(error, null);
|
||||
if (result.statusCode !== 401) console.log(result);
|
||||
chai.expect(result.statusCode).equal(401);
|
||||
done();
|
||||
});
|
||||
|
|
@ -189,6 +193,7 @@ describe('basic api v1 returns', () => {
|
|||
HTTP.get(url(`api/v1/mobile/subscriptions/all/${token}/${mobileToken}`), {
|
||||
}, (error, result) => {
|
||||
chai.expect(error, null);
|
||||
if (result.statusCode !== 200) console.log(result);
|
||||
chai.expect(result.statusCode).equal(200);
|
||||
const jsendResult = result.data;
|
||||
chai.expect(jsendResult.status).equal('success');
|
||||
|
|
@ -208,10 +213,11 @@ describe('basic api v1 returns', () => {
|
|||
data: {
|
||||
token,
|
||||
mobileToken,
|
||||
subsId
|
||||
subsId: returnedSubsId
|
||||
}
|
||||
}, (error, result) => {
|
||||
chai.expect(error, null);
|
||||
if (result.statusCode !== 200) console.log(result);
|
||||
chai.expect(result.statusCode).equal(200);
|
||||
const jsendResult = result.data;
|
||||
chai.expect(jsendResult.status).equal('success');
|
||||
|
|
@ -219,16 +225,33 @@ describe('basic api v1 returns', () => {
|
|||
});
|
||||
});
|
||||
|
||||
it('should get all mobile user subscriptions after deletion', async (done) => {
|
||||
HTTP.get(url(`api/v1/mobile/subscriptions/all/${token}/${mobileToken}`), {
|
||||
}, (error, result) => {
|
||||
chai.expect(error, null);
|
||||
if (result.statusCode !== 200) console.log(result);
|
||||
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);
|
||||
// console.log(JSON.stringify(jsendResult.data.subscriptions));
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
it('should not remove mobile user subscriptions with wrong token', async (done) => {
|
||||
HTTP.del(url('api/v1/mobile/subscriptions'), {
|
||||
data: {
|
||||
token: 'wrongOne',
|
||||
mobileToken,
|
||||
subsId
|
||||
subsId: returnedSubsId
|
||||
}
|
||||
}, (error, result) => {
|
||||
chai.expect(error, null);
|
||||
if (result.statusCode !== 401) console.log(result);
|
||||
chai.expect(result.statusCode).equal(401);
|
||||
done();
|
||||
});
|
||||
|
|
@ -238,6 +261,7 @@ describe('basic api v1 returns', () => {
|
|||
HTTP.get(url(`api/v1/mobile/subscriptions/all/wrongOne/${mobileToken}`), {
|
||||
}, (error, result) => {
|
||||
chai.expect(error, null);
|
||||
if (result.statusCode !== 401) console.log(result);
|
||||
chai.expect(result.statusCode).equal(401);
|
||||
done();
|
||||
});
|
||||
|
|
@ -247,7 +271,8 @@ describe('basic api v1 returns', () => {
|
|||
HTTP.get(url(`api/v1/mobile/subscriptions/all/${token}/${mobileToken}`), {
|
||||
}, (error, result) => {
|
||||
chai.expect(error, null);
|
||||
console.log(result);
|
||||
// console.log(result);
|
||||
if (result.statusCode !== 200) console.log(result);
|
||||
chai.expect(result.statusCode).equal(200);
|
||||
const jsendResult = result.data;
|
||||
chai.expect(jsendResult.status).equal('success');
|
||||
|
|
@ -264,11 +289,12 @@ describe('basic api v1 returns', () => {
|
|||
HTTP.del(url(`api/v1/mobile/subscriptions/all/${token}/${mobileToken}`), {
|
||||
}, (error, result) => {
|
||||
chai.expect(error, null);
|
||||
if (result.statusCode !== 200) console.log(result);
|
||||
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(1);
|
||||
chai.expect(jsendResult.data.count).to.be.at.most(1);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue