notifications: fixes

This commit is contained in:
Matteo Pagliazzi
2018-02-04 13:28:05 +01:00
parent 2af99d7c65
commit 4efbbd7bac
15 changed files with 118 additions and 37 deletions
+11 -4
View File
@@ -90,8 +90,15 @@ describe('response middleware', () => {
});
it('returns notifications if a user is authenticated', () => {
res.locals.user.notifications.push({type: 'NEW_CONTRIBUTOR_LEVEL'});
let notification = res.locals.user.notifications[0].toJSON();
const user = res.locals.user;
user.notifications = [
null, // invalid, not an object
{seen: true}, // invalid, no type or id
{id: 123}, // invalid, no type
// {type: 'ABC'}, // invalid, no id, not included here because the id would be added automatically
{type: 'ABC', id: '123'}, // valid
];
responseMiddleware(req, res, next);
res.respond(200, {field: 1});
@@ -103,8 +110,8 @@ describe('response middleware', () => {
data: {field: 1},
notifications: [
{
type: notification.type,
id: notification.id,
type: 'ABC',
id: '123',
data: {},
seen: false,
},
+22 -3
View File
@@ -64,6 +64,25 @@ describe('User Model', () => {
expect(userToJSON.notifications[0].seen).to.eql(false);
});
it('removes invalid notifications when calling toJSON', () => {
let user = new User();
user.notifications = [
null, // invalid, not an object
{seen: true}, // invalid, no type or id
{id: 123}, // invalid, no type
// {type: 'ABC'}, // invalid, no id, not included here because the id would be added automatically
{type: 'ABC', id: '123'}, // valid
];
const userToJSON = user.toJSON();
expect(userToJSON.notifications.length).to.equal(1);
expect(userToJSON.notifications[0]).to.have.all.keys(['data', 'id', 'type', 'seen']);
expect(userToJSON.notifications[0].type).to.equal('ABC');
expect(userToJSON.notifications[0].id).to.equal('123');
});
it('can add notifications with data and already marked as seen', () => {
let user = new User();
@@ -78,7 +97,7 @@ describe('User Model', () => {
});
context('static push method', () => {
it('adds notifications for a single member via static method', async() => {
it('adds notifications for a single member via static method', async () => {
let user = new User();
await user.save();
@@ -93,7 +112,7 @@ describe('User Model', () => {
expect(userToJSON.notifications[0].data).to.eql({});
});
it('validates notifications via static method', async() => {
it('validates notifications via static method', async () => {
let user = new User();
await user.save();
@@ -127,7 +146,7 @@ describe('User Model', () => {
expect(userToJSON.notifications[0].seen).to.eql(false);
});
it('adds notifications with data and seen status for all given users via static method', async() => {
it('adds notifications with data and seen status for all given users via static method', async () => {
let user = new User();
let otherUser = new User();
await Bluebird.all([user.save(), otherUser.save()]);
@@ -0,0 +1,21 @@
import { model as UserNotification } from '../../../../../website/server/models/userNotification';
describe('UserNotification Model', () => {
context('convertNotificationsToSafeJson', () => {
it('converts an array of notifications to a safe version', () => {
const notifications = [
null, // invalid, not an object
{seen: true}, // invalid, no type or id
{id: 123}, // invalid, no type
{type: 'ABC'}, // invalid, no id
new UserNotification({type: 'ABC', id: 123}), // valid
];
const notificationsToJSON = UserNotification.convertNotificationsToSafeJson(notifications);
expect(notificationsToJSON.length).to.equal(1);
expect(notificationsToJSON[0]).to.have.all.keys(['data', 'id', 'type', 'seen']);
expect(notificationsToJSON[0].type).to.equal('ABC');
expect(notificationsToJSON[0].id).to.equal('123');
});
});
});