UserControllerTests.js 25 KB

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