NotificationsTests.coffee 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. expect = chai.should
  4. should = chai.should()
  5. modulePath = "../../../app/js/Notifications.js"
  6. SandboxedModule = require('sandboxed-module')
  7. assert = require('assert')
  8. ObjectId = require("mongojs").ObjectId
  9. user_id = "51dc93e6fb625a261300003b"
  10. notification_id = "fb625a26f09d"
  11. notification_key = "notification-key"
  12. describe 'Notifications Tests', ->
  13. beforeEach ->
  14. self = @
  15. @findStub = sinon.stub()
  16. @insertStub = sinon.stub()
  17. @countStub = sinon.stub()
  18. @updateStub = sinon.stub()
  19. @removeStub = sinon.stub()
  20. @mongojs = =>
  21. notifications:
  22. update: self.mongojsUpdate
  23. find: @findStub
  24. insert: @insertStub
  25. count: @countStub
  26. update: @updateStub
  27. remove: @removeStub
  28. @mongojs.ObjectId = ObjectId
  29. @notifications = SandboxedModule.require modulePath, requires:
  30. 'logger-sharelatex': {
  31. log:()->
  32. error:()->
  33. }
  34. 'settings-sharelatex': {}
  35. 'mongojs':@mongojs
  36. 'metrics-sharelatex': {timeAsyncMethod: sinon.stub()}
  37. @stubbedNotification = {user_id: ObjectId(user_id), key:"notification-key", messageOpts:"some info", templateKey:"template-key"}
  38. @stubbedNotificationArray = [@stubbedNotification]
  39. describe 'getUserNotifications', ->
  40. it "should find all notifications and return i", (done)->
  41. @findStub.callsArgWith(1, null, @stubbedNotificationArray)
  42. @notifications.getUserNotifications user_id, (err, notifications)=>
  43. notifications.should.equal @stubbedNotificationArray
  44. assert.deepEqual(@findStub.args[0][0], {"user_id" :ObjectId(user_id), "templateKey": {"$exists":true}})
  45. done()
  46. describe 'addNotification', ->
  47. beforeEach ->
  48. @stubbedNotification = {
  49. user_id: ObjectId(user_id),
  50. key:"notification-key",
  51. messageOpts:"some info",
  52. templateKey:"template-key"
  53. }
  54. @expectedDocument = {
  55. user_id: @stubbedNotification.user_id,
  56. key:"notification-key",
  57. messageOpts:"some info",
  58. templateKey:"template-key"
  59. }
  60. @notifications.deleteNotificationByKeyOnly = sinon.stub()
  61. @notifications.deleteNotificationByKeyOnly.callsArgWith(1, null)
  62. it 'should insert the notification into the collection', (done)->
  63. @insertStub.callsArgWith(1, null)
  64. @countStub.callsArgWith(1, null, 0)
  65. @notifications.addNotification user_id, @stubbedNotification, (err)=>
  66. assert.deepEqual(@insertStub.lastCall.args[0], @expectedDocument)
  67. done()
  68. it 'should fail insert of existing notification key', (done)->
  69. @insertStub.callsArgWith(1, null)
  70. @countStub.callsArgWith(1, null, 1)
  71. @notifications.addNotification user_id, @stubbedNotification, (err)=>
  72. @insertStub.calledWith(@expectedDocument).should.equal false
  73. done()
  74. describe "when key already exists but forceCreate is passed", (done)->
  75. it "should delete the old key and insert the new one", (done) ->
  76. @insertStub.callsArgWith(1, null)
  77. @countStub.callsArgWith(1, null, 1)
  78. @stubbedNotification.forceCreate = true
  79. @notifications.addNotification user_id, @stubbedNotification, (err)=>
  80. assert.deepEqual(@insertStub.lastCall.args[0], @expectedDocument)
  81. @notifications.deleteNotificationByKeyOnly.calledWith(@stubbedNotification.key).should.equal true
  82. done()
  83. describe 'when the notification is set to expire', () ->
  84. beforeEach ->
  85. @stubbedNotification = {
  86. user_id: ObjectId(user_id),
  87. key:"notification-key",
  88. messageOpts:"some info",
  89. templateKey:"template-key",
  90. expires: '2922-02-13T09:32:56.289Z'
  91. }
  92. @expectedDocument = {
  93. user_id: @stubbedNotification.user_id,
  94. key:"notification-key",
  95. messageOpts:"some info",
  96. templateKey:"template-key",
  97. expires: new Date(@stubbedNotification.expires),
  98. }
  99. it 'should add an `expires` Date field to the document', (done)->
  100. @insertStub.callsArgWith(1, null)
  101. @countStub.callsArgWith(1, null, 0)
  102. @notifications.addNotification user_id, @stubbedNotification, (err)=>
  103. @insertStub.callCount.should.equal 1
  104. assert.deepEqual(@insertStub.lastCall.args[0], @expectedDocument)
  105. done()
  106. describe 'when the notification has a nonsensical expires field', () ->
  107. beforeEach ->
  108. @stubbedNotification = {
  109. user_id: ObjectId(user_id),
  110. key:"notification-key",
  111. messageOpts:"some info",
  112. templateKey:"template-key",
  113. expires: 'WAT'
  114. }
  115. @expectedDocument = {
  116. user_id: @stubbedNotification.user_id,
  117. key:"notification-key",
  118. messageOpts:"some info",
  119. templateKey:"template-key",
  120. expires: new Date(@stubbedNotification.expires),
  121. }
  122. it 'should produce an error', (done)->
  123. @insertStub.callsArgWith(1, null)
  124. @countStub.callsArgWith(1, null, 0)
  125. @notifications.addNotification user_id, @stubbedNotification, (err)=>
  126. (err instanceof Error).should.equal true
  127. @insertStub.callCount.should.equal 0
  128. @insertStub.calledWith(@expectedDocument).should.equal false
  129. done()
  130. describe 'removeNotificationId', ->
  131. it 'should mark the notification id as read', (done)->
  132. @updateStub.callsArgWith(2, null)
  133. @notifications.removeNotificationId user_id, notification_id, (err)=>
  134. searchOps =
  135. user_id:ObjectId(user_id)
  136. _id:ObjectId(notification_id)
  137. updateOperation =
  138. "$unset": {templateKey:true, messageOpts:true}
  139. assert.deepEqual(@updateStub.args[0][0], searchOps)
  140. assert.deepEqual(@updateStub.args[0][1], updateOperation)
  141. done()
  142. describe 'removeNotificationKey', ->
  143. it 'should mark the notification key as read', (done)->
  144. @updateStub.callsArgWith(2, null)
  145. @notifications.removeNotificationKey user_id, notification_key, (err)=>
  146. searchOps = {
  147. user_id:ObjectId(user_id)
  148. key: notification_key
  149. }
  150. updateOperation = {
  151. "$unset": {templateKey:true}
  152. }
  153. assert.deepEqual(@updateStub.args[0][0], searchOps)
  154. assert.deepEqual(@updateStub.args[0][1], updateOperation)
  155. done()
  156. describe 'removeNotificationByKeyOnly', ->
  157. it 'should mark the notification key as read', (done)->
  158. @updateStub.callsArgWith(2, null)
  159. @notifications.removeNotificationByKeyOnly notification_key, (err)=>
  160. searchOps =
  161. key: notification_key
  162. updateOperation =
  163. "$unset": {templateKey:true}
  164. assert.deepEqual(@updateStub.args[0][0], searchOps)
  165. assert.deepEqual(@updateStub.args[0][1], updateOperation)
  166. done()
  167. describe 'deleteNotificationByKeyOnly', ->
  168. it 'should completely remove the notification', (done)->
  169. @removeStub.callsArgWith(2, null)
  170. @notifications.deleteNotificationByKeyOnly notification_key, (err)=>
  171. searchOps =
  172. key: notification_key
  173. opts =
  174. justOne: true
  175. assert.deepEqual(@removeStub.args[0][0], searchOps)
  176. assert.deepEqual(@removeStub.args[0][1], opts)
  177. done()