UserGetterTests.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  1. const { ObjectId } = require('mongodb')
  2. const SandboxedModule = require('sandboxed-module')
  3. const assert = require('assert')
  4. const moment = require('moment')
  5. const path = require('path')
  6. const sinon = require('sinon')
  7. const modulePath = path.join(
  8. __dirname,
  9. '../../../../app/src/Features/User/UserGetter'
  10. )
  11. const { expect } = require('chai')
  12. const Errors = require('../../../../app/src/Features/Errors/Errors')
  13. const {
  14. normalizeQuery,
  15. normalizeMultiQuery,
  16. } = require('../../../../app/src/Features/Helpers/Mongo')
  17. describe('UserGetter', function () {
  18. beforeEach(function () {
  19. this.fakeUser = {
  20. _id: '12390i',
  21. email: 'email2@foo.bar',
  22. emails: [
  23. {
  24. email: 'email1@foo.bar',
  25. reversedHostname: 'rab.oof',
  26. confirmedAt: new Date(),
  27. },
  28. { email: 'email2@foo.bar', reversedHostname: 'rab.oof' },
  29. ],
  30. }
  31. this.findOne = sinon.stub().callsArgWith(2, null, this.fakeUser)
  32. this.findToArrayStub = sinon.stub().yields(null, [this.fakeUser])
  33. this.find = sinon.stub().returns({ toArray: this.findToArrayStub })
  34. this.Mongo = {
  35. db: {
  36. users: {
  37. findOne: this.findOne,
  38. find: this.find,
  39. },
  40. },
  41. ObjectId,
  42. }
  43. this.getUserAffiliations = sinon.stub().resolves([])
  44. this.UserGetter = SandboxedModule.require(modulePath, {
  45. requires: {
  46. '../Helpers/Mongo': { normalizeQuery, normalizeMultiQuery },
  47. '../../infrastructure/mongodb': this.Mongo,
  48. '@overleaf/metrics': {
  49. timeAsyncMethod: sinon.stub(),
  50. },
  51. 'settings-sharelatex': (this.settings = {
  52. reconfirmNotificationDays: 14,
  53. }),
  54. '../Institutions/InstitutionsAPI': {
  55. promises: {
  56. getUserAffiliations: this.getUserAffiliations,
  57. },
  58. },
  59. '../../infrastructure/Features': {
  60. hasFeature: sinon.stub().returns(true),
  61. },
  62. },
  63. })
  64. })
  65. describe('getUser', function () {
  66. it('should get user', function (done) {
  67. const query = { _id: '000000000000000000000000' }
  68. const projection = { email: 1 }
  69. this.UserGetter.getUser(query, projection, (error, user) => {
  70. expect(error).to.not.exist
  71. this.findOne.called.should.equal(true)
  72. this.findOne.calledWith(query, { projection }).should.equal(true)
  73. user.should.deep.equal(this.fakeUser)
  74. done()
  75. })
  76. })
  77. it('should not allow null query', function (done) {
  78. this.UserGetter.getUser(null, {}, error => {
  79. error.should.exist
  80. error.message.should.equal('no query provided')
  81. done()
  82. })
  83. })
  84. })
  85. describe('getUsers', function () {
  86. it('should get users with array of userIds', function (done) {
  87. const query = [new ObjectId()]
  88. const projection = { email: 1 }
  89. this.UserGetter.getUsers(query, projection, (error, users) => {
  90. expect(error).to.not.exist
  91. this.find.should.have.been.calledWithMatch(
  92. { _id: { $in: query } },
  93. { projection }
  94. )
  95. users.should.deep.equal([this.fakeUser])
  96. done()
  97. })
  98. })
  99. it('should not allow null query', function (done) {
  100. this.UserGetter.getUser(null, {}, error => {
  101. error.should.exist
  102. error.message.should.equal('no query provided')
  103. done()
  104. })
  105. })
  106. })
  107. describe('getUserFullEmails', function () {
  108. it('should get user', function (done) {
  109. this.UserGetter.promises.getUser = sinon.stub().resolves(this.fakeUser)
  110. const projection = { email: 1, emails: 1, samlIdentifiers: 1 }
  111. this.UserGetter.getUserFullEmails(
  112. this.fakeUser._id,
  113. (error, fullEmails) => {
  114. expect(error).to.not.exist
  115. this.UserGetter.promises.getUser.called.should.equal(true)
  116. this.UserGetter.promises.getUser
  117. .calledWith(this.fakeUser._id, projection)
  118. .should.equal(true)
  119. done()
  120. }
  121. )
  122. })
  123. it('should fetch emails data', function (done) {
  124. this.UserGetter.promises.getUser = sinon.stub().resolves(this.fakeUser)
  125. this.UserGetter.getUserFullEmails(
  126. this.fakeUser._id,
  127. (error, fullEmails) => {
  128. expect(error).to.not.exist
  129. assert.deepEqual(fullEmails, [
  130. {
  131. email: 'email1@foo.bar',
  132. reversedHostname: 'rab.oof',
  133. confirmedAt: this.fakeUser.emails[0].confirmedAt,
  134. emailHasInstitutionLicence: false,
  135. default: false,
  136. },
  137. {
  138. email: 'email2@foo.bar',
  139. reversedHostname: 'rab.oof',
  140. emailHasInstitutionLicence: false,
  141. default: true,
  142. },
  143. ])
  144. done()
  145. }
  146. )
  147. })
  148. it('should merge affiliation data', function (done) {
  149. this.UserGetter.promises.getUser = sinon.stub().resolves(this.fakeUser)
  150. const affiliationsData = [
  151. {
  152. email: 'email1@foo.bar',
  153. role: 'Prof',
  154. department: 'Maths',
  155. inferred: false,
  156. licence: 'pro_plus',
  157. institution: {
  158. name: 'University Name',
  159. isUniversity: true,
  160. confirmed: true,
  161. },
  162. portal: undefined,
  163. },
  164. ]
  165. this.getUserAffiliations.resolves(affiliationsData)
  166. this.UserGetter.getUserFullEmails(
  167. this.fakeUser._id,
  168. (error, fullEmails) => {
  169. expect(error).to.not.exist
  170. assert.deepEqual(fullEmails, [
  171. {
  172. email: 'email1@foo.bar',
  173. reversedHostname: 'rab.oof',
  174. confirmedAt: this.fakeUser.emails[0].confirmedAt,
  175. default: false,
  176. emailHasInstitutionLicence: true,
  177. affiliation: {
  178. institution: affiliationsData[0].institution,
  179. inferred: affiliationsData[0].inferred,
  180. department: affiliationsData[0].department,
  181. role: affiliationsData[0].role,
  182. lastDayToReconfirm: undefined,
  183. licence: affiliationsData[0].licence,
  184. inReconfirmNotificationPeriod: false,
  185. pastReconfirmDate: false,
  186. portal: undefined,
  187. },
  188. },
  189. {
  190. email: 'email2@foo.bar',
  191. reversedHostname: 'rab.oof',
  192. emailHasInstitutionLicence: false,
  193. default: true,
  194. },
  195. ])
  196. done()
  197. }
  198. )
  199. })
  200. it('should merge SAML identifier', function (done) {
  201. const fakeSamlIdentifiers = [
  202. { providerId: 'saml_id', exteranlUserId: 'whatever' },
  203. ]
  204. const fakeUserWithSaml = this.fakeUser
  205. fakeUserWithSaml.emails[0].samlProviderId = 'saml_id'
  206. fakeUserWithSaml.samlIdentifiers = fakeSamlIdentifiers
  207. this.UserGetter.promises.getUser = sinon.stub().resolves(this.fakeUser)
  208. this.getUserAffiliations.resolves([])
  209. this.UserGetter.getUserFullEmails(
  210. this.fakeUser._id,
  211. (error, fullEmails) => {
  212. expect(error).to.not.exist
  213. assert.deepEqual(fullEmails, [
  214. {
  215. email: 'email1@foo.bar',
  216. reversedHostname: 'rab.oof',
  217. confirmedAt: this.fakeUser.emails[0].confirmedAt,
  218. default: false,
  219. emailHasInstitutionLicence: false,
  220. samlProviderId: 'saml_id',
  221. samlIdentifier: fakeSamlIdentifiers[0],
  222. },
  223. {
  224. email: 'email2@foo.bar',
  225. reversedHostname: 'rab.oof',
  226. emailHasInstitutionLicence: false,
  227. default: true,
  228. },
  229. ])
  230. done()
  231. }
  232. )
  233. })
  234. it('should get user when it has no emails field', function (done) {
  235. this.fakeUser = {
  236. _id: '12390i',
  237. email: 'email2@foo.bar',
  238. }
  239. this.UserGetter.promises.getUser = sinon.stub().resolves(this.fakeUser)
  240. const projection = { email: 1, emails: 1, samlIdentifiers: 1 }
  241. this.UserGetter.getUserFullEmails(
  242. this.fakeUser._id,
  243. (error, fullEmails) => {
  244. expect(error).to.not.exist
  245. this.UserGetter.promises.getUser.called.should.equal(true)
  246. this.UserGetter.promises.getUser
  247. .calledWith(this.fakeUser._id, projection)
  248. .should.equal(true)
  249. assert.deepEqual(fullEmails, [])
  250. done()
  251. }
  252. )
  253. })
  254. describe('affiliation reconfirmation', function () {
  255. const institutionNonSSO = {
  256. id: 1,
  257. name: 'University Name',
  258. commonsAccount: true,
  259. isUniversity: true,
  260. confirmed: true,
  261. ssoBeta: false,
  262. ssoEnabled: false,
  263. maxConfirmationMonths: 12,
  264. }
  265. const institutionSSO = {
  266. id: 2,
  267. name: 'SSO University Name',
  268. isUniversity: true,
  269. confirmed: true,
  270. ssoBeta: false,
  271. ssoEnabled: true,
  272. maxConfirmationMonths: 12,
  273. }
  274. describe('non-SSO institutions', function () {
  275. const email1 = 'leonard@example-affiliation.com'
  276. const email2 = 'mccoy@example-affiliation.com'
  277. const affiliationsData = [
  278. {
  279. email: email1,
  280. role: 'Prof',
  281. department: 'Medicine',
  282. inferred: false,
  283. licence: 'pro_plus',
  284. institution: institutionNonSSO,
  285. },
  286. {
  287. email: email2,
  288. role: 'Prof',
  289. department: 'Medicine',
  290. inferred: false,
  291. licence: 'pro_plus',
  292. institution: institutionNonSSO,
  293. },
  294. ]
  295. it('should flag inReconfirmNotificationPeriod for all affiliations in period', function (done) {
  296. const user = {
  297. _id: '12390i',
  298. email: email1,
  299. emails: [
  300. {
  301. email: email1,
  302. reversedHostname: 'moc.noitailiffa-elpmaxe',
  303. confirmedAt: moment()
  304. .subtract(
  305. institutionNonSSO.maxConfirmationMonths + 2,
  306. 'months'
  307. )
  308. .toDate(),
  309. default: true,
  310. },
  311. {
  312. email: email2,
  313. reversedHostname: 'moc.noitailiffa-elpmaxe',
  314. confirmedAt: moment()
  315. .subtract(
  316. institutionNonSSO.maxConfirmationMonths + 1,
  317. 'months'
  318. )
  319. .toDate(),
  320. },
  321. ],
  322. }
  323. this.getUserAffiliations.resolves(affiliationsData)
  324. this.UserGetter.promises.getUser = sinon.stub().resolves(user)
  325. this.UserGetter.getUserFullEmails(
  326. this.fakeUser._id,
  327. (error, fullEmails) => {
  328. expect(error).to.not.exist
  329. expect(
  330. fullEmails[0].affiliation.inReconfirmNotificationPeriod
  331. ).to.equal(true)
  332. expect(
  333. fullEmails[1].affiliation.inReconfirmNotificationPeriod
  334. ).to.equal(true)
  335. done()
  336. }
  337. )
  338. })
  339. it('should not flag affiliations outside of notification period', function (done) {
  340. const aboutToBeWithinPeriod = moment()
  341. .subtract(institutionNonSSO.maxConfirmationMonths, 'months')
  342. .add(15, 'days')
  343. .toDate() // expires in 15 days
  344. const user = {
  345. _id: '12390i',
  346. email: email1,
  347. emails: [
  348. {
  349. email: email1,
  350. reversedHostname: 'moc.noitailiffa-elpmaxe',
  351. confirmedAt: new Date(),
  352. default: true,
  353. },
  354. {
  355. email: email2,
  356. reversedHostname: 'moc.noitailiffa-elpmaxe',
  357. confirmedAt: aboutToBeWithinPeriod,
  358. },
  359. ],
  360. }
  361. this.getUserAffiliations.resolves(affiliationsData)
  362. this.UserGetter.promises.getUser = sinon.stub().resolves(user)
  363. this.UserGetter.getUserFullEmails(
  364. this.fakeUser._id,
  365. (error, fullEmails) => {
  366. expect(error).to.not.exist
  367. expect(
  368. fullEmails[0].affiliation.inReconfirmNotificationPeriod
  369. ).to.equal(false)
  370. expect(
  371. fullEmails[1].affiliation.inReconfirmNotificationPeriod
  372. ).to.equal(false)
  373. done()
  374. }
  375. )
  376. })
  377. })
  378. describe('SSO institutions', function () {
  379. it('should flag only linked email, if in notification period', function (done) {
  380. const email1 = 'email1@sso.bar'
  381. const email2 = 'email2@sso.bar'
  382. const email3 = 'email3@sso.bar'
  383. const affiliationsData = [
  384. {
  385. email: email1,
  386. role: 'Prof',
  387. department: 'Maths',
  388. inferred: false,
  389. licence: 'pro_plus',
  390. institution: institutionSSO,
  391. },
  392. {
  393. email: email2,
  394. role: 'Prof',
  395. department: 'Maths',
  396. inferred: false,
  397. licence: 'pro_plus',
  398. institution: institutionSSO,
  399. },
  400. {
  401. email: email3,
  402. role: 'Prof',
  403. department: 'Maths',
  404. inferred: false,
  405. licence: 'pro_plus',
  406. institution: institutionSSO,
  407. },
  408. ]
  409. const user = {
  410. _id: '12390i',
  411. email: email1,
  412. emails: [
  413. {
  414. email: email1,
  415. reversedHostname: 'rab.oss',
  416. confirmedAt: new Date('2019-09-24'),
  417. reconfirmedAt: new Date('2019-09-24'),
  418. default: true,
  419. },
  420. {
  421. email: email2,
  422. reversedHostname: 'rab.oss',
  423. confirmedAt: new Date('2019-09-24'),
  424. reconfirmedAt: new Date('2019-09-24'),
  425. samlProviderId: institutionSSO.id,
  426. },
  427. {
  428. email: email3,
  429. reversedHostname: 'rab.oss',
  430. confirmedAt: new Date('2019-09-24'),
  431. reconfirmedAt: new Date('2019-09-24'),
  432. },
  433. ],
  434. samlIdentifiers: [
  435. {
  436. providerId: institutionSSO.id,
  437. externalUserId: 'abc123',
  438. },
  439. ],
  440. }
  441. this.getUserAffiliations.resolves(affiliationsData)
  442. this.UserGetter.promises.getUser = sinon.stub().resolves(user)
  443. this.UserGetter.getUserFullEmails(
  444. this.fakeUser._id,
  445. (error, fullEmails) => {
  446. expect(error).to.not.exist
  447. expect(
  448. fullEmails[0].affiliation.inReconfirmNotificationPeriod
  449. ).to.equal(false)
  450. expect(
  451. fullEmails[1].affiliation.inReconfirmNotificationPeriod
  452. ).to.equal(true)
  453. expect(
  454. fullEmails[2].affiliation.inReconfirmNotificationPeriod
  455. ).to.equal(false)
  456. done()
  457. }
  458. )
  459. })
  460. })
  461. describe('multiple institution affiliations', function () {
  462. it('should flag each institution', function (done) {
  463. const email1 = 'email1@sso.bar'
  464. const email2 = 'email2@sso.bar'
  465. const email3 = 'email3@foo.bar'
  466. const email4 = 'email4@foo.bar'
  467. const affiliationsData = [
  468. {
  469. email: email1,
  470. role: 'Prof',
  471. department: 'Maths',
  472. inferred: false,
  473. licence: 'pro_plus',
  474. institution: institutionSSO,
  475. },
  476. {
  477. email: email2,
  478. role: 'Prof',
  479. department: 'Maths',
  480. inferred: false,
  481. licence: 'pro_plus',
  482. institution: institutionSSO,
  483. },
  484. {
  485. email: email3,
  486. role: 'Prof',
  487. department: 'Maths',
  488. inferred: false,
  489. licence: 'pro_plus',
  490. institution: institutionNonSSO,
  491. },
  492. {
  493. email: email4,
  494. role: 'Prof',
  495. department: 'Maths',
  496. inferred: false,
  497. licence: 'pro_plus',
  498. institution: institutionNonSSO,
  499. },
  500. ]
  501. const user = {
  502. _id: '12390i',
  503. email: email1,
  504. emails: [
  505. {
  506. email: email1,
  507. reversedHostname: 'rab.oss',
  508. confirmedAt: '2019-09-24T20:25:08.503Z',
  509. default: true,
  510. },
  511. {
  512. email: email2,
  513. reversedHostname: 'rab.oss',
  514. confirmedAt: new Date('2019-09-24T20:25:08.503Z'),
  515. samlProviderId: institutionSSO.id,
  516. },
  517. {
  518. email: email3,
  519. reversedHostname: 'rab.oof',
  520. confirmedAt: new Date('2019-10-24T20:25:08.503Z'),
  521. },
  522. {
  523. email: email4,
  524. reversedHostname: 'rab.oof',
  525. confirmedAt: new Date('2019-09-24T20:25:08.503Z'),
  526. },
  527. ],
  528. samlIdentifiers: [
  529. {
  530. providerId: institutionSSO.id,
  531. externalUserId: 'abc123',
  532. },
  533. ],
  534. }
  535. this.getUserAffiliations.resolves(affiliationsData)
  536. this.UserGetter.promises.getUser = sinon.stub().resolves(user)
  537. this.UserGetter.getUserFullEmails(
  538. this.fakeUser._id,
  539. (error, fullEmails) => {
  540. expect(error).to.not.exist
  541. expect(
  542. fullEmails[0].affiliation.inReconfirmNotificationPeriod
  543. ).to.to.equal(false)
  544. expect(
  545. fullEmails[1].affiliation.inReconfirmNotificationPeriod
  546. ).to.equal(true)
  547. expect(
  548. fullEmails[2].affiliation.inReconfirmNotificationPeriod
  549. ).to.equal(true)
  550. expect(
  551. fullEmails[3].affiliation.inReconfirmNotificationPeriod
  552. ).to.equal(true)
  553. done()
  554. }
  555. )
  556. })
  557. })
  558. describe('reconfirmedAt', function () {
  559. it('only use confirmedAt when no reconfirmedAt', function (done) {
  560. const email1 = 'email1@foo.bar'
  561. const email2 = 'email2@foo.bar'
  562. const email3 = 'email3@foo.bar'
  563. const affiliationsData = [
  564. {
  565. email: email1,
  566. role: 'Prof',
  567. department: 'Maths',
  568. inferred: false,
  569. licence: 'pro_plus',
  570. institution: institutionNonSSO,
  571. },
  572. {
  573. email: email2,
  574. role: 'Prof',
  575. department: 'Maths',
  576. inferred: false,
  577. licence: 'pro_plus',
  578. institution: institutionNonSSO,
  579. },
  580. {
  581. email: email3,
  582. role: 'Prof',
  583. department: 'Maths',
  584. inferred: false,
  585. licence: 'pro_plus',
  586. institution: institutionNonSSO,
  587. },
  588. ]
  589. const user = {
  590. _id: '12390i',
  591. email: email1,
  592. emails: [
  593. {
  594. email: email1,
  595. reversedHostname: 'rab.oof',
  596. confirmedAt: moment().subtract(
  597. institutionSSO.maxConfirmationMonths * 2,
  598. 'months'
  599. ),
  600. reconfirmedAt: moment().subtract(
  601. institutionSSO.maxConfirmationMonths * 3,
  602. 'months'
  603. ),
  604. default: true,
  605. },
  606. {
  607. email: email2,
  608. reversedHostname: 'rab.oof',
  609. confirmedAt: moment().subtract(
  610. institutionSSO.maxConfirmationMonths * 3,
  611. 'months'
  612. ),
  613. reconfirmedAt: moment().subtract(
  614. institutionSSO.maxConfirmationMonths * 2,
  615. 'months'
  616. ),
  617. },
  618. {
  619. email: email3,
  620. reversedHostname: 'rab.oof',
  621. confirmedAt: moment().subtract(
  622. institutionSSO.maxConfirmationMonths * 4,
  623. 'months'
  624. ),
  625. reconfirmedAt: moment().subtract(
  626. institutionSSO.maxConfirmationMonths * 4,
  627. 'months'
  628. ),
  629. },
  630. ],
  631. }
  632. this.getUserAffiliations.resolves(affiliationsData)
  633. this.UserGetter.promises.getUser = sinon.stub().resolves(user)
  634. this.UserGetter.getUserFullEmails(
  635. this.fakeUser._id,
  636. (error, fullEmails) => {
  637. expect(error).to.not.exist
  638. expect(
  639. fullEmails[0].affiliation.inReconfirmNotificationPeriod
  640. ).to.equal(true)
  641. expect(
  642. fullEmails[1].affiliation.inReconfirmNotificationPeriod
  643. ).to.equal(true)
  644. expect(
  645. fullEmails[2].affiliation.inReconfirmNotificationPeriod
  646. ).to.equal(true)
  647. done()
  648. }
  649. )
  650. })
  651. })
  652. describe('before reconfirmation period expires and within reconfirmation notification period', function () {
  653. const email = 'leonard@example-affiliation.com'
  654. it('should flag the email', function (done) {
  655. const confirmedAt = moment()
  656. .subtract(institutionNonSSO.maxConfirmationMonths, 'months')
  657. .subtract(14, 'days')
  658. .toDate() // expires in 14 days
  659. const affiliationsData = [
  660. {
  661. email,
  662. role: 'Prof',
  663. department: 'Medicine',
  664. inferred: false,
  665. licence: 'pro_plus',
  666. institution: institutionNonSSO,
  667. },
  668. ]
  669. const user = {
  670. _id: '12390i',
  671. email,
  672. emails: [
  673. {
  674. email,
  675. confirmedAt,
  676. default: true,
  677. },
  678. ],
  679. }
  680. this.getUserAffiliations.resolves(affiliationsData)
  681. this.UserGetter.promises.getUser = sinon.stub().resolves(user)
  682. this.UserGetter.getUserFullEmails(
  683. this.fakeUser._id,
  684. (error, fullEmails) => {
  685. expect(error).to.not.exist
  686. expect(
  687. fullEmails[0].affiliation.inReconfirmNotificationPeriod
  688. ).to.equal(true)
  689. done()
  690. }
  691. )
  692. })
  693. })
  694. describe('when no Settings.reconfirmNotificationDays', function () {
  695. it('should always return inReconfirmNotificationPeriod:false', function (done) {
  696. const email1 = 'email1@sso.bar'
  697. const email2 = 'email2@foo.bar'
  698. const email3 = 'email3@foo.bar'
  699. const confirmedAtAboutToExpire = moment()
  700. .subtract(institutionNonSSO.maxConfirmationMonths, 'months')
  701. .subtract(14, 'days')
  702. .toDate() // expires in 14 days
  703. const affiliationsData = [
  704. {
  705. email: email1,
  706. institution: institutionSSO,
  707. },
  708. {
  709. email: email2,
  710. institution: institutionNonSSO,
  711. },
  712. {
  713. email: email3,
  714. institution: institutionNonSSO,
  715. },
  716. ]
  717. const user = {
  718. _id: '12390i',
  719. email: email1,
  720. emails: [
  721. {
  722. email: email1,
  723. confirmedAt: confirmedAtAboutToExpire,
  724. default: true,
  725. samlProviderId: institutionSSO.id,
  726. },
  727. {
  728. email: email2,
  729. confirmedAt: new Date('2019-09-24T20:25:08.503Z'),
  730. },
  731. {
  732. email: email3,
  733. confirmedAt: new Date('2019-10-24T20:25:08.503Z'),
  734. },
  735. ],
  736. samlIdentifiers: [
  737. {
  738. providerId: institutionSSO.id,
  739. externalUserId: 'abc123',
  740. },
  741. ],
  742. }
  743. this.settings.reconfirmNotificationDays = undefined
  744. this.getUserAffiliations.resolves(affiliationsData)
  745. this.UserGetter.promises.getUser = sinon.stub().resolves(user)
  746. this.UserGetter.getUserFullEmails(
  747. this.fakeUser._id,
  748. (error, fullEmails) => {
  749. expect(error).to.not.exist
  750. expect(
  751. fullEmails[0].affiliation.inReconfirmNotificationPeriod
  752. ).to.to.equal(false)
  753. expect(
  754. fullEmails[1].affiliation.inReconfirmNotificationPeriod
  755. ).to.equal(false)
  756. expect(
  757. fullEmails[2].affiliation.inReconfirmNotificationPeriod
  758. ).to.equal(false)
  759. done()
  760. }
  761. )
  762. })
  763. })
  764. })
  765. })
  766. describe('getUserbyMainEmail', function () {
  767. it('query user by main email', function (done) {
  768. const email = 'hello@world.com'
  769. const projection = { emails: 1 }
  770. this.UserGetter.getUserByMainEmail(email, projection, (error, user) => {
  771. expect(error).to.not.exist
  772. this.findOne.called.should.equal(true)
  773. this.findOne.calledWith({ email }, { projection }).should.equal(true)
  774. done()
  775. })
  776. })
  777. it('return user if found', function (done) {
  778. const email = 'hello@world.com'
  779. this.UserGetter.getUserByMainEmail(email, (error, user) => {
  780. expect(error).to.not.exist
  781. user.should.deep.equal(this.fakeUser)
  782. done()
  783. })
  784. })
  785. it('trim email', function (done) {
  786. const email = 'hello@world.com'
  787. this.UserGetter.getUserByMainEmail(` ${email} `, (error, user) => {
  788. expect(error).to.not.exist
  789. this.findOne.called.should.equal(true)
  790. this.findOne.calledWith({ email }).should.equal(true)
  791. done()
  792. })
  793. })
  794. })
  795. describe('getUserByAnyEmail', function () {
  796. it('query user for any email', function (done) {
  797. const email = 'hello@world.com'
  798. const expectedQuery = {
  799. emails: { $exists: true },
  800. 'emails.email': email,
  801. }
  802. const projection = { emails: 1 }
  803. this.UserGetter.getUserByAnyEmail(
  804. ` ${email} `,
  805. projection,
  806. (error, user) => {
  807. expect(error).to.not.exist
  808. this.findOne
  809. .calledWith(expectedQuery, { projection })
  810. .should.equal(true)
  811. user.should.deep.equal(this.fakeUser)
  812. done()
  813. }
  814. )
  815. })
  816. it('query contains $exists:true so partial index is used', function (done) {
  817. const expectedQuery = {
  818. emails: { $exists: true },
  819. 'emails.email': '',
  820. }
  821. this.UserGetter.getUserByAnyEmail('', {}, (error, user) => {
  822. expect(error).to.not.exist
  823. this.findOne
  824. .calledWith(expectedQuery, { projection: {} })
  825. .should.equal(true)
  826. done()
  827. })
  828. })
  829. it('checks main email as well', function (done) {
  830. this.findOne.callsArgWith(2, null, null)
  831. const email = 'hello@world.com'
  832. const projection = { emails: 1 }
  833. this.UserGetter.getUserByAnyEmail(
  834. ` ${email} `,
  835. projection,
  836. (error, user) => {
  837. expect(error).to.not.exist
  838. this.findOne.calledTwice.should.equal(true)
  839. this.findOne.calledWith({ email }, { projection }).should.equal(true)
  840. done()
  841. }
  842. )
  843. })
  844. })
  845. describe('getUsersByHostname', function () {
  846. it('should find user by hostname', function (done) {
  847. const hostname = 'bar.foo'
  848. const expectedQuery = {
  849. emails: { $exists: true },
  850. 'emails.reversedHostname': hostname.split('').reverse().join(''),
  851. }
  852. const projection = { emails: 1 }
  853. this.UserGetter.getUsersByHostname(
  854. hostname,
  855. projection,
  856. (error, users) => {
  857. expect(error).to.not.exist
  858. this.find.calledOnce.should.equal(true)
  859. this.find.calledWith(expectedQuery, { projection }).should.equal(true)
  860. done()
  861. }
  862. )
  863. })
  864. })
  865. describe('getUsersByAnyConfirmedEmail', function () {
  866. it('should find users by confirmed email', function (done) {
  867. const emails = ['confirmed@example.com']
  868. this.UserGetter.getUsersByAnyConfirmedEmail(emails, (error, users) => {
  869. expect(error).to.not.exist
  870. expect(this.find).to.be.calledOnceWith(
  871. {
  872. 'emails.email': { $in: emails }, // use the index on emails.email
  873. emails: {
  874. $exists: true,
  875. $elemMatch: {
  876. email: { $in: emails },
  877. confirmedAt: { $exists: true },
  878. },
  879. },
  880. },
  881. { projection: {} }
  882. )
  883. done()
  884. })
  885. })
  886. })
  887. describe('getUsersByV1Id', function () {
  888. it('should find users by list of v1 ids', function (done) {
  889. const v1Ids = [501]
  890. const expectedQuery = {
  891. 'overleaf.id': { $in: v1Ids },
  892. }
  893. const projection = { emails: 1 }
  894. this.UserGetter.getUsersByV1Ids(v1Ids, projection, (error, users) => {
  895. expect(error).to.not.exist
  896. this.find.calledOnce.should.equal(true)
  897. this.find.calledWith(expectedQuery, { projection }).should.equal(true)
  898. done()
  899. })
  900. })
  901. })
  902. describe('ensureUniqueEmailAddress', function () {
  903. beforeEach(function () {
  904. this.UserGetter.getUserByAnyEmail = sinon.stub()
  905. })
  906. it('should return error if existing user is found', function (done) {
  907. this.UserGetter.getUserByAnyEmail.callsArgWith(1, null, this.fakeUser)
  908. this.UserGetter.ensureUniqueEmailAddress(this.newEmail, err => {
  909. expect(err).to.exist
  910. expect(err).to.be.an.instanceof(Errors.EmailExistsError)
  911. done()
  912. })
  913. })
  914. it('should return null if no user is found', function (done) {
  915. this.UserGetter.getUserByAnyEmail.callsArgWith(1)
  916. this.UserGetter.ensureUniqueEmailAddress(this.newEmail, err => {
  917. expect(err).not.to.exist
  918. done()
  919. })
  920. })
  921. })
  922. })