NotificationsHandlerTests.coffee 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. SandboxedModule = require('sandboxed-module')
  2. assert = require('chai').assert
  3. require('chai').should()
  4. sinon = require('sinon')
  5. modulePath = require('path').join __dirname, '../../../../app/js/Features/Notifications/NotificationsHandler.js'
  6. _ = require('underscore')
  7. describe 'NotificationsHandler', ->
  8. user_id = "123nd3ijdks"
  9. notification_id = "123njdskj9jlk"
  10. notificationUrl = "notification.sharelatex.testing"
  11. beforeEach ->
  12. @request = sinon.stub().callsArgWith(1)
  13. @handler = SandboxedModule.require modulePath, requires:
  14. "settings-sharelatex": apis:{notifications:{url:notificationUrl}}
  15. "request":@request
  16. 'logger-sharelatex':
  17. log:->
  18. err:->
  19. describe "getUserNotifications", ->
  20. it 'should get unread notifications', (done)->
  21. stubbedNotifications = [{_id: notification_id, user_id: user_id}]
  22. @request.callsArgWith(1, null, {statusCode:200}, stubbedNotifications)
  23. @handler.getUserNotifications user_id, (err, unreadNotifications)=>
  24. stubbedNotifications.should.deep.equal unreadNotifications
  25. getOpts =
  26. uri: "#{notificationUrl}/user/#{user_id}"
  27. json:true
  28. timeout:1000
  29. method: "GET"
  30. @request.calledWith(getOpts).should.equal true
  31. done()
  32. it 'should return empty arrays if there are no notifications', ->
  33. @request.callsArgWith(1, null, {statusCode:200}, null)
  34. @handler.getUserNotifications user_id, (err, unreadNotifications)=>
  35. unreadNotifications.length.should.equal 0
  36. describe "markAsRead", ->
  37. beforeEach ->
  38. @key = "some key here"
  39. it 'should send a delete request when a delete has been received to mark a notification', (done)->
  40. @handler.markAsReadWithKey user_id, @key, =>
  41. opts =
  42. uri: "#{notificationUrl}/user/#{user_id}"
  43. json:
  44. key:@key
  45. timeout:1000
  46. method: "DELETE"
  47. @request.calledWith(opts).should.equal true
  48. done()
  49. describe "createNotification", ->
  50. beforeEach ->
  51. @key = "some key here"
  52. @messageOpts = {value:12344}
  53. @templateKey = "renderThisHtml"
  54. @expiry = null
  55. it "should post the message over", (done)->
  56. @handler.createNotification user_id, @key, @templateKey, @messageOpts, @expiry, =>
  57. args = @request.args[0][0]
  58. args.uri.should.equal "#{notificationUrl}/user/#{user_id}"
  59. args.timeout.should.equal 1000
  60. expectedJson = {key:@key, templateKey:@templateKey, messageOpts:@messageOpts, forceCreate:true}
  61. assert.deepEqual(args.json, expectedJson)
  62. done()
  63. describe 'when expiry date is supplied', ->
  64. beforeEach ->
  65. @key = "some key here"
  66. @messageOpts = {value:12344}
  67. @templateKey = "renderThisHtml"
  68. @expiry = new Date()
  69. it 'should post the message over with expiry field', (done) ->
  70. @handler.createNotification user_id, @key, @templateKey, @messageOpts, @expiry, =>
  71. args = @request.args[0][0]
  72. args.uri.should.equal "#{notificationUrl}/user/#{user_id}"
  73. args.timeout.should.equal 1000
  74. expectedJson = {key:@key, templateKey:@templateKey, messageOpts:@messageOpts, expires: @expiry, forceCreate:true}
  75. assert.deepEqual(args.json, expectedJson)
  76. done()
  77. describe "markAsReadByKeyOnly", ->
  78. beforeEach ->
  79. @key = "some key here"
  80. it 'should send a delete request when a delete has been received to mark a notification', (done)->
  81. @handler.markAsReadByKeyOnly @key, =>
  82. opts =
  83. uri: "#{notificationUrl}/key/#{@key}"
  84. timeout:1000
  85. method: "DELETE"
  86. @request.calledWith(opts).should.equal true
  87. done()