mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-16 06:07:21 +01:00
notifications tests
This commit is contained in:
@@ -195,17 +195,23 @@ describe('User Model', () => {
|
|||||||
expect(userToJSON.notifications[0].seen).to.eql(false);
|
expect(userToJSON.notifications[0].seen).to.eql(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('removes invalid notifications when calling toJSON', () => {
|
it('removes invalid notifications when loading the user', async () => {
|
||||||
const user = new User();
|
let user = new User();
|
||||||
|
await user.save();
|
||||||
|
await user.update({
|
||||||
|
$set: {
|
||||||
|
notifications: [
|
||||||
|
null, // invalid, not an object
|
||||||
|
{ seen: true }, // invalid, no type or id
|
||||||
|
{ id: 123 }, // invalid, no type
|
||||||
|
// invalid, no id, not included here because the id would be added automatically
|
||||||
|
// {type: 'ABC'},
|
||||||
|
{ type: 'ABC', id: '123' }, // valid
|
||||||
|
],
|
||||||
|
},
|
||||||
|
}).exec();
|
||||||
|
|
||||||
user.notifications = [
|
user = await User.findById(user._id).exec();
|
||||||
null, // invalid, not an object
|
|
||||||
{ seen: true }, // invalid, no type or id
|
|
||||||
{ id: 123 }, // invalid, no type
|
|
||||||
// invalid, no id, not included here because the id would be added automatically
|
|
||||||
// {type: 'ABC'},
|
|
||||||
{ type: 'ABC', id: '123' }, // valid
|
|
||||||
];
|
|
||||||
|
|
||||||
const userToJSON = user.toJSON();
|
const userToJSON = user.toJSON();
|
||||||
expect(userToJSON.notifications.length).to.equal(1);
|
expect(userToJSON.notifications.length).to.equal(1);
|
||||||
@@ -215,26 +221,6 @@ describe('User Model', () => {
|
|||||||
expect(userToJSON.notifications[0].id).to.equal('123');
|
expect(userToJSON.notifications[0].id).to.equal('123');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('removes invalid notifications when saving', async () => {
|
|
||||||
const user = new User();
|
|
||||||
|
|
||||||
user.notifications = [
|
|
||||||
null, // invalid, not an object
|
|
||||||
{ seen: true }, // invalid, no type or id
|
|
||||||
{ id: 123 }, // invalid, no type
|
|
||||||
// invalid, no id, not included here because the id would be added automatically
|
|
||||||
// {type: 'ABC'},
|
|
||||||
{ type: 'ABC', id: '123' }, // valid
|
|
||||||
];
|
|
||||||
|
|
||||||
await user.save();
|
|
||||||
expect(user.notifications.length).to.equal(1);
|
|
||||||
|
|
||||||
expect(user.notifications[0]).to.have.all.keys(['data', 'id', 'type', 'seen']);
|
|
||||||
expect(user.notifications[0].type).to.equal('ABC');
|
|
||||||
expect(user.notifications[0].id).to.equal('123');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('can add notifications with data and already marked as seen', () => {
|
it('can add notifications with data and already marked as seen', () => {
|
||||||
const user = new User();
|
const user = new User();
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { model as UserNotification } from '../../../../website/server/models/userNotification';
|
import { model as UserNotification } from '../../../../website/server/models/userNotification';
|
||||||
|
|
||||||
describe('UserNotification Model', () => {
|
describe('UserNotification Model', () => {
|
||||||
context('convertNotificationsToSafeJson', () => {
|
context('cleanupCorruptData', () => {
|
||||||
it('converts an array of notifications to a safe version', () => {
|
it('converts an array of notifications to a safe version', () => {
|
||||||
const notifications = [
|
const notifications = [
|
||||||
null, // invalid, not an object
|
null, // invalid, not an object
|
||||||
@@ -11,11 +11,12 @@ describe('UserNotification Model', () => {
|
|||||||
new UserNotification({ type: 'ABC', id: 123 }), // valid
|
new UserNotification({ type: 'ABC', id: 123 }), // valid
|
||||||
];
|
];
|
||||||
|
|
||||||
const notificationsToJSON = UserNotification.convertNotificationsToSafeJson(notifications);
|
const safeNotifications = UserNotification.cleanupCorruptData(notifications);
|
||||||
expect(notificationsToJSON.length).to.equal(1);
|
expect(safeNotifications.length).to.equal(1);
|
||||||
expect(notificationsToJSON[0]).to.have.all.keys(['data', 'id', 'type', 'seen']);
|
expect(safeNotifications[0].data).to.deep.equal({});
|
||||||
expect(notificationsToJSON[0].type).to.equal('ABC');
|
expect(safeNotifications[0].seen).to.equal(false);
|
||||||
expect(notificationsToJSON[0].id).to.equal('123');
|
expect(safeNotifications[0].type).to.equal('ABC');
|
||||||
|
expect(safeNotifications[0].id).to.equal('123');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -93,6 +93,7 @@ export const schema = new Schema({
|
|||||||
* Called by user's post init hook (models/user/hooks.js)
|
* Called by user's post init hook (models/user/hooks.js)
|
||||||
*/
|
*/
|
||||||
schema.statics.cleanupCorruptData = function cleanupCorruptNotificationsData (notifications) {
|
schema.statics.cleanupCorruptData = function cleanupCorruptNotificationsData (notifications) {
|
||||||
|
console.log('fixing stuff', notifications);
|
||||||
if (!notifications) return notifications;
|
if (!notifications) return notifications;
|
||||||
|
|
||||||
let filteredNotifications = notifications.filter(notification => {
|
let filteredNotifications = notifications.filter(notification => {
|
||||||
|
|||||||
Reference in New Issue
Block a user