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