Add support for push
This commit is contained in:
@@ -43,16 +43,18 @@ describe('APNS', () => {
|
||||
'alert': 'alert'
|
||||
}
|
||||
}
|
||||
// Mock registrationTokens
|
||||
var deviceTokens = ['token'];
|
||||
// Mock devices
|
||||
var devices = [
|
||||
{ deviceToken: 'token' }
|
||||
];
|
||||
|
||||
var promise = apns.send(data, deviceTokens);
|
||||
var promise = apns.send(data, devices);
|
||||
expect(sender.pushNotification).toHaveBeenCalled();
|
||||
var args = sender.pushNotification.calls.first().args;
|
||||
var notification = args[0];
|
||||
expect(notification.alert).toEqual(data.data.alert);
|
||||
expect(notification.expiry).toEqual(data['expiration_time']);
|
||||
expect(args[1]).toEqual(deviceTokens);
|
||||
expect(args[1]).toEqual(['token']);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -104,14 +104,18 @@ describe('GCM', () => {
|
||||
'alert': 'alert'
|
||||
}
|
||||
}
|
||||
// Mock registrationTokens
|
||||
var registrationTokens = ['token'];
|
||||
// Mock devices
|
||||
var devices = [
|
||||
{
|
||||
deviceToken: 'token'
|
||||
}
|
||||
];
|
||||
|
||||
var promise = gcm.send(data, registrationTokens);
|
||||
var promise = gcm.send(data, devices);
|
||||
expect(sender.send).toHaveBeenCalled();
|
||||
var args = sender.send.calls.first().args;
|
||||
// It is too hard to verify message of gcm library, we just verify tokens and retry times
|
||||
expect(args[1].registrationTokens).toEqual(registrationTokens);
|
||||
expect(args[1].registrationTokens).toEqual(['token']);
|
||||
expect(args[2]).toEqual(5);
|
||||
done();
|
||||
});
|
||||
@@ -123,14 +127,16 @@ describe('GCM', () => {
|
||||
send: jasmine.createSpy('send')
|
||||
};
|
||||
gcm.sender = sender;
|
||||
// Mock registrationTokens
|
||||
var registrationTokens = [];
|
||||
// Mock devices
|
||||
var devices = [];
|
||||
for (var i = 0; i <= 2000; i++) {
|
||||
registrationTokens.push(i.toString());
|
||||
devices.push({
|
||||
deviceToken: i.toString()
|
||||
});
|
||||
}
|
||||
|
||||
expect(function() {
|
||||
gcm.send({}, registrationTokens);
|
||||
gcm.send({}, devices);
|
||||
}).toThrow();
|
||||
done();
|
||||
});
|
||||
|
||||
237
spec/ParsePushAdapter.spec.js
Normal file
237
spec/ParsePushAdapter.spec.js
Normal file
@@ -0,0 +1,237 @@
|
||||
var ParsePushAdapter = require('../src/Adapters/Push/ParsePushAdapter');
|
||||
|
||||
describe('ParsePushAdapter', () => {
|
||||
it('can be initialized', (done) => {
|
||||
var parsePushAdapter = new ParsePushAdapter();
|
||||
|
||||
expect(parsePushAdapter.validPushTypes).toEqual(['ios', 'android']);
|
||||
done();
|
||||
});
|
||||
|
||||
it('can initialize', (done) => {
|
||||
var parsePushAdapter = new ParsePushAdapter();
|
||||
// Make mock config
|
||||
var pushConfig = {
|
||||
android: {
|
||||
senderId: 'senderId',
|
||||
apiKey: 'apiKey'
|
||||
},
|
||||
ios: [
|
||||
{
|
||||
cert: 'prodCert.pem',
|
||||
key: 'prodKey.pem',
|
||||
production: true
|
||||
},
|
||||
{
|
||||
cert: 'devCert.pem',
|
||||
key: 'devKey.pem',
|
||||
production: false
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
parsePushAdapter.initialize(pushConfig);
|
||||
// Check ios
|
||||
var iosSenders = parsePushAdapter.senders['ios'];
|
||||
expect(iosSenders.length).toBe(2);
|
||||
// TODO: Remove this checking onec we inject APNS
|
||||
var prodApnsOptions = iosSenders[0].sender.options;
|
||||
expect(prodApnsOptions.cert).toBe(pushConfig.ios[0].cert);
|
||||
expect(prodApnsOptions.key).toBe(pushConfig.ios[0].key);
|
||||
expect(prodApnsOptions.production).toBe(pushConfig.ios[0].production);
|
||||
var devApnsOptions = iosSenders[1].sender.options;
|
||||
expect(devApnsOptions.cert).toBe(pushConfig.ios[1].cert);
|
||||
expect(devApnsOptions.key).toBe(pushConfig.ios[1].key);
|
||||
expect(devApnsOptions.production).toBe(pushConfig.ios[1].production);
|
||||
// Check android
|
||||
var androidSenders = parsePushAdapter.senders['android'];
|
||||
expect(androidSenders.length).toBe(1);
|
||||
var androidSender = androidSenders[0];
|
||||
// TODO: Remove this checking onec we inject GCM
|
||||
expect(androidSender.sender.key).toBe(pushConfig.android.apiKey);
|
||||
done();
|
||||
});
|
||||
|
||||
it('can throw on initializing with unsupported push type', (done) => {
|
||||
var parsePushAdapter = new ParsePushAdapter();
|
||||
// Make mock config
|
||||
var pushConfig = {
|
||||
win: {
|
||||
senderId: 'senderId',
|
||||
apiKey: 'apiKey'
|
||||
}
|
||||
};
|
||||
|
||||
expect(function() {
|
||||
parsePushAdapter.initialize(pushConfig)
|
||||
}).toThrow();
|
||||
done();
|
||||
});
|
||||
|
||||
it('can throw on initializing with invalid pushConfig', (done) => {
|
||||
var parsePushAdapter = new ParsePushAdapter();
|
||||
// Make mock config
|
||||
var pushConfig = {
|
||||
android: 123
|
||||
};
|
||||
|
||||
expect(function() {
|
||||
parsePushAdapter.initialize(pushConfig)
|
||||
}).toThrow();
|
||||
done();
|
||||
});
|
||||
|
||||
it('can get push senders', (done) => {
|
||||
var parsePushAdapter = new ParsePushAdapter();
|
||||
// Mock push senders
|
||||
var androidSender = {};
|
||||
var iosSender = {};
|
||||
var iosSenderAgain = {};
|
||||
parsePushAdapter.senders = {
|
||||
android: [
|
||||
androidSender
|
||||
],
|
||||
ios: [
|
||||
iosSender,
|
||||
iosSenderAgain
|
||||
]
|
||||
};
|
||||
|
||||
expect(parsePushAdapter.getPushSenders('android')).toEqual([androidSender]);
|
||||
expect(parsePushAdapter.getPushSenders('ios')).toEqual([iosSender, iosSenderAgain]);
|
||||
done();
|
||||
});
|
||||
|
||||
it('can get empty push senders', (done) => {
|
||||
var parsePushAdapter = new ParsePushAdapter();
|
||||
|
||||
expect(parsePushAdapter.getPushSenders('android')).toEqual([]);
|
||||
done();
|
||||
});
|
||||
|
||||
it('can get valid push types', (done) => {
|
||||
var parsePushAdapter = new ParsePushAdapter();
|
||||
|
||||
expect(parsePushAdapter.getValidPushTypes()).toEqual(['ios', 'android']);
|
||||
done();
|
||||
});
|
||||
|
||||
it('can classify installation', (done) => {
|
||||
// Mock installations
|
||||
var validPushTypes = ['ios', 'android'];
|
||||
var installations = [
|
||||
{
|
||||
deviceType: 'android',
|
||||
deviceToken: 'androidToken'
|
||||
},
|
||||
{
|
||||
deviceType: 'ios',
|
||||
deviceToken: 'iosToken'
|
||||
},
|
||||
{
|
||||
deviceType: 'win',
|
||||
deviceToken: 'winToken'
|
||||
},
|
||||
{
|
||||
deviceType: 'android',
|
||||
deviceToken: undefined
|
||||
}
|
||||
];
|
||||
|
||||
var deviceTokenMap = ParsePushAdapter.classifyInstallation(installations, validPushTypes);
|
||||
expect(deviceTokenMap['android']).toEqual([makeDevice('androidToken')]);
|
||||
expect(deviceTokenMap['ios']).toEqual([makeDevice('iosToken')]);
|
||||
expect(deviceTokenMap['win']).toBe(undefined);
|
||||
done();
|
||||
});
|
||||
|
||||
it('can slice ios devices', (done) => {
|
||||
// Mock devices
|
||||
var devices = [makeDevice(1), makeDevice(2), makeDevice(3), makeDevice(4)];
|
||||
|
||||
var chunkDevices = ParsePushAdapter.sliceDevices('ios', devices, 2);
|
||||
expect(chunkDevices).toEqual([devices]);
|
||||
done();
|
||||
});
|
||||
|
||||
it('can slice android devices', (done) => {
|
||||
// Mock devices
|
||||
var devices = [makeDevice(1), makeDevice(2), makeDevice(3), makeDevice(4)];
|
||||
|
||||
var chunkDevices = ParsePushAdapter.sliceDevices('android', devices, 3);
|
||||
expect(chunkDevices).toEqual([
|
||||
[makeDevice(1), makeDevice(2), makeDevice(3)],
|
||||
[makeDevice(4)]
|
||||
]);
|
||||
done();
|
||||
});
|
||||
|
||||
|
||||
it('can send push notifications', (done) => {
|
||||
var parsePushAdapter = new ParsePushAdapter();
|
||||
// Mock android ios senders
|
||||
var androidSender = {
|
||||
send: jasmine.createSpy('send')
|
||||
};
|
||||
var iosSender = {
|
||||
send: jasmine.createSpy('send')
|
||||
};
|
||||
var iosSenderAgain = {
|
||||
send: jasmine.createSpy('send')
|
||||
};
|
||||
var senders = {
|
||||
ios: [iosSender, iosSenderAgain],
|
||||
android: [androidSender]
|
||||
};
|
||||
parsePushAdapter.senders = senders;
|
||||
// Mock installations
|
||||
var installations = [
|
||||
{
|
||||
deviceType: 'android',
|
||||
deviceToken: 'androidToken'
|
||||
},
|
||||
{
|
||||
deviceType: 'ios',
|
||||
deviceToken: 'iosToken'
|
||||
},
|
||||
{
|
||||
deviceType: 'win',
|
||||
deviceToken: 'winToken'
|
||||
},
|
||||
{
|
||||
deviceType: 'android',
|
||||
deviceToken: undefined
|
||||
}
|
||||
];
|
||||
var data = {};
|
||||
|
||||
parsePushAdapter.send(data, installations);
|
||||
// Check android sender
|
||||
expect(androidSender.send).toHaveBeenCalled();
|
||||
var args = androidSender.send.calls.first().args;
|
||||
expect(args[0]).toEqual(data);
|
||||
expect(args[1]).toEqual([
|
||||
makeDevice('androidToken')
|
||||
]);
|
||||
// Check ios sender
|
||||
expect(iosSender.send).toHaveBeenCalled();
|
||||
args = iosSender.send.calls.first().args;
|
||||
expect(args[0]).toEqual(data);
|
||||
expect(args[1]).toEqual([
|
||||
makeDevice('iosToken')
|
||||
]);
|
||||
expect(iosSenderAgain.send).toHaveBeenCalled();
|
||||
args = iosSenderAgain.send.calls.first().args;
|
||||
expect(args[0]).toEqual(data);
|
||||
expect(args[1]).toEqual([
|
||||
makeDevice('iosToken')
|
||||
]);
|
||||
done();
|
||||
});
|
||||
|
||||
function makeDevice(deviceToken) {
|
||||
return {
|
||||
deviceToken: deviceToken
|
||||
};
|
||||
}
|
||||
});
|
||||
@@ -104,10 +104,11 @@ describe('push', () => {
|
||||
it('can validate device type when no device type is set', (done) => {
|
||||
// Make query condition
|
||||
var where = {
|
||||
}
|
||||
};
|
||||
var validPushTypes = ['ios', 'android'];
|
||||
|
||||
expect(function(){
|
||||
push.validateDeviceType(where);
|
||||
push.validatePushType(where, validPushTypes);
|
||||
}).not.toThrow();
|
||||
done();
|
||||
});
|
||||
@@ -116,10 +117,11 @@ describe('push', () => {
|
||||
// Make query condition
|
||||
var where = {
|
||||
'deviceType': 'ios'
|
||||
}
|
||||
};
|
||||
var validPushTypes = ['ios', 'android'];
|
||||
|
||||
expect(function(){
|
||||
push.validateDeviceType(where);
|
||||
push.validatePushType(where, validPushTypes);
|
||||
}).not.toThrow();
|
||||
done();
|
||||
});
|
||||
@@ -130,10 +132,11 @@ describe('push', () => {
|
||||
'deviceType': {
|
||||
'$in': ['android', 'ios']
|
||||
}
|
||||
}
|
||||
};
|
||||
var validPushTypes = ['ios', 'android'];
|
||||
|
||||
expect(function(){
|
||||
push.validateDeviceType(where);
|
||||
push.validatePushType(where, validPushTypes);
|
||||
}).not.toThrow();
|
||||
done();
|
||||
});
|
||||
@@ -142,10 +145,11 @@ describe('push', () => {
|
||||
// Make query condition
|
||||
var where = {
|
||||
'deviceType': 'osx'
|
||||
}
|
||||
};
|
||||
var validPushTypes = ['ios', 'android'];
|
||||
|
||||
expect(function(){
|
||||
push.validateDeviceType(where);
|
||||
push.validatePushType(where, validPushTypes);
|
||||
}).toThrow();
|
||||
done();
|
||||
});
|
||||
@@ -154,10 +158,11 @@ describe('push', () => {
|
||||
// Make query condition
|
||||
var where = {
|
||||
'deviceType': 'osx'
|
||||
}
|
||||
};
|
||||
var validPushTypes = ['ios', 'android'];
|
||||
|
||||
expect(function(){
|
||||
push.validateDeviceType(where)
|
||||
push.validatePushType(where, validPushTypes);
|
||||
}).toThrow();
|
||||
done();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user