Files
kami-parse-server/spec/PushController.spec.js

349 lines
9.4 KiB
JavaScript
Raw Normal View History

2016-03-11 16:23:58 -05:00
"use strict";
var PushController = require('../src/Controllers/PushController').PushController;
2016-03-01 15:10:06 -05:00
var Config = require('../src/Config');
2016-03-13 23:34:44 -04:00
const successfulTransmissions = function(body, installations) {
let promises = installations.map((device) => {
return Promise.resolve({
transmitted: true,
device: device,
})
});
return Promise.all(promises);
}
const successfulIOS = function(body, installations) {
let promises = installations.map((device) => {
return Promise.resolve({
transmitted: device.deviceType == "ios",
device: device,
})
});
return Promise.all(promises);
}
describe('PushController', () => {
it('can validate device type when no device type is set', (done) => {
// Make query condition
var where = {
2016-02-08 12:02:07 -08:00
};
var validPushTypes = ['ios', 'android'];
expect(function(){
PushController.validatePushType(where, validPushTypes);
}).not.toThrow();
done();
});
it('can validate device type when single valid device type is set', (done) => {
// Make query condition
var where = {
'deviceType': 'ios'
2016-02-08 12:02:07 -08:00
};
var validPushTypes = ['ios', 'android'];
expect(function(){
PushController.validatePushType(where, validPushTypes);
}).not.toThrow();
done();
});
it('can validate device type when multiple valid device types are set', (done) => {
// Make query condition
var where = {
'deviceType': {
'$in': ['android', 'ios']
}
2016-02-08 12:02:07 -08:00
};
var validPushTypes = ['ios', 'android'];
expect(function(){
PushController.validatePushType(where, validPushTypes);
}).not.toThrow();
done();
});
it('can throw on validateDeviceType when single invalid device type is set', (done) => {
// Make query condition
var where = {
'deviceType': 'osx'
2016-02-08 12:02:07 -08:00
};
var validPushTypes = ['ios', 'android'];
expect(function(){
PushController.validatePushType(where, validPushTypes);
}).toThrow();
done();
});
it('can throw on validateDeviceType when single invalid device type is set', (done) => {
// Make query condition
var where = {
'deviceType': 'osx'
2016-02-08 12:02:07 -08:00
};
var validPushTypes = ['ios', 'android'];
expect(function(){
PushController.validatePushType(where, validPushTypes);
}).toThrow();
done();
});
it('can get expiration time in string format', (done) => {
// Make mock request
var timeStr = '2015-03-19T22:05:08Z';
var body = {
'expiration_time': timeStr
}
var time = PushController.getExpirationTime(body);
expect(time).toEqual(new Date(timeStr).valueOf());
done();
});
it('can get expiration time in number format', (done) => {
// Make mock request
var timeNumber = 1426802708;
var body = {
'expiration_time': timeNumber
}
var time = PushController.getExpirationTime(body);
expect(time).toEqual(timeNumber * 1000);
done();
});
it('can throw on getExpirationTime in invalid format', (done) => {
// Make mock request
var body = {
'expiration_time': 'abcd'
}
expect(function(){
PushController.getExpirationTime(body);
}).toThrow();
done();
});
2016-03-01 15:10:06 -05:00
it('properly increment badges', (done) => {
var payload = {data:{
2016-03-01 15:10:06 -05:00
alert: "Hello World!",
badge: "Increment",
}}
2016-03-01 15:10:06 -05:00
var installations = [];
while(installations.length != 10) {
var installation = new Parse.Object("_Installation");
installation.set("installationId", "installation_"+installations.length);
installation.set("deviceToken","device_token_"+installations.length)
installation.set("badge", installations.length);
installation.set("originalBadge", installations.length);
installation.set("deviceType", "ios");
installations.push(installation);
}
2016-03-01 15:10:06 -05:00
while(installations.length != 15) {
var installation = new Parse.Object("_Installation");
installation.set("installationId", "installation_"+installations.length);
installation.set("deviceToken","device_token_"+installations.length)
installation.set("deviceType", "android");
installations.push(installation);
}
2016-03-01 15:10:06 -05:00
var pushAdapter = {
send: function(body, installations) {
var badge = body.data.badge;
2016-03-01 15:10:06 -05:00
installations.forEach((installation) => {
if (installation.deviceType == "ios") {
expect(installation.badge).toEqual(badge);
expect(installation.originalBadge+1).toEqual(installation.badge);
} else {
expect(installation.badge).toBeUndefined();
}
})
2016-03-13 23:34:44 -04:00
return successfulTransmissions(body, installations);
2016-03-01 15:10:06 -05:00
},
getValidPushTypes: function() {
return ["ios", "android"];
}
}
2016-03-01 15:10:06 -05:00
var config = new Config(Parse.applicationId);
var auth = {
isMaster: true
}
2016-03-01 15:10:06 -05:00
var pushController = new PushController(pushAdapter, Parse.applicationId);
Parse.Object.saveAll(installations).then((installations) => {
2016-03-01 15:10:06 -05:00
return pushController.sendPush(payload, {}, config, auth);
}).then((result) => {
done();
}, (err) => {
console.error(err);
fail("should not fail");
done();
});
2016-03-01 15:10:06 -05:00
});
2016-03-01 15:10:06 -05:00
it('properly set badges to 1', (done) => {
var payload = {data: {
2016-03-01 15:10:06 -05:00
alert: "Hello World!",
badge: 1,
}}
2016-03-01 15:10:06 -05:00
var installations = [];
while(installations.length != 10) {
var installation = new Parse.Object("_Installation");
installation.set("installationId", "installation_"+installations.length);
installation.set("deviceToken","device_token_"+installations.length)
installation.set("badge", installations.length);
installation.set("originalBadge", installations.length);
installation.set("deviceType", "ios");
installations.push(installation);
}
2016-03-01 15:10:06 -05:00
var pushAdapter = {
send: function(body, installations) {
var badge = body.data.badge;
2016-03-01 15:10:06 -05:00
installations.forEach((installation) => {
expect(installation.badge).toEqual(badge);
expect(1).toEqual(installation.badge);
})
2016-03-13 23:34:44 -04:00
return successfulTransmissions(body, installations);
2016-03-01 15:10:06 -05:00
},
getValidPushTypes: function() {
return ["ios"];
}
}
2016-03-01 15:10:06 -05:00
var config = new Config(Parse.applicationId);
var auth = {
isMaster: true
}
2016-03-01 15:10:06 -05:00
var pushController = new PushController(pushAdapter, Parse.applicationId);
Parse.Object.saveAll(installations).then((installations) => {
2016-03-01 15:10:06 -05:00
return pushController.sendPush(payload, {}, config, auth);
}).then((result) => {
done();
}, (err) => {
console.error(err);
fail("should not fail");
done();
});
});
it('properly creates _PushStatus', (done) => {
2016-03-13 23:34:44 -04:00
var installations = [];
while(installations.length != 10) {
var installation = new Parse.Object("_Installation");
installation.set("installationId", "installation_"+installations.length);
installation.set("deviceToken","device_token_"+installations.length)
installation.set("badge", installations.length);
installation.set("originalBadge", installations.length);
installation.set("deviceType", "ios");
installations.push(installation);
}
while(installations.length != 15) {
var installation = new Parse.Object("_Installation");
installation.set("installationId", "installation_"+installations.length);
installation.set("deviceToken","device_token_"+installations.length)
installation.set("deviceType", "android");
installations.push(installation);
}
var payload = {data: {
alert: "Hello World!",
badge: 1,
}}
var pushAdapter = {
send: function(body, installations) {
2016-03-13 23:34:44 -04:00
return successfulIOS(body, installations);
},
getValidPushTypes: function() {
return ["ios"];
}
}
var config = new Config(Parse.applicationId);
var auth = {
isMaster: true
}
var pushController = new PushController(pushAdapter, Parse.applicationId);
2016-03-13 23:34:44 -04:00
Parse.Object.saveAll(installations).then(() => {
return pushController.sendPush(payload, {}, config, auth);
}).then((result) => {
let query = new Parse.Query('_PushStatus');
return query.find({useMasterKey: true});
}).then((results) => {
expect(results.length).toBe(1);
let result = results[0];
expect(result.get('source')).toEqual('rest');
expect(result.get('query')).toEqual(JSON.stringify({}));
expect(result.get('payload')).toEqual(payload.data);
2016-03-13 23:34:44 -04:00
expect(result.get('status')).toEqual('succeeded');
expect(result.get('numSent')).toEqual(10);
expect(result.get('sentPerType')).toEqual({
'ios': 10 // 10 ios
});
expect(result.get('numFailed')).toEqual(5);
expect(result.get('failedPerType')).toEqual({
'android': 5 // android
});
done();
});
});
2016-03-11 16:23:58 -05:00
it('should support full RESTQuery for increment', (done) => {
var payload = {data: {
alert: "Hello World!",
badge: 'Increment',
}}
2016-03-11 16:23:58 -05:00
var pushAdapter = {
send: function(body, installations) {
2016-03-13 23:34:44 -04:00
return successfulTransmissions(body, installations);
2016-03-11 16:23:58 -05:00
},
getValidPushTypes: function() {
return ["ios"];
}
}
2016-03-11 16:23:58 -05:00
var config = new Config(Parse.applicationId);
var auth = {
isMaster: true
}
2016-03-11 16:23:58 -05:00
let where = {
'deviceToken': {
'$inQuery': {
'where': {
'deviceType': 'ios'
},
className: '_Installation'
}
}
}
var pushController = new PushController(pushAdapter, Parse.applicationId);
pushController.sendPush(payload, where, config, auth).then((result) => {
done();
}).catch((err) => {
fail('should not fail');
done();
});
});
});