UserGetterTests.js 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  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. '@overleaf/settings': (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. '../../models/User': {
  63. User: (this.User = {}),
  64. },
  65. },
  66. })
  67. })
  68. describe('getSsoUsersAtInstitution', function () {
  69. it('should throw an error when no projection is passed', function (done) {
  70. this.UserGetter.getSsoUsersAtInstitution(1, undefined, error => {
  71. expect(error).to.exist
  72. expect(error.message).to.equal('missing projection')
  73. done()
  74. })
  75. })
  76. })
  77. describe('getUser', function () {
  78. it('should get user', function (done) {
  79. const query = { _id: '000000000000000000000000' }
  80. const projection = { email: 1 }
  81. this.UserGetter.getUser(query, projection, (error, user) => {
  82. expect(error).to.not.exist
  83. this.findOne.called.should.equal(true)
  84. this.findOne.calledWith(query, { projection }).should.equal(true)
  85. user.should.deep.equal(this.fakeUser)
  86. done()
  87. })
  88. })
  89. it('should not allow null query', function (done) {
  90. this.UserGetter.getUser(null, {}, error => {
  91. error.should.exist
  92. error.message.should.equal('no query provided')
  93. done()
  94. })
  95. })
  96. })
  97. describe('getUsers', function () {
  98. it('should get users with array of userIds', function (done) {
  99. const query = [new ObjectId()]
  100. const projection = { email: 1 }
  101. this.UserGetter.getUsers(query, projection, (error, users) => {
  102. expect(error).to.not.exist
  103. this.find.should.have.been.calledWithMatch(
  104. { _id: { $in: query } },
  105. { projection }
  106. )
  107. users.should.deep.equal([this.fakeUser])
  108. done()
  109. })
  110. })
  111. it('should not allow null query', function (done) {
  112. this.UserGetter.getUser(null, {}, error => {
  113. error.should.exist
  114. error.message.should.equal('no query provided')
  115. done()
  116. })
  117. })
  118. })
  119. describe('getUserFullEmails', function () {
  120. it('should get user', function (done) {
  121. this.UserGetter.promises.getUser = sinon.stub().resolves(this.fakeUser)
  122. const projection = { email: 1, emails: 1, samlIdentifiers: 1 }
  123. this.UserGetter.getUserFullEmails(
  124. this.fakeUser._id,
  125. (error, fullEmails) => {
  126. expect(error).to.not.exist
  127. this.UserGetter.promises.getUser.called.should.equal(true)
  128. this.UserGetter.promises.getUser
  129. .calledWith(this.fakeUser._id, projection)
  130. .should.equal(true)
  131. done()
  132. }
  133. )
  134. })
  135. it('should fetch emails data', function (done) {
  136. this.UserGetter.promises.getUser = sinon.stub().resolves(this.fakeUser)
  137. this.UserGetter.getUserFullEmails(
  138. this.fakeUser._id,
  139. (error, fullEmails) => {
  140. expect(error).to.not.exist
  141. assert.deepEqual(fullEmails, [
  142. {
  143. email: 'email1@foo.bar',
  144. reversedHostname: 'rab.oof',
  145. confirmedAt: this.fakeUser.emails[0].confirmedAt,
  146. emailHasInstitutionLicence: false,
  147. default: false,
  148. },
  149. {
  150. email: 'email2@foo.bar',
  151. reversedHostname: 'rab.oof',
  152. emailHasInstitutionLicence: false,
  153. default: true,
  154. },
  155. ])
  156. done()
  157. }
  158. )
  159. })
  160. it('should merge affiliation data', function (done) {
  161. this.UserGetter.promises.getUser = sinon.stub().resolves(this.fakeUser)
  162. const affiliationsData = [
  163. {
  164. email: 'email1@foo.bar',
  165. role: 'Prof',
  166. cached_confirmed_at: '2019-07-11T18:25:01.639Z',
  167. cached_reconfirmed_at: '2021-07-11T18:25:01.639Z',
  168. department: 'Maths',
  169. entitlement: false,
  170. inferred: false,
  171. licence: 'pro_plus',
  172. institution: {
  173. name: 'University Name',
  174. isUniversity: true,
  175. confirmed: true,
  176. },
  177. last_day_to_reconfirm: undefined,
  178. past_reconfirm_date: false,
  179. portal: undefined,
  180. },
  181. ]
  182. this.getUserAffiliations.resolves(affiliationsData)
  183. this.UserGetter.getUserFullEmails(
  184. this.fakeUser._id,
  185. (error, fullEmails) => {
  186. expect(error).to.not.exist
  187. assert.deepEqual(fullEmails, [
  188. {
  189. email: 'email1@foo.bar',
  190. reversedHostname: 'rab.oof',
  191. confirmedAt: this.fakeUser.emails[0].confirmedAt,
  192. default: false,
  193. emailHasInstitutionLicence: true,
  194. affiliation: {
  195. institution: affiliationsData[0].institution,
  196. inferred: affiliationsData[0].inferred,
  197. department: affiliationsData[0].department,
  198. role: affiliationsData[0].role,
  199. lastDayToReconfirm: undefined,
  200. licence: affiliationsData[0].licence,
  201. inReconfirmNotificationPeriod: false,
  202. cachedConfirmedAt: '2019-07-11T18:25:01.639Z',
  203. cachedReconfirmedAt: '2021-07-11T18:25:01.639Z',
  204. cachedEntitlement: false,
  205. cachedLastDayToReconfirm: undefined,
  206. cachedPastReconfirmDate: false,
  207. pastReconfirmDate: false,
  208. portal: undefined,
  209. },
  210. },
  211. {
  212. email: 'email2@foo.bar',
  213. reversedHostname: 'rab.oof',
  214. emailHasInstitutionLicence: false,
  215. default: true,
  216. },
  217. ])
  218. done()
  219. }
  220. )
  221. })
  222. it('should merge SAML identifier', function (done) {
  223. const fakeSamlIdentifiers = [
  224. { providerId: 'saml_id', exteranlUserId: 'whatever' },
  225. ]
  226. const fakeUserWithSaml = this.fakeUser
  227. fakeUserWithSaml.emails[0].samlProviderId = 'saml_id'
  228. fakeUserWithSaml.samlIdentifiers = fakeSamlIdentifiers
  229. this.UserGetter.promises.getUser = sinon.stub().resolves(this.fakeUser)
  230. this.getUserAffiliations.resolves([])
  231. this.UserGetter.getUserFullEmails(
  232. this.fakeUser._id,
  233. (error, fullEmails) => {
  234. expect(error).to.not.exist
  235. assert.deepEqual(fullEmails, [
  236. {
  237. email: 'email1@foo.bar',
  238. reversedHostname: 'rab.oof',
  239. confirmedAt: this.fakeUser.emails[0].confirmedAt,
  240. default: false,
  241. emailHasInstitutionLicence: false,
  242. samlProviderId: 'saml_id',
  243. samlIdentifier: fakeSamlIdentifiers[0],
  244. },
  245. {
  246. email: 'email2@foo.bar',
  247. reversedHostname: 'rab.oof',
  248. emailHasInstitutionLicence: false,
  249. default: true,
  250. },
  251. ])
  252. done()
  253. }
  254. )
  255. })
  256. it('should get user when it has no emails field', function (done) {
  257. this.fakeUser = {
  258. _id: '12390i',
  259. email: 'email2@foo.bar',
  260. }
  261. this.UserGetter.promises.getUser = sinon.stub().resolves(this.fakeUser)
  262. const projection = { email: 1, emails: 1, samlIdentifiers: 1 }
  263. this.UserGetter.getUserFullEmails(
  264. this.fakeUser._id,
  265. (error, fullEmails) => {
  266. expect(error).to.not.exist
  267. this.UserGetter.promises.getUser.called.should.equal(true)
  268. this.UserGetter.promises.getUser
  269. .calledWith(this.fakeUser._id, projection)
  270. .should.equal(true)
  271. assert.deepEqual(fullEmails, [])
  272. done()
  273. }
  274. )
  275. })
  276. describe('affiliation reconfirmation', function () {
  277. const institutionNonSSO = {
  278. id: 1,
  279. name: 'University Name',
  280. commonsAccount: true,
  281. isUniversity: true,
  282. confirmed: true,
  283. ssoBeta: false,
  284. ssoEnabled: false,
  285. maxConfirmationMonths: 12,
  286. }
  287. const institutionSSO = {
  288. id: 2,
  289. name: 'SSO University Name',
  290. isUniversity: true,
  291. confirmed: true,
  292. ssoBeta: false,
  293. ssoEnabled: true,
  294. maxConfirmationMonths: 12,
  295. }
  296. describe('non-SSO institutions', function () {
  297. const email1 = 'leonard@example-affiliation.com'
  298. const email2 = 'mccoy@example-affiliation.com'
  299. const affiliationsData = [
  300. {
  301. email: email1,
  302. role: 'Prof',
  303. department: 'Medicine',
  304. inferred: false,
  305. licence: 'pro_plus',
  306. institution: institutionNonSSO,
  307. },
  308. {
  309. email: email2,
  310. role: 'Prof',
  311. department: 'Medicine',
  312. inferred: false,
  313. licence: 'pro_plus',
  314. institution: institutionNonSSO,
  315. },
  316. ]
  317. it('should flag inReconfirmNotificationPeriod for all affiliations in period', function (done) {
  318. const { maxConfirmationMonths } = institutionNonSSO
  319. const confirmed1 = moment()
  320. .subtract(maxConfirmationMonths + 2, 'months')
  321. .toDate()
  322. const lastDayToReconfirm1 = moment(confirmed1)
  323. .add(maxConfirmationMonths, 'months')
  324. .toDate()
  325. const confirmed2 = moment()
  326. .subtract(maxConfirmationMonths + 1, 'months')
  327. .toDate()
  328. const lastDayToReconfirm2 = moment(confirmed2)
  329. .add(maxConfirmationMonths, 'months')
  330. .toDate()
  331. const user = {
  332. _id: '12390i',
  333. email: email1,
  334. emails: [
  335. {
  336. email: email1,
  337. reversedHostname: 'moc.noitailiffa-elpmaxe',
  338. confirmedAt: confirmed1,
  339. default: true,
  340. },
  341. {
  342. email: email2,
  343. reversedHostname: 'moc.noitailiffa-elpmaxe',
  344. confirmedAt: confirmed2,
  345. },
  346. ],
  347. }
  348. const affiliations = [...affiliationsData]
  349. affiliations[0].last_day_to_reconfirm = lastDayToReconfirm1
  350. affiliations[1].last_day_to_reconfirm = lastDayToReconfirm2
  351. this.getUserAffiliations.resolves(affiliations)
  352. this.UserGetter.promises.getUser = sinon.stub().resolves(user)
  353. this.UserGetter.getUserFullEmails(
  354. this.fakeUser._id,
  355. (error, fullEmails) => {
  356. expect(error).to.not.exist
  357. expect(
  358. fullEmails[0].affiliation.inReconfirmNotificationPeriod
  359. ).to.equal(true)
  360. expect(
  361. fullEmails[1].affiliation.inReconfirmNotificationPeriod
  362. ).to.equal(true)
  363. done()
  364. }
  365. )
  366. })
  367. it('should not flag affiliations outside of notification period', function (done) {
  368. const { maxConfirmationMonths } = institutionNonSSO
  369. const confirmed1 = new Date()
  370. const lastDayToReconfirm1 = moment(confirmed1)
  371. .add(maxConfirmationMonths, 'months')
  372. .toDate()
  373. const confirmed2 = moment()
  374. .subtract(maxConfirmationMonths, 'months')
  375. .add(15, 'days')
  376. .toDate() // expires in 15 days
  377. const lastDayToReconfirm2 = moment(confirmed2)
  378. .add(maxConfirmationMonths, 'months')
  379. .toDate()
  380. const user = {
  381. _id: '12390i',
  382. email: email1,
  383. emails: [
  384. {
  385. email: email1,
  386. reversedHostname: 'moc.noitailiffa-elpmaxe',
  387. confirmedAt: confirmed1,
  388. default: true,
  389. },
  390. {
  391. email: email2,
  392. reversedHostname: 'moc.noitailiffa-elpmaxe',
  393. confirmedAt: confirmed2,
  394. },
  395. ],
  396. }
  397. const affiliations = [...affiliationsData]
  398. affiliations[0].last_day_to_reconfirm = lastDayToReconfirm1
  399. affiliations[1].last_day_to_reconfirm = lastDayToReconfirm2
  400. this.getUserAffiliations.resolves(affiliations)
  401. this.UserGetter.promises.getUser = sinon.stub().resolves(user)
  402. this.UserGetter.getUserFullEmails(
  403. this.fakeUser._id,
  404. (error, fullEmails) => {
  405. expect(error).to.not.exist
  406. expect(
  407. fullEmails[0].affiliation.inReconfirmNotificationPeriod
  408. ).to.equal(false)
  409. expect(
  410. fullEmails[1].affiliation.inReconfirmNotificationPeriod
  411. ).to.equal(false)
  412. done()
  413. }
  414. )
  415. })
  416. })
  417. describe('SSO institutions', function () {
  418. it('should flag only linked email, if in notification period', function (done) {
  419. const { maxConfirmationMonths } = institutionSSO
  420. const email1 = 'email1@sso.bar'
  421. const email2 = 'email2@sso.bar'
  422. const email3 = 'email3@sso.bar'
  423. const reconfirmedAt = new Date('2019-09-24')
  424. const lastDayToReconfirm = moment(reconfirmedAt)
  425. .add(maxConfirmationMonths, 'months')
  426. .toDate()
  427. const user = {
  428. _id: '12390i',
  429. email: email1,
  430. emails: [
  431. {
  432. email: email1,
  433. reversedHostname: 'rab.oss',
  434. confirmedAt: new Date('2019-09-24'),
  435. reconfirmedAt: reconfirmedAt,
  436. default: true,
  437. },
  438. {
  439. email: email2,
  440. reversedHostname: 'rab.oss',
  441. confirmedAt: new Date('2019-09-24'),
  442. reconfirmedAt: reconfirmedAt,
  443. samlProviderId: institutionSSO.id,
  444. },
  445. {
  446. email: email3,
  447. reversedHostname: 'rab.oss',
  448. confirmedAt: new Date('2019-09-24'),
  449. reconfirmedAt: reconfirmedAt,
  450. },
  451. ],
  452. samlIdentifiers: [
  453. {
  454. providerId: institutionSSO.id,
  455. externalUserId: 'abc123',
  456. },
  457. ],
  458. }
  459. const affiliations = [
  460. {
  461. email: email1,
  462. role: 'Prof',
  463. department: 'Maths!',
  464. inferred: false,
  465. licence: 'pro_plus',
  466. institution: institutionSSO,
  467. last_day_to_reconfirm: lastDayToReconfirm,
  468. },
  469. {
  470. email: email2,
  471. role: 'Prof',
  472. department: 'Maths',
  473. inferred: false,
  474. licence: 'pro_plus',
  475. institution: institutionSSO,
  476. last_day_to_reconfirm: lastDayToReconfirm,
  477. },
  478. {
  479. email: email3,
  480. role: 'Prof',
  481. department: 'Maths',
  482. inferred: false,
  483. licence: 'pro_plus',
  484. institution: institutionSSO,
  485. last_day_to_reconfirm: lastDayToReconfirm,
  486. },
  487. ]
  488. this.getUserAffiliations.resolves(affiliations)
  489. this.UserGetter.promises.getUser = sinon.stub().resolves(user)
  490. this.UserGetter.getUserFullEmails(
  491. this.fakeUser._id,
  492. (error, fullEmails) => {
  493. expect(error).to.not.exist
  494. expect(
  495. fullEmails[0].affiliation.inReconfirmNotificationPeriod
  496. ).to.equal(false)
  497. expect(
  498. fullEmails[1].affiliation.inReconfirmNotificationPeriod
  499. ).to.equal(true)
  500. expect(
  501. fullEmails[2].affiliation.inReconfirmNotificationPeriod
  502. ).to.equal(false)
  503. done()
  504. }
  505. )
  506. })
  507. })
  508. describe('multiple institution affiliations', function () {
  509. it('should flag each institution', function (done) {
  510. const { maxConfirmationMonths } = institutionSSO
  511. const email1 = 'email1@sso.bar'
  512. const email2 = 'email2@sso.bar'
  513. const email3 = 'email3@foo.bar'
  514. const email4 = 'email4@foo.bar'
  515. const confirmed1 = new Date('2019-09-24T20:25:08.503Z')
  516. const lastDayToReconfirm1 = moment(confirmed1)
  517. .add(maxConfirmationMonths, 'months')
  518. .toDate()
  519. const confirmed2 = new Date('2019-10-24T20:25:08.503Z')
  520. const lastDayToReconfirm2 = moment(confirmed2)
  521. .add(maxConfirmationMonths, 'months')
  522. .toDate()
  523. const affiliationsData = [
  524. {
  525. email: email1,
  526. role: 'Prof',
  527. department: 'Maths',
  528. inferred: false,
  529. licence: 'pro_plus',
  530. institution: institutionSSO,
  531. last_day_to_reconfirm: lastDayToReconfirm1,
  532. },
  533. {
  534. email: email2,
  535. role: 'Prof',
  536. department: 'Maths',
  537. inferred: false,
  538. licence: 'pro_plus',
  539. institution: institutionSSO,
  540. last_day_to_reconfirm: lastDayToReconfirm1,
  541. },
  542. {
  543. email: email3,
  544. role: 'Prof',
  545. department: 'Maths',
  546. inferred: false,
  547. licence: 'pro_plus',
  548. institution: institutionNonSSO,
  549. last_day_to_reconfirm: lastDayToReconfirm2,
  550. },
  551. {
  552. email: email4,
  553. role: 'Prof',
  554. department: 'Maths',
  555. inferred: false,
  556. licence: 'pro_plus',
  557. institution: institutionNonSSO,
  558. last_day_to_reconfirm: lastDayToReconfirm1,
  559. },
  560. ]
  561. const user = {
  562. _id: '12390i',
  563. email: email1,
  564. emails: [
  565. {
  566. email: email1,
  567. reversedHostname: 'rab.oss',
  568. confirmedAt: confirmed1,
  569. default: true,
  570. },
  571. {
  572. email: email2,
  573. reversedHostname: 'rab.oss',
  574. confirmedAt: confirmed1,
  575. samlProviderId: institutionSSO.id,
  576. },
  577. {
  578. email: email3,
  579. reversedHostname: 'rab.oof',
  580. confirmedAt: confirmed2,
  581. },
  582. {
  583. email: email4,
  584. reversedHostname: 'rab.oof',
  585. confirmedAt: confirmed1,
  586. },
  587. ],
  588. samlIdentifiers: [
  589. {
  590. providerId: institutionSSO.id,
  591. externalUserId: 'abc123',
  592. },
  593. ],
  594. }
  595. this.getUserAffiliations.resolves(affiliationsData)
  596. this.UserGetter.promises.getUser = sinon.stub().resolves(user)
  597. this.UserGetter.getUserFullEmails(
  598. this.fakeUser._id,
  599. (error, fullEmails) => {
  600. expect(error).to.not.exist
  601. expect(
  602. fullEmails[0].affiliation.inReconfirmNotificationPeriod
  603. ).to.to.equal(false)
  604. expect(
  605. fullEmails[1].affiliation.inReconfirmNotificationPeriod
  606. ).to.equal(true)
  607. expect(
  608. fullEmails[2].affiliation.inReconfirmNotificationPeriod
  609. ).to.equal(true)
  610. expect(
  611. fullEmails[3].affiliation.inReconfirmNotificationPeriod
  612. ).to.equal(true)
  613. done()
  614. }
  615. )
  616. })
  617. })
  618. describe('reconfirmedAt', function () {
  619. it('only use confirmedAt when no reconfirmedAt', function (done) {
  620. const { maxConfirmationMonths } = institutionSSO
  621. const email1 = 'email1@foo.bar'
  622. const reconfirmed1 = moment().subtract(
  623. maxConfirmationMonths * 3,
  624. 'months'
  625. )
  626. const lastDayToReconfirm1 = moment(reconfirmed1)
  627. .add(maxConfirmationMonths, 'months')
  628. .toDate()
  629. const email2 = 'email2@foo.bar'
  630. const reconfirmed2 = moment().subtract(
  631. maxConfirmationMonths * 2,
  632. 'months'
  633. )
  634. const lastDayToReconfirm2 = moment(reconfirmed2)
  635. .add(maxConfirmationMonths, 'months')
  636. .toDate()
  637. const email3 = 'email3@foo.bar'
  638. const reconfirmed3 = moment().subtract(
  639. maxConfirmationMonths * 4,
  640. 'months'
  641. )
  642. const lastDayToReconfirm3 = moment(reconfirmed3)
  643. .add(maxConfirmationMonths, 'months')
  644. .toDate()
  645. const affiliationsData = [
  646. {
  647. email: email1,
  648. role: 'Prof',
  649. department: 'Maths',
  650. inferred: false,
  651. licence: 'pro_plus',
  652. institution: institutionNonSSO,
  653. last_day_to_reconfirm: lastDayToReconfirm1,
  654. },
  655. {
  656. email: email2,
  657. role: 'Prof',
  658. department: 'Maths',
  659. inferred: false,
  660. licence: 'pro_plus',
  661. institution: institutionNonSSO,
  662. last_day_to_reconfirm: lastDayToReconfirm2,
  663. },
  664. {
  665. email: email3,
  666. role: 'Prof',
  667. department: 'Maths',
  668. inferred: false,
  669. licence: 'pro_plus',
  670. institution: institutionNonSSO,
  671. last_day_to_reconfirm: lastDayToReconfirm3,
  672. },
  673. ]
  674. const user = {
  675. _id: '12390i',
  676. email: email1,
  677. emails: [
  678. {
  679. email: email1,
  680. reversedHostname: 'rab.oof',
  681. confirmedAt: moment().subtract(
  682. institutionSSO.maxConfirmationMonths * 2,
  683. 'months'
  684. ),
  685. reconfirmedAt: reconfirmed1,
  686. default: true,
  687. },
  688. {
  689. email: email2,
  690. reversedHostname: 'rab.oof',
  691. confirmedAt: moment().subtract(
  692. institutionSSO.maxConfirmationMonths * 3,
  693. 'months'
  694. ),
  695. reconfirmedAt: reconfirmed2,
  696. },
  697. {
  698. email: email3,
  699. reversedHostname: 'rab.oof',
  700. confirmedAt: moment().subtract(
  701. institutionSSO.maxConfirmationMonths * 4,
  702. 'months'
  703. ),
  704. reconfirmedAt: reconfirmed3,
  705. },
  706. ],
  707. }
  708. this.getUserAffiliations.resolves(affiliationsData)
  709. this.UserGetter.promises.getUser = sinon.stub().resolves(user)
  710. this.UserGetter.getUserFullEmails(
  711. this.fakeUser._id,
  712. (error, fullEmails) => {
  713. expect(error).to.not.exist
  714. expect(
  715. fullEmails[0].affiliation.inReconfirmNotificationPeriod
  716. ).to.equal(true)
  717. expect(
  718. fullEmails[1].affiliation.inReconfirmNotificationPeriod
  719. ).to.equal(true)
  720. expect(
  721. fullEmails[2].affiliation.inReconfirmNotificationPeriod
  722. ).to.equal(true)
  723. done()
  724. }
  725. )
  726. })
  727. })
  728. describe('before reconfirmation period expires and within reconfirmation notification period', function () {
  729. const email = 'leonard@example-affiliation.com'
  730. it('should flag the email', function (done) {
  731. const { maxConfirmationMonths } = institutionNonSSO
  732. const confirmedAt = moment()
  733. .subtract(maxConfirmationMonths, 'months')
  734. .subtract(14, 'days')
  735. .toDate() // expires in 14 days
  736. const affiliationsData = [
  737. {
  738. email,
  739. role: 'Prof',
  740. department: 'Medicine',
  741. inferred: false,
  742. licence: 'pro_plus',
  743. institution: institutionNonSSO,
  744. last_day_to_reconfirm: moment(confirmedAt)
  745. .add(maxConfirmationMonths, 'months')
  746. .toDate(),
  747. },
  748. ]
  749. const user = {
  750. _id: '12390i',
  751. email,
  752. emails: [
  753. {
  754. email,
  755. confirmedAt,
  756. default: true,
  757. },
  758. ],
  759. }
  760. this.getUserAffiliations.resolves(affiliationsData)
  761. this.UserGetter.promises.getUser = sinon.stub().resolves(user)
  762. this.UserGetter.getUserFullEmails(
  763. this.fakeUser._id,
  764. (error, fullEmails) => {
  765. expect(error).to.not.exist
  766. expect(
  767. fullEmails[0].affiliation.inReconfirmNotificationPeriod
  768. ).to.equal(true)
  769. done()
  770. }
  771. )
  772. })
  773. })
  774. describe('when no Settings.reconfirmNotificationDays', function () {
  775. it('should always return inReconfirmNotificationPeriod:false', function (done) {
  776. const email1 = 'email1@sso.bar'
  777. const email2 = 'email2@foo.bar'
  778. const email3 = 'email3@foo.bar'
  779. const confirmedAtAboutToExpire = moment()
  780. .subtract(institutionNonSSO.maxConfirmationMonths, 'months')
  781. .subtract(14, 'days')
  782. .toDate() // expires in 14 days
  783. const affiliationsData = [
  784. {
  785. email: email1,
  786. institution: institutionSSO,
  787. },
  788. {
  789. email: email2,
  790. institution: institutionNonSSO,
  791. },
  792. {
  793. email: email3,
  794. institution: institutionNonSSO,
  795. },
  796. ]
  797. const user = {
  798. _id: '12390i',
  799. email: email1,
  800. emails: [
  801. {
  802. email: email1,
  803. confirmedAt: confirmedAtAboutToExpire,
  804. default: true,
  805. samlProviderId: institutionSSO.id,
  806. },
  807. {
  808. email: email2,
  809. confirmedAt: new Date('2019-09-24T20:25:08.503Z'),
  810. },
  811. {
  812. email: email3,
  813. confirmedAt: new Date('2019-10-24T20:25:08.503Z'),
  814. },
  815. ],
  816. samlIdentifiers: [
  817. {
  818. providerId: institutionSSO.id,
  819. externalUserId: 'abc123',
  820. },
  821. ],
  822. }
  823. this.settings.reconfirmNotificationDays = undefined
  824. this.getUserAffiliations.resolves(affiliationsData)
  825. this.UserGetter.promises.getUser = sinon.stub().resolves(user)
  826. this.UserGetter.getUserFullEmails(
  827. this.fakeUser._id,
  828. (error, fullEmails) => {
  829. expect(error).to.not.exist
  830. expect(
  831. fullEmails[0].affiliation.inReconfirmNotificationPeriod
  832. ).to.to.equal(false)
  833. expect(
  834. fullEmails[1].affiliation.inReconfirmNotificationPeriod
  835. ).to.equal(false)
  836. expect(
  837. fullEmails[2].affiliation.inReconfirmNotificationPeriod
  838. ).to.equal(false)
  839. done()
  840. }
  841. )
  842. })
  843. })
  844. it('should flag to show notification if v1 shows as past reconfirmation but v2 does not', function (done) {
  845. const email = 'abc123@test.com'
  846. const confirmedAt = new Date()
  847. const affiliationsData = [
  848. {
  849. email,
  850. licence: 'free',
  851. institution: institutionNonSSO,
  852. last_day_to_reconfirm: '2020-07-11T18:25:01.639Z',
  853. past_reconfirm_date: true,
  854. },
  855. ]
  856. const user = {
  857. _id: '12390i',
  858. email,
  859. emails: [
  860. {
  861. email,
  862. confirmedAt,
  863. default: true,
  864. },
  865. ],
  866. }
  867. this.getUserAffiliations.resolves(affiliationsData)
  868. this.UserGetter.promises.getUser = sinon.stub().resolves(user)
  869. this.UserGetter.getUserFullEmails(
  870. this.fakeUser._id,
  871. (error, fullEmails) => {
  872. expect(error).to.not.exist
  873. expect(
  874. fullEmails[0].affiliation.inReconfirmNotificationPeriod
  875. ).to.equal(true)
  876. done()
  877. }
  878. )
  879. })
  880. it('should flag to show notification if v1 shows as reconfirmation upcoming but v2 does not', function (done) {
  881. const email = 'abc123@test.com'
  882. const { maxConfirmationMonths } = institutionNonSSO
  883. const affiliationsData = [
  884. {
  885. email,
  886. licence: 'free',
  887. institution: institutionNonSSO,
  888. last_day_to_reconfirm: moment()
  889. .subtract(maxConfirmationMonths, 'months')
  890. .add(3, 'day')
  891. .toDate(),
  892. },
  893. ]
  894. const user = {
  895. _id: '12390i',
  896. email,
  897. emails: [
  898. {
  899. email,
  900. confirmedAt: new Date(),
  901. default: true,
  902. },
  903. ],
  904. }
  905. this.getUserAffiliations.resolves(affiliationsData)
  906. this.UserGetter.promises.getUser = sinon.stub().resolves(user)
  907. this.UserGetter.getUserFullEmails(
  908. this.fakeUser._id,
  909. (error, fullEmails) => {
  910. expect(error).to.not.exist
  911. expect(
  912. fullEmails[0].affiliation.inReconfirmNotificationPeriod
  913. ).to.equal(true)
  914. done()
  915. }
  916. )
  917. })
  918. describe('cachedLastDayToReconfirm', function () {
  919. const email = 'abc123@test.com'
  920. const confirmedAt = new Date('2019-07-11T18:25:01.639Z')
  921. const lastDay = '2020-07-11T18:25:01.639Z'
  922. const affiliationsData = [
  923. {
  924. email,
  925. licence: 'professional',
  926. institution: institutionSSO,
  927. last_day_to_reconfirm: lastDay,
  928. past_reconfirm_date: true,
  929. },
  930. ]
  931. const user = {
  932. _id: '12390i',
  933. email,
  934. emails: [
  935. {
  936. email,
  937. confirmedAt,
  938. default: true,
  939. },
  940. ],
  941. }
  942. it('should set cachedLastDayToReconfirm for SSO institutions if email is linked to SSO', async function () {
  943. const userLinked = Object.assign({}, user)
  944. userLinked.emails[0].samlProviderId = institutionSSO.id.toString()
  945. this.getUserAffiliations.resolves(affiliationsData)
  946. this.UserGetter.promises.getUser = sinon.stub().resolves(userLinked)
  947. const fullEmails = await this.UserGetter.promises.getUserFullEmails(
  948. this.fakeUser._id
  949. )
  950. expect(fullEmails[0].affiliation.cachedLastDayToReconfirm).to.equal(
  951. lastDay
  952. )
  953. })
  954. it('should NOT set cachedLastDayToReconfirm for SSO institutions if email is NOT linked to SSO', async function () {
  955. this.getUserAffiliations.resolves(affiliationsData)
  956. this.UserGetter.promises.getUser = sinon.stub().resolves(user)
  957. const fullEmails = await this.UserGetter.promises.getUserFullEmails(
  958. this.fakeUser._id
  959. )
  960. expect(fullEmails[0].affiliation.cachedLastDayToReconfirm).to.equal(
  961. lastDay
  962. )
  963. })
  964. it('should set cachedLastDayToReconfirm for non-SSO institutions', async function () {
  965. this.getUserAffiliations.resolves(affiliationsData)
  966. this.UserGetter.promises.getUser = sinon.stub().resolves(user)
  967. const fullEmails = await this.UserGetter.promises.getUserFullEmails(
  968. this.fakeUser._id
  969. )
  970. expect(fullEmails[0].affiliation.cachedLastDayToReconfirm).to.equal(
  971. lastDay
  972. )
  973. })
  974. })
  975. })
  976. })
  977. describe('getUserbyMainEmail', function () {
  978. it('query user by main email', function (done) {
  979. const email = 'hello@world.com'
  980. const projection = { emails: 1 }
  981. this.UserGetter.getUserByMainEmail(email, projection, (error, user) => {
  982. expect(error).to.not.exist
  983. this.findOne.called.should.equal(true)
  984. this.findOne.calledWith({ email }, { projection }).should.equal(true)
  985. done()
  986. })
  987. })
  988. it('return user if found', function (done) {
  989. const email = 'hello@world.com'
  990. this.UserGetter.getUserByMainEmail(email, (error, user) => {
  991. expect(error).to.not.exist
  992. user.should.deep.equal(this.fakeUser)
  993. done()
  994. })
  995. })
  996. it('trim email', function (done) {
  997. const email = 'hello@world.com'
  998. this.UserGetter.getUserByMainEmail(` ${email} `, (error, user) => {
  999. expect(error).to.not.exist
  1000. this.findOne.called.should.equal(true)
  1001. this.findOne.calledWith({ email }).should.equal(true)
  1002. done()
  1003. })
  1004. })
  1005. })
  1006. describe('getUserByAnyEmail', function () {
  1007. it('query user for any email', function (done) {
  1008. const email = 'hello@world.com'
  1009. const expectedQuery = {
  1010. emails: { $exists: true },
  1011. 'emails.email': email,
  1012. }
  1013. const projection = { emails: 1 }
  1014. this.UserGetter.getUserByAnyEmail(
  1015. ` ${email} `,
  1016. projection,
  1017. (error, user) => {
  1018. expect(error).to.not.exist
  1019. this.findOne
  1020. .calledWith(expectedQuery, { projection })
  1021. .should.equal(true)
  1022. user.should.deep.equal(this.fakeUser)
  1023. done()
  1024. }
  1025. )
  1026. })
  1027. it('query contains $exists:true so partial index is used', function (done) {
  1028. const expectedQuery = {
  1029. emails: { $exists: true },
  1030. 'emails.email': '',
  1031. }
  1032. this.UserGetter.getUserByAnyEmail('', {}, (error, user) => {
  1033. expect(error).to.not.exist
  1034. this.findOne
  1035. .calledWith(expectedQuery, { projection: {} })
  1036. .should.equal(true)
  1037. done()
  1038. })
  1039. })
  1040. it('checks main email as well', function (done) {
  1041. this.findOne.callsArgWith(2, null, null)
  1042. const email = 'hello@world.com'
  1043. const projection = { emails: 1 }
  1044. this.UserGetter.getUserByAnyEmail(
  1045. ` ${email} `,
  1046. projection,
  1047. (error, user) => {
  1048. expect(error).to.not.exist
  1049. this.findOne.calledTwice.should.equal(true)
  1050. this.findOne.calledWith({ email }, { projection }).should.equal(true)
  1051. done()
  1052. }
  1053. )
  1054. })
  1055. })
  1056. describe('getUsersByHostname', function () {
  1057. it('should find user by hostname', function (done) {
  1058. const hostname = 'bar.foo'
  1059. const expectedQuery = {
  1060. emails: { $exists: true },
  1061. 'emails.reversedHostname': hostname.split('').reverse().join(''),
  1062. }
  1063. const projection = { emails: 1 }
  1064. this.UserGetter.getUsersByHostname(
  1065. hostname,
  1066. projection,
  1067. (error, users) => {
  1068. expect(error).to.not.exist
  1069. this.find.calledOnce.should.equal(true)
  1070. this.find.calledWith(expectedQuery, { projection }).should.equal(true)
  1071. done()
  1072. }
  1073. )
  1074. })
  1075. })
  1076. describe('getUsersByAnyConfirmedEmail', function () {
  1077. it('should find users by confirmed email', function (done) {
  1078. const emails = ['confirmed@example.com']
  1079. this.UserGetter.getUsersByAnyConfirmedEmail(emails, (error, users) => {
  1080. expect(error).to.not.exist
  1081. expect(this.find).to.be.calledOnceWith(
  1082. {
  1083. 'emails.email': { $in: emails }, // use the index on emails.email
  1084. emails: {
  1085. $exists: true,
  1086. $elemMatch: {
  1087. email: { $in: emails },
  1088. confirmedAt: { $exists: true },
  1089. },
  1090. },
  1091. },
  1092. { projection: {} }
  1093. )
  1094. done()
  1095. })
  1096. })
  1097. })
  1098. describe('getUsersByV1Id', function () {
  1099. it('should find users by list of v1 ids', function (done) {
  1100. const v1Ids = [501]
  1101. const expectedQuery = {
  1102. 'overleaf.id': { $in: v1Ids },
  1103. }
  1104. const projection = { emails: 1 }
  1105. this.UserGetter.getUsersByV1Ids(v1Ids, projection, (error, users) => {
  1106. expect(error).to.not.exist
  1107. this.find.calledOnce.should.equal(true)
  1108. this.find.calledWith(expectedQuery, { projection }).should.equal(true)
  1109. done()
  1110. })
  1111. })
  1112. })
  1113. describe('ensureUniqueEmailAddress', function () {
  1114. beforeEach(function () {
  1115. this.UserGetter.getUserByAnyEmail = sinon.stub()
  1116. })
  1117. it('should return error if existing user is found', function (done) {
  1118. this.UserGetter.getUserByAnyEmail.callsArgWith(1, null, this.fakeUser)
  1119. this.UserGetter.ensureUniqueEmailAddress(this.newEmail, err => {
  1120. expect(err).to.exist
  1121. expect(err).to.be.an.instanceof(Errors.EmailExistsError)
  1122. done()
  1123. })
  1124. })
  1125. it('should return null if no user is found', function (done) {
  1126. this.UserGetter.getUserByAnyEmail.callsArgWith(1)
  1127. this.UserGetter.ensureUniqueEmailAddress(this.newEmail, err => {
  1128. expect(err).not.to.exist
  1129. done()
  1130. })
  1131. })
  1132. })
  1133. })