NotificationsHandlerTests.coffee 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. it "should post the message over", (done)->
  55. @handler.createNotification user_id, @key, @templateKey, @messageOpts, =>
  56. args = @request.args[0][0]
  57. args.uri.should.equal "#{notificationUrl}/user/#{user_id}"
  58. args.timeout.should.equal 1000
  59. expectedJson = {key:@key, templateKey:@templateKey, messageOpts:@messageOpts}
  60. assert.deepEqual(args.json, expectedJson)
  61. done()