UserGetterTests.js 43 KB

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