2016-03-13 18:15:15 -04:00
|
|
|
|
'use strict';
|
2016-02-08 20:20:08 -08:00
|
|
|
|
var APNS = require('../src/APNS');
|
2016-02-04 00:08:31 -08:00
|
|
|
|
|
|
|
|
|
|
describe('APNS', () => {
|
2016-02-11 02:13:23 -08:00
|
|
|
|
|
|
|
|
|
|
it('can initialize with single cert', (done) => {
|
|
|
|
|
|
var args = {
|
|
|
|
|
|
cert: 'prodCert.pem',
|
|
|
|
|
|
key: 'prodKey.pem',
|
|
|
|
|
|
production: true,
|
|
|
|
|
|
bundleId: 'bundleId'
|
|
|
|
|
|
}
|
2016-03-13 18:15:15 -04:00
|
|
|
|
var apns = APNS(args);
|
2016-02-11 02:13:23 -08:00
|
|
|
|
|
2016-03-13 18:15:15 -04:00
|
|
|
|
var apnsConfiguration = apns.getConfiguration();
|
|
|
|
|
|
expect(apnsConfiguration.bundleId).toBe(args.bundleId);
|
|
|
|
|
|
expect(apnsConfiguration.cert).toBe(args.cert);
|
|
|
|
|
|
expect(apnsConfiguration.key).toBe(args.key);
|
|
|
|
|
|
expect(apnsConfiguration.production).toBe(args.production);
|
2016-02-11 02:13:23 -08:00
|
|
|
|
done();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('can initialize with multiple certs', (done) => {
|
|
|
|
|
|
var args = [
|
|
|
|
|
|
{
|
|
|
|
|
|
cert: 'devCert.pem',
|
|
|
|
|
|
key: 'devKey.pem',
|
|
|
|
|
|
production: false,
|
|
|
|
|
|
bundleId: 'bundleId'
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
cert: 'prodCert.pem',
|
|
|
|
|
|
key: 'prodKey.pem',
|
|
|
|
|
|
production: true,
|
|
|
|
|
|
bundleId: 'bundleIdAgain'
|
|
|
|
|
|
}
|
|
|
|
|
|
]
|
|
|
|
|
|
|
2016-03-13 18:15:15 -04:00
|
|
|
|
var apns = APNS(args);
|
|
|
|
|
|
var devApnsConfiguration = apns.getConfiguration('bundleId');
|
|
|
|
|
|
expect(devApnsConfiguration.cert).toBe(args[0].cert);
|
|
|
|
|
|
expect(devApnsConfiguration.key).toBe(args[0].key);
|
|
|
|
|
|
expect(devApnsConfiguration.production).toBe(args[0].production);
|
|
|
|
|
|
expect(devApnsConfiguration.bundleId).toBe(args[0].bundleId);
|
|
|
|
|
|
|
|
|
|
|
|
var prodApnsConfiguration = apns.getConfiguration('bundleIdAgain');
|
|
|
|
|
|
expect(prodApnsConfiguration.cert).toBe(args[1].cert);
|
|
|
|
|
|
expect(prodApnsConfiguration.key).toBe(args[1].key);
|
|
|
|
|
|
expect(prodApnsConfiguration.production).toBe(args[1].production);
|
|
|
|
|
|
expect(prodApnsConfiguration.bundleId).toBe(args[1].bundleId);
|
2016-02-11 02:13:23 -08:00
|
|
|
|
done();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2016-02-04 00:08:31 -08:00
|
|
|
|
it('can generate APNS notification', (done) => {
|
|
|
|
|
|
//Mock request data
|
|
|
|
|
|
var data = {
|
|
|
|
|
|
'alert': 'alert',
|
|
|
|
|
|
'badge': 100,
|
|
|
|
|
|
'sound': 'test',
|
|
|
|
|
|
'content-available': 1,
|
|
|
|
|
|
'category': 'INVITE_CATEGORY',
|
|
|
|
|
|
'key': 'value',
|
|
|
|
|
|
'keyAgain': 'valueAgain'
|
|
|
|
|
|
};
|
|
|
|
|
|
var expirationTime = 1454571491354
|
|
|
|
|
|
|
2016-03-13 18:15:15 -04:00
|
|
|
|
var notification = APNS.generateNotification(data);
|
|
|
|
|
|
expect(notification.aps.alert).toEqual(data.alert);
|
|
|
|
|
|
expect(notification.aps.badge).toEqual(data.badge);
|
|
|
|
|
|
expect(notification.aps.sound).toEqual(data.sound);
|
|
|
|
|
|
expect(notification.aps['content-available']).toEqual(1);
|
|
|
|
|
|
expect(notification.aps.category).toEqual(data.category);
|
|
|
|
|
|
expect(notification.key).toEqual('value');
|
|
|
|
|
|
expect(notification.keyAgain).toEqual('valueAgain');
|
2016-02-11 02:13:23 -08:00
|
|
|
|
done();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('can choose conns for device with invalid appIdentifier', (done) => {
|
|
|
|
|
|
// Mock conns
|
|
|
|
|
|
var conns = [
|
|
|
|
|
|
{
|
2016-03-13 18:15:15 -04:00
|
|
|
|
bundleId: 'bundleId',
|
2016-02-11 02:13:23 -08:00
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
bundleId: 'bundleIdAgain'
|
|
|
|
|
|
}
|
|
|
|
|
|
];
|
|
|
|
|
|
// Mock device
|
|
|
|
|
|
var device = {
|
|
|
|
|
|
appIdentifier: 'invalid'
|
|
|
|
|
|
};
|
2016-03-13 18:15:15 -04:00
|
|
|
|
let apns = APNS(conns);
|
|
|
|
|
|
var config = apns.getConfiguration(device.appIdentifier);
|
|
|
|
|
|
expect(config).toBeUndefined();
|
2016-02-11 02:13:23 -08:00
|
|
|
|
done();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2016-02-04 00:08:31 -08:00
|
|
|
|
it('can send APNS notification', (done) => {
|
2016-02-11 02:13:23 -08:00
|
|
|
|
var args = {
|
|
|
|
|
|
production: true,
|
|
|
|
|
|
bundleId: 'bundleId'
|
|
|
|
|
|
}
|
2016-03-13 18:15:15 -04:00
|
|
|
|
var apns = APNS(args);
|
2016-02-04 00:08:31 -08:00
|
|
|
|
// Mock data
|
|
|
|
|
|
var expirationTime = 1454571491354
|
|
|
|
|
|
var data = {
|
|
|
|
|
|
'expiration_time': expirationTime,
|
|
|
|
|
|
'data': {
|
|
|
|
|
|
'alert': 'alert'
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-02-08 12:02:07 -08:00
|
|
|
|
// Mock devices
|
|
|
|
|
|
var devices = [
|
2016-02-11 02:13:23 -08:00
|
|
|
|
{
|
|
|
|
|
|
deviceToken: '112233',
|
|
|
|
|
|
appIdentifier: 'bundleId'
|
|
|
|
|
|
}
|
2016-02-08 12:02:07 -08:00
|
|
|
|
];
|
2016-02-04 00:08:31 -08:00
|
|
|
|
|
2016-03-13 18:15:15 -04:00
|
|
|
|
apns.send(data, devices).then((results) => {
|
|
|
|
|
|
let isArray = Array.isArray(results);
|
|
|
|
|
|
expect(isArray).toBe(true);
|
|
|
|
|
|
expect(results.length).toBe(1);
|
|
|
|
|
|
// No provided certificates
|
|
|
|
|
|
expect(results[0].status).toBe(403);
|
|
|
|
|
|
expect(results[0].device).toEqual(devices[0]);
|
|
|
|
|
|
expect(results[0].transmitted).toBe(false);
|
|
|
|
|
|
done();
|
|
|
|
|
|
}, (err) => {
|
|
|
|
|
|
fail('should not fail');
|
|
|
|
|
|
done();
|
|
|
|
|
|
});
|
2016-02-04 00:08:31 -08:00
|
|
|
|
});
|
|
|
|
|
|
});
|