NotificationsHandlerTests.coffee 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 =
  13. post: sinon.stub().callsArgWith(1)
  14. del: sinon.stub().callsArgWith(1)
  15. get: sinon.stub()
  16. @handler = SandboxedModule.require modulePath, requires:
  17. "settings-sharelatex": apis:{notifications:{url:notificationUrl}}
  18. "request":@request
  19. 'logger-sharelatex':
  20. log:->
  21. err:->
  22. describe "getUserNotifications", ->
  23. it 'should get unread notifications', (done)->
  24. stubbedNotifications = [{_id: notification_id, user_id: user_id}]
  25. @request.get.callsArgWith(1, null, {statusCode:200}, stubbedNotifications)
  26. @handler.getUserNotifications user_id, (err, unreadNotifications)=>
  27. stubbedNotifications.should.deep.equal unreadNotifications
  28. getOpts =
  29. uri: "#{notificationUrl}/user/#{user_id}"
  30. json:true
  31. timeout:1000
  32. @request.get.calledWith(getOpts).should.equal true
  33. done()
  34. it 'should return empty arrays if there are no notifications', ->
  35. @request.get.callsArgWith(1, null, {statusCode:200}, null)
  36. @handler.getUserNotifications user_id, (err, unreadNotifications)=>
  37. unreadNotifications.length.should.equal 0
  38. describe "markAsRead", ->
  39. beforeEach ->
  40. @key = "some key here"
  41. it 'should send a delete request when a delete has been received to mark a notification', (done)->
  42. @handler.markAsReadWithKey user_id, @key, =>
  43. opts =
  44. uri: "#{notificationUrl}/user/#{user_id}"
  45. json:
  46. key:@key
  47. timeout:1000
  48. @request.del.calledWith(opts).should.equal true
  49. done()
  50. describe "createNotification", ->
  51. beforeEach ->
  52. @key = "some key here"
  53. @messageOpts = {value:12344}
  54. @templateKey = "renderThisHtml"
  55. it "should post the message over", (done)->
  56. @handler.createNotification user_id, @key, @templateKey, @messageOpts, =>
  57. args = @request.post.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}
  61. assert.deepEqual(args.json, expectedJson)
  62. done()