| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411 |
- const { ObjectId } = require('mongojs')
- const should = require('chai').should()
- const SandboxedModule = require('sandboxed-module')
- const assert = require('assert')
- const path = require('path')
- const sinon = require('sinon')
- const modulePath = path.join(
- __dirname,
- '../../../../app/src/Features/User/UserGetter'
- )
- const { expect } = require('chai')
- const Errors = require('../../../../app/src/Features/Errors/Errors')
- describe('UserGetter', function() {
- beforeEach(function() {
- this.fakeUser = {
- _id: '12390i',
- email: 'email2@foo.bar',
- emails: [
- {
- email: 'email1@foo.bar',
- reversedHostname: 'rab.oof',
- confirmedAt: new Date()
- },
- { email: 'email2@foo.bar', reversedHostname: 'rab.oof' }
- ]
- }
- this.findOne = sinon.stub().callsArgWith(2, null, this.fakeUser)
- this.find = sinon.stub().callsArgWith(2, null, [this.fakeUser])
- this.Mongo = {
- db: {
- users: {
- findOne: this.findOne,
- find: this.find
- }
- },
- ObjectId
- }
- const settings = { apis: { v1: { url: 'v1.url', user: '', pass: '' } } }
- this.getUserAffiliations = sinon.stub().callsArgWith(1, null, [])
- this.UserGetter = SandboxedModule.require(modulePath, {
- globals: {
- console: console
- },
- requires: {
- 'logger-sharelatex': {
- log() {}
- },
- '../../infrastructure/mongojs': this.Mongo,
- 'metrics-sharelatex': {
- timeAsyncMethod: sinon.stub()
- },
- 'settings-sharelatex': settings,
- '../Institutions/InstitutionsAPI': {
- getUserAffiliations: this.getUserAffiliations
- },
- '../../infrastructure/Features': {
- hasFeature: sinon.stub().returns(true)
- },
- '../Errors/Errors': Errors
- }
- })
- })
- describe('getUser', function() {
- it('should get user', function(done) {
- const query = { _id: 'foo' }
- const projection = { email: 1 }
- this.UserGetter.getUser(query, projection, (error, user) => {
- expect(error).to.not.exist
- this.findOne.called.should.equal(true)
- this.findOne.calledWith(query, projection).should.equal(true)
- user.should.deep.equal(this.fakeUser)
- done()
- })
- })
- it('should not allow null query', function(done) {
- this.UserGetter.getUser(null, {}, error => {
- error.should.exist
- error.message.should.equal('no query provided')
- done()
- })
- })
- })
- describe('getUsers', function() {
- it('should get users with array of userIds', function(done) {
- const query = [new ObjectId()]
- const projection = { email: 1 }
- this.UserGetter.getUsers(query, projection, (error, users) => {
- expect(error).to.not.exist
- this.find.should.have.been.calledWithMatch(
- { _id: { $in: query } },
- projection
- )
- users.should.deep.equal([this.fakeUser])
- done()
- })
- })
- it('should not allow null query', function(done) {
- this.UserGetter.getUser(null, {}, error => {
- error.should.exist
- error.message.should.equal('no query provided')
- done()
- })
- })
- })
- describe('getUserFullEmails', function() {
- it('should get user', function(done) {
- this.UserGetter.getUser = sinon
- .stub()
- .callsArgWith(2, null, this.fakeUser)
- const projection = { email: 1, emails: 1, samlIdentifiers: 1 }
- this.UserGetter.getUserFullEmails(
- this.fakeUser._id,
- (error, fullEmails) => {
- expect(error).to.not.exist
- this.UserGetter.getUser.called.should.equal(true)
- this.UserGetter.getUser
- .calledWith(this.fakeUser._id, projection)
- .should.equal(true)
- done()
- }
- )
- })
- it('should fetch emails data', function(done) {
- this.UserGetter.getUser = sinon
- .stub()
- .callsArgWith(2, null, this.fakeUser)
- this.UserGetter.getUserFullEmails(
- this.fakeUser._id,
- (error, fullEmails) => {
- expect(error).to.not.exist
- assert.deepEqual(fullEmails, [
- {
- email: 'email1@foo.bar',
- reversedHostname: 'rab.oof',
- confirmedAt: this.fakeUser.emails[0].confirmedAt,
- emailHasInstitutionLicence: false,
- default: false
- },
- {
- email: 'email2@foo.bar',
- reversedHostname: 'rab.oof',
- emailHasInstitutionLicence: false,
- default: true
- }
- ])
- done()
- }
- )
- })
- it('should merge affiliation data', function(done) {
- this.UserGetter.getUser = sinon
- .stub()
- .callsArgWith(2, null, this.fakeUser)
- const affiliationsData = [
- {
- email: 'email1@foo.bar',
- role: 'Prof',
- department: 'Maths',
- inferred: false,
- licence: 'pro_plus',
- institution: {
- name: 'University Name',
- isUniversity: true,
- confirmed: true
- }
- }
- ]
- this.getUserAffiliations.callsArgWith(1, null, affiliationsData)
- this.UserGetter.getUserFullEmails(
- this.fakeUser._id,
- (error, fullEmails) => {
- expect(error).to.not.exist
- assert.deepEqual(fullEmails, [
- {
- email: 'email1@foo.bar',
- reversedHostname: 'rab.oof',
- confirmedAt: this.fakeUser.emails[0].confirmedAt,
- default: false,
- emailHasInstitutionLicence: true,
- affiliation: {
- institution: affiliationsData[0].institution,
- inferred: affiliationsData[0].inferred,
- department: affiliationsData[0].department,
- role: affiliationsData[0].role,
- licence: affiliationsData[0].licence
- }
- },
- {
- email: 'email2@foo.bar',
- reversedHostname: 'rab.oof',
- emailHasInstitutionLicence: false,
- default: true
- }
- ])
- done()
- }
- )
- })
- it('should merge SAML identifier', function(done) {
- const fakeSamlIdentifiers = [
- { providerId: 'saml_id', exteranlUserId: 'whatever' }
- ]
- const fakeUserWithSaml = this.fakeUser
- fakeUserWithSaml.emails[0].samlProviderId = 'saml_id'
- fakeUserWithSaml.samlIdentifiers = fakeSamlIdentifiers
- this.UserGetter.getUser = sinon.stub().yields(null, this.fakeUser)
- this.getUserAffiliations.callsArgWith(1, null, [])
- this.UserGetter.getUserFullEmails(
- this.fakeUser._id,
- (error, fullEmails) => {
- expect(error).to.not.exist
- assert.deepEqual(fullEmails, [
- {
- email: 'email1@foo.bar',
- reversedHostname: 'rab.oof',
- confirmedAt: this.fakeUser.emails[0].confirmedAt,
- default: false,
- emailHasInstitutionLicence: false,
- samlProviderId: 'saml_id',
- samlIdentifier: fakeSamlIdentifiers[0]
- },
- {
- email: 'email2@foo.bar',
- reversedHostname: 'rab.oof',
- emailHasInstitutionLicence: false,
- default: true
- }
- ])
- done()
- }
- )
- })
- it('should get user when it has no emails field', function(done) {
- this.fakeUser = {
- _id: '12390i',
- email: 'email2@foo.bar'
- }
- this.UserGetter.getUser = sinon
- .stub()
- .callsArgWith(2, null, this.fakeUser)
- const projection = { email: 1, emails: 1, samlIdentifiers: 1 }
- this.UserGetter.getUserFullEmails(
- this.fakeUser._id,
- (error, fullEmails) => {
- expect(error).to.not.exist
- this.UserGetter.getUser.called.should.equal(true)
- this.UserGetter.getUser
- .calledWith(this.fakeUser._id, projection)
- .should.equal(true)
- assert.deepEqual(fullEmails, [])
- done()
- }
- )
- })
- })
- describe('getUserbyMainEmail', function() {
- it('query user by main email', function(done) {
- const email = 'hello@world.com'
- const projection = { emails: 1 }
- this.UserGetter.getUserByMainEmail(email, projection, (error, user) => {
- expect(error).to.not.exist
- this.findOne.called.should.equal(true)
- this.findOne.calledWith({ email }, projection).should.equal(true)
- done()
- })
- })
- it('return user if found', function(done) {
- const email = 'hello@world.com'
- this.UserGetter.getUserByMainEmail(email, (error, user) => {
- expect(error).to.not.exist
- user.should.deep.equal(this.fakeUser)
- done()
- })
- })
- it('trim email', function(done) {
- const email = 'hello@world.com'
- this.UserGetter.getUserByMainEmail(` ${email} `, (error, user) => {
- expect(error).to.not.exist
- this.findOne.called.should.equal(true)
- this.findOne.calledWith({ email }).should.equal(true)
- done()
- })
- })
- })
- describe('getUserByAnyEmail', function() {
- it('query user for any email', function(done) {
- const email = 'hello@world.com'
- const expectedQuery = {
- emails: { $exists: true },
- 'emails.email': email
- }
- const projection = { emails: 1 }
- this.UserGetter.getUserByAnyEmail(
- ` ${email} `,
- projection,
- (error, user) => {
- expect(error).to.not.exist
- this.findOne.calledWith(expectedQuery, projection).should.equal(true)
- user.should.deep.equal(this.fakeUser)
- done()
- }
- )
- })
- it('query contains $exists:true so partial index is used', function(done) {
- const expectedQuery = {
- emails: { $exists: true },
- 'emails.email': ''
- }
- this.UserGetter.getUserByAnyEmail('', {}, (error, user) => {
- expect(error).to.not.exist
- this.findOne.calledWith(expectedQuery, {}).should.equal(true)
- done()
- })
- })
- it('checks main email as well', function(done) {
- this.findOne.callsArgWith(2, null, null)
- const email = 'hello@world.com'
- const projection = { emails: 1 }
- this.UserGetter.getUserByAnyEmail(
- ` ${email} `,
- projection,
- (error, user) => {
- expect(error).to.not.exist
- this.findOne.calledTwice.should.equal(true)
- this.findOne.calledWith({ email }, projection).should.equal(true)
- done()
- }
- )
- })
- })
- describe('getUsersByHostname', function() {
- it('should find user by hostname', function(done) {
- const hostname = 'bar.foo'
- const expectedQuery = {
- emails: { $exists: true },
- 'emails.reversedHostname': hostname
- .split('')
- .reverse()
- .join('')
- }
- const projection = { emails: 1 }
- this.UserGetter.getUsersByHostname(
- hostname,
- projection,
- (error, users) => {
- expect(error).to.not.exist
- this.find.calledOnce.should.equal(true)
- this.find.calledWith(expectedQuery, projection).should.equal(true)
- done()
- }
- )
- })
- })
- describe('getUsersByV1Id', function() {
- it('should find users by list of v1 ids', function(done) {
- const v1Ids = [501]
- const expectedQuery = {
- 'overleaf.id': { $in: v1Ids }
- }
- const projection = { emails: 1 }
- this.UserGetter.getUsersByV1Ids(v1Ids, projection, (error, users) => {
- expect(error).to.not.exist
- this.find.calledOnce.should.equal(true)
- this.find.calledWith(expectedQuery, projection).should.equal(true)
- done()
- })
- })
- })
- describe('ensureUniqueEmailAddress', function() {
- beforeEach(function() {
- this.UserGetter.getUserByAnyEmail = sinon.stub()
- })
- it('should return error if existing user is found', function(done) {
- this.UserGetter.getUserByAnyEmail.callsArgWith(1, null, this.fakeUser)
- this.UserGetter.ensureUniqueEmailAddress(this.newEmail, err => {
- should.exist(err)
- expect(err).to.be.an.instanceof(Errors.EmailExistsError)
- done()
- })
- })
- it('should return null if no user is found', function(done) {
- this.UserGetter.getUserByAnyEmail.callsArgWith(1)
- this.UserGetter.ensureUniqueEmailAddress(this.newEmail, err => {
- should.not.exist(err)
- done()
- })
- })
- })
- })
|