fix invalid push devices

This commit is contained in:
Matteo Pagliazzi
2020-03-01 21:49:52 +01:00
parent 6deee0ffc8
commit 3f5ee32684
5 changed files with 89 additions and 10 deletions
+19
View File
@@ -0,0 +1,19 @@
import { model as PushDevice } from '../../../../website/server/models/pushDevice';
describe('PushDevice Model', () => {
context('cleanupCorruptData', () => {
it('converts an array of push devices to a safe version', () => {
const pushDevices = [
null, // invalid, not an object
{ regId: '123' }, // invalid, no type
{ type: 'android' }, // invalid, no regId
new PushDevice({ type: 'android', regId: '1234' }), // valid
];
const safePushDevices = PushDevice.cleanupCorruptData(pushDevices);
expect(safePushDevices.length).to.equal(1);
expect(safePushDevices[0].type).to.equal('android');
expect(safePushDevices[0].regId).to.equal('1234');
});
});
});
+35 -9
View File
@@ -181,18 +181,29 @@ describe('User Model', () => {
});
});
context('notifications', () => {
it('can add notifications without data', () => {
const user = new User();
context('post init', () => {
it('removes invalid push devices when loading the user', async () => {
let user = new User();
await user.save();
await user.update({
$set: {
pushDevices: [
null, // invalid, not an object
{ regId: '123' }, // invalid, no type
{ type: 'android' }, // invalid, no regId
{ type: 'android', regId: '1234' }, // valid
],
},
}).exec();
user.addNotification('CRON');
user = await User.findById(user._id).exec();
const userToJSON = user.toJSON();
expect(user.notifications.length).to.equal(1);
expect(userToJSON.notifications[0]).to.have.all.keys(['data', 'id', 'type', 'seen']);
expect(userToJSON.notifications[0].type).to.equal('CRON');
expect(userToJSON.notifications[0].data).to.eql({});
expect(userToJSON.notifications[0].seen).to.eql(false);
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 () => {
@@ -220,6 +231,21 @@ describe('User Model', () => {
expect(userToJSON.notifications[0].type).to.equal('ABC');
expect(userToJSON.notifications[0].id).to.equal('123');
});
});
context('notifications', () => {
it('can add notifications without data', () => {
const user = new User();
user.addNotification('CRON');
const userToJSON = user.toJSON();
expect(user.notifications.length).to.equal(1);
expect(userToJSON.notifications[0]).to.have.all.keys(['data', 'id', 'type', 'seen']);
expect(userToJSON.notifications[0].type).to.equal('CRON');
expect(userToJSON.notifications[0].data).to.eql({});
expect(userToJSON.notifications[0].seen).to.eql(false);
});
it('can add notifications with data and already marked as seen', () => {
const user = new User();