Migrate from GCM to FCM notifications (#7860)

* Fixed logging. Fixed lint issues. Temporarly removed GCM code

* Removed extra packages. Removed lingering gcm code

* Update message structure to send a data notification instead of a regular notification

* Removed excess code

* switch from fcm-push to node-gcm
This commit is contained in:
Matteo Pagliazzi
2016-08-02 23:12:39 +02:00
committed by GitHub
parent 4244c7519e
commit fb939e0300
4 changed files with 29 additions and 94 deletions

View File

@@ -2,12 +2,13 @@ import { model as User } from '../../../../../website/server/models/user';
import requireAgain from 'require-again';
import pushNotify from 'push-notify';
import nconf from 'nconf';
import gcmLib from 'node-gcm'; // works with FCM notifications too
describe('pushNotifications', () => {
let user;
let sendPushNotification;
let pathToPushNotifications = '../../../../../website/server/libs/pushNotifications';
let gcmSendSpy;
let fcmSendSpy;
let apnSendSpy;
let identifier = 'identifier';
@@ -16,15 +17,12 @@ describe('pushNotifications', () => {
beforeEach(() => {
user = new User();
gcmSendSpy = sinon.spy();
fcmSendSpy = sinon.spy();
apnSendSpy = sinon.spy();
sandbox.stub(nconf, 'get').returns('true');
sandbox.stub(nconf, 'get').returns('true-key');
sandbox.stub(pushNotify, 'gcm').returns({
on: () => null,
send: gcmSendSpy,
});
sandbox.stub(gcmLib.Sender.prototype, 'send', fcmSendSpy);
sandbox.stub(pushNotify, 'apn').returns({
on: () => null,
@@ -40,14 +38,14 @@ describe('pushNotifications', () => {
it('throws if user is not supplied', () => {
expect(sendPushNotification).to.throw;
expect(gcmSendSpy).to.not.have.been.called;
expect(fcmSendSpy).to.not.have.been.called;
expect(apnSendSpy).to.not.have.been.called;
});
it('throws if user.preferences.pushNotifications.unsubscribeFromAll is true', () => {
user.preferences.pushNotifications.unsubscribeFromAll = true;
expect(() => sendPushNotification(user)).to.throw;
expect(gcmSendSpy).to.not.have.been.called;
expect(fcmSendSpy).to.not.have.been.called;
expect(apnSendSpy).to.not.have.been.called;
});
@@ -56,7 +54,7 @@ describe('pushNotifications', () => {
title,
message,
})).to.throw;
expect(gcmSendSpy).to.not.have.been.called;
expect(fcmSendSpy).to.not.have.been.called;
expect(apnSendSpy).to.not.have.been.called;
});
@@ -65,7 +63,7 @@ describe('pushNotifications', () => {
identifier,
message,
})).to.throw;
expect(gcmSendSpy).to.not.have.been.called;
expect(fcmSendSpy).to.not.have.been.called;
expect(apnSendSpy).to.not.have.been.called;
});
@@ -74,7 +72,7 @@ describe('pushNotifications', () => {
identifier,
title,
})).to.throw;
expect(gcmSendSpy).to.not.have.been.called;
expect(fcmSendSpy).to.not.have.been.called;
expect(apnSendSpy).to.not.have.been.called;
});
@@ -84,68 +82,7 @@ describe('pushNotifications', () => {
title,
message,
});
expect(gcmSendSpy).to.not.have.been.called;
expect(apnSendSpy).to.not.have.been.called;
});
it('uses GCM for Android devices', () => {
user.pushDevices.push({
type: 'android',
regId: '123',
});
let details = {
identifier,
title,
message,
payload: {
a: true,
b: true,
},
timeToLive: 23,
};
sendPushNotification(user, details);
expect(gcmSendSpy).to.have.been.calledOnce;
expect(gcmSendSpy).to.have.been.calledWithMatch({
registrationId: '123',
delayWhileIdle: true,
timeToLive: 23,
data: {
identifier,
title,
message,
a: true,
b: true,
},
});
expect(apnSendSpy).to.not.have.been.called;
});
it('defaults timeToLive to 15', () => {
user.pushDevices.push({
type: 'android',
regId: '123',
});
let details = {
identifier,
title,
message,
};
sendPushNotification(user, details);
expect(gcmSendSpy).to.have.been.calledOnce;
expect(gcmSendSpy).to.have.been.calledWithMatch({
registrationId: '123',
delayWhileIdle: true,
timeToLive: 15,
data: {
identifier,
title,
message,
},
});
expect(fcmSendSpy).to.not.have.been.called;
expect(apnSendSpy).to.not.have.been.called;
});
@@ -180,6 +117,6 @@ describe('pushNotifications', () => {
b: true,
},
});
expect(gcmSendSpy).to.not.have.been.called;
expect(fcmSendSpy).to.not.have.been.called;
});
});