2016-01-14 07:35:16 -05:00
|
|
|
sinon = require('sinon')
|
|
|
|
chai = require('chai')
|
2016-08-11 06:05:11 -04:00
|
|
|
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()
|
2016-08-17 09:10:36 -04:00
|
|
|
@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
|
2016-08-17 09:10:36 -04:00
|
|
|
remove: @removeStub
|
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:
|
2016-08-15 03:54:45 -04:00
|
|
|
'logger-sharelatex': {
|
|
|
|
log:()->
|
|
|
|
error:()->
|
|
|
|
}
|
2016-01-14 07:35:16 -05:00
|
|
|
'settings-sharelatex': {}
|
|
|
|
'mongojs':@mongojs
|
2017-03-17 05:47:45 -04:00
|
|
|
'metrics-sharelatex': {timeAsyncMethod: sinon.stub()}
|
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', ->
|
2016-08-11 06:05:11 -04:00
|
|
|
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",
|
2016-08-12 06:38:17 -04:00
|
|
|
templateKey:"template-key"
|
2016-08-11 06:05:11 -04:00
|
|
|
}
|
2016-08-17 09:10:36 -04:00
|
|
|
@notifications.deleteNotificationByKeyOnly = sinon.stub()
|
|
|
|
@notifications.deleteNotificationByKeyOnly.callsArgWith(1, null)
|
2016-08-11 06:05:11 -04:00
|
|
|
|
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)=>
|
2016-08-15 04:50:59 -04:00
|
|
|
assert.deepEqual(@insertStub.lastCall.args[0], @expectedDocument)
|
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)=>
|
2016-08-11 06:05:11 -04:00
|
|
|
@insertStub.calledWith(@expectedDocument).should.equal false
|
2016-01-16 15:28:19 -05:00
|
|
|
done()
|
|
|
|
|
2016-08-17 06:05:29 -04:00
|
|
|
describe "when key already exists but forceCreate is passed", (done)->
|
2016-08-17 05:52:35 -04:00
|
|
|
|
2016-08-17 09:10:36 -04:00
|
|
|
it "should delete the old key and insert the new one", (done) ->
|
2016-08-17 05:52:35 -04:00
|
|
|
@insertStub.callsArgWith(1, null)
|
|
|
|
@countStub.callsArgWith(1, null, 1)
|
2016-08-17 06:05:29 -04:00
|
|
|
@stubbedNotification.forceCreate = true
|
2016-08-17 05:52:35 -04:00
|
|
|
@notifications.addNotification user_id, @stubbedNotification, (err)=>
|
|
|
|
assert.deepEqual(@insertStub.lastCall.args[0], @expectedDocument)
|
2016-08-17 09:10:36 -04:00
|
|
|
@notifications.deleteNotificationByKeyOnly.calledWith(@stubbedNotification.key).should.equal true
|
2016-08-17 05:52:35 -04:00
|
|
|
done()
|
|
|
|
|
2016-08-11 06:05:11 -04:00
|
|
|
describe 'when the notification is set to expire', () ->
|
|
|
|
beforeEach ->
|
|
|
|
@stubbedNotification = {
|
|
|
|
user_id: ObjectId(user_id),
|
|
|
|
key:"notification-key",
|
|
|
|
messageOpts:"some info",
|
|
|
|
templateKey:"template-key",
|
2016-08-12 06:38:17 -04:00
|
|
|
expires: '2922-02-13T09:32:56.289Z'
|
2016-08-11 06:05:11 -04:00
|
|
|
}
|
|
|
|
@expectedDocument = {
|
|
|
|
user_id: @stubbedNotification.user_id,
|
|
|
|
key:"notification-key",
|
|
|
|
messageOpts:"some info",
|
|
|
|
templateKey:"template-key",
|
2016-08-12 06:38:17 -04:00
|
|
|
expires: new Date(@stubbedNotification.expires),
|
2016-08-11 06:05:11 -04:00
|
|
|
}
|
|
|
|
|
2016-08-12 06:38:17 -04:00
|
|
|
it 'should add an `expires` Date field to the document', (done)->
|
2016-08-11 06:05:11 -04:00
|
|
|
@insertStub.callsArgWith(1, null)
|
|
|
|
@countStub.callsArgWith(1, null, 0)
|
|
|
|
|
|
|
|
@notifications.addNotification user_id, @stubbedNotification, (err)=>
|
|
|
|
@insertStub.callCount.should.equal 1
|
2016-08-15 04:50:59 -04:00
|
|
|
assert.deepEqual(@insertStub.lastCall.args[0], @expectedDocument)
|
2016-08-11 06:05:11 -04:00
|
|
|
done()
|
|
|
|
|
2016-08-15 03:54:45 -04:00
|
|
|
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)=>
|
2016-08-11 05:01:21 -04:00
|
|
|
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)
|
2016-08-11 05:01:21 -04:00
|
|
|
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 = {
|
2016-08-11 05:01:21 -04:00
|
|
|
"$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)
|
2016-08-11 05:01:21 -04:00
|
|
|
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)
|
2016-02-05 04:41:34 -05:00
|
|
|
done()
|
2016-08-17 09:10:36 -04:00
|
|
|
|
|
|
|
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()
|