diff --git a/test/api/unit/models/pushDevice.test.js b/test/api/unit/models/pushDevice.test.js index 17d34a07b9..6077a75113 100644 --- a/test/api/unit/models/pushDevice.test.js +++ b/test/api/unit/models/pushDevice.test.js @@ -15,5 +15,19 @@ describe('PushDevice Model', () => { expect(safePushDevices[0].type).to.equal('android'); expect(safePushDevices[0].regId).to.equal('1234'); }); + + it('removes duplicates', () => { + const pushDevices = [ + new PushDevice({ type: 'android', regId: '1234' }), + new PushDevice({ type: 'android', regId: '1234' }), + new PushDevice({ type: 'iphone', regId: '1234' }), // not duplicate + new PushDevice({ type: 'android', regId: '12345' }), // not duplicate + ]; + + const safePushDevices = PushDevice.cleanupCorruptData(pushDevices); + expect(safePushDevices.length).to.equal(3); + expect(safePushDevices[0].type).to.equal('android'); + expect(safePushDevices[0].regId).to.equal('1234'); + }); }); }); diff --git a/test/api/unit/models/user.test.js b/test/api/unit/models/user.test.js index 429cfa1082..9425fee275 100644 --- a/test/api/unit/models/user.test.js +++ b/test/api/unit/models/user.test.js @@ -206,6 +206,28 @@ describe('User Model', () => { expect(userToJSON.pushDevices[0].regId).to.equal('1234'); }); + it('removes duplicate push devices when loading the user', async () => { + let user = new User(); + await user.save(); + await user.update({ + $set: { + pushDevices: [ + { type: 'android', regId: '1234' }, + { type: 'android', regId: '1234' }, + ], + }, + }).exec(); + + user = await User.findById(user._id).exec(); + + const userToJSON = user.toJSON(); + expect(userToJSON.pushDevices.length).to.equal(1); + + expect(userToJSON.pushDevices[0]).to.have.all.keys(['regId', 'type', 'createdAt', 'updatedAt']); + expect(userToJSON.pushDevices[0].type).to.equal('android'); + expect(userToJSON.pushDevices[0].regId).to.equal('1234'); + }); + it('removes invalid notifications when loading the user', async () => { let user = new User(); await user.save(); @@ -231,6 +253,48 @@ describe('User Model', () => { expect(userToJSON.notifications[0].type).to.equal('ABC'); expect(userToJSON.notifications[0].id).to.equal('123'); }); + + it('removes multiple NEW_CHAT_MESSAGE for the same group', async () => { + let user = new User(); + await user.save(); + await user.update({ + $set: { + notifications: [ + { + type: 'NEW_CHAT_MESSAGE', + id: 123, + data: { group: { id: 12345 } }, + }, + { + type: 'NEW_CHAT_MESSAGE', + id: 1234, + data: { group: { id: 12345 } }, + }, + { + type: 'NEW_CHAT_MESSAGE', + id: 123, + data: { group: { id: 123456 } }, + }, // not duplicate, different group + { + type: 'NEW_CHAT_MESSAGE_DIFF', + id: 123, + data: { group: { id: 12345 } }, + }, // not duplicate, different type + ], + }, + }).exec(); + + user = await User.findById(user._id).exec(); + + const userToJSON = user.toJSON(); + expect(userToJSON.notifications.length).to.equal(3); + + expect(userToJSON.notifications[0]).to.have.all.keys(['data', 'id', 'type', 'seen']); + expect(userToJSON.notifications[0].type).to.equal('NEW_CHAT_MESSAGE'); + expect(userToJSON.notifications[0].id).to.equal('123'); + expect(userToJSON.notifications[0].data).to.deep.equal({ group: { id: 12345 } }); + expect(userToJSON.notifications[0].seen).to.equal(false); + }); }); context('notifications', () => { diff --git a/test/api/unit/models/userNotification.test.js b/test/api/unit/models/userNotification.test.js index e9fb8f8c10..772e8dba8f 100644 --- a/test/api/unit/models/userNotification.test.js +++ b/test/api/unit/models/userNotification.test.js @@ -18,5 +18,37 @@ describe('UserNotification Model', () => { expect(safeNotifications[0].type).to.equal('ABC'); expect(safeNotifications[0].id).to.equal('123'); }); + + it('removes multiple NEW_CHAT_MESSAGE for the same group', () => { + const notifications = [ + new UserNotification({ + type: 'NEW_CHAT_MESSAGE', + id: 123, + data: { group: { id: 12345 } }, + }), + new UserNotification({ + type: 'NEW_CHAT_MESSAGE', + id: 1234, + data: { group: { id: 12345 } }, + }), + new UserNotification({ + type: 'NEW_CHAT_MESSAGE', + id: 123, + data: { group: { id: 123456 } }, + }), // not duplicate, different group + new UserNotification({ + type: 'NEW_CHAT_MESSAGE_DIFF', + id: 123, + data: { group: { id: 12345 } }, + }), // not duplicate, different type + ]; + + const safeNotifications = UserNotification.cleanupCorruptData(notifications); + expect(safeNotifications.length).to.equal(3); + expect(safeNotifications[0].data).to.deep.equal({ group: { id: 12345 } }); + expect(safeNotifications[0].seen).to.equal(false); + expect(safeNotifications[0].type).to.equal('NEW_CHAT_MESSAGE'); + expect(safeNotifications[0].id).to.equal('123'); + }); }); });