mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-14 13:17:24 +01:00
* fixing typos in comments. yes, I am that kind of nerd * replacing push-notify with node-apn in deps and in pushNotifications.js * updating calling code and tests to use node-apn * updating APN configs to new format * migrating team ID and key ID to config.json * update code to use env variables and add correct topic
124 lines
3.2 KiB
JavaScript
124 lines
3.2 KiB
JavaScript
import { model as User } from '../../../../website/server/models/user';
|
|
import requireAgain from 'require-again';
|
|
import apn from 'apn/mock';
|
|
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 fcmSendSpy;
|
|
let apnSendSpy;
|
|
|
|
let identifier = 'identifier';
|
|
let title = 'title';
|
|
let message = 'message';
|
|
|
|
beforeEach(() => {
|
|
user = new User();
|
|
fcmSendSpy = sinon.spy();
|
|
apnSendSpy = sinon.spy();
|
|
|
|
sandbox.stub(nconf, 'get').returns('true-key');
|
|
|
|
sandbox.stub(gcmLib.Sender.prototype, 'send').callsFake(fcmSendSpy);
|
|
|
|
sandbox.stub(apn.Provider.prototype, 'send').returns({
|
|
on: () => null,
|
|
send: apnSendSpy,
|
|
});
|
|
|
|
sendPushNotification = requireAgain(pathToPushNotifications).sendNotification;
|
|
});
|
|
|
|
afterEach(() => {
|
|
sandbox.restore();
|
|
});
|
|
|
|
it('throws if user is not supplied', () => {
|
|
expect(sendPushNotification).to.throw;
|
|
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(fcmSendSpy).to.not.have.been.called;
|
|
expect(apnSendSpy).to.not.have.been.called;
|
|
});
|
|
|
|
it('throws if details.identifier is not supplied', () => {
|
|
expect(() => sendPushNotification(user, {
|
|
title,
|
|
message,
|
|
})).to.throw;
|
|
expect(fcmSendSpy).to.not.have.been.called;
|
|
expect(apnSendSpy).to.not.have.been.called;
|
|
});
|
|
|
|
it('throws if details.title is not supplied', () => {
|
|
expect(() => sendPushNotification(user, {
|
|
identifier,
|
|
message,
|
|
})).to.throw;
|
|
expect(fcmSendSpy).to.not.have.been.called;
|
|
expect(apnSendSpy).to.not.have.been.called;
|
|
});
|
|
|
|
it('throws if details.message is not supplied', () => {
|
|
expect(() => sendPushNotification(user, {
|
|
identifier,
|
|
title,
|
|
})).to.throw;
|
|
expect(fcmSendSpy).to.not.have.been.called;
|
|
expect(apnSendSpy).to.not.have.been.called;
|
|
});
|
|
|
|
it('returns if no device is registered', () => {
|
|
sendPushNotification(user, {
|
|
identifier,
|
|
title,
|
|
message,
|
|
});
|
|
expect(fcmSendSpy).to.not.have.been.called;
|
|
expect(apnSendSpy).to.not.have.been.called;
|
|
});
|
|
|
|
// TODO disabled because APN relies on a Promise
|
|
xit('uses APN for iOS devices', () => {
|
|
user.pushDevices.push({
|
|
type: 'ios',
|
|
regId: '123',
|
|
});
|
|
|
|
let details = {
|
|
identifier,
|
|
title,
|
|
message,
|
|
category: 'fun',
|
|
payload: {
|
|
a: true,
|
|
b: true,
|
|
},
|
|
};
|
|
|
|
const expectedNotification = new apn.Notification({
|
|
alert: message,
|
|
sound: 'default',
|
|
category: 'fun',
|
|
payload: {
|
|
identifier,
|
|
a: true,
|
|
b: true,
|
|
},
|
|
});
|
|
|
|
sendPushNotification(user, details);
|
|
expect(apnSendSpy).to.have.been.calledOnce;
|
|
expect(apnSendSpy).to.have.been.calledWithMatch(expectedNotification, '123');
|
|
expect(fcmSendSpy).to.not.have.been.called;
|
|
});
|
|
});
|