SudoModeControllerTests.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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 sinon = require('sinon')
  15. const should = require('chai').should()
  16. const { expect } = require('chai')
  17. const MockRequest = require('../helpers/MockRequest')
  18. const MockResponse = require('../helpers/MockResponse')
  19. const modulePath = '../../../../app/src/Features/SudoMode/SudoModeController'
  20. describe('SudoModeController', function() {
  21. beforeEach(function() {
  22. this.user = {
  23. _id: 'abcd',
  24. email: 'user@example.com'
  25. }
  26. this.UserGetter = { getUser: sinon.stub().callsArgWith(2, null, this.user) }
  27. this.SudoModeHandler = {
  28. authenticate: sinon.stub(),
  29. isSudoModeActive: sinon.stub(),
  30. activateSudoMode: sinon.stub()
  31. }
  32. this.AuthenticationController = {
  33. getLoggedInUserId: sinon.stub().returns(this.user._id),
  34. _getRediretFromSession: sinon.stub()
  35. }
  36. this.UserGetter = { getUser: sinon.stub() }
  37. return (this.SudoModeController = SandboxedModule.require(modulePath, {
  38. globals: {
  39. console: console
  40. },
  41. requires: {
  42. 'logger-sharelatex': {
  43. log: sinon.stub(),
  44. warn: sinon.stub(),
  45. err: sinon.stub()
  46. },
  47. './SudoModeHandler': this.SudoModeHandler,
  48. '../Authentication/AuthenticationController': this
  49. .AuthenticationController,
  50. '../../infrastructure/Mongoose': {
  51. mongo: {
  52. ObjectId() {
  53. return 'some_object_id'
  54. }
  55. }
  56. },
  57. '../User/UserGetter': this.UserGetter,
  58. 'settings-sharelatex': (this.Settings = {})
  59. }
  60. }))
  61. })
  62. describe('sudoModePrompt', function() {
  63. beforeEach(function() {
  64. this.SudoModeHandler.isSudoModeActive = sinon
  65. .stub()
  66. .callsArgWith(1, null, false)
  67. this.req = {
  68. externalAuthenticationSystemUsed: sinon.stub().returns(false)
  69. }
  70. this.res = { redirect: sinon.stub(), render: sinon.stub() }
  71. return (this.next = sinon.stub())
  72. })
  73. it('should get the logged in user id', function() {
  74. this.SudoModeController.sudoModePrompt(this.req, this.res, this.next)
  75. this.AuthenticationController.getLoggedInUserId.callCount.should.equal(1)
  76. return this.AuthenticationController.getLoggedInUserId
  77. .calledWith(this.req)
  78. .should.equal(true)
  79. })
  80. it('should check if sudo-mode is active', function() {
  81. this.SudoModeController.sudoModePrompt(this.req, this.res, this.next)
  82. this.SudoModeHandler.isSudoModeActive.callCount.should.equal(1)
  83. return this.SudoModeHandler.isSudoModeActive
  84. .calledWith(this.user._id)
  85. .should.equal(true)
  86. })
  87. it('should redirect when sudo-mode is active', function() {
  88. this.SudoModeHandler.isSudoModeActive = sinon
  89. .stub()
  90. .callsArgWith(1, null, true)
  91. this.SudoModeController.sudoModePrompt(this.req, this.res, this.next)
  92. this.res.redirect.callCount.should.equal(1)
  93. return this.res.redirect.calledWith('/project').should.equal(true)
  94. })
  95. it('should render the sudo_mode_prompt page when sudo mode is not active', function() {
  96. this.SudoModeHandler.isSudoModeActive = sinon
  97. .stub()
  98. .callsArgWith(1, null, false)
  99. this.SudoModeController.sudoModePrompt(this.req, this.res, this.next)
  100. this.res.render.callCount.should.equal(1)
  101. return this.res.render
  102. .calledWith('sudo_mode/sudo_mode_prompt')
  103. .should.equal(true)
  104. })
  105. describe('when isSudoModeActive produces an error', function() {
  106. beforeEach(function() {
  107. this.SudoModeHandler.isSudoModeActive = sinon
  108. .stub()
  109. .callsArgWith(1, new Error('woops'))
  110. return (this.next = sinon.stub())
  111. })
  112. it('should call next with an error', function() {
  113. this.SudoModeController.sudoModePrompt(this.req, this.res, this.next)
  114. this.next.callCount.should.equal(1)
  115. return expect(this.next.lastCall.args[0]).to.be.instanceof(Error)
  116. })
  117. it('should not render page', function() {
  118. this.SudoModeController.sudoModePrompt(this.req, this.res, this.next)
  119. return this.res.render.callCount.should.equal(0)
  120. })
  121. })
  122. describe('when external auth system is used', function() {
  123. beforeEach(function() {
  124. return (this.req.externalAuthenticationSystemUsed = sinon
  125. .stub()
  126. .returns(true))
  127. })
  128. it('should redirect', function() {
  129. this.SudoModeController.sudoModePrompt(this.req, this.res, this.next)
  130. this.res.redirect.callCount.should.equal(1)
  131. return this.res.redirect.calledWith('/project').should.equal(true)
  132. })
  133. it('should not check if sudo mode is active', function() {
  134. this.SudoModeController.sudoModePrompt(this.req, this.res, this.next)
  135. return this.SudoModeHandler.isSudoModeActive.callCount.should.equal(0)
  136. })
  137. it('should not render page', function() {
  138. this.SudoModeController.sudoModePrompt(this.req, this.res, this.next)
  139. return this.res.render.callCount.should.equal(0)
  140. })
  141. })
  142. })
  143. describe('submitPassword', function() {
  144. beforeEach(function() {
  145. this.AuthenticationController._getRedirectFromSession = sinon
  146. .stub()
  147. .returns('/somewhere')
  148. this.UserGetter.getUser = sinon.stub().callsArgWith(2, null, this.user)
  149. this.SudoModeHandler.authenticate = sinon
  150. .stub()
  151. .callsArgWith(2, null, this.user)
  152. this.SudoModeHandler.activateSudoMode = sinon.stub().callsArgWith(1, null)
  153. this.password = 'a_terrible_secret'
  154. this.req = { body: { password: this.password } }
  155. this.res = { json: sinon.stub() }
  156. return (this.next = sinon.stub())
  157. })
  158. describe('when all goes well', function() {
  159. beforeEach(function() {})
  160. it('should get the logged in user id', function() {
  161. this.SudoModeController.submitPassword(this.req, this.res, this.next)
  162. this.AuthenticationController.getLoggedInUserId.callCount.should.equal(
  163. 1
  164. )
  165. return this.AuthenticationController.getLoggedInUserId
  166. .calledWith(this.req)
  167. .should.equal(true)
  168. })
  169. it('should get redirect from session', function() {
  170. this.SudoModeController.submitPassword(this.req, this.res, this.next)
  171. this.AuthenticationController._getRedirectFromSession.callCount.should.equal(
  172. 1
  173. )
  174. return this.AuthenticationController._getRedirectFromSession
  175. .calledWith(this.req)
  176. .should.equal(true)
  177. })
  178. it('should get the user from storage', function() {
  179. this.SudoModeController.submitPassword(this.req, this.res, this.next)
  180. this.UserGetter.getUser.callCount.should.equal(1)
  181. return this.UserGetter.getUser
  182. .calledWith('some_object_id', { email: 1 })
  183. .should.equal(true)
  184. })
  185. it('should try to authenticate the user with the password', function() {
  186. this.SudoModeController.submitPassword(this.req, this.res, this.next)
  187. this.SudoModeHandler.authenticate.callCount.should.equal(1)
  188. return this.SudoModeHandler.authenticate
  189. .calledWith(this.user.email, this.password)
  190. .should.equal(true)
  191. })
  192. it('should activate sudo mode', function() {
  193. this.SudoModeController.submitPassword(this.req, this.res, this.next)
  194. this.SudoModeHandler.activateSudoMode.callCount.should.equal(1)
  195. return this.SudoModeHandler.activateSudoMode
  196. .calledWith(this.user._id)
  197. .should.equal(true)
  198. })
  199. it('should send back a json response', function() {
  200. this.SudoModeController.submitPassword(this.req, this.res, this.next)
  201. this.res.json.callCount.should.equal(1)
  202. return this.res.json
  203. .calledWith({ redir: '/somewhere' })
  204. .should.equal(true)
  205. })
  206. it('should not call next', function() {
  207. this.SudoModeController.submitPassword(this.req, this.res, this.next)
  208. return this.next.callCount.should.equal(0)
  209. })
  210. describe('when no password is supplied', function() {
  211. beforeEach(function() {
  212. this.req.body.password = ''
  213. return (this.next = sinon.stub())
  214. })
  215. it('should return next with an error', function() {
  216. this.SudoModeController.submitPassword(this.req, this.res, this.next)
  217. this.next.callCount.should.equal(1)
  218. return expect(this.next.lastCall.args[0]).to.be.instanceof(Error)
  219. })
  220. it('should not get the user from storage', function() {
  221. this.SudoModeController.submitPassword(this.req, this.res, this.next)
  222. return this.UserGetter.getUser.callCount.should.equal(0)
  223. })
  224. it('should not try to authenticate the user with the password', function() {
  225. this.SudoModeController.submitPassword(this.req, this.res, this.next)
  226. return this.SudoModeHandler.authenticate.callCount.should.equal(0)
  227. })
  228. it('should not activate sudo mode', function() {
  229. this.SudoModeController.submitPassword(this.req, this.res, this.next)
  230. return this.SudoModeHandler.activateSudoMode.callCount.should.equal(0)
  231. })
  232. it('should not send back a json response', function() {
  233. this.SudoModeController.submitPassword(this.req, this.res, this.next)
  234. return this.res.json.callCount.should.equal(0)
  235. })
  236. })
  237. describe('when getUser produces an error', function() {
  238. beforeEach(function() {
  239. this.UserGetter.getUser = sinon
  240. .stub()
  241. .callsArgWith(2, new Error('woops'))
  242. return (this.next = sinon.stub())
  243. })
  244. it('should return next with an error', function() {
  245. this.SudoModeController.submitPassword(this.req, this.res, this.next)
  246. this.next.callCount.should.equal(1)
  247. return expect(this.next.lastCall.args[0]).to.be.instanceof(Error)
  248. })
  249. it('should get the user from storage', function() {
  250. this.SudoModeController.submitPassword(this.req, this.res, this.next)
  251. this.UserGetter.getUser.callCount.should.equal(1)
  252. return this.UserGetter.getUser
  253. .calledWith('some_object_id', { email: 1 })
  254. .should.equal(true)
  255. })
  256. it('should not try to authenticate the user with the password', function() {
  257. this.SudoModeController.submitPassword(this.req, this.res, this.next)
  258. return this.SudoModeHandler.authenticate.callCount.should.equal(0)
  259. })
  260. it('should not activate sudo mode', function() {
  261. this.SudoModeController.submitPassword(this.req, this.res, this.next)
  262. return this.SudoModeHandler.activateSudoMode.callCount.should.equal(0)
  263. })
  264. it('should not send back a json response', function() {
  265. this.SudoModeController.submitPassword(this.req, this.res, this.next)
  266. return this.res.json.callCount.should.equal(0)
  267. })
  268. })
  269. describe('when getUser does not find a user', function() {
  270. beforeEach(function() {
  271. this.UserGetter.getUser = sinon.stub().callsArgWith(2, null, null)
  272. return (this.next = sinon.stub())
  273. })
  274. it('should return next with an error', function() {
  275. this.SudoModeController.submitPassword(this.req, this.res, this.next)
  276. this.next.callCount.should.equal(1)
  277. return expect(this.next.lastCall.args[0]).to.be.instanceof(Error)
  278. })
  279. it('should get the user from storage', function() {
  280. this.SudoModeController.submitPassword(this.req, this.res, this.next)
  281. this.UserGetter.getUser.callCount.should.equal(1)
  282. return this.UserGetter.getUser
  283. .calledWith('some_object_id', { email: 1 })
  284. .should.equal(true)
  285. })
  286. it('should not try to authenticate the user with the password', function() {
  287. this.SudoModeController.submitPassword(this.req, this.res, this.next)
  288. return this.SudoModeHandler.authenticate.callCount.should.equal(0)
  289. })
  290. it('should not activate sudo mode', function() {
  291. this.SudoModeController.submitPassword(this.req, this.res, this.next)
  292. return this.SudoModeHandler.activateSudoMode.callCount.should.equal(0)
  293. })
  294. it('should not send back a json response', function() {
  295. this.SudoModeController.submitPassword(this.req, this.res, this.next)
  296. return this.res.json.callCount.should.equal(0)
  297. })
  298. })
  299. describe('when authentication fails', function() {
  300. beforeEach(function() {
  301. this.SudoModeHandler.authenticate = sinon
  302. .stub()
  303. .callsArgWith(2, null, null)
  304. this.res.json = sinon.stub()
  305. return (this.req.i18n = { translate: sinon.stub() })
  306. })
  307. it('should send back a failure message', function() {
  308. this.SudoModeController.submitPassword(this.req, this.res, this.next)
  309. this.res.json.callCount.should.equal(1)
  310. expect(this.res.json.lastCall.args[0]).to.have.keys(['message'])
  311. expect(this.res.json.lastCall.args[0].message).to.have.keys([
  312. 'text',
  313. 'type'
  314. ])
  315. this.req.i18n.translate.callCount.should.equal(1)
  316. return this.req.i18n.translate.calledWith('invalid_password')
  317. })
  318. it('should get the user from storage', function() {
  319. this.SudoModeController.submitPassword(this.req, this.res, this.next)
  320. this.UserGetter.getUser.callCount.should.equal(1)
  321. return this.UserGetter.getUser
  322. .calledWith('some_object_id', { email: 1 })
  323. .should.equal(true)
  324. })
  325. it('should try to authenticate the user with the password', function() {
  326. this.SudoModeController.submitPassword(this.req, this.res, this.next)
  327. this.SudoModeHandler.authenticate.callCount.should.equal(1)
  328. return this.SudoModeHandler.authenticate
  329. .calledWith(this.user.email, this.password)
  330. .should.equal(true)
  331. })
  332. it('should not activate sudo mode', function() {
  333. this.SudoModeController.submitPassword(this.req, this.res, this.next)
  334. return this.SudoModeHandler.activateSudoMode.callCount.should.equal(0)
  335. })
  336. })
  337. describe('when authentication produces an error', function() {
  338. beforeEach(function() {
  339. this.SudoModeHandler.authenticate = sinon
  340. .stub()
  341. .callsArgWith(2, new Error('woops'))
  342. return (this.next = sinon.stub())
  343. })
  344. it('should return next with an error', function() {
  345. this.SudoModeController.submitPassword(this.req, this.res, this.next)
  346. this.next.callCount.should.equal(1)
  347. return expect(this.next.lastCall.args[0]).to.be.instanceof(Error)
  348. })
  349. it('should get the user from storage', function() {
  350. this.SudoModeController.submitPassword(this.req, this.res, this.next)
  351. this.UserGetter.getUser.callCount.should.equal(1)
  352. return this.UserGetter.getUser
  353. .calledWith('some_object_id', { email: 1 })
  354. .should.equal(true)
  355. })
  356. it('should try to authenticate the user with the password', function() {
  357. this.SudoModeController.submitPassword(this.req, this.res, this.next)
  358. this.SudoModeHandler.authenticate.callCount.should.equal(1)
  359. return this.SudoModeHandler.authenticate
  360. .calledWith(this.user.email, this.password)
  361. .should.equal(true)
  362. })
  363. it('should not activate sudo mode', function() {
  364. this.SudoModeController.submitPassword(this.req, this.res, this.next)
  365. return this.SudoModeHandler.activateSudoMode.callCount.should.equal(0)
  366. })
  367. })
  368. describe('when sudo mode activation produces an error', function() {
  369. beforeEach(function() {
  370. this.SudoModeHandler.activateSudoMode = sinon
  371. .stub()
  372. .callsArgWith(1, new Error('woops'))
  373. return (this.next = sinon.stub())
  374. })
  375. it('should return next with an error', function() {
  376. this.SudoModeController.submitPassword(this.req, this.res, this.next)
  377. this.next.callCount.should.equal(1)
  378. return expect(this.next.lastCall.args[0]).to.be.instanceof(Error)
  379. })
  380. it('should get the user from storage', function() {
  381. this.SudoModeController.submitPassword(this.req, this.res, this.next)
  382. this.UserGetter.getUser.callCount.should.equal(1)
  383. return this.UserGetter.getUser
  384. .calledWith('some_object_id', { email: 1 })
  385. .should.equal(true)
  386. })
  387. it('should try to authenticate the user with the password', function() {
  388. this.SudoModeController.submitPassword(this.req, this.res, this.next)
  389. this.SudoModeHandler.authenticate.callCount.should.equal(1)
  390. return this.SudoModeHandler.authenticate
  391. .calledWith(this.user.email, this.password)
  392. .should.equal(true)
  393. })
  394. it('should have tried to activate sudo mode', function() {
  395. this.SudoModeController.submitPassword(this.req, this.res, this.next)
  396. this.SudoModeHandler.activateSudoMode.callCount.should.equal(1)
  397. return this.SudoModeHandler.activateSudoMode
  398. .calledWith(this.user._id)
  399. .should.equal(true)
  400. })
  401. })
  402. })
  403. })
  404. })