| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570 |
- /* eslint-disable
- node/handle-callback-err,
- max-len,
- mocha/no-identical-title,
- no-return-assign,
- no-unused-vars,
- */
- // TODO: This file was created by bulk-decaffeinate.
- // Fix any style issues and re-enable lint.
- /*
- * decaffeinate suggestions:
- * DS102: Remove unnecessary code created because of implicit returns
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
- */
- const SandboxedModule = require('sandboxed-module')
- const should = require('chai').should()
- const { expect } = require('chai')
- const sinon = require('sinon')
- const { assert } = require('chai')
- const modulePath = '../../../../app/src/Features/References/ReferencesHandler'
- describe('ReferencesHandler', function() {
- beforeEach(function() {
- this.projectId = '222'
- this.fakeProject = {
- _id: this.projectId,
- owner_ref: (this.fakeOwner = {
- _id: 'some_owner',
- features: {
- references: false
- }
- }),
- rootFolder: [
- {
- docs: [
- { name: 'one.bib', _id: 'aaa' },
- { name: 'two.txt', _id: 'bbb' }
- ],
- folders: [
- {
- docs: [{ name: 'three.bib', _id: 'ccc' }],
- fileRefs: [{ name: 'four.bib', _id: 'ghg' }],
- folders: []
- }
- ]
- }
- ]
- }
- this.docIds = ['aaa', 'ccc']
- this.handler = SandboxedModule.require(modulePath, {
- globals: {
- console: console
- },
- requires: {
- 'logger-sharelatex': {
- log() {},
- warn() {},
- err() {}
- },
- 'settings-sharelatex': (this.settings = {
- apis: {
- references: { url: 'http://some.url/references' },
- docstore: { url: 'http://some.url/docstore' },
- filestore: { url: 'http://some.url/filestore' }
- }
- }),
- request: (this.request = {
- get: sinon.stub(),
- post: sinon.stub()
- }),
- '../Project/ProjectGetter': (this.ProjectGetter = {
- getProject: sinon.stub().callsArgWith(2, null, this.fakeProject)
- }),
- '../User/UserGetter': (this.UserGetter = {
- getUser: sinon.stub()
- }),
- '../DocumentUpdater/DocumentUpdaterHandler': (this.DocumentUpdaterHandler = {
- flushDocToMongo: sinon.stub().callsArgWith(2, null)
- }),
- '../../infrastructure/Features': (this.Features = {
- hasFeature: sinon.stub().returns(true)
- })
- }
- })
- return (this.fakeResponseData = {
- projectId: this.projectId,
- keys: ['k1', 'k2']
- })
- })
- describe('index', function() {
- beforeEach(function() {
- sinon.stub(this.handler, '_findBibDocIds')
- sinon.stub(this.handler, '_findBibFileIds')
- sinon.stub(this.handler, '_isFullIndex').callsArgWith(1, null, true)
- this.request.post.callsArgWith(
- 1,
- null,
- { statusCode: 200 },
- this.fakeResponseData
- )
- return (this.call = callback => {
- return this.handler.index(this.projectId, this.docIds, callback)
- })
- })
- describe('when references feature is disabled', function() {
- beforeEach(function() {
- this.Features.hasFeature.withArgs('references').returns(false)
- })
- it('should not try to retrieve any user information', function(done) {
- this.call(() => {
- this.UserGetter.getUser.callCount.should.equal(0)
- done()
- })
- })
- it('should not produce an error', function(done) {
- return this.call(err => {
- expect(err).to.equal(undefined)
- return done()
- })
- })
- })
- describe('with docIds as an array', function() {
- beforeEach(function() {
- return (this.docIds = ['aaa', 'ccc'])
- })
- it('should not call _findBibDocIds', function(done) {
- return this.call((err, data) => {
- this.handler._findBibDocIds.callCount.should.equal(0)
- return done()
- })
- })
- it('should call ProjectGetter.getProject', function(done) {
- return this.call((err, data) => {
- this.ProjectGetter.getProject.callCount.should.equal(1)
- this.ProjectGetter.getProject
- .calledWith(this.projectId)
- .should.equal(true)
- return done()
- })
- })
- it('should not call _findBibDocIds', function(done) {
- return this.call((err, data) => {
- this.handler._findBibDocIds.callCount.should.equal(0)
- return done()
- })
- })
- it('should call DocumentUpdaterHandler.flushDocToMongo', function(done) {
- return this.call((err, data) => {
- this.DocumentUpdaterHandler.flushDocToMongo.callCount.should.equal(2)
- this.docIds.forEach(docId => {
- return this.DocumentUpdaterHandler.flushDocToMongo
- .calledWith(this.projectId, docId)
- .should.equal(true)
- })
- return done()
- })
- })
- it('should make a request to references service', function(done) {
- return this.call((err, data) => {
- this.request.post.callCount.should.equal(1)
- const arg = this.request.post.firstCall.args[0]
- expect(arg.json).to.have.all.keys('docUrls', 'fullIndex')
- expect(arg.json.docUrls.length).to.equal(2)
- expect(arg.json.fullIndex).to.equal(true)
- return done()
- })
- })
- it('should not produce an error', function(done) {
- return this.call((err, data) => {
- expect(err).to.equal(null)
- return done()
- })
- })
- it('should return data', function(done) {
- return this.call((err, data) => {
- expect(data).to.not.equal(null)
- expect(data).to.not.equal(undefined)
- expect(data).to.equal(this.fakeResponseData)
- return done()
- })
- })
- })
- describe('when ProjectGetter.getProject produces an error', function() {
- beforeEach(function() {
- return this.ProjectGetter.getProject.callsArgWith(2, new Error('woops'))
- })
- it('should produce an error', function(done) {
- return this.call((err, data) => {
- expect(err).to.not.equal(null)
- expect(err).to.be.instanceof(Error)
- expect(data).to.equal(undefined)
- return done()
- })
- })
- it('should not send request', function(done) {
- return this.call((err, data) => {
- this.request.post.callCount.should.equal(0)
- return done()
- })
- })
- })
- describe('when _isFullIndex produces an error', function() {
- beforeEach(function() {
- this.ProjectGetter.getProject.callsArgWith(2, null, this.fakeProject)
- return this.handler._isFullIndex.callsArgWith(1, new Error('woops'))
- })
- it('should produce an error', function(done) {
- return this.call((err, data) => {
- expect(err).to.not.equal(null)
- expect(err).to.be.instanceof(Error)
- expect(data).to.equal(undefined)
- return done()
- })
- })
- it('should not send request', function(done) {
- return this.call((err, data) => {
- this.request.post.callCount.should.equal(0)
- return done()
- })
- })
- })
- describe('when flushDocToMongo produces an error', function() {
- beforeEach(function() {
- this.ProjectGetter.getProject.callsArgWith(2, null, this.fakeProject)
- this.handler._isFullIndex.callsArgWith(1, false)
- return this.DocumentUpdaterHandler.flushDocToMongo.callsArgWith(
- 2,
- new Error('woops')
- )
- })
- it('should produce an error', function(done) {
- return this.call((err, data) => {
- expect(err).to.not.equal(null)
- expect(err).to.be.instanceof(Error)
- expect(data).to.equal(undefined)
- return done()
- })
- })
- it('should not send request', function(done) {
- return this.call((err, data) => {
- this.request.post.callCount.should.equal(0)
- return done()
- })
- })
- })
- describe('when request produces an error', function() {
- beforeEach(function() {
- this.ProjectGetter.getProject.callsArgWith(2, null, this.fakeProject)
- this.handler._isFullIndex.callsArgWith(1, null, false)
- this.DocumentUpdaterHandler.flushDocToMongo.callsArgWith(2, null)
- return this.request.post.callsArgWith(1, new Error('woops'))
- })
- it('should produce an error', function(done) {
- return this.call((err, data) => {
- expect(err).to.not.equal(null)
- expect(err).to.be.instanceof(Error)
- expect(data).to.equal(undefined)
- return done()
- })
- })
- })
- describe('when request responds with error status', function() {
- beforeEach(function() {
- this.ProjectGetter.getProject.callsArgWith(2, null, this.fakeProject)
- this.handler._isFullIndex.callsArgWith(1, null, false)
- return this.request.post.callsArgWith(
- 1,
- null,
- { statusCode: 500 },
- null
- )
- })
- it('should produce an error', function(done) {
- return this.call((err, data) => {
- expect(err).to.not.equal(null)
- expect(err).to.be.instanceof(Error)
- expect(data).to.equal(undefined)
- return done()
- })
- })
- })
- })
- describe('indexAll', function() {
- beforeEach(function() {
- sinon.stub(this.handler, '_findBibDocIds').returns(['aaa', 'ccc'])
- sinon.stub(this.handler, '_findBibFileIds').returns(['fff', 'ggg'])
- sinon.stub(this.handler, '_isFullIndex').callsArgWith(1, null, true)
- this.request.post.callsArgWith(
- 1,
- null,
- { statusCode: 200 },
- this.fakeResponseData
- )
- return (this.call = callback => {
- return this.handler.indexAll(this.projectId, callback)
- })
- })
- it('should call _findBibDocIds', function(done) {
- return this.call((err, data) => {
- this.handler._findBibDocIds.callCount.should.equal(1)
- this.handler._findBibDocIds
- .calledWith(this.fakeProject)
- .should.equal(true)
- return done()
- })
- })
- it('should call _findBibFileIds', function(done) {
- return this.call((err, data) => {
- this.handler._findBibDocIds.callCount.should.equal(1)
- this.handler._findBibDocIds
- .calledWith(this.fakeProject)
- .should.equal(true)
- return done()
- })
- })
- it('should call DocumentUpdaterHandler.flushDocToMongo', function(done) {
- return this.call((err, data) => {
- this.DocumentUpdaterHandler.flushDocToMongo.callCount.should.equal(2)
- return done()
- })
- })
- it('should make a request to references service', function(done) {
- return this.call((err, data) => {
- this.request.post.callCount.should.equal(1)
- const arg = this.request.post.firstCall.args[0]
- expect(arg.json).to.have.all.keys('docUrls', 'fullIndex')
- expect(arg.json.docUrls.length).to.equal(4)
- expect(arg.json.fullIndex).to.equal(true)
- return done()
- })
- })
- it('should not produce an error', function(done) {
- return this.call((err, data) => {
- expect(err).to.equal(null)
- return done()
- })
- })
- it('should return data', function(done) {
- return this.call((err, data) => {
- expect(data).to.not.equal(null)
- expect(data).to.not.equal(undefined)
- expect(data).to.equal(this.fakeResponseData)
- return done()
- })
- })
- describe('when ProjectGetter.getProject produces an error', function() {
- beforeEach(function() {
- return this.ProjectGetter.getProject.callsArgWith(2, new Error('woops'))
- })
- it('should produce an error', function(done) {
- return this.call((err, data) => {
- expect(err).to.not.equal(null)
- expect(err).to.be.instanceof(Error)
- expect(data).to.equal(undefined)
- return done()
- })
- })
- it('should not send request', function(done) {
- return this.call((err, data) => {
- this.request.post.callCount.should.equal(0)
- return done()
- })
- })
- })
- describe('when _isFullIndex produces an error', function() {
- beforeEach(function() {
- this.ProjectGetter.getProject.callsArgWith(2, null, this.fakeProject)
- return this.handler._isFullIndex.callsArgWith(1, new Error('woops'))
- })
- it('should produce an error', function(done) {
- return this.call((err, data) => {
- expect(err).to.not.equal(null)
- expect(err).to.be.instanceof(Error)
- expect(data).to.equal(undefined)
- return done()
- })
- })
- it('should not send request', function(done) {
- return this.call((err, data) => {
- this.request.post.callCount.should.equal(0)
- return done()
- })
- })
- })
- describe('when flushDocToMongo produces an error', function() {
- beforeEach(function() {
- this.ProjectGetter.getProject.callsArgWith(2, null, this.fakeProject)
- this.handler._isFullIndex.callsArgWith(1, false)
- return this.DocumentUpdaterHandler.flushDocToMongo.callsArgWith(
- 2,
- new Error('woops')
- )
- })
- it('should produce an error', function(done) {
- return this.call((err, data) => {
- expect(err).to.not.equal(null)
- expect(err).to.be.instanceof(Error)
- expect(data).to.equal(undefined)
- return done()
- })
- })
- it('should not send request', function(done) {
- return this.call((err, data) => {
- this.request.post.callCount.should.equal(0)
- return done()
- })
- })
- })
- })
- describe('_findBibDocIds', function() {
- beforeEach(function() {
- this.fakeProject = {
- rootFolder: [
- {
- docs: [
- { name: 'one.bib', _id: 'aaa' },
- { name: 'two.txt', _id: 'bbb' }
- ],
- folders: [
- { docs: [{ name: 'three.bib', _id: 'ccc' }], folders: [] }
- ]
- }
- ]
- }
- return (this.expectedIds = ['aaa', 'ccc'])
- })
- it('should select the correct docIds', function() {
- const result = this.handler._findBibDocIds(this.fakeProject)
- return expect(result).to.deep.equal(this.expectedIds)
- })
- it('should not error with a non array of folders from dirty data', function() {
- this.fakeProject.rootFolder[0].folders[0].folders = {}
- const result = this.handler._findBibDocIds(this.fakeProject)
- return expect(result).to.deep.equal(this.expectedIds)
- })
- })
- describe('_findBibFileIds', function() {
- beforeEach(function() {
- this.fakeProject = {
- rootFolder: [
- {
- docs: [
- { name: 'one.bib', _id: 'aaa' },
- { name: 'two.txt', _id: 'bbb' }
- ],
- fileRefs: [{ name: 'other.bib', _id: 'ddd' }],
- folders: [
- {
- docs: [{ name: 'three.bib', _id: 'ccc' }],
- fileRefs: [{ name: 'four.bib', _id: 'ghg' }],
- folders: []
- }
- ]
- }
- ]
- }
- return (this.expectedIds = ['ddd', 'ghg'])
- })
- it('should select the correct docIds', function() {
- const result = this.handler._findBibFileIds(this.fakeProject)
- return expect(result).to.deep.equal(this.expectedIds)
- })
- })
- describe('_isFullIndex', function() {
- beforeEach(function() {
- this.fakeProject = { owner_ref: (this.owner_ref = 'owner-ref-123') }
- this.owner = {
- features: {
- references: false
- }
- }
- this.UserGetter.getUser = sinon.stub()
- this.UserGetter.getUser
- .withArgs(this.owner_ref, { features: true })
- .yields(null, this.owner)
- return (this.call = callback => {
- return this.handler._isFullIndex(this.fakeProject, callback)
- })
- })
- describe('with references feature on', function() {
- beforeEach(function() {
- return (this.owner.features.references = true)
- })
- it('should return true', function() {
- return this.call((err, isFullIndex) => {
- expect(err).to.equal(null)
- return expect(isFullIndex).to.equal(true)
- })
- })
- })
- describe('with references feature off', function() {
- beforeEach(function() {
- return (this.owner.features.references = false)
- })
- it('should return false', function() {
- return this.call((err, isFullIndex) => {
- expect(err).to.equal(null)
- return expect(isFullIndex).to.equal(false)
- })
- })
- })
- describe('with referencesSearch', function() {
- beforeEach(function() {
- return (this.owner.features = {
- referencesSearch: true,
- references: false
- })
- })
- it('should return true', function() {
- return this.call((err, isFullIndex) => {
- expect(err).to.equal(null)
- return expect(isFullIndex).to.equal(true)
- })
- })
- })
- })
- })
|