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

172 lines
5.4 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()
@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
2016-01-16 15:28:19 -05:00
@mongojs.ObjectId = ObjectId
2016-01-14 07:35:16 -05:00
2016-01-16 15:28:19 -05:00
@notifications = SandboxedModule.require modulePath, requires:
'logger-sharelatex': {
log:()->
error:()->
}
2016-01-14 07:35:16 -05:00
'settings-sharelatex': {}
'mongojs':@mongojs
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"
}
2016-01-16 15:28:19 -05:00
it 'should insert the notification into the collection', (done)->
@insertStub.callsArgWith(1, null)
@countStub.callsArgWith(1, null, 0)
@notifications.addNotification user_id, @stubbedNotification, (err)=>
@insertStub.calledWith(@expectedDocument).should.equal true
2016-01-16 15:28:19 -05:00
done()
it 'should fail insert of existing notification key', (done)->
@insertStub.callsArgWith(1, null)
@countStub.callsArgWith(1, null, 1)
@notifications.addNotification user_id, @stubbedNotification, (err)=>
@insertStub.calledWith(@expectedDocument).should.equal false
2016-01-16 15:28:19 -05:00
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),
}
it 'should add an `expires` Date field to the document', (done)->
@insertStub.callsArgWith(1, null)
@countStub.callsArgWith(1, null, 0)
@notifications.addNotification user_id, @stubbedNotification, (err)=>
@insertStub.callCount.should.equal 1
@insertStub.calledWith(@expectedDocument).should.equal 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)->
@insertStub.callsArgWith(1, null)
@countStub.callsArgWith(1, null, 0)
@notifications.addNotification user_id, @stubbedNotification, (err)=>
(err instanceof Error).should.equal true
@insertStub.callCount.should.equal 0
@insertStub.calledWith(@expectedDocument).should.equal false
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)=>
searchOps =
2016-02-05 04:38:32 -05:00
user_id:ObjectId(user_id)
key: notification_key
updateOperation =
"$unset": {templateKey:true}
@updateStub.calledWith(searchOps, updateOperation).should.equal true
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()