overleaf/services/notifications/test/unit/js/NotificationsTests.js

305 lines
9.2 KiB
JavaScript
Raw Normal View History

/* eslint-disable
camelcase,
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.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const sinon = require('sinon')
const { expect } = require('chai')
const modulePath = '../../../app/js/Notifications.js'
const SandboxedModule = require('sandboxed-module')
const assert = require('assert')
const { ObjectId } = require('mongodb')
const user_id = '51dc93e6fb625a261300003b'
const notification_id = 'fb625a26f09d'
const notification_key = 'notification-key'
2020-06-04 03:50:05 -04:00
describe('Notifications Tests', function () {
beforeEach(function () {
this.findToArrayStub = sinon.stub()
this.findStub = sinon.stub().returns({ toArray: this.findToArrayStub })
this.countStub = sinon.stub()
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,
},
}
this.notifications = SandboxedModule.require(modulePath, {
requires: {
'@overleaf/settings': {},
'./mongodb': { db: this.db, ObjectId },
2021-07-13 07:04:44 -04:00
'@overleaf/metrics': { timeAsyncMethod: sinon.stub() },
},
})
this.stubbedNotification = {
user_id: ObjectId(user_id),
key: 'notification-key',
messageOpts: 'some info',
2021-07-13 07:04:44 -04:00
templateKey: 'template-key',
}
return (this.stubbedNotificationArray = [this.stubbedNotification])
})
2020-06-04 03:50:05 -04:00
describe('getUserNotifications', function () {
return it('should find all notifications and return i', function (done) {
this.findToArrayStub.callsArgWith(0, null, this.stubbedNotificationArray)
return this.notifications.getUserNotifications(
user_id,
(err, notifications) => {
if (err) return done(err)
notifications.should.equal(this.stubbedNotificationArray)
assert.deepEqual(this.findStub.args[0][0], {
user_id: ObjectId(user_id),
2021-07-13 07:04:44 -04:00
templateKey: { $exists: true },
})
return done()
}
)
2020-02-11 04:08:35 -05:00
})
})
2020-06-04 03:50:05 -04:00
describe('addNotification', function () {
beforeEach(function () {
this.stubbedNotification = {
user_id: ObjectId(user_id),
key: 'notification-key',
messageOpts: 'some info',
2021-07-13 07:04:44 -04:00
templateKey: 'template-key',
}
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',
}
this.expectedQuery = {
user_id: this.stubbedNotification.user_id,
2021-07-13 07:04:44 -04:00
key: 'notification-key',
}
this.updateOneStub.yields()
return this.countStub.yields(null, 0)
})
2020-06-04 03:50:05 -04:00
it('should insert the notification into the collection', function (done) {
return this.notifications.addNotification(
user_id,
this.stubbedNotification,
2021-07-13 07:04:44 -04:00
err => {
expect(err).not.to.exist
sinon.assert.calledWith(
this.updateOneStub,
this.expectedQuery,
2020-04-01 11:03:52 -04:00
{ $set: this.expectedDocument },
{ upsert: true }
)
return done()
}
)
})
2020-06-04 03:50:05 -04:00
describe('when there is an existing notification', function (done) {
beforeEach(function () {
return this.countStub.yields(null, 1)
})
2020-06-04 03:50:05 -04:00
it('should fail to insert', function (done) {
return this.notifications.addNotification(
user_id,
this.stubbedNotification,
2021-07-13 07:04:44 -04:00
err => {
expect(err).not.to.exist
sinon.assert.notCalled(this.updateOneStub)
return done()
}
)
})
2020-06-04 03:50:05 -04:00
return it('should update the key if forceCreate is true', function (done) {
this.stubbedNotification.forceCreate = true
return this.notifications.addNotification(
user_id,
this.stubbedNotification,
2021-07-13 07:04:44 -04:00
err => {
expect(err).not.to.exist
sinon.assert.calledWith(
this.updateOneStub,
this.expectedQuery,
2020-04-01 11:03:52 -04:00
{ $set: this.expectedDocument },
{ upsert: true }
)
return done()
}
)
})
})
2020-06-04 03:50:05 -04:00
describe('when the notification is set to expire', function () {
beforeEach(function () {
this.stubbedNotification = {
user_id: ObjectId(user_id),
key: 'notification-key',
messageOpts: 'some info',
templateKey: 'template-key',
2021-07-13 07:04:44 -04:00
expires: '2922-02-13T09:32:56.289Z',
}
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),
}
return (this.expectedQuery = {
user_id: this.stubbedNotification.user_id,
2021-07-13 07:04:44 -04:00
key: 'notification-key',
})
})
2020-06-04 03:50:05 -04:00
return it('should add an `expires` Date field to the document', function (done) {
return this.notifications.addNotification(
user_id,
this.stubbedNotification,
2021-07-13 07:04:44 -04:00
err => {
expect(err).not.to.exist
sinon.assert.calledWith(
this.updateOneStub,
this.expectedQuery,
2020-04-01 11:03:52 -04:00
{ $set: this.expectedDocument },
{ upsert: true }
)
return done()
}
)
})
})
2020-06-04 03:50:05 -04:00
return describe('when the notification has a nonsensical expires field', function () {
beforeEach(function () {
this.stubbedNotification = {
user_id: ObjectId(user_id),
key: 'notification-key',
messageOpts: 'some info',
templateKey: 'template-key',
2021-07-13 07:04:44 -04:00
expires: 'WAT',
}
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-06-04 03:50:05 -04:00
return it('should produce an error', function (done) {
return this.notifications.addNotification(
user_id,
this.stubbedNotification,
2021-07-13 07:04:44 -04:00
err => {
;(err instanceof Error).should.equal(true)
sinon.assert.notCalled(this.updateOneStub)
return done()
}
)
})
})
})
2020-06-04 03:50:05 -04:00
describe('removeNotificationId', function () {
return it('should mark the notification id as read', function (done) {
this.updateOneStub.callsArgWith(2, null)
return this.notifications.removeNotificationId(
user_id,
notification_id,
2021-07-13 07:04:44 -04:00
err => {
if (err) return done(err)
const searchOps = {
user_id: ObjectId(user_id),
2021-07-13 07:04:44 -04:00
_id: ObjectId(notification_id),
}
const updateOperation = {
2021-07-13 07:04:44 -04:00
$unset: { templateKey: true, messageOpts: true },
}
assert.deepEqual(this.updateOneStub.args[0][0], searchOps)
assert.deepEqual(this.updateOneStub.args[0][1], updateOperation)
return done()
}
)
2020-02-11 04:08:35 -05:00
})
})
2020-06-04 03:50:05 -04:00
describe('removeNotificationKey', function () {
return it('should mark the notification key as read', function (done) {
this.updateOneStub.callsArgWith(2, null)
return this.notifications.removeNotificationKey(
user_id,
notification_key,
2021-07-13 07:04:44 -04:00
err => {
if (err) return done(err)
const searchOps = {
user_id: ObjectId(user_id),
2021-07-13 07:04:44 -04:00
key: notification_key,
}
const updateOperation = {
2021-07-13 07:04:44 -04:00
$unset: { templateKey: true },
}
assert.deepEqual(this.updateOneStub.args[0][0], searchOps)
assert.deepEqual(this.updateOneStub.args[0][1], updateOperation)
return done()
}
)
2020-02-11 04:08:35 -05:00
})
})
2020-06-04 03:50:05 -04:00
describe('removeNotificationByKeyOnly', function () {
return it('should mark the notification key as read', function (done) {
this.updateOneStub.callsArgWith(2, null)
return this.notifications.removeNotificationByKeyOnly(
notification_key,
2021-07-13 07:04:44 -04:00
err => {
if (err) return done(err)
const searchOps = { key: notification_key }
const updateOperation = { $unset: { templateKey: true } }
assert.deepEqual(this.updateOneStub.args[0][0], searchOps)
assert.deepEqual(this.updateOneStub.args[0][1], updateOperation)
return done()
}
)
2020-02-11 04:08:35 -05:00
})
})
2020-06-04 03:50:05 -04:00
return describe('deleteNotificationByKeyOnly', function () {
return it('should completely remove the notification', function (done) {
this.deleteOneStub.callsArgWith(1, null)
return this.notifications.deleteNotificationByKeyOnly(
notification_key,
2021-07-13 07:04:44 -04:00
err => {
if (err) return done(err)
const searchOps = { key: notification_key }
assert.deepEqual(this.deleteOneStub.args[0][0], searchOps)
return done()
}
)
2020-02-11 04:08:35 -05:00
})
})
})