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

204 lines
6.3 KiB
CoffeeScript
Raw Normal View History

2016-01-14 07:35:16 -05:00
sinon = require('sinon')
chai = require('chai')
expect = chai.should
2016-01-14 07:35:16 -05:00
should = chai.should()
modulePath = "../../../app/js/Notifications.js"
SandboxedModule = require('sandboxed-module')
assert = require('assert')
2016-01-16 15:28:19 -05:00
ObjectId = require("mongojs").ObjectId
2016-01-14 07:35:16 -05:00
user_id = "51dc93e6fb625a261300003b"
2016-01-16 15:28:19 -05:00
notification_id = "fb625a26f09d"
notification_key = "notification-key"
2016-01-14 07:35:16 -05:00
2016-02-05 04:38:32 -05:00
describe 'Notifications Tests', ->
2016-01-14 07:35:16 -05:00
beforeEach ->
self = @
@findStub = sinon.stub()
2016-01-16 15:28:19 -05:00
@insertStub = sinon.stub()
@countStub = sinon.stub()
2016-01-14 07:35:16 -05:00
@updateStub = sinon.stub()
@removeStub = sinon.stub()
2016-01-14 07:35:16 -05:00
@mongojs = =>
notifications:
2016-01-16 15:28:19 -05:00
update: self.mongojsUpdate
2016-01-14 07:35:16 -05:00
find: @findStub
2016-01-16 15:28:19 -05:00
insert: @insertStub
count: @countStub
2016-01-14 07:35:16 -05:00
update: @updateStub
remove: @removeStub
2016-01-16 15:28:19 -05:00
@mongojs.ObjectId = ObjectId
2016-01-14 07:35:16 -05:00
@notifications = SandboxedModule.require modulePath,
requires:
'logger-sharelatex': {
log:()->
error:()->
}
'settings-sharelatex': {}
'mongojs':@mongojs
'metrics-sharelatex': {timeAsyncMethod: sinon.stub()}
globals:
console: console
2016-01-16 15:28:19 -05:00
2016-01-21 11:34:12 -05:00
@stubbedNotification = {user_id: ObjectId(user_id), key:"notification-key", messageOpts:"some info", templateKey:"template-key"}
2016-01-16 15:28:19 -05:00
@stubbedNotificationArray = [@stubbedNotification]
describe 'getUserNotifications', ->
2016-08-05 10:10:13 -04:00
it "should find all notifications and return i", (done)->
2016-01-16 15:28:19 -05:00
@findStub.callsArgWith(1, null, @stubbedNotificationArray)
@notifications.getUserNotifications user_id, (err, notifications)=>
notifications.should.equal @stubbedNotificationArray
2016-08-05 10:10:13 -04:00
assert.deepEqual(@findStub.args[0][0], {"user_id" :ObjectId(user_id), "templateKey": {"$exists":true}})
2016-01-16 15:28:19 -05:00
done()
describe 'addNotification', ->
beforeEach ->
@stubbedNotification = {
user_id: ObjectId(user_id),
key:"notification-key",
messageOpts:"some info",
templateKey:"template-key"
}
@expectedDocument = {
user_id: @stubbedNotification.user_id,
key:"notification-key",
messageOpts:"some info",
templateKey:"template-key"
}
@expectedQuery = {
user_id: @stubbedNotification.user_id,
key:"notification-key",
}
@updateStub.yields()
@countStub.yields(null, 0)
2016-01-16 15:28:19 -05:00
it 'should insert the notification into the collection', (done)->
@notifications.addNotification user_id, @stubbedNotification, (err)=>
expect(err).not.exists
sinon.assert.calledWith(@updateStub, @expectedQuery, @expectedDocument, { upsert: true })
2016-01-16 15:28:19 -05:00
done()
describe 'when there is an existing notification', (done) ->
beforeEach ->
@countStub.yields(null, 1)
2016-01-16 15:28:19 -05:00
it 'should fail to insert', (done)->
@notifications.addNotification user_id, @stubbedNotification, (err)=>
expect(err).not.exists
sinon.assert.notCalled(@updateStub)
done()
it "should update the key if forceCreate is true", (done) ->
2016-08-17 06:05:29 -04:00
@stubbedNotification.forceCreate = true
@notifications.addNotification user_id, @stubbedNotification, (err)=>
expect(err).not.exists
sinon.assert.calledWith(@updateStub, @expectedQuery, @expectedDocument, { upsert: true })
done()
describe 'when the notification is set to expire', () ->
beforeEach ->
@stubbedNotification = {
user_id: ObjectId(user_id),
key:"notification-key",
messageOpts:"some info",
templateKey:"template-key",
expires: '2922-02-13T09:32:56.289Z'
}
@expectedDocument = {
user_id: @stubbedNotification.user_id,
key:"notification-key",
messageOpts:"some info",
templateKey:"template-key",
expires: new Date(@stubbedNotification.expires),
}
@expectedQuery = {
user_id: @stubbedNotification.user_id,
key:"notification-key",
}
it 'should add an `expires` Date field to the document', (done)->
@notifications.addNotification user_id, @stubbedNotification, (err)=>
expect(err).not.exists
sinon.assert.calledWith(@updateStub, @expectedQuery, @expectedDocument, { upsert: true })
done()
describe 'when the notification has a nonsensical expires field', () ->
beforeEach ->
@stubbedNotification = {
user_id: ObjectId(user_id),
key:"notification-key",
messageOpts:"some info",
templateKey:"template-key",
expires: 'WAT'
}
@expectedDocument = {
user_id: @stubbedNotification.user_id,
key:"notification-key",
messageOpts:"some info",
templateKey:"template-key",
expires: new Date(@stubbedNotification.expires),
}
it 'should produce an error', (done)->
@notifications.addNotification user_id, @stubbedNotification, (err)=>
(err instanceof Error).should.equal true
sinon.assert.notCalled(@updateStub)
done()
2016-02-05 04:38:32 -05:00
describe 'removeNotificationId', ->
2016-01-16 15:28:19 -05:00
it 'should mark the notification id as read', (done)->
@updateStub.callsArgWith(2, null)
2016-02-05 04:38:32 -05:00
@notifications.removeNotificationId user_id, notification_id, (err)=>
searchOps =
2016-01-21 11:34:12 -05:00
user_id:ObjectId(user_id)
2016-01-16 15:28:19 -05:00
_id:ObjectId(notification_id)
updateOperation =
2016-01-21 08:40:24 -05:00
"$unset": {templateKey:true, messageOpts:true}
2016-08-05 10:10:13 -04:00
assert.deepEqual(@updateStub.args[0][0], searchOps)
assert.deepEqual(@updateStub.args[0][1], updateOperation)
2016-01-16 15:28:19 -05:00
done()
2016-02-05 04:38:32 -05:00
describe 'removeNotificationKey', ->
it 'should mark the notification key as read', (done)->
@updateStub.callsArgWith(2, null)
@notifications.removeNotificationKey user_id, notification_key, (err)=>
2016-08-15 04:50:59 -04:00
searchOps = {
2016-02-05 04:38:32 -05:00
user_id:ObjectId(user_id)
key: notification_key
2016-08-15 04:50:59 -04:00
}
updateOperation = {
"$unset": {templateKey:true}
2016-08-15 04:50:59 -04:00
}
assert.deepEqual(@updateStub.args[0][0], searchOps)
assert.deepEqual(@updateStub.args[0][1], updateOperation)
done()
describe 'removeNotificationByKeyOnly', ->
it 'should mark the notification key as read', (done)->
@updateStub.callsArgWith(2, null)
@notifications.removeNotificationByKeyOnly notification_key, (err)=>
searchOps =
key: notification_key
updateOperation =
2016-02-05 04:38:32 -05:00
"$unset": {templateKey:true}
2016-08-05 10:10:13 -04:00
assert.deepEqual(@updateStub.args[0][0], searchOps)
assert.deepEqual(@updateStub.args[0][1], updateOperation)
done()
describe 'deleteNotificationByKeyOnly', ->
it 'should completely remove the notification', (done)->
@removeStub.callsArgWith(2, null)
@notifications.deleteNotificationByKeyOnly notification_key, (err)=>
searchOps =
key: notification_key
opts =
justOne: true
assert.deepEqual(@removeStub.args[0][0], searchOps)
assert.deepEqual(@removeStub.args[0][1], opts)
done()