UserGetterTests.js 43 KB

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