NotificationsControllerTests.coffee 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. SandboxedModule = require('sandboxed-module')
  2. assert = require('assert')
  3. require('chai').should()
  4. sinon = require('sinon')
  5. modulePath = require('path').join __dirname, '../../../../app/js/Features/Notifications/NotificationsController.js'
  6. describe 'NotificationsController', ->
  7. user_id = "123nd3ijdks"
  8. notification_id = "123njdskj9jlk"
  9. beforeEach ->
  10. @handler =
  11. getUserNotifications: sinon.stub().callsArgWith(1)
  12. markAsRead: sinon.stub().callsArgWith(2)
  13. @builder =
  14. ipMatcherAffiliation: sinon.stub().returns({create: sinon.stub()})
  15. @userGetter =
  16. getUser: sinon.stub().callsArgWith 2, null, {lastLoginIp: '192.170.18.2'}
  17. @settings =
  18. apis: { v1: { url: 'v1.url', user: '', pass: '' } }
  19. @req =
  20. headers: {}
  21. connection:
  22. remoteAddress: "192.170.18.1"
  23. params:
  24. notification_id:notification_id
  25. session:
  26. user:
  27. _id:user_id
  28. i18n:
  29. translate:->
  30. @AuthenticationController =
  31. getLoggedInUserId: sinon.stub().returns(@req.session.user._id)
  32. @controller = SandboxedModule.require modulePath, requires:
  33. "./NotificationsHandler":@handler
  34. "./NotificationsBuilder":@builder
  35. "../User/UserGetter": @userGetter
  36. "settings-sharelatex":@settings
  37. "underscore":@underscore =
  38. map:(arr)-> return arr
  39. 'logger-sharelatex':
  40. log:->
  41. err:->
  42. '../Authentication/AuthenticationController': @AuthenticationController
  43. it 'should ask the handler for all unread notifications', (done)->
  44. allNotifications = [{_id: notification_id, user_id: user_id}]
  45. @handler.getUserNotifications = sinon.stub().callsArgWith(1, null, allNotifications)
  46. @controller.getAllUnreadNotifications @req, send:(body)=>
  47. body.should.equal allNotifications
  48. @handler.getUserNotifications.calledWith(user_id).should.equal true
  49. done()
  50. it 'should send a remove request when notification read', (done)->
  51. @controller.markNotificationAsRead @req, send:=>
  52. @handler.markAsRead.calledWith(user_id, notification_id).should.equal true
  53. done()
  54. it 'should call the builder with the user ip if v2', (done)->
  55. @settings.overleaf = true
  56. @controller.getAllUnreadNotifications @req, send:(body)=>
  57. @builder.ipMatcherAffiliation.calledWith(user_id, @req.connection.remoteAddress).should.equal true
  58. done()