SudoModeMiddlewareTests.coffee 4.5 KB

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