UserGetterTests.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. const { ObjectId } = require('mongodb')
  2. const should = require('chai').should()
  3. const SandboxedModule = require('sandboxed-module')
  4. const assert = require('assert')
  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. describe('UserGetter', function() {
  14. beforeEach(function() {
  15. this.fakeUser = {
  16. _id: '12390i',
  17. email: 'email2@foo.bar',
  18. emails: [
  19. {
  20. email: 'email1@foo.bar',
  21. reversedHostname: 'rab.oof',
  22. confirmedAt: new Date()
  23. },
  24. { email: 'email2@foo.bar', reversedHostname: 'rab.oof' }
  25. ]
  26. }
  27. this.findOne = sinon.stub().callsArgWith(2, null, this.fakeUser)
  28. this.findToArrayStub = sinon.stub().yields(null, [this.fakeUser])
  29. this.find = sinon.stub().returns({ toArray: this.findToArrayStub })
  30. this.Mongo = {
  31. db: {
  32. users: {
  33. findOne: this.findOne,
  34. find: this.find
  35. }
  36. },
  37. ObjectId
  38. }
  39. const settings = { apis: { v1: { url: 'v1.url', user: '', pass: '' } } }
  40. this.getUserAffiliations = sinon.stub().callsArgWith(1, null, [])
  41. this.UserGetter = SandboxedModule.require(modulePath, {
  42. globals: {
  43. console: console
  44. },
  45. requires: {
  46. 'logger-sharelatex': {
  47. log() {}
  48. },
  49. '../../infrastructure/mongodb': this.Mongo,
  50. 'metrics-sharelatex': {
  51. timeAsyncMethod: sinon.stub()
  52. },
  53. 'settings-sharelatex': settings,
  54. '../Institutions/InstitutionsAPI': {
  55. getUserAffiliations: this.getUserAffiliations
  56. },
  57. '../../infrastructure/Features': {
  58. hasFeature: sinon.stub().returns(true)
  59. },
  60. '../Errors/Errors': Errors
  61. }
  62. })
  63. })
  64. describe('getUser', function() {
  65. it('should get user', function(done) {
  66. const query = { _id: 'foo' }
  67. const projection = { email: 1 }
  68. this.UserGetter.getUser(query, projection, (error, user) => {
  69. expect(error).to.not.exist
  70. this.findOne.called.should.equal(true)
  71. this.findOne.calledWith(query, { projection }).should.equal(true)
  72. user.should.deep.equal(this.fakeUser)
  73. done()
  74. })
  75. })
  76. it('should not allow null query', function(done) {
  77. this.UserGetter.getUser(null, {}, error => {
  78. error.should.exist
  79. error.message.should.equal('no query provided')
  80. done()
  81. })
  82. })
  83. })
  84. describe('getUsers', function() {
  85. it('should get users with array of userIds', function(done) {
  86. const query = [new ObjectId()]
  87. const projection = { email: 1 }
  88. this.UserGetter.getUsers(query, projection, (error, users) => {
  89. expect(error).to.not.exist
  90. this.find.should.have.been.calledWithMatch(
  91. { _id: { $in: query } },
  92. { projection }
  93. )
  94. users.should.deep.equal([this.fakeUser])
  95. done()
  96. })
  97. })
  98. it('should not allow null query', function(done) {
  99. this.UserGetter.getUser(null, {}, error => {
  100. error.should.exist
  101. error.message.should.equal('no query provided')
  102. done()
  103. })
  104. })
  105. })
  106. describe('getUserFullEmails', function() {
  107. it('should get user', function(done) {
  108. this.UserGetter.getUser = sinon
  109. .stub()
  110. .callsArgWith(2, null, this.fakeUser)
  111. const projection = { email: 1, emails: 1, samlIdentifiers: 1 }
  112. this.UserGetter.getUserFullEmails(
  113. this.fakeUser._id,
  114. (error, fullEmails) => {
  115. expect(error).to.not.exist
  116. this.UserGetter.getUser.called.should.equal(true)
  117. this.UserGetter.getUser
  118. .calledWith(this.fakeUser._id, projection)
  119. .should.equal(true)
  120. done()
  121. }
  122. )
  123. })
  124. it('should fetch emails data', function(done) {
  125. this.UserGetter.getUser = sinon
  126. .stub()
  127. .callsArgWith(2, null, this.fakeUser)
  128. this.UserGetter.getUserFullEmails(
  129. this.fakeUser._id,
  130. (error, fullEmails) => {
  131. expect(error).to.not.exist
  132. assert.deepEqual(fullEmails, [
  133. {
  134. email: 'email1@foo.bar',
  135. reversedHostname: 'rab.oof',
  136. confirmedAt: this.fakeUser.emails[0].confirmedAt,
  137. emailHasInstitutionLicence: false,
  138. default: false
  139. },
  140. {
  141. email: 'email2@foo.bar',
  142. reversedHostname: 'rab.oof',
  143. emailHasInstitutionLicence: false,
  144. default: true
  145. }
  146. ])
  147. done()
  148. }
  149. )
  150. })
  151. it('should merge affiliation data', function(done) {
  152. this.UserGetter.getUser = sinon
  153. .stub()
  154. .callsArgWith(2, null, this.fakeUser)
  155. const affiliationsData = [
  156. {
  157. email: 'email1@foo.bar',
  158. role: 'Prof',
  159. department: 'Maths',
  160. inferred: false,
  161. licence: 'pro_plus',
  162. institution: {
  163. name: 'University Name',
  164. isUniversity: true,
  165. confirmed: true
  166. }
  167. }
  168. ]
  169. this.getUserAffiliations.callsArgWith(1, null, affiliationsData)
  170. this.UserGetter.getUserFullEmails(
  171. this.fakeUser._id,
  172. (error, fullEmails) => {
  173. expect(error).to.not.exist
  174. assert.deepEqual(fullEmails, [
  175. {
  176. email: 'email1@foo.bar',
  177. reversedHostname: 'rab.oof',
  178. confirmedAt: this.fakeUser.emails[0].confirmedAt,
  179. default: false,
  180. emailHasInstitutionLicence: true,
  181. affiliation: {
  182. institution: affiliationsData[0].institution,
  183. inferred: affiliationsData[0].inferred,
  184. department: affiliationsData[0].department,
  185. role: affiliationsData[0].role,
  186. licence: affiliationsData[0].licence
  187. }
  188. },
  189. {
  190. email: 'email2@foo.bar',
  191. reversedHostname: 'rab.oof',
  192. emailHasInstitutionLicence: false,
  193. default: true
  194. }
  195. ])
  196. done()
  197. }
  198. )
  199. })
  200. it('should merge SAML identifier', function(done) {
  201. const fakeSamlIdentifiers = [
  202. { providerId: 'saml_id', exteranlUserId: 'whatever' }
  203. ]
  204. const fakeUserWithSaml = this.fakeUser
  205. fakeUserWithSaml.emails[0].samlProviderId = 'saml_id'
  206. fakeUserWithSaml.samlIdentifiers = fakeSamlIdentifiers
  207. this.UserGetter.getUser = sinon.stub().yields(null, this.fakeUser)
  208. this.getUserAffiliations.callsArgWith(1, null, [])
  209. this.UserGetter.getUserFullEmails(
  210. this.fakeUser._id,
  211. (error, fullEmails) => {
  212. expect(error).to.not.exist
  213. assert.deepEqual(fullEmails, [
  214. {
  215. email: 'email1@foo.bar',
  216. reversedHostname: 'rab.oof',
  217. confirmedAt: this.fakeUser.emails[0].confirmedAt,
  218. default: false,
  219. emailHasInstitutionLicence: false,
  220. samlProviderId: 'saml_id',
  221. samlIdentifier: fakeSamlIdentifiers[0]
  222. },
  223. {
  224. email: 'email2@foo.bar',
  225. reversedHostname: 'rab.oof',
  226. emailHasInstitutionLicence: false,
  227. default: true
  228. }
  229. ])
  230. done()
  231. }
  232. )
  233. })
  234. it('should get user when it has no emails field', function(done) {
  235. this.fakeUser = {
  236. _id: '12390i',
  237. email: 'email2@foo.bar'
  238. }
  239. this.UserGetter.getUser = sinon
  240. .stub()
  241. .callsArgWith(2, null, this.fakeUser)
  242. const projection = { email: 1, emails: 1, samlIdentifiers: 1 }
  243. this.UserGetter.getUserFullEmails(
  244. this.fakeUser._id,
  245. (error, fullEmails) => {
  246. expect(error).to.not.exist
  247. this.UserGetter.getUser.called.should.equal(true)
  248. this.UserGetter.getUser
  249. .calledWith(this.fakeUser._id, projection)
  250. .should.equal(true)
  251. assert.deepEqual(fullEmails, [])
  252. done()
  253. }
  254. )
  255. })
  256. })
  257. describe('getUserbyMainEmail', function() {
  258. it('query user by main email', function(done) {
  259. const email = 'hello@world.com'
  260. const projection = { emails: 1 }
  261. this.UserGetter.getUserByMainEmail(email, projection, (error, user) => {
  262. expect(error).to.not.exist
  263. this.findOne.called.should.equal(true)
  264. this.findOne.calledWith({ email }, { projection }).should.equal(true)
  265. done()
  266. })
  267. })
  268. it('return user if found', function(done) {
  269. const email = 'hello@world.com'
  270. this.UserGetter.getUserByMainEmail(email, (error, user) => {
  271. expect(error).to.not.exist
  272. user.should.deep.equal(this.fakeUser)
  273. done()
  274. })
  275. })
  276. it('trim email', function(done) {
  277. const email = 'hello@world.com'
  278. this.UserGetter.getUserByMainEmail(` ${email} `, (error, user) => {
  279. expect(error).to.not.exist
  280. this.findOne.called.should.equal(true)
  281. this.findOne.calledWith({ email }).should.equal(true)
  282. done()
  283. })
  284. })
  285. })
  286. describe('getUserByAnyEmail', function() {
  287. it('query user for any email', function(done) {
  288. const email = 'hello@world.com'
  289. const expectedQuery = {
  290. emails: { $exists: true },
  291. 'emails.email': email
  292. }
  293. const projection = { emails: 1 }
  294. this.UserGetter.getUserByAnyEmail(
  295. ` ${email} `,
  296. projection,
  297. (error, user) => {
  298. expect(error).to.not.exist
  299. this.findOne
  300. .calledWith(expectedQuery, { projection })
  301. .should.equal(true)
  302. user.should.deep.equal(this.fakeUser)
  303. done()
  304. }
  305. )
  306. })
  307. it('query contains $exists:true so partial index is used', function(done) {
  308. const expectedQuery = {
  309. emails: { $exists: true },
  310. 'emails.email': ''
  311. }
  312. this.UserGetter.getUserByAnyEmail('', {}, (error, user) => {
  313. expect(error).to.not.exist
  314. this.findOne
  315. .calledWith(expectedQuery, { projection: {} })
  316. .should.equal(true)
  317. done()
  318. })
  319. })
  320. it('checks main email as well', function(done) {
  321. this.findOne.callsArgWith(2, null, null)
  322. const email = 'hello@world.com'
  323. const projection = { emails: 1 }
  324. this.UserGetter.getUserByAnyEmail(
  325. ` ${email} `,
  326. projection,
  327. (error, user) => {
  328. expect(error).to.not.exist
  329. this.findOne.calledTwice.should.equal(true)
  330. this.findOne.calledWith({ email }, { projection }).should.equal(true)
  331. done()
  332. }
  333. )
  334. })
  335. })
  336. describe('getUsersByHostname', function() {
  337. it('should find user by hostname', function(done) {
  338. const hostname = 'bar.foo'
  339. const expectedQuery = {
  340. emails: { $exists: true },
  341. 'emails.reversedHostname': hostname
  342. .split('')
  343. .reverse()
  344. .join('')
  345. }
  346. const projection = { emails: 1 }
  347. this.UserGetter.getUsersByHostname(
  348. hostname,
  349. projection,
  350. (error, users) => {
  351. expect(error).to.not.exist
  352. this.find.calledOnce.should.equal(true)
  353. this.find.calledWith(expectedQuery, { projection }).should.equal(true)
  354. done()
  355. }
  356. )
  357. })
  358. })
  359. describe('getUsersByV1Id', function() {
  360. it('should find users by list of v1 ids', function(done) {
  361. const v1Ids = [501]
  362. const expectedQuery = {
  363. 'overleaf.id': { $in: v1Ids }
  364. }
  365. const projection = { emails: 1 }
  366. this.UserGetter.getUsersByV1Ids(v1Ids, projection, (error, users) => {
  367. expect(error).to.not.exist
  368. this.find.calledOnce.should.equal(true)
  369. this.find.calledWith(expectedQuery, { projection }).should.equal(true)
  370. done()
  371. })
  372. })
  373. })
  374. describe('ensureUniqueEmailAddress', function() {
  375. beforeEach(function() {
  376. this.UserGetter.getUserByAnyEmail = sinon.stub()
  377. })
  378. it('should return error if existing user is found', function(done) {
  379. this.UserGetter.getUserByAnyEmail.callsArgWith(1, null, this.fakeUser)
  380. this.UserGetter.ensureUniqueEmailAddress(this.newEmail, err => {
  381. should.exist(err)
  382. expect(err).to.be.an.instanceof(Errors.EmailExistsError)
  383. done()
  384. })
  385. })
  386. it('should return null if no user is found', function(done) {
  387. this.UserGetter.getUserByAnyEmail.callsArgWith(1)
  388. this.UserGetter.ensureUniqueEmailAddress(this.newEmail, err => {
  389. should.not.exist(err)
  390. done()
  391. })
  392. })
  393. })
  394. })