2020-01-13 14:01:13 -05: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 14:01:07 -05: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 14:01:20 -05:00
|
|
|
const sinon = require('sinon')
|
2021-03-17 16:34:06 -04:00
|
|
|
const { expect } = require('chai')
|
2020-01-13 14:01:20 -05:00
|
|
|
const modulePath = '../../../app/js/Notifications.js'
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const assert = require('assert')
|
2020-08-31 04:58:47 -04:00
|
|
|
const { ObjectId } = require('mongodb')
|
2020-01-13 14:01:07 -05:00
|
|
|
|
2023-03-16 08:38:09 -04:00
|
|
|
const userId = '51dc93e6fb625a261300003b'
|
|
|
|
const notificationId = 'fb625a26f09d'
|
|
|
|
const notificationKey = 'notification-key'
|
2020-01-13 14:01:07 -05:00
|
|
|
|
2020-06-04 03:50:05 -04:00
|
|
|
describe('Notifications Tests', function () {
|
|
|
|
beforeEach(function () {
|
2020-08-31 04:58:47 -04:00
|
|
|
this.findToArrayStub = sinon.stub()
|
|
|
|
this.findStub = sinon.stub().returns({ toArray: this.findToArrayStub })
|
2020-01-13 14:01:20 -05:00
|
|
|
this.countStub = sinon.stub()
|
2020-08-31 04:58:47 -04:00
|
|
|
this.updateOneStub = sinon.stub()
|
|
|
|
this.deleteOneStub = sinon.stub()
|
|
|
|
this.db = {
|
|
|
|
notifications: {
|
|
|
|
find: this.findStub,
|
|
|
|
count: this.countStub,
|
|
|
|
updateOne: this.updateOneStub,
|
2021-07-13 07:04:44 -04:00
|
|
|
deleteOne: this.deleteOneStub,
|
|
|
|
},
|
2020-01-13 14:01:20 -05:00
|
|
|
}
|
2020-01-13 14:01:07 -05:00
|
|
|
|
2020-01-13 14:01:20 -05:00
|
|
|
this.notifications = SandboxedModule.require(modulePath, {
|
|
|
|
requires: {
|
2021-07-12 12:47:16 -04:00
|
|
|
'@overleaf/settings': {},
|
2020-08-31 04:58:47 -04:00
|
|
|
'./mongodb': { db: this.db, ObjectId },
|
2021-07-13 07:04:44 -04:00
|
|
|
'@overleaf/metrics': { timeAsyncMethod: sinon.stub() },
|
|
|
|
},
|
2020-01-13 14:01:20 -05:00
|
|
|
})
|
2020-01-13 14:01:07 -05:00
|
|
|
|
2020-01-13 14:01:20 -05:00
|
|
|
this.stubbedNotification = {
|
2023-03-16 08:38:09 -04:00
|
|
|
user_id: ObjectId(userId),
|
2020-01-13 14:01:20 -05:00
|
|
|
key: 'notification-key',
|
|
|
|
messageOpts: 'some info',
|
2021-07-13 07:04:44 -04:00
|
|
|
templateKey: 'template-key',
|
2020-01-13 14:01:20 -05:00
|
|
|
}
|
|
|
|
return (this.stubbedNotificationArray = [this.stubbedNotification])
|
|
|
|
})
|
2020-01-13 14:01:07 -05:00
|
|
|
|
2020-06-04 03:50:05 -04:00
|
|
|
describe('getUserNotifications', function () {
|
|
|
|
return it('should find all notifications and return i', function (done) {
|
2020-08-31 04:58:47 -04:00
|
|
|
this.findToArrayStub.callsArgWith(0, null, this.stubbedNotificationArray)
|
2020-01-13 14:01:20 -05:00
|
|
|
return this.notifications.getUserNotifications(
|
2023-03-16 08:38:09 -04:00
|
|
|
userId,
|
2020-01-13 14:01:20 -05:00
|
|
|
(err, notifications) => {
|
2021-10-27 05:49:18 -04:00
|
|
|
if (err) return done(err)
|
2020-01-13 14:01:20 -05:00
|
|
|
notifications.should.equal(this.stubbedNotificationArray)
|
|
|
|
assert.deepEqual(this.findStub.args[0][0], {
|
2023-03-16 08:38:09 -04:00
|
|
|
user_id: ObjectId(userId),
|
2021-07-13 07:04:44 -04:00
|
|
|
templateKey: { $exists: true },
|
2020-01-13 14:01:20 -05:00
|
|
|
})
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
2020-02-11 04:08:35 -05:00
|
|
|
})
|
|
|
|
})
|
2020-01-13 14:01:07 -05:00
|
|
|
|
2020-06-04 03:50:05 -04:00
|
|
|
describe('addNotification', function () {
|
|
|
|
beforeEach(function () {
|
2020-01-13 14:01:20 -05:00
|
|
|
this.stubbedNotification = {
|
2023-03-16 08:38:09 -04:00
|
|
|
user_id: ObjectId(userId),
|
2020-01-13 14:01:20 -05:00
|
|
|
key: 'notification-key',
|
|
|
|
messageOpts: 'some info',
|
2021-07-13 07:04:44 -04:00
|
|
|
templateKey: 'template-key',
|
2020-01-13 14:01:20 -05:00
|
|
|
}
|
|
|
|
this.expectedDocument = {
|
|
|
|
user_id: this.stubbedNotification.user_id,
|
|
|
|
key: 'notification-key',
|
|
|
|
messageOpts: 'some info',
|
2021-07-13 07:04:44 -04:00
|
|
|
templateKey: 'template-key',
|
2020-01-13 14:01:20 -05:00
|
|
|
}
|
|
|
|
this.expectedQuery = {
|
|
|
|
user_id: this.stubbedNotification.user_id,
|
2021-07-13 07:04:44 -04:00
|
|
|
key: 'notification-key',
|
2020-01-13 14:01:20 -05:00
|
|
|
}
|
2020-08-31 04:58:47 -04:00
|
|
|
this.updateOneStub.yields()
|
2020-01-13 14:01:20 -05:00
|
|
|
return this.countStub.yields(null, 0)
|
|
|
|
})
|
2020-01-13 14:01:07 -05:00
|
|
|
|
2020-06-04 03:50:05 -04:00
|
|
|
it('should insert the notification into the collection', function (done) {
|
2020-01-13 14:01:20 -05:00
|
|
|
return this.notifications.addNotification(
|
2023-03-16 08:38:09 -04:00
|
|
|
userId,
|
2020-01-13 14:01:20 -05:00
|
|
|
this.stubbedNotification,
|
2021-07-13 07:04:44 -04:00
|
|
|
err => {
|
2021-03-17 16:34:06 -04:00
|
|
|
expect(err).not.to.exist
|
2020-01-13 14:01:20 -05:00
|
|
|
sinon.assert.calledWith(
|
2020-08-31 04:58:47 -04:00
|
|
|
this.updateOneStub,
|
2020-01-13 14:01:20 -05:00
|
|
|
this.expectedQuery,
|
2020-04-01 11:03:52 -04:00
|
|
|
{ $set: this.expectedDocument },
|
2020-01-13 14:01:20 -05:00
|
|
|
{ upsert: true }
|
|
|
|
)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2020-01-13 14:01:07 -05:00
|
|
|
|
2020-06-04 03:50:05 -04:00
|
|
|
describe('when there is an existing notification', function (done) {
|
|
|
|
beforeEach(function () {
|
2020-01-13 14:01:20 -05:00
|
|
|
return this.countStub.yields(null, 1)
|
|
|
|
})
|
2020-01-13 14:01:07 -05:00
|
|
|
|
2020-06-04 03:50:05 -04:00
|
|
|
it('should fail to insert', function (done) {
|
2020-01-13 14:01:20 -05:00
|
|
|
return this.notifications.addNotification(
|
2023-03-16 08:38:09 -04:00
|
|
|
userId,
|
2020-01-13 14:01:20 -05:00
|
|
|
this.stubbedNotification,
|
2021-07-13 07:04:44 -04:00
|
|
|
err => {
|
2021-03-17 16:34:06 -04:00
|
|
|
expect(err).not.to.exist
|
2020-08-31 04:58:47 -04:00
|
|
|
sinon.assert.notCalled(this.updateOneStub)
|
2020-01-13 14:01:20 -05:00
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2020-01-13 14:01:07 -05:00
|
|
|
|
2020-06-04 03:50:05 -04:00
|
|
|
return it('should update the key if forceCreate is true', function (done) {
|
2020-01-13 14:01:20 -05:00
|
|
|
this.stubbedNotification.forceCreate = true
|
|
|
|
return this.notifications.addNotification(
|
2023-03-16 08:38:09 -04:00
|
|
|
userId,
|
2020-01-13 14:01:20 -05:00
|
|
|
this.stubbedNotification,
|
2021-07-13 07:04:44 -04:00
|
|
|
err => {
|
2021-03-17 16:34:06 -04:00
|
|
|
expect(err).not.to.exist
|
2020-01-13 14:01:20 -05:00
|
|
|
sinon.assert.calledWith(
|
2020-08-31 04:58:47 -04:00
|
|
|
this.updateOneStub,
|
2020-01-13 14:01:20 -05:00
|
|
|
this.expectedQuery,
|
2020-04-01 11:03:52 -04:00
|
|
|
{ $set: this.expectedDocument },
|
2020-01-13 14:01:20 -05:00
|
|
|
{ upsert: true }
|
|
|
|
)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
2020-01-13 14:01:07 -05:00
|
|
|
|
2020-06-04 03:50:05 -04:00
|
|
|
describe('when the notification is set to expire', function () {
|
|
|
|
beforeEach(function () {
|
2020-01-13 14:01:20 -05:00
|
|
|
this.stubbedNotification = {
|
2023-03-16 08:38:09 -04:00
|
|
|
user_id: ObjectId(userId),
|
2020-01-13 14:01:20 -05:00
|
|
|
key: 'notification-key',
|
|
|
|
messageOpts: 'some info',
|
|
|
|
templateKey: 'template-key',
|
2021-07-13 07:04:44 -04:00
|
|
|
expires: '2922-02-13T09:32:56.289Z',
|
2020-01-13 14:01:20 -05:00
|
|
|
}
|
|
|
|
this.expectedDocument = {
|
|
|
|
user_id: this.stubbedNotification.user_id,
|
|
|
|
key: 'notification-key',
|
|
|
|
messageOpts: 'some info',
|
|
|
|
templateKey: 'template-key',
|
2021-07-13 07:04:44 -04:00
|
|
|
expires: new Date(this.stubbedNotification.expires),
|
2020-01-13 14:01:20 -05:00
|
|
|
}
|
|
|
|
return (this.expectedQuery = {
|
|
|
|
user_id: this.stubbedNotification.user_id,
|
2021-07-13 07:04:44 -04:00
|
|
|
key: 'notification-key',
|
2020-01-13 14:01:20 -05:00
|
|
|
})
|
|
|
|
})
|
2020-01-13 14:01:07 -05:00
|
|
|
|
2020-06-04 03:50:05 -04:00
|
|
|
return it('should add an `expires` Date field to the document', function (done) {
|
2020-01-13 14:01:20 -05:00
|
|
|
return this.notifications.addNotification(
|
2023-03-16 08:38:09 -04:00
|
|
|
userId,
|
2020-01-13 14:01:20 -05:00
|
|
|
this.stubbedNotification,
|
2021-07-13 07:04:44 -04:00
|
|
|
err => {
|
2021-03-17 16:34:06 -04:00
|
|
|
expect(err).not.to.exist
|
2020-01-13 14:01:20 -05:00
|
|
|
sinon.assert.calledWith(
|
2020-08-31 04:58:47 -04:00
|
|
|
this.updateOneStub,
|
2020-01-13 14:01:20 -05:00
|
|
|
this.expectedQuery,
|
2020-04-01 11:03:52 -04:00
|
|
|
{ $set: this.expectedDocument },
|
2020-01-13 14:01:20 -05:00
|
|
|
{ upsert: true }
|
|
|
|
)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
2020-01-13 14:01:07 -05:00
|
|
|
|
2020-06-04 03:50:05 -04:00
|
|
|
return describe('when the notification has a nonsensical expires field', function () {
|
|
|
|
beforeEach(function () {
|
2020-01-13 14:01:20 -05:00
|
|
|
this.stubbedNotification = {
|
2023-03-16 08:38:09 -04:00
|
|
|
user_id: ObjectId(userId),
|
2020-01-13 14:01:20 -05:00
|
|
|
key: 'notification-key',
|
|
|
|
messageOpts: 'some info',
|
|
|
|
templateKey: 'template-key',
|
2021-07-13 07:04:44 -04:00
|
|
|
expires: 'WAT',
|
2020-01-13 14:01:20 -05:00
|
|
|
}
|
|
|
|
return (this.expectedDocument = {
|
|
|
|
user_id: this.stubbedNotification.user_id,
|
|
|
|
key: 'notification-key',
|
|
|
|
messageOpts: 'some info',
|
|
|
|
templateKey: 'template-key',
|
2021-07-13 07:04:44 -04:00
|
|
|
expires: new Date(this.stubbedNotification.expires),
|
2020-01-13 14:01:20 -05:00
|
|
|
})
|
|
|
|
})
|
2020-01-13 14:01:07 -05:00
|
|
|
|
2020-06-04 03:50:05 -04:00
|
|
|
return it('should produce an error', function (done) {
|
2020-01-13 14:01:20 -05:00
|
|
|
return this.notifications.addNotification(
|
2023-03-16 08:38:09 -04:00
|
|
|
userId,
|
2020-01-13 14:01:20 -05:00
|
|
|
this.stubbedNotification,
|
2021-07-13 07:04:44 -04:00
|
|
|
err => {
|
2020-01-13 14:01:20 -05:00
|
|
|
;(err instanceof Error).should.equal(true)
|
2020-08-31 04:58:47 -04:00
|
|
|
sinon.assert.notCalled(this.updateOneStub)
|
2020-01-13 14:01:20 -05:00
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2020-01-13 14:01:07 -05:00
|
|
|
|
2020-06-04 03:50:05 -04:00
|
|
|
describe('removeNotificationId', function () {
|
|
|
|
return it('should mark the notification id as read', function (done) {
|
2020-08-31 04:58:47 -04:00
|
|
|
this.updateOneStub.callsArgWith(2, null)
|
2020-01-13 14:01:07 -05:00
|
|
|
|
2020-01-13 14:01:20 -05:00
|
|
|
return this.notifications.removeNotificationId(
|
2023-03-16 08:38:09 -04:00
|
|
|
userId,
|
|
|
|
notificationId,
|
2021-07-13 07:04:44 -04:00
|
|
|
err => {
|
2021-10-27 05:49:18 -04:00
|
|
|
if (err) return done(err)
|
2020-01-13 14:01:20 -05:00
|
|
|
const searchOps = {
|
2023-03-16 08:38:09 -04:00
|
|
|
user_id: ObjectId(userId),
|
|
|
|
_id: ObjectId(notificationId),
|
2020-01-13 14:01:20 -05:00
|
|
|
}
|
|
|
|
const updateOperation = {
|
2021-07-13 07:04:44 -04:00
|
|
|
$unset: { templateKey: true, messageOpts: true },
|
2020-01-13 14:01:20 -05:00
|
|
|
}
|
2020-08-31 04:58:47 -04:00
|
|
|
assert.deepEqual(this.updateOneStub.args[0][0], searchOps)
|
|
|
|
assert.deepEqual(this.updateOneStub.args[0][1], updateOperation)
|
2020-01-13 14:01:20 -05:00
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
2020-02-11 04:08:35 -05:00
|
|
|
})
|
|
|
|
})
|
2020-01-13 14:01:07 -05:00
|
|
|
|
2020-06-04 03:50:05 -04:00
|
|
|
describe('removeNotificationKey', function () {
|
|
|
|
return it('should mark the notification key as read', function (done) {
|
2020-08-31 04:58:47 -04:00
|
|
|
this.updateOneStub.callsArgWith(2, null)
|
2020-01-13 14:01:07 -05:00
|
|
|
|
2020-01-13 14:01:20 -05:00
|
|
|
return this.notifications.removeNotificationKey(
|
2023-03-16 08:38:09 -04:00
|
|
|
userId,
|
|
|
|
notificationKey,
|
2021-07-13 07:04:44 -04:00
|
|
|
err => {
|
2021-10-27 05:49:18 -04:00
|
|
|
if (err) return done(err)
|
2020-01-13 14:01:20 -05:00
|
|
|
const searchOps = {
|
2023-03-16 08:38:09 -04:00
|
|
|
user_id: ObjectId(userId),
|
|
|
|
key: notificationKey,
|
2020-01-13 14:01:20 -05:00
|
|
|
}
|
|
|
|
const updateOperation = {
|
2021-07-13 07:04:44 -04:00
|
|
|
$unset: { templateKey: true },
|
2020-01-13 14:01:20 -05:00
|
|
|
}
|
2020-08-31 04:58:47 -04:00
|
|
|
assert.deepEqual(this.updateOneStub.args[0][0], searchOps)
|
|
|
|
assert.deepEqual(this.updateOneStub.args[0][1], updateOperation)
|
2020-01-13 14:01:20 -05:00
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
2020-02-11 04:08:35 -05:00
|
|
|
})
|
|
|
|
})
|
2020-01-13 14:01:07 -05:00
|
|
|
|
2020-06-04 03:50:05 -04:00
|
|
|
describe('removeNotificationByKeyOnly', function () {
|
|
|
|
return it('should mark the notification key as read', function (done) {
|
2020-08-31 04:58:47 -04:00
|
|
|
this.updateOneStub.callsArgWith(2, null)
|
2020-01-13 14:01:07 -05:00
|
|
|
|
2020-01-13 14:01:20 -05:00
|
|
|
return this.notifications.removeNotificationByKeyOnly(
|
2023-03-16 08:38:09 -04:00
|
|
|
notificationKey,
|
2021-07-13 07:04:44 -04:00
|
|
|
err => {
|
2021-10-27 05:49:18 -04:00
|
|
|
if (err) return done(err)
|
2023-03-16 08:38:09 -04:00
|
|
|
const searchOps = { key: notificationKey }
|
2020-01-13 14:01:20 -05:00
|
|
|
const updateOperation = { $unset: { templateKey: true } }
|
2020-08-31 04:58:47 -04:00
|
|
|
assert.deepEqual(this.updateOneStub.args[0][0], searchOps)
|
|
|
|
assert.deepEqual(this.updateOneStub.args[0][1], updateOperation)
|
2020-01-13 14:01:20 -05:00
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
2020-02-11 04:08:35 -05:00
|
|
|
})
|
|
|
|
})
|
2020-01-13 14:01:07 -05:00
|
|
|
|
2020-06-04 03:50:05 -04:00
|
|
|
return describe('deleteNotificationByKeyOnly', function () {
|
|
|
|
return it('should completely remove the notification', function (done) {
|
2020-08-31 04:58:47 -04:00
|
|
|
this.deleteOneStub.callsArgWith(1, null)
|
2020-01-13 14:01:07 -05:00
|
|
|
|
2020-01-13 14:01:20 -05:00
|
|
|
return this.notifications.deleteNotificationByKeyOnly(
|
2023-03-16 08:38:09 -04:00
|
|
|
notificationKey,
|
2021-07-13 07:04:44 -04:00
|
|
|
err => {
|
2021-10-27 05:49:18 -04:00
|
|
|
if (err) return done(err)
|
2023-03-16 08:38:09 -04:00
|
|
|
const searchOps = { key: notificationKey }
|
2020-08-31 04:58:47 -04:00
|
|
|
assert.deepEqual(this.deleteOneStub.args[0][0], searchOps)
|
2020-01-13 14:01:20 -05:00
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
2020-02-11 04:08:35 -05:00
|
|
|
})
|
|
|
|
})
|
2020-01-13 14:01:20 -05:00
|
|
|
})
|