|
@@ -4,27 +4,71 @@ should = chai.should()
|
|
|
modulePath = "../../../app/js/Notifications.js"
|
|
modulePath = "../../../app/js/Notifications.js"
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
assert = require('assert')
|
|
assert = require('assert')
|
|
|
|
|
+ObjectId = require("mongojs").ObjectId
|
|
|
|
|
|
|
|
user_id = "51dc93e6fb625a261300003b"
|
|
user_id = "51dc93e6fb625a261300003b"
|
|
|
-notification_key = '123434'
|
|
|
|
|
|
|
+notification_id = "fb625a26f09d"
|
|
|
|
|
+notification_key = "notification-key"
|
|
|
|
|
|
|
|
describe 'creating a user', ->
|
|
describe 'creating a user', ->
|
|
|
beforeEach ->
|
|
beforeEach ->
|
|
|
self = @
|
|
self = @
|
|
|
- @findOneStub = sinon.stub()
|
|
|
|
|
@findStub = sinon.stub()
|
|
@findStub = sinon.stub()
|
|
|
- @saveStub = sinon.stub()
|
|
|
|
|
|
|
+ @insertStub = sinon.stub()
|
|
|
|
|
+ @countStub = sinon.stub()
|
|
|
@updateStub = sinon.stub()
|
|
@updateStub = sinon.stub()
|
|
|
|
|
|
|
|
@mongojs = =>
|
|
@mongojs = =>
|
|
|
notifications:
|
|
notifications:
|
|
|
- update: self.mongojsUpdate
|
|
|
|
|
|
|
+ update: self.mongojsUpdate
|
|
|
find: @findStub
|
|
find: @findStub
|
|
|
- findOne: @findOneStub
|
|
|
|
|
- save: @saveStub
|
|
|
|
|
|
|
+ insert: @insertStub
|
|
|
|
|
+ count: @countStub
|
|
|
update: @updateStub
|
|
update: @updateStub
|
|
|
|
|
+ @mongojs.ObjectId = ObjectId
|
|
|
|
|
|
|
|
- @repository = SandboxedModule.require modulePath, requires:
|
|
|
|
|
|
|
+ @notifications = SandboxedModule.require modulePath, requires:
|
|
|
'logger-sharelatex': log:->
|
|
'logger-sharelatex': log:->
|
|
|
'settings-sharelatex': {}
|
|
'settings-sharelatex': {}
|
|
|
'mongojs':@mongojs
|
|
'mongojs':@mongojs
|
|
|
|
|
+
|
|
|
|
|
+ @stubbedNotification = {user_id: user_id, key:"notification-key", messageOpts:"some info", templateKey:"template-key"}
|
|
|
|
|
+ @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
|
|
|
|
|
+ @findStub.calledWith({"user_id" : user_id, "templateKey": {"$exists":true}}).should.equal true
|
|
|
|
|
+ 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 =
|
|
|
|
|
+ user_id:user_id
|
|
|
|
|
+ _id:ObjectId(notification_id)
|
|
|
|
|
+ updateOperation =
|
|
|
|
|
+ "$unset": {templateKey:true}
|
|
|
|
|
+ @updateStub.calledWith(searchOps, updateOperation).should.equal true
|
|
|
|
|
+ done()
|