Skip to content

Coverage #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .istanbul.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
instrumentation:
excludes: ["**/spec/**"]
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"babel-preset-es2015": "^6.6.0",
"babel-preset-stage-0": "^6.22.0",
"codecov": "^1.0.1",
"istanbul": "^0.4.5",
"istanbul": "1.1.0-alpha.1",
"jasmine": "2.5.3",
"jasmine-spec-reporter": "^3.2.0"
},
Expand Down
28 changes: 28 additions & 0 deletions spec/APNS.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var APNS = require('../src/APNS');
var Parse = require('parse/node');

describe('APNS', () => {

Expand All @@ -23,6 +24,33 @@ describe('APNS', () => {
done();
});

it('fails to initialize with bad data', (done) => {
try {
new APNS("args");
} catch(e) {
expect(e.code).toBe(Parse.Error.PUSH_MISCONFIGURED);
done();
return;
}
fail('should not be reached');
});

it('fails to initialize without a bundleID', (done) => {
const apnsArgs = {
production: true,
bundle: 'hello'
};
try {
new APNS(apnsArgs);
} catch(e) {
expect(e.code).toBe(Parse.Error.PUSH_MISCONFIGURED);
done();
return;
}
fail('should not be reached');
done();
});

it('can initialize with multiple certs', (done) => {
var args = [
{
Expand Down
14 changes: 14 additions & 0 deletions spec/MockAPNConnection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const EventEmitter = require('events');

module.exports = function (args) {
let emitter = new EventEmitter();
emitter.options = args;
emitter.pushNotification = function(push, devices) {
devices.forEach((device) => {
process.nextTick(() => {
emitter.emit('transmitted', push, device);
});
});
};
return emitter;
}
69 changes: 62 additions & 7 deletions spec/ParsePushAdapter.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
var ParsePushAdapter = require('../src/index').ParsePushAdapter;
var randomString = require('../src/PushAdapterUtils').randomString;
var APNS = require('../src/APNS');
var GCM = require('../src/GCM');
var MockAPNConnection = require('./MockAPNConnection');

describe('ParsePushAdapter', () => {

beforeEach(() => {
jasmine.mockLibrary('apn', 'Connection', MockAPNConnection);
});

afterEach(() => {
jasmine.restoreLibrary('apn', 'Connection');
});

it('can be initialized', (done) => {
// Make mock config
var pushConfig = {
Expand Down Expand Up @@ -278,22 +289,22 @@ describe('ParsePushAdapter', () => {
{
deviceType: 'ios',
deviceToken: '0d72a1baa92a2febd9a254cbd6584f750c70b2350af5fc9052d1d12584b738e6',
appIdentifier: 'invalidiosbundleId'
appIdentifier: 'iosbundleId'
},
{
deviceType: 'ios',
deviceToken: 'ff3943ed0b2090c47e5d6f07d8f202a10427941d7897fda5a6b18c6d9fd07d48',
appIdentifier: 'invalidiosbundleId'
appIdentifier: 'iosbundleId'
},
{
deviceType: 'osx',
deviceToken: '5cda62a8d88eb48d9111a6c436f2e326a053eb0cd72dfc3a0893089342602235',
appIdentifier: 'invalidosxbundleId'
appIdentifier: 'osxbundleId'
},
{
deviceType: 'tvos',
deviceToken: '3e72a1baa92a2febd9a254cbd6584f750c70b2350af5fc9052d1d12584b738e6',
appIdentifier: 'invalidiosbundleId' // ios and tvos share the same bundleid
appIdentifier: 'iosbundleId' // ios and tvos share the same bundleid
},
{
deviceType: 'win',
Expand All @@ -312,10 +323,19 @@ describe('ParsePushAdapter', () => {
// 2x iOS, 1x android, 1x osx, 1x tvos
expect(results.length).toBe(5);
results.forEach((result) => {
expect(result.transmitted).toBe(false);
expect(typeof result.device).toBe('object');
expect(typeof result.device.deviceType).toBe('string');
expect(typeof result.device.deviceToken).toBe('string');
if (!result.device) {
fail('result should have device');
return;
}
const device = result.device;
expect(typeof device.deviceType).toBe('string');
expect(typeof device.deviceToken).toBe('string');
if (device.deviceType === 'ios' || device.deviceType === 'osx') {
expect(result.transmitted).toBe(true);
} else {
expect(result.transmitted).toBe(false);
}
})
done();
}).catch((err) => {
Expand All @@ -324,6 +344,41 @@ describe('ParsePushAdapter', () => {
})
});

it('properly marks not transmitter when sender is missing', (done) => {
var pushConfig = {
android: {
senderId: 'senderId',
apiKey: 'apiKey'
}
};
var installations = [{
deviceType: 'ios',
deviceToken: '0d72a1baa92a2febd9a254cbd6584f750c70b2350af5fc9052d1d12584b738e6',
appIdentifier: 'invalidiosbundleId'
},
{
deviceType: 'ios',
deviceToken: 'ff3943ed0b2090c47e5d6f07d8f202a10427941d7897fda5a6b18c6d9fd07d48',
appIdentifier: 'invalidiosbundleId'
}]
var parsePushAdapter = new ParsePushAdapter(pushConfig);
parsePushAdapter.send({data: {alert: 'some'}}, installations).then((results) => {
expect(results.length).toBe(2);
results.forEach((result) => {
expect(result.transmitted).toBe(false);
expect(typeof result.device).toBe('object');
expect(typeof result.device.deviceType).toBe('string');
expect(typeof result.device.deviceToken).toBe('string');
expect(result.response.error.indexOf('Can not find sender for push type ios, ')).toBe(0);
});
done();
});
});

it('random string throws with size <=0', () => {
expect(() => randomString(0)).toThrow();
});

function makeDevice(deviceToken, deviceType, appIdentifier) {
return {
deviceToken: deviceToken,
Expand Down
16 changes: 16 additions & 0 deletions spec/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,19 @@ jasmine.DEFAULT_TIMEOUT_INTERVAL = process.env.PARSE_SERVER_TEST_TIMEOUT || 5000
jasmine.getEnv().clearReporters();
jasmine.getEnv().addReporter(new SpecReporter());

var libraryCache = {};
jasmine.mockLibrary = function(library, name, mock) {
var original = require(library)[name];
if (!libraryCache[library]) {
libraryCache[library] = {};
}
require(library)[name] = mock;
libraryCache[library][name] = original;
}

jasmine.restoreLibrary = function(library, name) {
if (!libraryCache[library] || !libraryCache[library][name]) {
throw 'Can not find library ' + library + ' ' + name;
}
require(library)[name] = libraryCache[library][name];
}
6 changes: 5 additions & 1 deletion src/APNS.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,25 @@ function APNS(args) {
}

// Set apns client callbacks
/* istanbul ignore next */
conn.on('connected', () => {
log.verbose(LOG_PREFIX, 'APNS Connection %d Connected', conn.index);
});

conn.on('transmissionError', (errCode, notification, apnDevice) => {
handleTransmissionError(this.conns, errCode, notification, apnDevice);
});

/* istanbul ignore next */
conn.on('timeout', () => {
log.verbose(LOG_PREFIX, 'APNS Connection %d Timeout', conn.index);
});

/* istanbul ignore next */
conn.on('disconnected', () => {
log.verbose(LOG_PREFIX, 'APNS Connection %d Disconnected', conn.index);
});

/* istanbul ignore next */
conn.on('socketError', () => {
log.verbose(LOG_PREFIX, 'APNS Connection %d Socket Error', conn.index);
});
Expand Down Expand Up @@ -121,6 +124,7 @@ APNS.prototype.send = function(data, devices) {
allPromises.push(promise);
} else {
let apnDevice = new apn.Device(device.deviceToken);
apnDevice.deviceType = device.deviceType;
apnDevice.connIndex = qualifiedConnIndexs[0];
if (device.appIdentifier) {
apnDevice.appIdentifier = device.appIdentifier;
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// for ios push.
import log from 'npmlog';

/* istanbul ignore if */
if (process.env.VERBOSE || process.env.VERBOSE_PARSE_SERVER_PUSH_ADAPTER) {
log.level = 'verbose';
}
Expand Down