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

75 lines
2.4 KiB
CoffeeScript
Raw Normal View History

2016-01-14 12:35:16 +00:00
sinon = require('sinon')
chai = require('chai')
should = chai.should()
modulePath = "../../../app/js/Notifications.js"
SandboxedModule = require('sandboxed-module')
assert = require('assert')
2016-01-16 20:28:19 +00:00
ObjectId = require("mongojs").ObjectId
2016-01-14 12:35:16 +00:00
user_id = "51dc93e6fb625a261300003b"
2016-01-16 20:28:19 +00:00
notification_id = "fb625a26f09d"
notification_key = "notification-key"
2016-01-14 12:35:16 +00:00
describe 'creating a user', ->
beforeEach ->
self = @
@findStub = sinon.stub()
2016-01-16 20:28:19 +00:00
@insertStub = sinon.stub()
@countStub = sinon.stub()
2016-01-14 12:35:16 +00:00
@updateStub = sinon.stub()
@mongojs = =>
notifications:
2016-01-16 20:28:19 +00:00
update: self.mongojsUpdate
2016-01-14 12:35:16 +00:00
find: @findStub
2016-01-16 20:28:19 +00:00
insert: @insertStub
count: @countStub
2016-01-14 12:35:16 +00:00
update: @updateStub
2016-01-16 20:28:19 +00:00
@mongojs.ObjectId = ObjectId
2016-01-14 12:35:16 +00:00
2016-01-16 20:28:19 +00:00
@notifications = SandboxedModule.require modulePath, requires:
2016-01-14 12:35:16 +00:00
'logger-sharelatex': log:->
'settings-sharelatex': {}
'mongojs':@mongojs
2016-01-16 20:28:19 +00:00
2016-01-21 16:34:12 +00:00
@stubbedNotification = {user_id: ObjectId(user_id), key:"notification-key", messageOpts:"some info", templateKey:"template-key"}
2016-01-16 20:28:19 +00:00
@stubbedNotificationArray = [@stubbedNotification]
describe 'getUserNotifications', ->
it "should find all notifications and return it", (done)->
@findStub.callsArgWith(1, null, @stubbedNotificationArray)
@notifications.getUserNotifications user_id, (err, notifications)=>
notifications.should.equal @stubbedNotificationArray
2016-01-21 16:34:12 +00:00
@findStub.calledWith({"user_id" : ObjectId(user_id), "templateKey": {"$exists":true}}).should.equal true
2016-01-16 20:28:19 +00:00
done()
describe 'addNotification', ->
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(@stubbedNotification).should.equal true
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(@stubbedNotification).should.equal false
done()
describe 'removeNotification', ->
it 'should mark the notification id as read', (done)->
@updateStub.callsArgWith(2, null)
@notifications.removeNotification user_id, notification_id, (err)=>
searchOps =
2016-01-21 16:34:12 +00:00
user_id:ObjectId(user_id)
2016-01-16 20:28:19 +00:00
_id:ObjectId(notification_id)
updateOperation =
2016-01-21 13:40:24 +00:00
"$unset": {templateKey:true, messageOpts:true}
2016-01-16 20:28:19 +00:00
@updateStub.calledWith(searchOps, updateOperation).should.equal true
done()