NotificationsTests.coffee 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. modulePath = "../../../app/js/Notifications.js"
  5. SandboxedModule = require('sandboxed-module')
  6. assert = require('assert')
  7. ObjectId = require("mongojs").ObjectId
  8. user_id = "51dc93e6fb625a261300003b"
  9. notification_id = "fb625a26f09d"
  10. notification_key = "notification-key"
  11. describe 'Notifications Tests', ->
  12. beforeEach ->
  13. self = @
  14. @findStub = sinon.stub()
  15. @insertStub = sinon.stub()
  16. @countStub = sinon.stub()
  17. @updateStub = sinon.stub()
  18. @mongojs = =>
  19. notifications:
  20. update: self.mongojsUpdate
  21. find: @findStub
  22. insert: @insertStub
  23. count: @countStub
  24. update: @updateStub
  25. @mongojs.ObjectId = ObjectId
  26. @notifications = SandboxedModule.require modulePath, requires:
  27. 'logger-sharelatex': log:->
  28. 'settings-sharelatex': {}
  29. 'mongojs':@mongojs
  30. @stubbedNotification = {user_id: ObjectId(user_id), key:"notification-key", messageOpts:"some info", templateKey:"template-key"}
  31. @stubbedNotificationArray = [@stubbedNotification]
  32. describe 'getUserNotifications', ->
  33. it "should find all notifications and return it", (done)->
  34. @findStub.callsArgWith(1, null, @stubbedNotificationArray)
  35. @notifications.getUserNotifications user_id, (err, notifications)=>
  36. notifications.should.equal @stubbedNotificationArray
  37. @findStub.calledWith({"user_id" : ObjectId(user_id), "templateKey": {"$exists":true}}).should.equal true
  38. done()
  39. describe 'addNotification', ->
  40. it 'should insert the notification into the collection', (done)->
  41. @insertStub.callsArgWith(1, null)
  42. @countStub.callsArgWith(1, null, 0)
  43. @notifications.addNotification user_id, @stubbedNotification, (err)=>
  44. @insertStub.calledWith(@stubbedNotification).should.equal true
  45. done()
  46. it 'should fail insert of existing notification key', (done)->
  47. @insertStub.callsArgWith(1, null)
  48. @countStub.callsArgWith(1, null, 1)
  49. @notifications.addNotification user_id, @stubbedNotification, (err)=>
  50. @insertStub.calledWith(@stubbedNotification).should.equal false
  51. done()
  52. describe 'removeNotificationId', ->
  53. it 'should mark the notification id as read', (done)->
  54. @updateStub.callsArgWith(2, null)
  55. @notifications.removeNotificationId user_id, notification_id, (err)=>
  56. searchOps =
  57. user_id:ObjectId(user_id)
  58. _id:ObjectId(notification_id)
  59. updateOperation =
  60. "$unset": {templateKey:true, messageOpts:true}
  61. @updateStub.calledWith(searchOps, updateOperation).should.equal true
  62. done()
  63. describe 'removeNotificationKey', ->
  64. it 'should mark the notification key as read', (done)->
  65. @updateStub.callsArgWith(2, null)
  66. @notifications.removeNotificationKey user_id, notification_key, (err)=>
  67. searchOps =
  68. user_id:ObjectId(user_id)
  69. key: notification_key
  70. updateOperation =
  71. "$unset": {templateKey:true}
  72. @updateStub.calledWith(searchOps, updateOperation).should.equal true
  73. done()