|
| 1 | +import type { IPushNotificationConfig } from '@rocket.chat/core-typings/src/IPushNotificationConfig'; |
| 2 | +import { pick, truncateString } from '@rocket.chat/tools'; |
| 3 | +import { expect } from 'chai'; |
| 4 | +import proxyquire from 'proxyquire'; |
| 5 | +import sinon from 'sinon'; |
| 6 | + |
| 7 | +const loggerStub = { debug: sinon.stub(), warn: sinon.stub(), error: sinon.stub(), info: sinon.stub(), log: sinon.stub() }; |
| 8 | +const settingsStub = { get: sinon.stub().returns('') }; |
| 9 | + |
| 10 | +const { Push } = proxyquire.noCallThru().load('../../../../app/push/server/push', { |
| 11 | + './logger': { logger: loggerStub }, |
| 12 | + '../../settings/server': { settings: settingsStub }, |
| 13 | + '@rocket.chat/tools': { pick, truncateString }, |
| 14 | + 'meteor/check': { |
| 15 | + check: sinon.stub(), |
| 16 | + Match: { |
| 17 | + Optional: () => sinon.stub(), |
| 18 | + Integer: Number, |
| 19 | + OneOf: () => sinon.stub(), |
| 20 | + test: sinon.stub().returns(true), |
| 21 | + }, |
| 22 | + }, |
| 23 | + 'meteor/meteor': { |
| 24 | + Meteor: { |
| 25 | + absoluteUrl: sinon.stub().returns('http://localhost'), |
| 26 | + }, |
| 27 | + }, |
| 28 | +}); |
| 29 | + |
| 30 | +describe('Push Notifications [PushClass]', () => { |
| 31 | + afterEach(() => { |
| 32 | + sinon.restore(); |
| 33 | + }); |
| 34 | + |
| 35 | + describe('send()', () => { |
| 36 | + let sendNotificationStub: sinon.SinonStub; |
| 37 | + beforeEach(() => { |
| 38 | + sendNotificationStub = sinon.stub(Push, 'sendNotification').resolves({ apn: [], gcm: [] }); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should call sendNotification with required fields', async () => { |
| 42 | + const options: IPushNotificationConfig = { |
| 43 | + from: 'test', |
| 44 | + title: 'title', |
| 45 | + text: 'body', |
| 46 | + userId: 'user1', |
| 47 | + apn: { category: 'MESSAGE' }, |
| 48 | + gcm: { style: 'inbox', image: 'url' }, |
| 49 | + }; |
| 50 | + |
| 51 | + await Push.send(options); |
| 52 | + |
| 53 | + expect(sendNotificationStub.calledOnce).to.be.true; |
| 54 | + |
| 55 | + const notification = sendNotificationStub.firstCall.args[0]; |
| 56 | + expect(notification.from).to.equal('test'); |
| 57 | + expect(notification.title).to.equal('title'); |
| 58 | + expect(notification.text).to.equal('body'); |
| 59 | + expect(notification.userId).to.equal('user1'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should truncate text if longer than 240 chars', async () => { |
| 63 | + const longText = 'a'.repeat(300); |
| 64 | + const options: IPushNotificationConfig = { |
| 65 | + from: 'test', |
| 66 | + title: 'title', |
| 67 | + text: longText, |
| 68 | + userId: 'user1', |
| 69 | + apn: { category: 'MESSAGE' }, |
| 70 | + gcm: { style: 'inbox', image: 'url' }, |
| 71 | + }; |
| 72 | + |
| 73 | + await Push.send(options); |
| 74 | + |
| 75 | + const notification = sendNotificationStub.firstCall.args[0]; |
| 76 | + |
| 77 | + expect(notification.text.length).to.equal(240); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should truncate title if longer than 65 chars', async () => { |
| 81 | + const longTitle = 'a'.repeat(100); |
| 82 | + const options: IPushNotificationConfig = { |
| 83 | + from: 'test', |
| 84 | + title: longTitle, |
| 85 | + text: 'bpdu', |
| 86 | + userId: 'user1', |
| 87 | + apn: { category: 'MESSAGE' }, |
| 88 | + gcm: { style: 'inbox', image: 'url' }, |
| 89 | + }; |
| 90 | + |
| 91 | + await Push.send(options); |
| 92 | + |
| 93 | + const notification = sendNotificationStub.firstCall.args[0]; |
| 94 | + |
| 95 | + expect(notification.title.length).to.equal(65); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should throw if userId is missing', async () => { |
| 99 | + const options = { |
| 100 | + from: 'test', |
| 101 | + title: 'title', |
| 102 | + text: 'body', |
| 103 | + apn: { category: 'MESSAGE' }, |
| 104 | + gcm: { style: 'inbox', image: 'url' }, |
| 105 | + }; |
| 106 | + |
| 107 | + await expect(Push.send(options)).to.be.rejectedWith('No userId found'); |
| 108 | + |
| 109 | + expect(sendNotificationStub.called).to.be.false; |
| 110 | + }); |
| 111 | + }); |
| 112 | +}); |
0 commit comments