2020-01-13 19:01:13 +00:00
|
|
|
/* eslint-disable
|
|
|
|
no-dupe-keys,
|
|
|
|
no-return-assign,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-01-13 19:01:07 +00:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-01-13 19:01:20 +00:00
|
|
|
const sinon = require('sinon')
|
2021-03-17 20:34:06 +00:00
|
|
|
const { expect } = require('chai')
|
2020-01-13 19:01:20 +00:00
|
|
|
const modulePath = '../../../app/js/Notifications.js'
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const assert = require('assert')
|
2024-07-15 11:10:48 +00:00
|
|
|
const { ObjectId } = require('mongodb-legacy')
|
2020-01-13 19:01:07 +00:00
|
|
|
|
2023-03-16 12:38:09 +00:00
|
|
|
const userId = '51dc93e6fb625a261300003b'
|
2023-10-23 08:45:57 +00:00
|
|
|
const notificationId = '574ee8d6f40c3a244e704249'
|
2023-03-16 12:38:09 +00:00
|
|
|
const notificationKey = 'notification-key'
|
2020-01-13 19:01:07 +00:00
|
|
|
|
2020-06-04 07:50:05 +00:00
|
|
|
describe('Notifications Tests', function () {
|
|
|
|
beforeEach(function () {
|
2020-08-31 08:58:47 +00:00
|
|
|
this.findToArrayStub = sinon.stub()
|
|
|
|
this.findStub = sinon.stub().returns({ toArray: this.findToArrayStub })
|
2020-01-13 19:01:20 +00:00
|
|
|
this.countStub = sinon.stub()
|
2020-08-31 08:58:47 +00:00
|
|
|
this.updateOneStub = sinon.stub()
|
|
|
|
this.deleteOneStub = sinon.stub()
|
|
|
|
this.db = {
|
|
|
|
notifications: {
|
|
|
|
find: this.findStub,
|
|
|
|
count: this.countStub,
|
|
|
|
updateOne: this.updateOneStub,
|
2021-07-13 11:04:44 +00:00
|
|
|
deleteOne: this.deleteOneStub,
|
|
|
|
},
|
2020-01-13 19:01:20 +00:00
|
|
|
}
|
2020-01-13 19:01:07 +00:00
|
|
|
|
2020-01-13 19:01:20 +00:00
|
|
|
this.notifications = SandboxedModule.require(modulePath, {
|
|
|
|
requires: {
|
2021-07-12 16:47:16 +00:00
|
|
|
'@overleaf/settings': {},
|
2020-08-31 08:58:47 +00:00
|
|
|
'./mongodb': { db: this.db, ObjectId },
|
2021-07-13 11:04:44 +00:00
|
|
|
},
|
2020-01-13 19:01:20 +00:00
|
|
|
})
|
2020-01-13 19:01:07 +00:00
|
|
|
|
2020-01-13 19:01:20 +00:00
|
|
|
this.stubbedNotification = {
|
2023-10-23 08:45:57 +00:00
|
|
|
user_id: new ObjectId(userId),
|
2020-01-13 19:01:20 +00:00
|
|
|
key: 'notification-key',
|
|
|
|
messageOpts: 'some info',
|
2021-07-13 11:04:44 +00:00
|
|
|
templateKey: 'template-key',
|
2020-01-13 19:01:20 +00:00
|
|
|
}
|
|
|
|
return (this.stubbedNotificationArray = [this.stubbedNotification])
|
|
|
|
})
|
2020-01-13 19:01:07 +00:00
|
|
|
|
2020-06-04 07:50:05 +00:00
|
|
|
describe('getUserNotifications', function () {
|
|
|
|
return it('should find all notifications and return i', function (done) {
|
2020-08-31 08:58:47 +00:00
|
|
|
this.findToArrayStub.callsArgWith(0, null, this.stubbedNotificationArray)
|
2020-01-13 19:01:20 +00:00
|
|
|
return this.notifications.getUserNotifications(
|
2023-03-16 12:38:09 +00:00
|
|
|
userId,
|
2020-01-13 19:01:20 +00:00
|
|
|
(err, notifications) => {
|
2021-10-27 09:49:18 +00:00
|
|
|
if (err) return done(err)
|
2020-01-13 19:01:20 +00:00
|
|
|
notifications.should.equal(this.stubbedNotificationArray)
|
|
|
|
assert.deepEqual(this.findStub.args[0][0], {
|
2023-10-23 08:45:57 +00:00
|
|
|
user_id: new ObjectId(userId),
|
2021-07-13 11:04:44 +00:00
|
|
|
templateKey: { $exists: true },
|
2020-01-13 19:01:20 +00:00
|
|
|
})
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
2020-02-11 09:08:35 +00:00
|
|
|
})
|
|
|
|
})
|
2020-01-13 19:01:07 +00:00
|
|
|
|
2020-06-04 07:50:05 +00:00
|
|
|
describe('addNotification', function () {
|
|
|
|
beforeEach(function () {
|
2020-01-13 19:01:20 +00:00
|
|
|
this.stubbedNotification = {
|
2023-10-23 08:45:57 +00:00
|
|
|
user_id: new ObjectId(userId),
|
2020-01-13 19:01:20 +00:00
|
|
|
key: 'notification-key',
|
|
|
|
messageOpts: 'some info',
|
2021-07-13 11:04:44 +00:00
|
|
|
templateKey: 'template-key',
|
2020-01-13 19:01:20 +00:00
|
|
|
}
|
|
|
|
this.expectedDocument = {
|
|
|
|
user_id: this.stubbedNotification.user_id,
|
|
|
|
key: 'notification-key',
|
|
|
|
messageOpts: 'some info',
|
2021-07-13 11:04:44 +00:00
|
|
|
templateKey: 'template-key',
|
2020-01-13 19:01:20 +00:00
|
|
|
}
|
|
|
|
this.expectedQuery = {
|
|
|
|
user_id: this.stubbedNotification.user_id,
|
2021-07-13 11:04:44 +00:00
|
|
|
key: 'notification-key',
|
2020-01-13 19:01:20 +00:00
|
|
|
}
|
2020-08-31 08:58:47 +00:00
|
|
|
this.updateOneStub.yields()
|
2020-01-13 19:01:20 +00:00
|
|
|
return this.countStub.yields(null, 0)
|
|
|
|
})
|
2020-01-13 19:01:07 +00:00
|
|
|
|
2020-06-04 07:50:05 +00:00
|
|
|
it('should insert the notification into the collection', function (done) {
|
2020-01-13 19:01:20 +00:00
|
|
|
return this.notifications.addNotification(
|
2023-03-16 12:38:09 +00:00
|
|
|
userId,
|
2020-01-13 19:01:20 +00:00
|
|
|
this.stubbedNotification,
|
2021-07-13 11:04:44 +00:00
|
|
|
err => {
|
2021-03-17 20:34:06 +00:00
|
|
|
expect(err).not.to.exist
|
2020-01-13 19:01:20 +00:00
|
|
|
sinon.assert.calledWith(
|
2020-08-31 08:58:47 +00:00
|
|
|
this.updateOneStub,
|
2020-01-13 19:01:20 +00:00
|
|
|
this.expectedQuery,
|
2020-04-01 15:03:52 +00:00
|
|
|
{ $set: this.expectedDocument },
|
2020-01-13 19:01:20 +00:00
|
|
|
{ upsert: true }
|
|
|
|
)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2020-01-13 19:01:07 +00:00
|
|
|
|
2020-06-04 07:50:05 +00:00
|
|
|
describe('when there is an existing notification', function (done) {
|
|
|
|
beforeEach(function () {
|
2020-01-13 19:01:20 +00:00
|
|
|
return this.countStub.yields(null, 1)
|
|
|
|
})
|
2020-01-13 19:01:07 +00:00
|
|
|
|
2020-06-04 07:50:05 +00:00
|
|
|
it('should fail to insert', function (done) {
|
2020-01-13 19:01:20 +00:00
|
|
|
return this.notifications.addNotification(
|
2023-03-16 12:38:09 +00:00
|
|
|
userId,
|
2020-01-13 19:01:20 +00:00
|
|
|
this.stubbedNotification,
|
2021-07-13 11:04:44 +00:00
|
|
|
err => {
|
2021-03-17 20:34:06 +00:00
|
|
|
expect(err).not.to.exist
|
2020-08-31 08:58:47 +00:00
|
|
|
sinon.assert.notCalled(this.updateOneStub)
|
2020-01-13 19:01:20 +00:00
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2020-01-13 19:01:07 +00:00
|
|
|
|
2020-06-04 07:50:05 +00:00
|
|
|
return it('should update the key if forceCreate is true', function (done) {
|
2020-01-13 19:01:20 +00:00
|
|
|
this.stubbedNotification.forceCreate = true
|
|
|
|
return this.notifications.addNotification(
|
2023-03-16 12:38:09 +00:00
|
|
|
userId,
|
2020-01-13 19:01:20 +00:00
|
|
|
this.stubbedNotification,
|
2021-07-13 11:04:44 +00:00
|
|
|
err => {
|
2021-03-17 20:34:06 +00:00
|
|
|
expect(err).not.to.exist
|
2020-01-13 19:01:20 +00:00
|
|
|
sinon.assert.calledWith(
|
2020-08-31 08:58:47 +00:00
|
|
|
this.updateOneStub,
|
2020-01-13 19:01:20 +00:00
|
|
|
this.expectedQuery,
|
2020-04-01 15:03:52 +00:00
|
|
|
{ $set: this.expectedDocument },
|
2020-01-13 19:01:20 +00:00
|
|
|
{ upsert: true }
|
|
|
|
)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
2020-01-13 19:01:07 +00:00
|
|
|
|
2020-06-04 07:50:05 +00:00
|
|
|
describe('when the notification is set to expire', function () {
|
|
|
|
beforeEach(function () {
|
2020-01-13 19:01:20 +00:00
|
|
|
this.stubbedNotification = {
|
2023-10-23 08:45:57 +00:00
|
|
|
user_id: new ObjectId(userId),
|
2020-01-13 19:01:20 +00:00
|
|
|
key: 'notification-key',
|
|
|
|
messageOpts: 'some info',
|
|
|
|
templateKey: 'template-key',
|
2021-07-13 11:04:44 +00:00
|
|
|
expires: '2922-02-13T09:32:56.289Z',
|
2020-01-13 19:01:20 +00:00
|
|
|
}
|
|
|
|
this.expectedDocument = {
|
|
|
|
user_id: this.stubbedNotification.user_id,
|
|
|
|
key: 'notification-key',
|
|
|
|
messageOpts: 'some info',
|
|
|
|
templateKey: 'template-key',
|
2021-07-13 11:04:44 +00:00
|
|
|
expires: new Date(this.stubbedNotification.expires),
|
2020-01-13 19:01:20 +00:00
|
|
|
}
|
|
|
|
return (this.expectedQuery = {
|
|
|
|
user_id: this.stubbedNotification.user_id,
|
2021-07-13 11:04:44 +00:00
|
|
|
key: 'notification-key',
|
2020-01-13 19:01:20 +00:00
|
|
|
})
|
|
|
|
})
|
2020-01-13 19:01:07 +00:00
|
|
|
|
2020-06-04 07:50:05 +00:00
|
|
|
return it('should add an `expires` Date field to the document', function (done) {
|
2020-01-13 19:01:20 +00:00
|
|
|
return this.notifications.addNotification(
|
2023-03-16 12:38:09 +00:00
|
|
|
userId,
|
2020-01-13 19:01:20 +00:00
|
|
|
this.stubbedNotification,
|
2021-07-13 11:04:44 +00:00
|
|
|
err => {
|
2021-03-17 20:34:06 +00:00
|
|
|
expect(err).not.to.exist
|
2020-01-13 19:01:20 +00:00
|
|
|
sinon.assert.calledWith(
|
2020-08-31 08:58:47 +00:00
|
|
|
this.updateOneStub,
|
2020-01-13 19:01:20 +00:00
|
|
|
this.expectedQuery,
|
2020-04-01 15:03:52 +00:00
|
|
|
{ $set: this.expectedDocument },
|
2020-01-13 19:01:20 +00:00
|
|
|
{ upsert: true }
|
|
|
|
)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
2020-01-13 19:01:07 +00:00
|
|
|
|
2020-06-04 07:50:05 +00:00
|
|
|
return describe('when the notification has a nonsensical expires field', function () {
|
|
|
|
beforeEach(function () {
|
2020-01-13 19:01:20 +00:00
|
|
|
this.stubbedNotification = {
|
2023-10-23 08:45:57 +00:00
|
|
|
user_id: new ObjectId(userId),
|
2020-01-13 19:01:20 +00:00
|
|
|
key: 'notification-key',
|
|
|
|
messageOpts: 'some info',
|
|
|
|
templateKey: 'template-key',
|
2021-07-13 11:04:44 +00:00
|
|
|
expires: 'WAT',
|
2020-01-13 19:01:20 +00:00
|
|
|
}
|
|
|
|
return (this.expectedDocument = {
|
|
|
|
user_id: this.stubbedNotification.user_id,
|
|
|
|
key: 'notification-key',
|
|
|
|
messageOpts: 'some info',
|
|
|
|
templateKey: 'template-key',
|
2021-07-13 11:04:44 +00:00
|
|
|
expires: new Date(this.stubbedNotification.expires),
|
2020-01-13 19:01:20 +00:00
|
|
|
})
|
|
|
|
})
|
2020-01-13 19:01:07 +00:00
|
|
|
|
2020-06-04 07:50:05 +00:00
|
|
|
return it('should produce an error', function (done) {
|
2020-01-13 19:01:20 +00:00
|
|
|
return this.notifications.addNotification(
|
2023-03-16 12:38:09 +00:00
|
|
|
userId,
|
2020-01-13 19:01:20 +00:00
|
|
|
this.stubbedNotification,
|
2021-07-13 11:04:44 +00:00
|
|
|
err => {
|
2020-01-13 19:01:20 +00:00
|
|
|
;(err instanceof Error).should.equal(true)
|
2020-08-31 08:58:47 +00:00
|
|
|
sinon.assert.notCalled(this.updateOneStub)
|
2020-01-13 19:01:20 +00:00
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2020-01-13 19:01:07 +00:00
|
|
|
|
2020-06-04 07:50:05 +00:00
|
|
|
describe('removeNotificationId', function () {
|
|
|
|
return it('should mark the notification id as read', function (done) {
|
2020-08-31 08:58:47 +00:00
|
|
|
this.updateOneStub.callsArgWith(2, null)
|
2020-01-13 19:01:07 +00:00
|
|
|
|
2020-01-13 19:01:20 +00:00
|
|
|
return this.notifications.removeNotificationId(
|
2023-03-16 12:38:09 +00:00
|
|
|
userId,
|
|
|
|
notificationId,
|
2021-07-13 11:04:44 +00:00
|
|
|
err => {
|
2021-10-27 09:49:18 +00:00
|
|
|
if (err) return done(err)
|
2020-01-13 19:01:20 +00:00
|
|
|
const searchOps = {
|
2023-10-23 08:45:57 +00:00
|
|
|
user_id: new ObjectId(userId),
|
|
|
|
_id: new ObjectId(notificationId),
|
2020-01-13 19:01:20 +00:00
|
|
|
}
|
|
|
|
const updateOperation = {
|
2021-07-13 11:04:44 +00:00
|
|
|
$unset: { templateKey: true, messageOpts: true },
|
2020-01-13 19:01:20 +00:00
|
|
|
}
|
2020-08-31 08:58:47 +00:00
|
|
|
assert.deepEqual(this.updateOneStub.args[0][0], searchOps)
|
|
|
|
assert.deepEqual(this.updateOneStub.args[0][1], updateOperation)
|
2020-01-13 19:01:20 +00:00
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
2020-02-11 09:08:35 +00:00
|
|
|
})
|
|
|
|
})
|
2020-01-13 19:01:07 +00:00
|
|
|
|
2020-06-04 07:50:05 +00:00
|
|
|
describe('removeNotificationKey', function () {
|
|
|
|
return it('should mark the notification key as read', function (done) {
|
2020-08-31 08:58:47 +00:00
|
|
|
this.updateOneStub.callsArgWith(2, null)
|
2020-01-13 19:01:07 +00:00
|
|
|
|
2020-01-13 19:01:20 +00:00
|
|
|
return this.notifications.removeNotificationKey(
|
2023-03-16 12:38:09 +00:00
|
|
|
userId,
|
|
|
|
notificationKey,
|
2021-07-13 11:04:44 +00:00
|
|
|
err => {
|
2021-10-27 09:49:18 +00:00
|
|
|
if (err) return done(err)
|
2020-01-13 19:01:20 +00:00
|
|
|
const searchOps = {
|
2023-10-23 08:45:57 +00:00
|
|
|
user_id: new ObjectId(userId),
|
2023-03-16 12:38:09 +00:00
|
|
|
key: notificationKey,
|
2020-01-13 19:01:20 +00:00
|
|
|
}
|
|
|
|
const updateOperation = {
|
2021-07-13 11:04:44 +00:00
|
|
|
$unset: { templateKey: true },
|
2020-01-13 19:01:20 +00:00
|
|
|
}
|
2020-08-31 08:58:47 +00:00
|
|
|
assert.deepEqual(this.updateOneStub.args[0][0], searchOps)
|
|
|
|
assert.deepEqual(this.updateOneStub.args[0][1], updateOperation)
|
2020-01-13 19:01:20 +00:00
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
2020-02-11 09:08:35 +00:00
|
|
|
})
|
|
|
|
})
|
2020-01-13 19:01:07 +00:00
|
|
|
|
2020-06-04 07:50:05 +00:00
|
|
|
describe('removeNotificationByKeyOnly', function () {
|
|
|
|
return it('should mark the notification key as read', function (done) {
|
2020-08-31 08:58:47 +00:00
|
|
|
this.updateOneStub.callsArgWith(2, null)
|
2020-01-13 19:01:07 +00:00
|
|
|
|
2020-01-13 19:01:20 +00:00
|
|
|
return this.notifications.removeNotificationByKeyOnly(
|
2023-03-16 12:38:09 +00:00
|
|
|
notificationKey,
|
2021-07-13 11:04:44 +00:00
|
|
|
err => {
|
2021-10-27 09:49:18 +00:00
|
|
|
if (err) return done(err)
|
2023-03-16 12:38:09 +00:00
|
|
|
const searchOps = { key: notificationKey }
|
2020-01-13 19:01:20 +00:00
|
|
|
const updateOperation = { $unset: { templateKey: true } }
|
2020-08-31 08:58:47 +00:00
|
|
|
assert.deepEqual(this.updateOneStub.args[0][0], searchOps)
|
|
|
|
assert.deepEqual(this.updateOneStub.args[0][1], updateOperation)
|
2020-01-13 19:01:20 +00:00
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
2020-02-11 09:08:35 +00:00
|
|
|
})
|
|
|
|
})
|
2020-01-13 19:01:07 +00:00
|
|
|
|
2020-06-04 07:50:05 +00:00
|
|
|
return describe('deleteNotificationByKeyOnly', function () {
|
|
|
|
return it('should completely remove the notification', function (done) {
|
2020-08-31 08:58:47 +00:00
|
|
|
this.deleteOneStub.callsArgWith(1, null)
|
2020-01-13 19:01:07 +00:00
|
|
|
|
2020-01-13 19:01:20 +00:00
|
|
|
return this.notifications.deleteNotificationByKeyOnly(
|
2023-03-16 12:38:09 +00:00
|
|
|
notificationKey,
|
2021-07-13 11:04:44 +00:00
|
|
|
err => {
|
2021-10-27 09:49:18 +00:00
|
|
|
if (err) return done(err)
|
2023-03-16 12:38:09 +00:00
|
|
|
const searchOps = { key: notificationKey }
|
2020-08-31 08:58:47 +00:00
|
|
|
assert.deepEqual(this.deleteOneStub.args[0][0], searchOps)
|
2020-01-13 19:01:20 +00:00
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
2020-02-11 09:08:35 +00:00
|
|
|
})
|
|
|
|
})
|
2020-01-13 19:01:20 +00:00
|
|
|
})
|