SudoModeMiddlewearTests.coffee 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. SandboxedModule = require('sandboxed-module')
  2. assert = require('assert')
  3. require('chai').should()
  4. expect = require('chai').expect
  5. sinon = require('sinon')
  6. modulePath = require('path').join __dirname, '../../../../app/js/Features/SudoMode/SudoModeMiddlewear'
  7. describe 'SudoModeMiddlewear', ->
  8. beforeEach ->
  9. @userId = 'some_user_id'
  10. @SudoModeHandler =
  11. isSudoModeActive: sinon.stub()
  12. @AuthenticationController =
  13. getLoggedInUserId: sinon.stub().returns(@userId)
  14. _setRedirectInSession: sinon.stub()
  15. @SudoModeMiddlewear = SandboxedModule.require modulePath, requires:
  16. './SudoModeHandler': @SudoModeHandler
  17. '../Authentication/AuthenticationController': @AuthenticationController
  18. 'logger-sharelatex': {log: sinon.stub(), err: sinon.stub()}
  19. describe 'protectPage', ->
  20. beforeEach ->
  21. @externalAuth = false
  22. @call = (cb) =>
  23. @req = {externalAuthenticationSystemUsed: sinon.stub().returns(@externalAuth)}
  24. @res = {redirect: sinon.stub()}
  25. @next = sinon.stub()
  26. @SudoModeMiddlewear.protectPage @req, @res, @next
  27. cb()
  28. describe 'when sudo mode is active', ->
  29. beforeEach ->
  30. @AuthenticationController.getLoggedInUserId = sinon.stub().returns(@userId)
  31. @SudoModeHandler.isSudoModeActive = sinon.stub().callsArgWith(1, null, true)
  32. it 'should get the current user id', (done) ->
  33. @call () =>
  34. @AuthenticationController.getLoggedInUserId.callCount.should.equal 1
  35. done()
  36. it 'should check if sudo-mode is active', (done) ->
  37. @call () =>
  38. @SudoModeHandler.isSudoModeActive.callCount.should.equal 1
  39. @SudoModeHandler.isSudoModeActive.calledWith(@userId).should.equal true
  40. done()
  41. it 'should call next', (done) ->
  42. @call () =>
  43. @next.callCount.should.equal 1
  44. expect(@next.lastCall.args[0]).to.equal undefined
  45. done()
  46. describe 'when sudo mode is not active', ->
  47. beforeEach ->
  48. @AuthenticationController._setRedirectInSession = sinon.stub()
  49. @AuthenticationController.getLoggedInUserId = sinon.stub().returns(@userId)
  50. @SudoModeHandler.isSudoModeActive = sinon.stub().callsArgWith(1, null, false)
  51. it 'should get the current user id', (done) ->
  52. @call () =>
  53. @AuthenticationController.getLoggedInUserId.callCount.should.equal 1
  54. done()
  55. it 'should check if sudo-mode is active', (done) ->
  56. @call () =>
  57. @SudoModeHandler.isSudoModeActive.callCount.should.equal 1
  58. @SudoModeHandler.isSudoModeActive.calledWith(@userId).should.equal true
  59. done()
  60. it 'should set redirect in session', (done) ->
  61. @call () =>
  62. @AuthenticationController._setRedirectInSession.callCount.should.equal 1
  63. @AuthenticationController._setRedirectInSession.calledWith(@req).should.equal true
  64. done()
  65. it 'should redirect to the password-prompt page', (done) ->
  66. @call () =>
  67. @res.redirect.callCount.should.equal 1
  68. @res.redirect.calledWith('/confirm-password').should.equal true
  69. done()
  70. describe 'when isSudoModeActive produces an error', ->
  71. beforeEach ->
  72. @AuthenticationController.getLoggedInUserId = sinon.stub().returns(@userId)
  73. @SudoModeHandler.isSudoModeActive = sinon.stub().callsArgWith(1, new Error('woops'))
  74. it 'should get the current user id', (done) ->
  75. @call () =>
  76. @AuthenticationController.getLoggedInUserId.callCount.should.equal 1
  77. done()
  78. it 'should check if sudo-mode is active', (done) ->
  79. @call () =>
  80. @SudoModeHandler.isSudoModeActive.callCount.should.equal 1
  81. @SudoModeHandler.isSudoModeActive.calledWith(@userId).should.equal true
  82. done()
  83. it 'should call next with an error', (done) ->
  84. @call () =>
  85. @next.callCount.should.equal 1
  86. expect(@next.lastCall.args[0]).to.be.instanceof Error
  87. done()
  88. describe 'when external auth is being used', ->
  89. beforeEach ->
  90. @externalAuth = true
  91. it 'should immediately return next with no args', (done) ->
  92. @call () =>
  93. @next.callCount.should.equal 1
  94. expect(@next.lastCall.args[0]).to.not.exist
  95. done()
  96. it 'should not get the current user id', (done) ->
  97. @call () =>
  98. @AuthenticationController.getLoggedInUserId.callCount.should.equal 0
  99. done()
  100. it 'should not check if sudo-mode is active', (done) ->
  101. @call () =>
  102. @SudoModeHandler.isSudoModeActive.callCount.should.equal 0
  103. done()