NotificationsControllerTests.coffee 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. @settings =
  16. apis: { v1: { url: 'v1.url', user: '', pass: '' } }
  17. @req =
  18. headers: {}
  19. connection:
  20. remoteAddress: "192.170.18.1"
  21. params:
  22. notification_id:notification_id
  23. session:
  24. user:
  25. _id:user_id
  26. i18n:
  27. translate:->
  28. @AuthenticationController =
  29. getLoggedInUserId: sinon.stub().returns(@req.session.user._id)
  30. @controller = SandboxedModule.require modulePath, requires:
  31. "./NotificationsHandler":@handler
  32. "./NotificationsBuilder":@builder
  33. "settings-sharelatex":@settings
  34. "underscore":@underscore =
  35. map:(arr)-> return arr
  36. 'logger-sharelatex':
  37. log:->
  38. err:->
  39. '../Authentication/AuthenticationController': @AuthenticationController
  40. it 'should ask the handler for all unread notifications', (done)->
  41. allNotifications = [{_id: notification_id, user_id: user_id}]
  42. @handler.getUserNotifications = sinon.stub().callsArgWith(1, null, allNotifications)
  43. @controller.getAllUnreadNotifications @req, send:(body)=>
  44. body.should.equal allNotifications
  45. @handler.getUserNotifications.calledWith(user_id).should.equal true
  46. done()
  47. it 'should send a delete request when a delete has been received to mark a notification', (done)->
  48. @controller.markNotificationAsRead @req, send:=>
  49. @handler.markAsRead.calledWith(user_id, notification_id).should.equal true
  50. done()
  51. it 'should call the builder with the user ip if v2', (done)->
  52. @settings.overleaf = true
  53. @controller.getAllUnreadNotifications @req, send:(body)=>
  54. @builder.ipMatcherAffiliation.calledWith(user_id, @req.connection.remoteAddress).should.equal true
  55. done()