SudoModeMiddlewareTests.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const SandboxedModule = require('sandboxed-module')
  14. const assert = require('assert')
  15. require('chai').should()
  16. const { expect } = require('chai')
  17. const sinon = require('sinon')
  18. const modulePath = require('path').join(
  19. __dirname,
  20. '../../../../app/src/Features/SudoMode/SudoModeMiddleware'
  21. )
  22. describe('SudoModeMiddleware', function() {
  23. beforeEach(function() {
  24. this.userId = 'some_user_id'
  25. this.SudoModeHandler = { isSudoModeActive: sinon.stub() }
  26. this.AuthenticationController = {
  27. getLoggedInUserId: sinon.stub().returns(this.userId),
  28. setRedirectInSession: sinon.stub()
  29. }
  30. return (this.SudoModeMiddleware = SandboxedModule.require(modulePath, {
  31. globals: {
  32. console: console
  33. },
  34. requires: {
  35. './SudoModeHandler': this.SudoModeHandler,
  36. '../Authentication/AuthenticationController': this
  37. .AuthenticationController,
  38. 'logger-sharelatex': {
  39. log: sinon.stub(),
  40. warn: sinon.stub(),
  41. err: sinon.stub()
  42. },
  43. 'settings-sharelatex': (this.Settings = {})
  44. }
  45. }))
  46. })
  47. describe('protectPage', function() {
  48. beforeEach(function() {
  49. this.externalAuth = false
  50. return (this.call = cb => {
  51. this.req = {
  52. externalAuthenticationSystemUsed: sinon
  53. .stub()
  54. .returns(this.externalAuth)
  55. }
  56. this.res = { redirect: sinon.stub() }
  57. this.next = sinon.stub()
  58. this.SudoModeMiddleware.protectPage(this.req, this.res, this.next)
  59. return cb()
  60. })
  61. })
  62. describe('when sudo mode is active', function() {
  63. beforeEach(function() {
  64. this.AuthenticationController.getLoggedInUserId = sinon
  65. .stub()
  66. .returns(this.userId)
  67. return (this.SudoModeHandler.isSudoModeActive = sinon
  68. .stub()
  69. .callsArgWith(1, null, true))
  70. })
  71. it('should get the current user id', function(done) {
  72. return this.call(() => {
  73. this.AuthenticationController.getLoggedInUserId.callCount.should.equal(
  74. 1
  75. )
  76. return done()
  77. })
  78. })
  79. it('should check if sudo-mode is active', function(done) {
  80. return this.call(() => {
  81. this.SudoModeHandler.isSudoModeActive.callCount.should.equal(1)
  82. this.SudoModeHandler.isSudoModeActive
  83. .calledWith(this.userId)
  84. .should.equal(true)
  85. return done()
  86. })
  87. })
  88. it('should call next', function(done) {
  89. return this.call(() => {
  90. this.next.callCount.should.equal(1)
  91. expect(this.next.lastCall.args[0]).to.equal(undefined)
  92. return done()
  93. })
  94. })
  95. })
  96. describe('when sudo mode is not active', function() {
  97. beforeEach(function() {
  98. this.AuthenticationController.setRedirectInSession = sinon.stub()
  99. this.AuthenticationController.getLoggedInUserId = sinon
  100. .stub()
  101. .returns(this.userId)
  102. return (this.SudoModeHandler.isSudoModeActive = sinon
  103. .stub()
  104. .callsArgWith(1, null, false))
  105. })
  106. it('should get the current user id', function(done) {
  107. return this.call(() => {
  108. this.AuthenticationController.getLoggedInUserId.callCount.should.equal(
  109. 1
  110. )
  111. return done()
  112. })
  113. })
  114. it('should check if sudo-mode is active', function(done) {
  115. return this.call(() => {
  116. this.SudoModeHandler.isSudoModeActive.callCount.should.equal(1)
  117. this.SudoModeHandler.isSudoModeActive
  118. .calledWith(this.userId)
  119. .should.equal(true)
  120. return done()
  121. })
  122. })
  123. it('should set redirect in session', function(done) {
  124. return this.call(() => {
  125. this.AuthenticationController.setRedirectInSession.callCount.should.equal(
  126. 1
  127. )
  128. this.AuthenticationController.setRedirectInSession
  129. .calledWith(this.req)
  130. .should.equal(true)
  131. return done()
  132. })
  133. })
  134. it('should redirect to the password-prompt page', function(done) {
  135. return this.call(() => {
  136. this.res.redirect.callCount.should.equal(1)
  137. this.res.redirect.calledWith('/confirm-password').should.equal(true)
  138. return done()
  139. })
  140. })
  141. })
  142. describe('when isSudoModeActive produces an error', function() {
  143. beforeEach(function() {
  144. this.AuthenticationController.getLoggedInUserId = sinon
  145. .stub()
  146. .returns(this.userId)
  147. return (this.SudoModeHandler.isSudoModeActive = sinon
  148. .stub()
  149. .callsArgWith(1, new Error('woops')))
  150. })
  151. it('should get the current user id', function(done) {
  152. return this.call(() => {
  153. this.AuthenticationController.getLoggedInUserId.callCount.should.equal(
  154. 1
  155. )
  156. return done()
  157. })
  158. })
  159. it('should check if sudo-mode is active', function(done) {
  160. return this.call(() => {
  161. this.SudoModeHandler.isSudoModeActive.callCount.should.equal(1)
  162. this.SudoModeHandler.isSudoModeActive
  163. .calledWith(this.userId)
  164. .should.equal(true)
  165. return done()
  166. })
  167. })
  168. it('should call next with an error', function(done) {
  169. return this.call(() => {
  170. this.next.callCount.should.equal(1)
  171. expect(this.next.lastCall.args[0]).to.be.instanceof(Error)
  172. return done()
  173. })
  174. })
  175. })
  176. describe('when external auth is being used', function() {
  177. beforeEach(function() {
  178. this.externalAuth = true
  179. return (this.call = cb => {
  180. this.req = {
  181. externalAuthenticationSystemUsed: sinon
  182. .stub()
  183. .returns(this.externalAuth)
  184. }
  185. this.res = { redirect: sinon.stub() }
  186. this.next = sinon.stub()
  187. this.SudoModeMiddleware.protectPage(this.req, this.res, this.next)
  188. return cb()
  189. })
  190. })
  191. it('should immediately return next with no args', function(done) {
  192. return this.call(() => {
  193. this.next.callCount.should.equal(1)
  194. expect(this.next.lastCall.args[0]).to.not.exist
  195. return done()
  196. })
  197. })
  198. it('should not get the current user id', function(done) {
  199. return this.call(() => {
  200. this.AuthenticationController.getLoggedInUserId.callCount.should.equal(
  201. 0
  202. )
  203. return done()
  204. })
  205. })
  206. it('should not check if sudo-mode is active', function(done) {
  207. return this.call(() => {
  208. this.SudoModeHandler.isSudoModeActive.callCount.should.equal(0)
  209. return done()
  210. })
  211. })
  212. })
  213. })
  214. })