UserGetterTests.js 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262
  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(30, 'days')
  381. .toDate() // expires in 30 days and reconfirmNotificationDays is set to 14
  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. it('should flag to show notification if v2 shows as reconfirmation upcoming but v1 does not', function (done) {
  924. const email = 'abc123@test.com'
  925. const { maxConfirmationMonths } = institutionNonSSO
  926. const datePastReconfirmation = moment()
  927. .subtract(maxConfirmationMonths, 'months')
  928. .add(3, 'day')
  929. .toDate()
  930. const dateNotPastReconfirmation = moment().add(1, 'month').toDate()
  931. const affiliationsData = [
  932. {
  933. email,
  934. licence: 'free',
  935. institution: institutionNonSSO,
  936. last_day_to_reconfirm: dateNotPastReconfirmation,
  937. },
  938. ]
  939. const user = {
  940. _id: '12390i',
  941. email,
  942. emails: [
  943. {
  944. email,
  945. confirmedAt: datePastReconfirmation,
  946. default: true,
  947. },
  948. ],
  949. }
  950. this.getUserAffiliations.resolves(affiliationsData)
  951. this.UserGetter.promises.getUser = sinon.stub().resolves(user)
  952. this.UserGetter.getUserFullEmails(
  953. this.fakeUser._id,
  954. (error, fullEmails) => {
  955. expect(error).to.not.exist
  956. expect(
  957. fullEmails[0].affiliation.inReconfirmNotificationPeriod
  958. ).to.equal(true)
  959. done()
  960. }
  961. )
  962. })
  963. describe('cachedLastDayToReconfirm', function () {
  964. const email = 'abc123@test.com'
  965. const confirmedAt = new Date('2019-07-11T18:25:01.639Z')
  966. const lastDay = '2020-07-11T18:25:01.639Z'
  967. const affiliationsData = [
  968. {
  969. email,
  970. licence: 'professional',
  971. institution: institutionSSO,
  972. last_day_to_reconfirm: lastDay,
  973. past_reconfirm_date: true,
  974. },
  975. ]
  976. const user = {
  977. _id: '12390i',
  978. email,
  979. emails: [
  980. {
  981. email,
  982. confirmedAt,
  983. default: true,
  984. },
  985. ],
  986. }
  987. it('should set cachedLastDayToReconfirm for SSO institutions if email is linked to SSO', async function () {
  988. const userLinked = Object.assign({}, user)
  989. userLinked.emails[0].samlProviderId = institutionSSO.id.toString()
  990. this.getUserAffiliations.resolves(affiliationsData)
  991. this.UserGetter.promises.getUser = sinon.stub().resolves(userLinked)
  992. const fullEmails = await this.UserGetter.promises.getUserFullEmails(
  993. this.fakeUser._id
  994. )
  995. expect(fullEmails[0].affiliation.cachedLastDayToReconfirm).to.equal(
  996. lastDay
  997. )
  998. })
  999. it('should NOT set cachedLastDayToReconfirm for SSO institutions if email is NOT linked to SSO', async function () {
  1000. this.getUserAffiliations.resolves(affiliationsData)
  1001. this.UserGetter.promises.getUser = sinon.stub().resolves(user)
  1002. const fullEmails = await this.UserGetter.promises.getUserFullEmails(
  1003. this.fakeUser._id
  1004. )
  1005. expect(fullEmails[0].affiliation.cachedLastDayToReconfirm).to.equal(
  1006. lastDay
  1007. )
  1008. })
  1009. it('should set cachedLastDayToReconfirm for non-SSO institutions', async function () {
  1010. this.getUserAffiliations.resolves(affiliationsData)
  1011. this.UserGetter.promises.getUser = sinon.stub().resolves(user)
  1012. const fullEmails = await this.UserGetter.promises.getUserFullEmails(
  1013. this.fakeUser._id
  1014. )
  1015. expect(fullEmails[0].affiliation.cachedLastDayToReconfirm).to.equal(
  1016. lastDay
  1017. )
  1018. })
  1019. })
  1020. })
  1021. })
  1022. describe('getUserConfirmedEmails', function () {
  1023. beforeEach(function () {
  1024. this.fakeUser = {
  1025. emails: [
  1026. {
  1027. email: 'email1@foo.bar',
  1028. reversedHostname: 'rab.oof',
  1029. confirmedAt: new Date(),
  1030. },
  1031. { email: 'email2@foo.bar', reversedHostname: 'rab.oof' },
  1032. {
  1033. email: 'email3@foo.bar',
  1034. reversedHostname: 'rab.oof',
  1035. confirmedAt: new Date(),
  1036. },
  1037. ],
  1038. }
  1039. this.UserGetter.promises.getUser = sinon.stub().resolves(this.fakeUser)
  1040. })
  1041. it('should get user', async function () {
  1042. const projection = { emails: 1 }
  1043. await this.UserGetter.promises.getUserConfirmedEmails(this.fakeUser._id)
  1044. this.UserGetter.promises.getUser
  1045. .calledWith(this.fakeUser._id, projection)
  1046. .should.equal(true)
  1047. })
  1048. it('should return only confirmed emails', async function () {
  1049. const confirmedEmails =
  1050. await this.UserGetter.promises.getUserConfirmedEmails(this.fakeUser._id)
  1051. expect(confirmedEmails.length).to.equal(2)
  1052. expect(confirmedEmails[0].email).to.equal('email1@foo.bar')
  1053. expect(confirmedEmails[1].email).to.equal('email3@foo.bar')
  1054. })
  1055. })
  1056. describe('getUserbyMainEmail', function () {
  1057. it('query user by main email', function (done) {
  1058. const email = 'hello@world.com'
  1059. const projection = { emails: 1 }
  1060. this.UserGetter.getUserByMainEmail(email, projection, (error, user) => {
  1061. expect(error).to.not.exist
  1062. this.findOne.called.should.equal(true)
  1063. this.findOne.calledWith({ email }, { projection }).should.equal(true)
  1064. done()
  1065. })
  1066. })
  1067. it('return user if found', function (done) {
  1068. const email = 'hello@world.com'
  1069. this.UserGetter.getUserByMainEmail(email, (error, user) => {
  1070. expect(error).to.not.exist
  1071. user.should.deep.equal(this.fakeUser)
  1072. done()
  1073. })
  1074. })
  1075. it('trim email', function (done) {
  1076. const email = 'hello@world.com'
  1077. this.UserGetter.getUserByMainEmail(` ${email} `, (error, user) => {
  1078. expect(error).to.not.exist
  1079. this.findOne.called.should.equal(true)
  1080. this.findOne.calledWith({ email }).should.equal(true)
  1081. done()
  1082. })
  1083. })
  1084. })
  1085. describe('getUserByAnyEmail', function () {
  1086. it('query user for any email', function (done) {
  1087. const email = 'hello@world.com'
  1088. const expectedQuery = {
  1089. emails: { $exists: true },
  1090. 'emails.email': email,
  1091. }
  1092. const projection = { emails: 1 }
  1093. this.UserGetter.getUserByAnyEmail(
  1094. ` ${email} `,
  1095. projection,
  1096. (error, user) => {
  1097. expect(error).to.not.exist
  1098. this.findOne
  1099. .calledWith(expectedQuery, { projection })
  1100. .should.equal(true)
  1101. user.should.deep.equal(this.fakeUser)
  1102. done()
  1103. }
  1104. )
  1105. })
  1106. it('query contains $exists:true so partial index is used', function (done) {
  1107. const expectedQuery = {
  1108. emails: { $exists: true },
  1109. 'emails.email': '',
  1110. }
  1111. this.UserGetter.getUserByAnyEmail('', {}, (error, user) => {
  1112. expect(error).to.not.exist
  1113. this.findOne
  1114. .calledWith(expectedQuery, { projection: {} })
  1115. .should.equal(true)
  1116. done()
  1117. })
  1118. })
  1119. it('checks main email as well', function (done) {
  1120. this.findOne.callsArgWith(2, null, null)
  1121. const email = 'hello@world.com'
  1122. const projection = { emails: 1 }
  1123. this.UserGetter.getUserByAnyEmail(
  1124. ` ${email} `,
  1125. projection,
  1126. (error, user) => {
  1127. expect(error).to.not.exist
  1128. this.findOne.calledTwice.should.equal(true)
  1129. this.findOne.calledWith({ email }, { projection }).should.equal(true)
  1130. done()
  1131. }
  1132. )
  1133. })
  1134. })
  1135. describe('getUsersByHostname', function () {
  1136. it('should find user by hostname', function (done) {
  1137. const hostname = 'bar.foo'
  1138. const expectedQuery = {
  1139. emails: { $exists: true },
  1140. 'emails.reversedHostname': hostname.split('').reverse().join(''),
  1141. }
  1142. const projection = { emails: 1 }
  1143. this.UserGetter.getUsersByHostname(
  1144. hostname,
  1145. projection,
  1146. (error, users) => {
  1147. expect(error).to.not.exist
  1148. this.find.calledOnce.should.equal(true)
  1149. this.find.calledWith(expectedQuery, { projection }).should.equal(true)
  1150. done()
  1151. }
  1152. )
  1153. })
  1154. })
  1155. describe('getUsersByAnyConfirmedEmail', function () {
  1156. it('should find users by confirmed email', function (done) {
  1157. const emails = ['confirmed@example.com']
  1158. this.UserGetter.getUsersByAnyConfirmedEmail(emails, (error, users) => {
  1159. expect(error).to.not.exist
  1160. expect(this.find).to.be.calledOnceWith(
  1161. {
  1162. 'emails.email': { $in: emails }, // use the index on emails.email
  1163. emails: {
  1164. $exists: true,
  1165. $elemMatch: {
  1166. email: { $in: emails },
  1167. confirmedAt: { $exists: true },
  1168. },
  1169. },
  1170. },
  1171. { projection: {} }
  1172. )
  1173. done()
  1174. })
  1175. })
  1176. })
  1177. describe('getUsersByV1Id', function () {
  1178. it('should find users by list of v1 ids', function (done) {
  1179. const v1Ids = [501]
  1180. const expectedQuery = {
  1181. 'overleaf.id': { $in: v1Ids },
  1182. }
  1183. const projection = { emails: 1 }
  1184. this.UserGetter.getUsersByV1Ids(v1Ids, projection, (error, users) => {
  1185. expect(error).to.not.exist
  1186. this.find.calledOnce.should.equal(true)
  1187. this.find.calledWith(expectedQuery, { projection }).should.equal(true)
  1188. done()
  1189. })
  1190. })
  1191. })
  1192. describe('ensureUniqueEmailAddress', function () {
  1193. beforeEach(function () {
  1194. this.UserGetter.getUserByAnyEmail = sinon.stub()
  1195. })
  1196. it('should return error if existing user is found', function (done) {
  1197. this.UserGetter.getUserByAnyEmail.callsArgWith(1, null, this.fakeUser)
  1198. this.UserGetter.ensureUniqueEmailAddress(this.newEmail, err => {
  1199. expect(err).to.exist
  1200. expect(err).to.be.an.instanceof(Errors.EmailExistsError)
  1201. done()
  1202. })
  1203. })
  1204. it('should return null if no user is found', function (done) {
  1205. this.UserGetter.getUserByAnyEmail.callsArgWith(1)
  1206. this.UserGetter.ensureUniqueEmailAddress(this.newEmail, err => {
  1207. expect(err).not.to.exist
  1208. done()
  1209. })
  1210. })
  1211. })
  1212. })