diff --git a/test/api/v3/unit/libs/payments.test.js b/test/api/v3/unit/libs/payments.test.js index 0f95c1c8ce..cc73c6bf8f 100644 --- a/test/api/v3/unit/libs/payments.test.js +++ b/test/api/v3/unit/libs/payments.test.js @@ -1,6 +1,7 @@ import * as sender from '../../../../../website/server/libs/email'; import * as api from '../../../../../website/server/libs/payments'; import analytics from '../../../../../website/server/libs/analyticsService'; +import notifications from '../../../../../website/server/libs/pushNotifications'; import { model as User } from '../../../../../website/server/models/user'; import moment from 'moment'; @@ -15,6 +16,7 @@ describe('payments/index', () => { sandbox.stub(user, 'sendMessage'); sandbox.stub(analytics, 'trackPurchase'); sandbox.stub(analytics, 'track'); + sandbox.stub(notifications, 'sendNotification'); data = { user, @@ -149,15 +151,15 @@ describe('payments/index', () => { it('sends an email about the gift', async () => { await api.createSubscription(data); - expect(sender.sendTxn).to.be.calledOnce; expect(sender.sendTxn).to.be.calledWith(recipient, 'gifted-subscription', [ {name: 'GIFTER', content: 'sender'}, {name: 'X_MONTHS_SUBSCRIPTION', content: 3}, ]); }); - xit('sends a push notification about the gift', async () => { - // TODO: the push notification library needs to be refactored to export an object with properties to better stub it + it('sends a push notification about the gift', async () => { + await api.createSubscription(data); + expect(notifications.sendNotification).to.be.calledOnce; }); it('tracks subscription purchase as gift', async () => { @@ -518,8 +520,9 @@ describe('payments/index', () => { expect(user.sendMessage).to.be.calledWith(recipient, '\`Hello recipient, sender has sent you 4 gems!\`'); }); - xit('sends a push notification if user did not gift to self', async () => { - // TODO: the push notification library needs to be refactored to export an object with properties to better stub it + it('sends a push notification if user did not gift to self', async () => { + await api.buyGems(data); + expect(notifications.sendNotification).to.be.calledOnce; }); }); }); diff --git a/test/api/v3/unit/libs/pushNotifications.js b/test/api/v3/unit/libs/pushNotifications.js index 0d6fead7ae..ee877d517f 100644 --- a/test/api/v3/unit/libs/pushNotifications.js +++ b/test/api/v3/unit/libs/pushNotifications.js @@ -29,7 +29,7 @@ describe('pushNotifications', () => { send: apnSendSpy, }); - sendPushNotification = requireAgain(pathToPushNotifications); + sendPushNotification = requireAgain(pathToPushNotifications).sendNotification; }); afterEach(() => { diff --git a/website/server/controllers/api-v3/groups.js b/website/server/controllers/api-v3/groups.js index 15cf34cba8..09f23cbd6f 100644 --- a/website/server/controllers/api-v3/groups.js +++ b/website/server/controllers/api-v3/groups.js @@ -19,7 +19,7 @@ import { import { removeFromArray } from '../../libs/collectionManipulators'; import { sendTxn as sendTxnEmail } from '../../libs/email'; import { encrypt } from '../../libs/encryption'; -import sendPushNotification from '../../libs/pushNotifications'; +import { sendNotification as sendPushNotification } from '../../libs/pushNotifications'; import pusher from '../../libs/pusher'; let api = {}; diff --git a/website/server/controllers/api-v3/members.js b/website/server/controllers/api-v3/members.js index bb61f2073e..bb6e0fcb21 100644 --- a/website/server/controllers/api-v3/members.js +++ b/website/server/controllers/api-v3/members.js @@ -16,7 +16,7 @@ import { sendTxn as sendTxnEmail, } from '../../libs/email'; import Bluebird from 'bluebird'; -import sendPushNotification from '../../libs/pushNotifications'; +import { sendNotification as sendPushNotification } from '../../libs/pushNotifications'; let api = {}; diff --git a/website/server/controllers/api-v3/quests.js b/website/server/controllers/api-v3/quests.js index 180646e619..433d13dfeb 100644 --- a/website/server/controllers/api-v3/quests.js +++ b/website/server/controllers/api-v3/quests.js @@ -16,7 +16,7 @@ import { sendTxn as sendTxnEmail, } from '../../libs/email'; import common from '../../../../common'; -import sendPushNotification from '../../libs/pushNotifications'; +import { sendNotification as sendPushNotification } from '../../libs/pushNotifications'; const questScrolls = common.content.quests; diff --git a/website/server/libs/payments.js b/website/server/libs/payments.js index 939f45f9f8..eea4637e19 100644 --- a/website/server/libs/payments.js +++ b/website/server/libs/payments.js @@ -5,7 +5,7 @@ import { sendTxn as txnEmail, } from './email'; import moment from 'moment'; -import sendPushNotification from './pushNotifications'; +import { sendNotification as sendPushNotification } from './pushNotifications'; import shared from '../../../common' ; let api = {}; diff --git a/website/server/libs/pushNotifications.js b/website/server/libs/pushNotifications.js index c093d1a432..ddd7f12667 100644 --- a/website/server/libs/pushNotifications.js +++ b/website/server/libs/pushNotifications.js @@ -59,8 +59,7 @@ if (APN_ENABLED) { }); }); } - -module.exports = function sendNotification (user, details = {}) { +function sendNotification (user, details = {}) { if (!user) throw new Error('User is required.'); if (user.preferences.pushNotifications.unsubscribeFromAll === true) return; let pushDevices = user.pushDevices.toObject ? user.pushDevices.toObject() : user.pushDevices; @@ -103,4 +102,8 @@ module.exports = function sendNotification (user, details = {}) { break; } }); +} + +module.exports = { + sendNotification, }; diff --git a/website/server/models/challenge.js b/website/server/models/challenge.js index 9c444389e9..72a3691348 100644 --- a/website/server/models/challenge.js +++ b/website/server/models/challenge.js @@ -12,7 +12,7 @@ import { import { removeFromArray } from '../libs/collectionManipulators'; import shared from '../../../common'; import { sendTxn as txnEmail } from '../libs/email'; -import sendPushNotification from '../libs/pushNotifications'; +import { sendNotification as sendPushNotification } from '../libs/pushNotifications'; import cwait from 'cwait'; import { syncableAttrs } from '../libs/taskManager'; diff --git a/website/server/models/group.js b/website/server/models/group.js index d5aa3d4536..66dbaacb67 100644 --- a/website/server/models/group.js +++ b/website/server/models/group.js @@ -17,7 +17,7 @@ import baseModel from '../libs/baseModel'; import { sendTxn as sendTxnEmail } from '../libs/email'; import Bluebird from 'bluebird'; import nconf from 'nconf'; -import sendPushNotification from '../libs/pushNotifications'; +import { sendNotification as sendPushNotification } from '../libs/pushNotifications'; import pusher from '../libs/pusher'; import { syncableAttrs,