UserControllerTests.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. const sinon = require('sinon')
  2. const chai = require('chai')
  3. const { expect } = chai
  4. const modulePath = '../../../../app/src/Features/User/UserController.js'
  5. const SandboxedModule = require('sandboxed-module')
  6. const OError = require('@overleaf/o-error')
  7. const Errors = require('../../../../app/src/Features/Errors/Errors')
  8. const HttpErrors = require('@overleaf/o-error/http')
  9. describe('UserController', function() {
  10. beforeEach(function() {
  11. this.user_id = '323123'
  12. this.user = {
  13. _id: this.user_id,
  14. save: sinon.stub().callsArgWith(0),
  15. ace: {}
  16. }
  17. this.req = {
  18. user: {},
  19. session: {
  20. destroy() {},
  21. user: {
  22. _id: this.user_id,
  23. email: 'old@something.com'
  24. }
  25. },
  26. sessionID: '123',
  27. body: {},
  28. i18n: {
  29. translate: text => text
  30. },
  31. query: {}
  32. }
  33. this.UserDeleter = { deleteUser: sinon.stub().yields() }
  34. this.UserGetter = {
  35. getUser: sinon.stub().callsArgWith(1, null, this.user),
  36. promises: { getUser: sinon.stub().resolves(this.user) }
  37. }
  38. this.User = { findById: sinon.stub().callsArgWith(1, null, this.user) }
  39. this.NewsLetterManager = { unsubscribe: sinon.stub().callsArgWith(1) }
  40. this.UserRegistrationHandler = { registerNewUser: sinon.stub() }
  41. this.AuthenticationController = {
  42. establishUserSession: sinon.stub().callsArg(2),
  43. getLoggedInUserId: sinon.stub().returns(this.user._id),
  44. getSessionUser: sinon.stub().returns(this.req.session.user),
  45. setInSessionUser: sinon.stub()
  46. }
  47. this.AuthenticationManager = {
  48. authenticate: sinon.stub(),
  49. setUserPassword: sinon.stub(),
  50. validatePassword: sinon.stub()
  51. }
  52. this.ReferalAllocator = { allocate: sinon.stub() }
  53. this.SubscriptionDomainHandler = { autoAllocate: sinon.stub() }
  54. this.UserUpdater = {
  55. changeEmailAddress: sinon.stub(),
  56. promises: {
  57. confirmEmail: sinon.stub().resolves(),
  58. addAffiliationForNewUser: sinon.stub().resolves()
  59. }
  60. }
  61. this.settings = { siteUrl: 'sharelatex.example.com' }
  62. this.UserHandler = { populateTeamInvites: sinon.stub().callsArgWith(1) }
  63. this.UserSessionsManager = {
  64. trackSession: sinon.stub(),
  65. untrackSession: sinon.stub(),
  66. revokeAllUserSessions: sinon.stub().callsArgWith(2, null),
  67. promises: {
  68. getAllUserSessions: sinon.stub().resolves(),
  69. revokeAllUserSessions: sinon.stub().resolves()
  70. }
  71. }
  72. this.SudoModeHandler = { clearSudoMode: sinon.stub() }
  73. this.HttpErrorHandler = {
  74. conflict: sinon.stub(),
  75. unprocessableEntity: sinon.stub(),
  76. legacyInternal: sinon.stub()
  77. }
  78. this.UserController = SandboxedModule.require(modulePath, {
  79. globals: {
  80. console: console
  81. },
  82. requires: {
  83. './UserGetter': this.UserGetter,
  84. './UserDeleter': this.UserDeleter,
  85. './UserUpdater': this.UserUpdater,
  86. '../../models/User': {
  87. User: this.User
  88. },
  89. '../Newsletter/NewsletterManager': this.NewsLetterManager,
  90. './UserRegistrationHandler': this.UserRegistrationHandler,
  91. '../Authentication/AuthenticationController': this
  92. .AuthenticationController,
  93. '../Authentication/AuthenticationManager': this.AuthenticationManager,
  94. '../../infrastructure/Features': (this.Features = {
  95. hasFeature: sinon.stub()
  96. }),
  97. '../Referal/ReferalAllocator': this.ReferalAllocator,
  98. '../Subscription/SubscriptionDomainHandler': this
  99. .SubscriptionDomainHandler,
  100. './UserAuditLogHandler': (this.UserAuditLogHandler = {
  101. promises: {
  102. addEntry: sinon.stub().resolves()
  103. }
  104. }),
  105. './UserHandler': this.UserHandler,
  106. './UserSessionsManager': this.UserSessionsManager,
  107. '../SudoMode/SudoModeHandler': this.SudoModeHandler,
  108. '../Errors/HttpErrorHandler': this.HttpErrorHandler,
  109. 'settings-sharelatex': this.settings,
  110. 'logger-sharelatex': {
  111. log() {},
  112. warn() {},
  113. err() {},
  114. error() {}
  115. },
  116. 'metrics-sharelatex': {
  117. inc() {}
  118. },
  119. '../Errors/Errors': Errors,
  120. '@overleaf/o-error': OError,
  121. '@overleaf/o-error/http': HttpErrors,
  122. '../Email/EmailHandler': { sendEmail: sinon.stub() }
  123. }
  124. })
  125. this.res = {
  126. send: sinon.stub(),
  127. status: sinon.stub(),
  128. sendStatus: sinon.stub(),
  129. json: sinon.stub()
  130. }
  131. this.res.status.returns(this.res)
  132. this.next = sinon.stub()
  133. this.callback = sinon.stub()
  134. })
  135. describe('tryDeleteUser', function() {
  136. beforeEach(function() {
  137. this.req.body.password = 'wat'
  138. this.req.logout = sinon.stub()
  139. this.req.session.destroy = sinon.stub().callsArgWith(0, null)
  140. this.AuthenticationController.getLoggedInUserId = sinon
  141. .stub()
  142. .returns(this.user._id)
  143. this.AuthenticationManager.authenticate = sinon
  144. .stub()
  145. .callsArgWith(2, null, this.user)
  146. })
  147. it('should send 200', function(done) {
  148. this.res.sendStatus = code => {
  149. code.should.equal(200)
  150. done()
  151. }
  152. this.UserController.tryDeleteUser(this.req, this.res, this.next)
  153. })
  154. it('should try to authenticate user', function(done) {
  155. this.res.sendStatus = code => {
  156. this.AuthenticationManager.authenticate.callCount.should.equal(1)
  157. this.AuthenticationManager.authenticate
  158. .calledWith({ _id: this.user._id }, this.req.body.password)
  159. .should.equal(true)
  160. done()
  161. }
  162. this.UserController.tryDeleteUser(this.req, this.res, this.next)
  163. })
  164. it('should delete the user', function(done) {
  165. this.res.sendStatus = code => {
  166. this.UserDeleter.deleteUser.callCount.should.equal(1)
  167. this.UserDeleter.deleteUser.calledWith(this.user._id).should.equal(true)
  168. done()
  169. }
  170. this.UserController.tryDeleteUser(this.req, this.res, this.next)
  171. })
  172. describe('when no password is supplied', function() {
  173. beforeEach(function() {
  174. this.req.body.password = ''
  175. })
  176. it('should return 403', function(done) {
  177. this.res.sendStatus = code => {
  178. code.should.equal(403)
  179. done()
  180. }
  181. this.UserController.tryDeleteUser(this.req, this.res, this.next)
  182. })
  183. })
  184. describe('when authenticate produces an error', function() {
  185. beforeEach(function() {
  186. this.AuthenticationManager.authenticate = sinon
  187. .stub()
  188. .callsArgWith(2, new Error('woops'))
  189. })
  190. it('should call next with an error', function(done) {
  191. this.next = err => {
  192. expect(err).to.not.equal(null)
  193. expect(err).to.be.instanceof(Error)
  194. done()
  195. }
  196. this.UserController.tryDeleteUser(this.req, this.res, this.next)
  197. })
  198. })
  199. describe('when authenticate does not produce a user', function() {
  200. beforeEach(function() {
  201. this.AuthenticationManager.authenticate = sinon
  202. .stub()
  203. .callsArgWith(2, null, null)
  204. })
  205. it('should return 403', function(done) {
  206. this.res.sendStatus = code => {
  207. code.should.equal(403)
  208. done()
  209. }
  210. this.UserController.tryDeleteUser(this.req, this.res, this.next)
  211. })
  212. })
  213. describe('when deleteUser produces an error', function() {
  214. beforeEach(function() {
  215. this.UserDeleter.deleteUser = sinon.stub().yields(new Error('woops'))
  216. })
  217. it('should call next with an error', function(done) {
  218. this.next = err => {
  219. expect(err).to.not.equal(null)
  220. expect(err).to.be.instanceof(Error)
  221. done()
  222. }
  223. this.UserController.tryDeleteUser(this.req, this.res, this.next)
  224. })
  225. })
  226. describe('when deleteUser produces a known error', function() {
  227. beforeEach(function() {
  228. this.UserDeleter.deleteUser = sinon
  229. .stub()
  230. .yields(new Errors.SubscriptionAdminDeletionError())
  231. })
  232. it('should return a HTTP Unprocessable Entity error', function(done) {
  233. this.HttpErrorHandler.unprocessableEntity = sinon.spy(
  234. (req, res, message, info) => {
  235. expect(req).to.exist
  236. expect(res).to.exist
  237. expect(message).to.equal('error while deleting user account')
  238. expect(info).to.deep.equal({
  239. error: 'SubscriptionAdminDeletionError'
  240. })
  241. done()
  242. }
  243. )
  244. this.UserController.tryDeleteUser(this.req, this.res)
  245. })
  246. })
  247. describe('when session.destroy produces an error', function() {
  248. beforeEach(function() {
  249. this.req.session.destroy = sinon
  250. .stub()
  251. .callsArgWith(0, new Error('woops'))
  252. })
  253. it('should call next with an error', function(done) {
  254. this.next = err => {
  255. expect(err).to.not.equal(null)
  256. expect(err).to.be.instanceof(Error)
  257. done()
  258. }
  259. this.UserController.tryDeleteUser(this.req, this.res, this.next)
  260. })
  261. })
  262. })
  263. describe('unsubscribe', function() {
  264. it('should send the user to unsubscribe', function(done) {
  265. this.res.sendStatus = () => {
  266. this.NewsLetterManager.unsubscribe
  267. .calledWith(this.user)
  268. .should.equal(true)
  269. done()
  270. }
  271. this.UserController.unsubscribe(this.req, this.res)
  272. })
  273. })
  274. describe('updateUserSettings', function() {
  275. beforeEach(function() {
  276. this.newEmail = 'hello@world.com'
  277. this.req.externalAuthenticationSystemUsed = sinon.stub().returns(false)
  278. })
  279. it('should call save', function(done) {
  280. this.req.body = {}
  281. this.res.sendStatus = code => {
  282. this.user.save.called.should.equal(true)
  283. done()
  284. }
  285. this.UserController.updateUserSettings(this.req, this.res)
  286. })
  287. it('should set the first name', function(done) {
  288. this.req.body = { first_name: 'bobby ' }
  289. this.res.sendStatus = code => {
  290. this.user.first_name.should.equal('bobby')
  291. done()
  292. }
  293. this.UserController.updateUserSettings(this.req, this.res)
  294. })
  295. it('should set the role', function(done) {
  296. this.req.body = { role: 'student' }
  297. this.res.sendStatus = code => {
  298. this.user.role.should.equal('student')
  299. done()
  300. }
  301. this.UserController.updateUserSettings(this.req, this.res)
  302. })
  303. it('should set the institution', function(done) {
  304. this.req.body = { institution: 'MIT' }
  305. this.res.sendStatus = code => {
  306. this.user.institution.should.equal('MIT')
  307. done()
  308. }
  309. this.UserController.updateUserSettings(this.req, this.res)
  310. })
  311. it('should set some props on ace', function(done) {
  312. this.req.body = { editorTheme: 'something' }
  313. this.res.sendStatus = code => {
  314. this.user.ace.theme.should.equal('something')
  315. done()
  316. }
  317. this.UserController.updateUserSettings(this.req, this.res)
  318. })
  319. it('should set the overall theme', function(done) {
  320. this.req.body = { overallTheme: 'green-ish' }
  321. this.res.sendStatus = code => {
  322. this.user.ace.overallTheme.should.equal('green-ish')
  323. done()
  324. }
  325. this.UserController.updateUserSettings(this.req, this.res)
  326. })
  327. it('should send an error if the email is 0 len', function(done) {
  328. this.req.body.email = ''
  329. this.res.sendStatus = function(code) {
  330. code.should.equal(400)
  331. done()
  332. }
  333. this.UserController.updateUserSettings(this.req, this.res)
  334. })
  335. it('should send an error if the email does not contain an @', function(done) {
  336. this.req.body.email = 'bob at something dot com'
  337. this.res.sendStatus = function(code) {
  338. code.should.equal(400)
  339. done()
  340. }
  341. this.UserController.updateUserSettings(this.req, this.res)
  342. })
  343. it('should call the user updater with the new email and user _id', function(done) {
  344. this.req.body.email = this.newEmail.toUpperCase()
  345. this.UserUpdater.changeEmailAddress.callsArgWith(2)
  346. this.res.sendStatus = code => {
  347. code.should.equal(200)
  348. this.UserUpdater.changeEmailAddress
  349. .calledWith(this.user_id, this.newEmail)
  350. .should.equal(true)
  351. done()
  352. }
  353. this.UserController.updateUserSettings(this.req, this.res)
  354. })
  355. it('should update the email on the session', function(done) {
  356. this.req.body.email = this.newEmail.toUpperCase()
  357. this.UserUpdater.changeEmailAddress.callsArgWith(2)
  358. let callcount = 0
  359. this.User.findById = (id, cb) => {
  360. if (++callcount === 2) {
  361. this.user.email = this.newEmail
  362. }
  363. cb(null, this.user)
  364. }
  365. this.res.sendStatus = code => {
  366. code.should.equal(200)
  367. this.AuthenticationController.setInSessionUser
  368. .calledWith(this.req, {
  369. email: this.newEmail,
  370. first_name: undefined,
  371. last_name: undefined
  372. })
  373. .should.equal(true)
  374. done()
  375. }
  376. this.UserController.updateUserSettings(this.req, this.res)
  377. })
  378. it('should call populateTeamInvites', function(done) {
  379. this.req.body.email = this.newEmail.toUpperCase()
  380. this.UserUpdater.changeEmailAddress.callsArgWith(2)
  381. this.res.sendStatus = code => {
  382. code.should.equal(200)
  383. this.UserHandler.populateTeamInvites
  384. .calledWith(this.user)
  385. .should.equal(true)
  386. done()
  387. }
  388. this.UserController.updateUserSettings(this.req, this.res)
  389. })
  390. describe('when changeEmailAddress yields an error', function() {
  391. it('should pass on an error and not send a success status', function(done) {
  392. this.req.body.email = this.newEmail.toUpperCase()
  393. this.UserUpdater.changeEmailAddress.callsArgWith(2, new Error())
  394. this.HttpErrorHandler.legacyInternal = sinon.spy(
  395. (req, res, message, error) => {
  396. expect(req).to.exist
  397. expect(req).to.exist
  398. message.should.equal('problem_changing_email_address')
  399. expect(error).to.be.instanceof(OError)
  400. done()
  401. }
  402. )
  403. this.UserController.updateUserSettings(this.req, this.res, this.next)
  404. })
  405. it('should call the HTTP conflict error handler when the email already exists', function(done) {
  406. this.HttpErrorHandler.conflict = sinon.spy((req, res, message) => {
  407. expect(req).to.exist
  408. expect(req).to.exist
  409. message.should.equal('email_already_registered')
  410. done()
  411. })
  412. this.req.body.email = this.newEmail.toUpperCase()
  413. this.UserUpdater.changeEmailAddress.callsArgWith(
  414. 2,
  415. new Errors.EmailExistsError()
  416. )
  417. this.UserController.updateUserSettings(this.req, this.res)
  418. })
  419. })
  420. describe('when using an external auth source', function() {
  421. beforeEach(function() {
  422. this.UserUpdater.changeEmailAddress.callsArgWith(2)
  423. this.newEmail = 'someone23@example.com'
  424. this.req.externalAuthenticationSystemUsed = sinon.stub().returns(true)
  425. })
  426. it('should not set a new email', function(done) {
  427. this.req.body.email = this.newEmail
  428. this.res.sendStatus = code => {
  429. code.should.equal(200)
  430. this.UserUpdater.changeEmailAddress
  431. .calledWith(this.user_id, this.newEmail)
  432. .should.equal(false)
  433. done()
  434. }
  435. this.UserController.updateUserSettings(this.req, this.res)
  436. })
  437. })
  438. })
  439. describe('logout', function() {
  440. it('should destroy the session', function(done) {
  441. this.req.session.destroy = sinon.stub().callsArgWith(0)
  442. this.res.redirect = url => {
  443. url.should.equal('/login')
  444. this.req.session.destroy.called.should.equal(true)
  445. done()
  446. }
  447. this.UserController.logout(this.req, this.res)
  448. })
  449. it('should clear sudo-mode', function(done) {
  450. this.req.session.destroy = sinon.stub().callsArgWith(0)
  451. this.SudoModeHandler.clearSudoMode = sinon.stub()
  452. this.res.redirect = url => {
  453. url.should.equal('/login')
  454. this.SudoModeHandler.clearSudoMode.callCount.should.equal(1)
  455. this.SudoModeHandler.clearSudoMode
  456. .calledWith(this.user._id)
  457. .should.equal(true)
  458. done()
  459. }
  460. this.UserController.logout(this.req, this.res)
  461. })
  462. it('should untrack session', function(done) {
  463. this.req.session.destroy = sinon.stub().callsArgWith(0)
  464. this.SudoModeHandler.clearSudoMode = sinon.stub()
  465. this.res.redirect = url => {
  466. url.should.equal('/login')
  467. this.UserSessionsManager.untrackSession.callCount.should.equal(1)
  468. this.UserSessionsManager.untrackSession
  469. .calledWith(sinon.match(this.req.user), this.req.sessionID)
  470. .should.equal(true)
  471. done()
  472. }
  473. this.UserController.logout(this.req, this.res)
  474. })
  475. it('should redirect after logout', function(done) {
  476. this.req.body.redirect = '/institutional-login'
  477. this.req.session.destroy = sinon.stub().callsArgWith(0)
  478. this.SudoModeHandler.clearSudoMode = sinon.stub()
  479. this.res.redirect = url => {
  480. url.should.equal(this.req.body.redirect)
  481. done()
  482. }
  483. this.UserController.logout(this.req, this.res)
  484. })
  485. it('should redirect to login after logout when no redirect set', function(done) {
  486. this.req.session.destroy = sinon.stub().callsArgWith(0)
  487. this.SudoModeHandler.clearSudoMode = sinon.stub()
  488. this.res.redirect = url => {
  489. url.should.equal('/login')
  490. done()
  491. }
  492. this.UserController.logout(this.req, this.res)
  493. })
  494. })
  495. describe('register', function() {
  496. beforeEach(function() {
  497. this.UserRegistrationHandler.registerNewUserAndSendActivationEmail = sinon
  498. .stub()
  499. .callsArgWith(1, null, this.user, (this.url = 'mock/url'))
  500. this.req.body.email = this.user.email = this.email = 'email@example.com'
  501. this.UserController.register(this.req, this.res)
  502. })
  503. it('should register the user and send them an email', function() {
  504. this.UserRegistrationHandler.registerNewUserAndSendActivationEmail
  505. .calledWith(this.email)
  506. .should.equal(true)
  507. })
  508. it('should return the user and activation url', function() {
  509. this.res.json
  510. .calledWith({
  511. email: this.email,
  512. setNewPasswordUrl: this.url
  513. })
  514. .should.equal(true)
  515. })
  516. })
  517. describe('clearSessions', function() {
  518. it('should call revokeAllUserSessions', function(done) {
  519. this.res.sendStatus.callsFake(() => {
  520. this.UserSessionsManager.promises.revokeAllUserSessions.callCount.should.equal(
  521. 1
  522. )
  523. done()
  524. })
  525. this.UserController.clearSessions(this.req, this.res)
  526. })
  527. it('send a 201 response', function(done) {
  528. this.res.sendStatus.callsFake(status => {
  529. status.should.equal(201)
  530. done()
  531. })
  532. this.UserController.clearSessions(this.req, this.res, () => {
  533. done()
  534. })
  535. })
  536. describe('when getAllUserSessions produces an error', function() {
  537. it('should return an error', function(done) {
  538. this.UserSessionsManager.promises.getAllUserSessions.rejects(
  539. new Error('woops')
  540. )
  541. this.UserController.clearSessions(this.req, this.res, error => {
  542. expect(error).to.be.instanceof(Error)
  543. done()
  544. })
  545. })
  546. })
  547. describe('when audit log addEntry produces an error', function() {
  548. it('should call next with an error', function(done) {
  549. this.UserAuditLogHandler.promises.addEntry.rejects(new Error('woops'))
  550. this.UserController.clearSessions(this.req, this.res, error => {
  551. expect(error).to.be.instanceof(Error)
  552. done()
  553. })
  554. })
  555. })
  556. describe('when revokeAllUserSessions produces an error', function() {
  557. it('should call next with an error', function(done) {
  558. this.UserSessionsManager.promises.revokeAllUserSessions.rejects(
  559. new Error('woops')
  560. )
  561. this.UserController.clearSessions(this.req, this.res, error => {
  562. expect(error).to.be.instanceof(Error)
  563. done()
  564. })
  565. })
  566. })
  567. })
  568. describe('changePassword', function() {
  569. it('should check the old password is the current one at the moment', function() {
  570. this.AuthenticationManager.authenticate.yields()
  571. this.req.body = { currentPassword: 'oldpasshere' }
  572. this.UserController.changePassword(this.req, this.res, this.callback)
  573. this.AuthenticationManager.authenticate.should.have.been.calledWith(
  574. { _id: this.user._id },
  575. 'oldpasshere'
  576. )
  577. this.AuthenticationManager.setUserPassword.callCount.should.equal(0)
  578. })
  579. it('it should not set the new password if they do not match', function() {
  580. this.AuthenticationManager.authenticate.yields(null, {})
  581. this.req.body = {
  582. newPassword1: '1',
  583. newPassword2: '2'
  584. }
  585. this.UserController.changePassword(this.req, this.res, this.callback)
  586. this.res.status.should.have.been.calledWith(400)
  587. this.AuthenticationManager.setUserPassword.callCount.should.equal(0)
  588. })
  589. it('should set the new password if they do match', function() {
  590. this.AuthenticationManager.authenticate.yields(null, this.user)
  591. this.AuthenticationManager.setUserPassword.yields()
  592. this.req.body = {
  593. newPassword1: 'newpass',
  594. newPassword2: 'newpass'
  595. }
  596. this.UserController.changePassword(this.req, this.res, this.callback)
  597. this.AuthenticationManager.setUserPassword.should.have.been.calledWith(
  598. this.user._id,
  599. 'newpass'
  600. )
  601. })
  602. it('it should not set the new password if it is invalid', function() {
  603. this.AuthenticationManager.validatePassword = sinon
  604. .stub()
  605. .returns({ message: 'validation-error' })
  606. this.AuthenticationManager.authenticate.yields(null, {})
  607. this.req.body = {
  608. newPassword1: 'newpass',
  609. newPassword2: 'newpass'
  610. }
  611. this.UserController.changePassword(this.req, this.res, this.callback)
  612. this.AuthenticationManager.setUserPassword.callCount.should.equal(0)
  613. this.res.status.should.have.been.calledWith(400)
  614. this.res.json.should.have.been.calledWith({
  615. message: {
  616. type: 'error',
  617. text: 'validation-error'
  618. }
  619. })
  620. })
  621. })
  622. describe('ensureAffiliationMiddleware', function() {
  623. describe('without affiliations feature', function() {
  624. beforeEach(async function() {
  625. await this.UserController.promises.ensureAffiliationMiddleware(
  626. this.req,
  627. this.res,
  628. this.next
  629. )
  630. })
  631. it('should not run affiliation check', function() {
  632. expect(this.UserGetter.promises.getUser).to.not.have.been.called
  633. expect(this.UserUpdater.promises.confirmEmail).to.not.have.been.called
  634. expect(this.UserUpdater.promises.addAffiliationForNewUser).to.not.have
  635. .been.called
  636. })
  637. it('should not return an error', function() {
  638. expect(this.next).to.be.calledWith()
  639. })
  640. })
  641. describe('without ensureAffiliation query parameter', function() {
  642. beforeEach(async function() {
  643. this.Features.hasFeature.withArgs('affiliations').returns(true)
  644. await this.UserController.promises.ensureAffiliationMiddleware(
  645. this.req,
  646. this.res,
  647. this.next
  648. )
  649. })
  650. it('should not run middleware', function() {
  651. expect(this.UserGetter.promises.getUser).to.not.have.been.called
  652. expect(this.UserUpdater.promises.confirmEmail).to.not.have.been.called
  653. expect(this.UserUpdater.promises.addAffiliationForNewUser).to.not.have
  654. .been.called
  655. })
  656. it('should not return an error', function() {
  657. expect(this.next).to.be.calledWith()
  658. })
  659. })
  660. describe('no flagged email', function() {
  661. beforeEach(async function() {
  662. const email = 'unit-test@overleaf.com'
  663. this.user.email = email
  664. this.user.emails = [
  665. {
  666. email
  667. }
  668. ]
  669. this.Features.hasFeature.withArgs('affiliations').returns(true)
  670. this.req.query.ensureAffiliation = true
  671. await this.UserController.promises.ensureAffiliationMiddleware(
  672. this.req,
  673. this.res,
  674. this.next
  675. )
  676. })
  677. it('should get the user', function() {
  678. expect(this.UserGetter.promises.getUser).to.have.been.calledWith(
  679. this.user._id
  680. )
  681. })
  682. it('should not try to add affiliation or update user', function() {
  683. expect(this.UserUpdater.promises.addAffiliationForNewUser).to.not.have
  684. .been.called
  685. })
  686. it('should not return an error', function() {
  687. expect(this.next).to.be.calledWith()
  688. })
  689. })
  690. describe('flagged non-SSO email', function() {
  691. let emailFlagged
  692. beforeEach(async function() {
  693. emailFlagged = 'flagged@overleaf.com'
  694. this.user.email = emailFlagged
  695. this.user.emails = [
  696. {
  697. email: emailFlagged,
  698. affiliationUnchecked: true
  699. }
  700. ]
  701. this.Features.hasFeature.withArgs('affiliations').returns(true)
  702. this.req.query.ensureAffiliation = true
  703. await this.UserController.promises.ensureAffiliationMiddleware(
  704. this.req,
  705. this.res,
  706. this.next
  707. )
  708. })
  709. it('should unflag the emails but not confirm', function() {
  710. expect(
  711. this.UserUpdater.promises.addAffiliationForNewUser
  712. ).to.have.been.calledWith(this.user._id, emailFlagged)
  713. expect(
  714. this.UserUpdater.promises.confirmEmail
  715. ).to.not.have.been.calledWith(this.user._id, emailFlagged)
  716. })
  717. it('should not return an error', function() {
  718. expect(this.next).to.be.calledWith()
  719. })
  720. })
  721. describe('flagged SSO email', function() {
  722. let emailFlagged
  723. beforeEach(async function() {
  724. emailFlagged = 'flagged@overleaf.com'
  725. this.user.email = emailFlagged
  726. this.user.emails = [
  727. {
  728. email: emailFlagged,
  729. affiliationUnchecked: true,
  730. samlProviderId: '123'
  731. }
  732. ]
  733. this.Features.hasFeature.withArgs('affiliations').returns(true)
  734. this.req.query.ensureAffiliation = true
  735. await this.UserController.promises.ensureAffiliationMiddleware(
  736. this.req,
  737. this.res,
  738. this.next
  739. )
  740. })
  741. it('should add affiliation to v1, unflag and confirm on v2', function() {
  742. expect(this.UserUpdater.promises.addAffiliationForNewUser).to.have.not
  743. .been.called
  744. expect(this.UserUpdater.promises.confirmEmail).to.have.been.calledWith(
  745. this.user._id,
  746. emailFlagged
  747. )
  748. })
  749. it('should not return an error', function() {
  750. expect(this.next).to.be.calledWith()
  751. })
  752. })
  753. describe('when v1 returns an error', function() {
  754. let emailFlagged
  755. beforeEach(async function() {
  756. this.UserUpdater.promises.addAffiliationForNewUser.rejects()
  757. emailFlagged = 'flagged@overleaf.com'
  758. this.user.email = emailFlagged
  759. this.user.emails = [
  760. {
  761. email: emailFlagged,
  762. affiliationUnchecked: true
  763. }
  764. ]
  765. this.Features.hasFeature.withArgs('affiliations').returns(true)
  766. this.req.query.ensureAffiliation = true
  767. await this.UserController.promises.ensureAffiliationMiddleware(
  768. this.req,
  769. this.res,
  770. this.next
  771. )
  772. })
  773. it('should return the error', function() {
  774. expect(this.next).to.be.calledWith(sinon.match.instanceOf(Error))
  775. })
  776. })
  777. })
  778. })