UserGetterTests.js 40 KB

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