Merge pull request #2097 from overleaf/em-chai-spies

Remove chai-spies

GitOrigin-RevId: 7fa41fe32c1be985d761b7327950995d6afe7c4e
This commit is contained in:
Chrystal Maria Griffiths 2019-08-27 11:30:15 +01:00 committed by sharelatex
parent 03460ba229
commit 1335d21136
5 changed files with 412 additions and 419 deletions

View file

@ -4184,12 +4184,6 @@
"type-detect": "^1.0.0" "type-detect": "^1.0.0"
} }
}, },
"chai-spies": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/chai-spies/-/chai-spies-1.0.0.tgz",
"integrity": "sha512-elF2ZUczBsFoP07qCfMO/zeggs8pqCf3fZGyK5+2X4AndS8jycZYID91ztD9oQ7d/0tnS963dPkd0frQEThDsg==",
"dev": true
},
"chalk": { "chalk": {
"version": "1.1.3", "version": "1.1.3",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-1.1.3.tgz", "resolved": "https://registry.npmjs.org/chalk/-/chalk-1.1.3.tgz",
@ -7702,13 +7696,11 @@
}, },
"balanced-match": { "balanced-match": {
"version": "1.0.0", "version": "1.0.0",
"bundled": true, "bundled": true
"optional": true
}, },
"brace-expansion": { "brace-expansion": {
"version": "1.1.11", "version": "1.1.11",
"bundled": true, "bundled": true,
"optional": true,
"requires": { "requires": {
"balanced-match": "^1.0.0", "balanced-match": "^1.0.0",
"concat-map": "0.0.1" "concat-map": "0.0.1"
@ -7725,8 +7717,7 @@
}, },
"concat-map": { "concat-map": {
"version": "0.0.1", "version": "0.0.1",
"bundled": true, "bundled": true
"optional": true
}, },
"console-control-strings": { "console-control-strings": {
"version": "1.1.0", "version": "1.1.0",
@ -7855,7 +7846,6 @@
"minimatch": { "minimatch": {
"version": "3.0.4", "version": "3.0.4",
"bundled": true, "bundled": true,
"optional": true,
"requires": { "requires": {
"brace-expansion": "^1.1.7" "brace-expansion": "^1.1.7"
} }

View file

@ -119,7 +119,6 @@
"babel-loader": "^8.0.6", "babel-loader": "^8.0.6",
"babel-plugin-angularjs-annotate": "^0.10.0", "babel-plugin-angularjs-annotate": "^0.10.0",
"chai": "3.5.0", "chai": "3.5.0",
"chai-spies": "^1.0.0",
"clean-css-cli": "^4.2.1", "clean-css-cli": "^4.2.1",
"es6-promise": "^4.0.5", "es6-promise": "^4.0.5",
"eslint": "^4.18.1", "eslint": "^4.18.1",

View file

@ -1,23 +1,6 @@
/* eslint-disable
max-len,
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 should = require('chai').should()
const spies = require('chai-spies')
const chai = require('chai').use(spies)
const sinon = require('sinon') const sinon = require('sinon')
const SandboxedModule = require('sandboxed-module') const SandboxedModule = require('sandboxed-module')
const { assert } = require('chai')
const path = require('path') const path = require('path')
const _ = require('underscore')
const { ObjectId } = require('mongojs') const { ObjectId } = require('mongojs')
const modulePath = path.join( const modulePath = path.join(
__dirname, __dirname,
@ -48,7 +31,7 @@ describe('DocumentUpdaterHandler', function() {
} }
this.callback = sinon.stub() this.callback = sinon.stub()
return (this.handler = SandboxedModule.require(modulePath, { this.handler = SandboxedModule.require(modulePath, {
globals: { globals: {
console: console console: console
}, },
@ -71,18 +54,18 @@ describe('DocumentUpdaterHandler', function() {
} }
} }
} }
})) })
}) })
describe('flushProjectToMongo', function() { describe('flushProjectToMongo', function() {
describe('successfully', function() { describe('successfully', function() {
beforeEach(function() { beforeEach(function() {
this.request.callsArgWith(1, null, { statusCode: 204 }, '') this.request.callsArgWith(1, null, { statusCode: 204 }, '')
return this.handler.flushProjectToMongo(this.project_id, this.callback) this.handler.flushProjectToMongo(this.project_id, this.callback)
}) })
it('should flush the document from the document updater', function() { it('should flush the document from the document updater', function() {
return this.request this.request
.calledWithMatch({ .calledWithMatch({
url: `${this.settings.apis.documentupdater.url}/project/${ url: `${this.settings.apis.documentupdater.url}/project/${
this.project_id this.project_id
@ -93,7 +76,7 @@ describe('DocumentUpdaterHandler', function() {
}) })
it('should call the callback with no error', function() { it('should call the callback with no error', function() {
return this.callback.calledWith(null).should.equal(true) this.callback.calledWith(null).should.equal(true)
}) })
}) })
@ -105,22 +88,22 @@ describe('DocumentUpdaterHandler', function() {
null, null,
null null
) )
return this.handler.flushProjectToMongo(this.project_id, this.callback) this.handler.flushProjectToMongo(this.project_id, this.callback)
}) })
it('should return an error to the callback', function() { it('should return an error to the callback', function() {
return this.callback.calledWith(this.error).should.equal(true) this.callback.calledWith(this.error).should.equal(true)
}) })
}) })
describe('when the document updater returns a failure error code', function() { describe('when the document updater returns a failure error code', function() {
beforeEach(function() { beforeEach(function() {
this.request.callsArgWith(1, null, { statusCode: 500 }, '') this.request.callsArgWith(1, null, { statusCode: 500 }, '')
return this.handler.flushProjectToMongo(this.project_id, this.callback) this.handler.flushProjectToMongo(this.project_id, this.callback)
}) })
it('should return the callback with an error', function() { it('should return the callback with an error', function() {
return this.callback this.callback
.calledWith( .calledWith(
new Error('doc updater returned failure status code: 500') new Error('doc updater returned failure status code: 500')
) )
@ -133,14 +116,14 @@ describe('DocumentUpdaterHandler', function() {
describe('successfully', function() { describe('successfully', function() {
beforeEach(function() { beforeEach(function() {
this.request.callsArgWith(1, null, { statusCode: 204 }, '') this.request.callsArgWith(1, null, { statusCode: 204 }, '')
return this.handler.flushProjectToMongoAndDelete( this.handler.flushProjectToMongoAndDelete(
this.project_id, this.project_id,
this.callback this.callback
) )
}) })
it('should delete the project from the document updater', function() { it('should delete the project from the document updater', function() {
return this.request this.request
.calledWithMatch({ .calledWithMatch({
url: `${this.settings.apis.documentupdater.url}/project/${ url: `${this.settings.apis.documentupdater.url}/project/${
this.project_id this.project_id
@ -151,7 +134,7 @@ describe('DocumentUpdaterHandler', function() {
}) })
it('should call the callback with no error', function() { it('should call the callback with no error', function() {
return this.callback.calledWith(null).should.equal(true) this.callback.calledWith(null).should.equal(true)
}) })
}) })
@ -163,28 +146,28 @@ describe('DocumentUpdaterHandler', function() {
null, null,
null null
) )
return this.handler.flushProjectToMongoAndDelete( this.handler.flushProjectToMongoAndDelete(
this.project_id, this.project_id,
this.callback this.callback
) )
}) })
it('should return an error to the callback', function() { it('should return an error to the callback', function() {
return this.callback.calledWith(this.error).should.equal(true) this.callback.calledWith(this.error).should.equal(true)
}) })
}) })
describe('when the document updater returns a failure error code', function() { describe('when the document updater returns a failure error code', function() {
beforeEach(function() { beforeEach(function() {
this.request.callsArgWith(1, null, { statusCode: 500 }, '') this.request.callsArgWith(1, null, { statusCode: 500 }, '')
return this.handler.flushProjectToMongoAndDelete( this.handler.flushProjectToMongoAndDelete(
this.project_id, this.project_id,
this.callback this.callback
) )
}) })
it('should return the callback with an error', function() { it('should return the callback with an error', function() {
return this.callback this.callback
.calledWith( .calledWith(
new Error('doc updater returned failure status code: 500') new Error('doc updater returned failure status code: 500')
) )
@ -197,7 +180,7 @@ describe('DocumentUpdaterHandler', function() {
describe('successfully', function() { describe('successfully', function() {
beforeEach(function() { beforeEach(function() {
this.request.callsArgWith(1, null, { statusCode: 204 }, '') this.request.callsArgWith(1, null, { statusCode: 204 }, '')
return this.handler.flushDocToMongo( this.handler.flushDocToMongo(
this.project_id, this.project_id,
this.doc_id, this.doc_id,
this.callback this.callback
@ -205,7 +188,7 @@ describe('DocumentUpdaterHandler', function() {
}) })
it('should flush the document from the document updater', function() { it('should flush the document from the document updater', function() {
return this.request this.request
.calledWithMatch({ .calledWithMatch({
url: `${this.settings.apis.documentupdater.url}/project/${ url: `${this.settings.apis.documentupdater.url}/project/${
this.project_id this.project_id
@ -216,7 +199,7 @@ describe('DocumentUpdaterHandler', function() {
}) })
it('should call the callback with no error', function() { it('should call the callback with no error', function() {
return this.callback.calledWith(null).should.equal(true) this.callback.calledWith(null).should.equal(true)
}) })
}) })
@ -228,7 +211,7 @@ describe('DocumentUpdaterHandler', function() {
null, null,
null null
) )
return this.handler.flushDocToMongo( this.handler.flushDocToMongo(
this.project_id, this.project_id,
this.doc_id, this.doc_id,
this.callback this.callback
@ -236,14 +219,14 @@ describe('DocumentUpdaterHandler', function() {
}) })
it('should return an error to the callback', function() { it('should return an error to the callback', function() {
return this.callback.calledWith(this.error).should.equal(true) this.callback.calledWith(this.error).should.equal(true)
}) })
}) })
describe('when the document updater returns a failure error code', function() { describe('when the document updater returns a failure error code', function() {
beforeEach(function() { beforeEach(function() {
this.request.callsArgWith(1, null, { statusCode: 500 }, '') this.request.callsArgWith(1, null, { statusCode: 500 }, '')
return this.handler.flushDocToMongo( this.handler.flushDocToMongo(
this.project_id, this.project_id,
this.doc_id, this.doc_id,
this.callback this.callback
@ -251,7 +234,7 @@ describe('DocumentUpdaterHandler', function() {
}) })
it('should return the callback with an error', function() { it('should return the callback with an error', function() {
return this.callback this.callback
.calledWith( .calledWith(
new Error('doc updater returned failure status code: 500') new Error('doc updater returned failure status code: 500')
) )
@ -264,15 +247,11 @@ describe('DocumentUpdaterHandler', function() {
describe('successfully', function() { describe('successfully', function() {
beforeEach(function() { beforeEach(function() {
this.request.callsArgWith(1, null, { statusCode: 204 }, '') this.request.callsArgWith(1, null, { statusCode: 204 }, '')
return this.handler.deleteDoc( this.handler.deleteDoc(this.project_id, this.doc_id, this.callback)
this.project_id,
this.doc_id,
this.callback
)
}) })
it('should delete the document from the document updater', function() { it('should delete the document from the document updater', function() {
return this.request this.request
.calledWithMatch({ .calledWithMatch({
url: `${this.settings.apis.documentupdater.url}/project/${ url: `${this.settings.apis.documentupdater.url}/project/${
this.project_id this.project_id
@ -283,7 +262,7 @@ describe('DocumentUpdaterHandler', function() {
}) })
it('should call the callback with no error', function() { it('should call the callback with no error', function() {
return this.callback.calledWith(null).should.equal(true) this.callback.calledWith(null).should.equal(true)
}) })
}) })
@ -295,30 +274,22 @@ describe('DocumentUpdaterHandler', function() {
null, null,
null null
) )
return this.handler.deleteDoc( this.handler.deleteDoc(this.project_id, this.doc_id, this.callback)
this.project_id,
this.doc_id,
this.callback
)
}) })
it('should return an error to the callback', function() { it('should return an error to the callback', function() {
return this.callback.calledWith(this.error).should.equal(true) this.callback.calledWith(this.error).should.equal(true)
}) })
}) })
describe('when the document updater returns a failure error code', function() { describe('when the document updater returns a failure error code', function() {
beforeEach(function() { beforeEach(function() {
this.request.callsArgWith(1, null, { statusCode: 500 }, '') this.request.callsArgWith(1, null, { statusCode: 500 }, '')
return this.handler.deleteDoc( this.handler.deleteDoc(this.project_id, this.doc_id, this.callback)
this.project_id,
this.doc_id,
this.callback
)
}) })
it('should return the callback with an error', function() { it('should return the callback with an error', function() {
return this.callback this.callback
.calledWith( .calledWith(
new Error('doc updater returned failure status code: 500') new Error('doc updater returned failure status code: 500')
) )
@ -329,13 +300,13 @@ describe('DocumentUpdaterHandler', function() {
describe('setDocument', function() { describe('setDocument', function() {
beforeEach(function() { beforeEach(function() {
return (this.source = 'dropbox') this.source = 'dropbox'
}) })
describe('successfully', function() { describe('successfully', function() {
beforeEach(function() { beforeEach(function() {
this.request.callsArgWith(1, null, { statusCode: 204 }, '') this.request.callsArgWith(1, null, { statusCode: 204 }, '')
return this.handler.setDocument( this.handler.setDocument(
this.project_id, this.project_id,
this.doc_id, this.doc_id,
this.user_id, this.user_id,
@ -346,7 +317,7 @@ describe('DocumentUpdaterHandler', function() {
}) })
it('should set the document in the document updater', function() { it('should set the document in the document updater', function() {
return this.request this.request
.calledWith({ .calledWith({
url: `${this.settings.apis.documentupdater.url}/project/${ url: `${this.settings.apis.documentupdater.url}/project/${
this.project_id this.project_id
@ -362,7 +333,7 @@ describe('DocumentUpdaterHandler', function() {
}) })
it('should call the callback with no error', function() { it('should call the callback with no error', function() {
return this.callback.calledWith(null).should.equal(true) this.callback.calledWith(null).should.equal(true)
}) })
}) })
@ -374,7 +345,7 @@ describe('DocumentUpdaterHandler', function() {
null, null,
null null
) )
return this.handler.setDocument( this.handler.setDocument(
this.project_id, this.project_id,
this.doc_id, this.doc_id,
this.user_id, this.user_id,
@ -385,14 +356,14 @@ describe('DocumentUpdaterHandler', function() {
}) })
it('should return an error to the callback', function() { it('should return an error to the callback', function() {
return this.callback.calledWith(this.error).should.equal(true) this.callback.calledWith(this.error).should.equal(true)
}) })
}) })
describe('when the document updater returns a failure error code', function() { describe('when the document updater returns a failure error code', function() {
beforeEach(function() { beforeEach(function() {
this.request.callsArgWith(1, null, { statusCode: 500 }, '') this.request.callsArgWith(1, null, { statusCode: 500 }, '')
return this.handler.setDocument( this.handler.setDocument(
this.project_id, this.project_id,
this.doc_id, this.doc_id,
this.user_id, this.user_id,
@ -403,7 +374,7 @@ describe('DocumentUpdaterHandler', function() {
}) })
it('should return the callback with an error', function() { it('should return the callback with an error', function() {
return this.callback this.callback
.calledWith( .calledWith(
new Error('doc updater returned failure status code: 500') new Error('doc updater returned failure status code: 500')
) )
@ -423,7 +394,7 @@ describe('DocumentUpdaterHandler', function() {
} }
this.fromVersion = 2 this.fromVersion = 2
this.request.callsArgWith(1, null, { statusCode: 200 }, this.body) this.request.callsArgWith(1, null, { statusCode: 200 }, this.body)
return this.handler.getDocument( this.handler.getDocument(
this.project_id, this.project_id,
this.doc_id, this.doc_id,
this.fromVersion, this.fromVersion,
@ -432,7 +403,7 @@ describe('DocumentUpdaterHandler', function() {
}) })
it('should get the document from the document updater', function() { it('should get the document from the document updater', function() {
return this.request this.request
.calledWith({ .calledWith({
url: `${this.settings.apis.documentupdater.url}/project/${ url: `${this.settings.apis.documentupdater.url}/project/${
this.project_id this.project_id
@ -444,7 +415,7 @@ describe('DocumentUpdaterHandler', function() {
}) })
it('should call the callback with the lines and version', function() { it('should call the callback with the lines and version', function() {
return this.callback this.callback
.calledWith(null, this.lines, this.version, this.ranges, this.ops) .calledWith(null, this.lines, this.version, this.ranges, this.ops)
.should.equal(true) .should.equal(true)
}) })
@ -458,7 +429,7 @@ describe('DocumentUpdaterHandler', function() {
null, null,
null null
) )
return this.handler.getDocument( this.handler.getDocument(
this.project_id, this.project_id,
this.doc_id, this.doc_id,
this.fromVersion, this.fromVersion,
@ -467,14 +438,14 @@ describe('DocumentUpdaterHandler', function() {
}) })
it('should return an error to the callback', function() { it('should return an error to the callback', function() {
return this.callback.calledWith(this.error).should.equal(true) this.callback.calledWith(this.error).should.equal(true)
}) })
}) })
describe('when the document updater returns a failure error code', function() { describe('when the document updater returns a failure error code', function() {
beforeEach(function() { beforeEach(function() {
this.request.callsArgWith(1, null, { statusCode: 500 }, '') this.request.callsArgWith(1, null, { statusCode: 500 }, '')
return this.handler.getDocument( this.handler.getDocument(
this.project_id, this.project_id,
this.doc_id, this.doc_id,
this.fromVersion, this.fromVersion,
@ -483,7 +454,7 @@ describe('DocumentUpdaterHandler', function() {
}) })
it('should return the callback with an error', function() { it('should return the callback with an error', function() {
return this.callback this.callback
.calledWith( .calledWith(
new Error('doc updater returned failure status code: 500') new Error('doc updater returned failure status code: 500')
) )
@ -494,7 +465,7 @@ describe('DocumentUpdaterHandler', function() {
describe('getProjectDocsIfMatch', function() { describe('getProjectDocsIfMatch', function() {
beforeEach(function() { beforeEach(function() {
return (this.project_state_hash = '1234567890abcdef') this.project_state_hash = '1234567890abcdef'
}) })
describe('successfully', function() { describe('successfully', function() {
@ -509,7 +480,7 @@ describe('DocumentUpdaterHandler', function() {
this.request.post = sinon this.request.post = sinon
.stub() .stub()
.callsArgWith(1, null, { statusCode: 200 }, this.body) .callsArgWith(1, null, { statusCode: 200 }, this.body)
return this.handler.getProjectDocsIfMatch( this.handler.getProjectDocsIfMatch(
this.project_id, this.project_id,
this.project_state_hash, this.project_state_hash,
this.callback this.callback
@ -520,13 +491,11 @@ describe('DocumentUpdaterHandler', function() {
const url = `${this.settings.apis.documentupdater.url}/project/${ const url = `${this.settings.apis.documentupdater.url}/project/${
this.project_id this.project_id
}/get_and_flush_if_old?state=${this.project_state_hash}` }/get_and_flush_if_old?state=${this.project_state_hash}`
return this.request.post.calledWith(url).should.equal(true) this.request.post.calledWith(url).should.equal(true)
}) })
it('should call the callback with the documents', function() { it('should call the callback with the documents', function() {
return this.callback this.callback.calledWithExactly(null, this.docs).should.equal(true)
.calledWithExactly(null, this.docs)
.should.equal(true)
}) })
}) })
@ -540,7 +509,7 @@ describe('DocumentUpdaterHandler', function() {
null, null,
null null
) )
return this.handler.getProjectDocsIfMatch( this.handler.getProjectDocsIfMatch(
this.project_id, this.project_id,
this.project_state_hash, this.project_state_hash,
this.callback this.callback
@ -548,7 +517,7 @@ describe('DocumentUpdaterHandler', function() {
}) })
it('should return an error to the callback', function() { it('should return an error to the callback', function() {
return this.callback.calledWith(this.error).should.equal(true) this.callback.calledWith(this.error).should.equal(true)
}) })
}) })
@ -557,7 +526,7 @@ describe('DocumentUpdaterHandler', function() {
this.request.post = sinon this.request.post = sinon
.stub() .stub()
.callsArgWith(1, null, { statusCode: 409 }, 'Conflict') .callsArgWith(1, null, { statusCode: 409 }, 'Conflict')
return this.handler.getProjectDocsIfMatch( this.handler.getProjectDocsIfMatch(
this.project_id, this.project_id,
this.project_state_hash, this.project_state_hash,
this.callback this.callback
@ -565,7 +534,7 @@ describe('DocumentUpdaterHandler', function() {
}) })
it('should return the callback with no documents', function() { it('should return the callback with no documents', function() {
return this.callback.alwaysCalledWithExactly().should.equal(true) this.callback.alwaysCalledWithExactly().should.equal(true)
}) })
}) })
}) })
@ -574,11 +543,11 @@ describe('DocumentUpdaterHandler', function() {
describe('successfully', function() { describe('successfully', function() {
beforeEach(function() { beforeEach(function() {
this.request.callsArgWith(1, null, { statusCode: 200 }) this.request.callsArgWith(1, null, { statusCode: 200 })
return this.handler.clearProjectState(this.project_id, this.callback) this.handler.clearProjectState(this.project_id, this.callback)
}) })
it('should clear the project state from the document updater', function() { it('should clear the project state from the document updater', function() {
return this.request this.request
.calledWithMatch({ .calledWithMatch({
url: `${this.settings.apis.documentupdater.url}/project/${ url: `${this.settings.apis.documentupdater.url}/project/${
this.project_id this.project_id
@ -589,7 +558,7 @@ describe('DocumentUpdaterHandler', function() {
}) })
it('should call the callback', function() { it('should call the callback', function() {
return this.callback.calledWith(null).should.equal(true) this.callback.calledWith(null).should.equal(true)
}) })
}) })
@ -601,22 +570,22 @@ describe('DocumentUpdaterHandler', function() {
null, null,
null null
) )
return this.handler.clearProjectState(this.project_id, this.callback) this.handler.clearProjectState(this.project_id, this.callback)
}) })
it('should return an error to the callback', function() { it('should return an error to the callback', function() {
return this.callback.calledWith(this.error).should.equal(true) this.callback.calledWith(this.error).should.equal(true)
}) })
}) })
describe('when the document updater returns an error code', function() { describe('when the document updater returns an error code', function() {
beforeEach(function() { beforeEach(function() {
this.request.callsArgWith(1, null, { statusCode: 500 }, null) this.request.callsArgWith(1, null, { statusCode: 500 }, null)
return this.handler.clearProjectState(this.project_id, this.callback) this.handler.clearProjectState(this.project_id, this.callback)
}) })
it('should return the callback with no documents', function() { it('should return the callback with no documents', function() {
return this.callback this.callback
.calledWith( .calledWith(
new Error('doc updater returned failure status code: 500') new Error('doc updater returned failure status code: 500')
) )
@ -627,13 +596,13 @@ describe('DocumentUpdaterHandler', function() {
describe('acceptChanges', function() { describe('acceptChanges', function() {
beforeEach(function() { beforeEach(function() {
return (this.change_id = 'mock-change-id-1') this.change_id = 'mock-change-id-1'
}) })
describe('successfully', function() { describe('successfully', function() {
beforeEach(function() { beforeEach(function() {
this.request.callsArgWith(1, null, { statusCode: 200 }, this.body) this.request.callsArgWith(1, null, { statusCode: 200 }, this.body)
return this.handler.acceptChanges( this.handler.acceptChanges(
this.project_id, this.project_id,
this.doc_id, this.doc_id,
[this.change_id], [this.change_id],
@ -642,7 +611,7 @@ describe('DocumentUpdaterHandler', function() {
}) })
it('should accept the change in the document updater', function() { it('should accept the change in the document updater', function() {
return this.request this.request
.calledWith({ .calledWith({
url: `${this.settings.apis.documentupdater.url}/project/${ url: `${this.settings.apis.documentupdater.url}/project/${
this.project_id this.project_id
@ -656,7 +625,7 @@ describe('DocumentUpdaterHandler', function() {
}) })
it('should call the callback', function() { it('should call the callback', function() {
return this.callback.calledWith(null).should.equal(true) this.callback.calledWith(null).should.equal(true)
}) })
}) })
@ -668,7 +637,7 @@ describe('DocumentUpdaterHandler', function() {
null, null,
null null
) )
return this.handler.acceptChanges( this.handler.acceptChanges(
this.project_id, this.project_id,
this.doc_id, this.doc_id,
[this.change_id], [this.change_id],
@ -677,14 +646,14 @@ describe('DocumentUpdaterHandler', function() {
}) })
it('should return an error to the callback', function() { it('should return an error to the callback', function() {
return this.callback.calledWith(this.error).should.equal(true) this.callback.calledWith(this.error).should.equal(true)
}) })
}) })
describe('when the document updater returns a failure error code', function() { describe('when the document updater returns a failure error code', function() {
beforeEach(function() { beforeEach(function() {
this.request.callsArgWith(1, null, { statusCode: 500 }, '') this.request.callsArgWith(1, null, { statusCode: 500 }, '')
return this.handler.acceptChanges( this.handler.acceptChanges(
this.project_id, this.project_id,
this.doc_id, this.doc_id,
[this.change_id], [this.change_id],
@ -693,7 +662,7 @@ describe('DocumentUpdaterHandler', function() {
}) })
it('should return the callback with an error', function() { it('should return the callback with an error', function() {
return this.callback this.callback
.calledWith( .calledWith(
new Error('doc updater returned failure status code: 500') new Error('doc updater returned failure status code: 500')
) )
@ -704,13 +673,13 @@ describe('DocumentUpdaterHandler', function() {
describe('deleteThread', function() { describe('deleteThread', function() {
beforeEach(function() { beforeEach(function() {
return (this.thread_id = 'mock-thread-id-1') this.thread_id = 'mock-thread-id-1'
}) })
describe('successfully', function() { describe('successfully', function() {
beforeEach(function() { beforeEach(function() {
this.request.callsArgWith(1, null, { statusCode: 200 }, this.body) this.request.callsArgWith(1, null, { statusCode: 200 }, this.body)
return this.handler.deleteThread( this.handler.deleteThread(
this.project_id, this.project_id,
this.doc_id, this.doc_id,
this.thread_id, this.thread_id,
@ -719,7 +688,7 @@ describe('DocumentUpdaterHandler', function() {
}) })
it('should delete the thread in the document updater', function() { it('should delete the thread in the document updater', function() {
return this.request this.request
.calledWithMatch({ .calledWithMatch({
url: `${this.settings.apis.documentupdater.url}/project/${ url: `${this.settings.apis.documentupdater.url}/project/${
this.project_id this.project_id
@ -730,7 +699,7 @@ describe('DocumentUpdaterHandler', function() {
}) })
it('should call the callback', function() { it('should call the callback', function() {
return this.callback.calledWith(null).should.equal(true) this.callback.calledWith(null).should.equal(true)
}) })
}) })
@ -742,7 +711,7 @@ describe('DocumentUpdaterHandler', function() {
null, null,
null null
) )
return this.handler.deleteThread( this.handler.deleteThread(
this.project_id, this.project_id,
this.doc_id, this.doc_id,
this.thread_id, this.thread_id,
@ -751,14 +720,14 @@ describe('DocumentUpdaterHandler', function() {
}) })
it('should return an error to the callback', function() { it('should return an error to the callback', function() {
return this.callback.calledWith(this.error).should.equal(true) this.callback.calledWith(this.error).should.equal(true)
}) })
}) })
describe('when the document updater returns a failure error code', function() { describe('when the document updater returns a failure error code', function() {
beforeEach(function() { beforeEach(function() {
this.request.callsArgWith(1, null, { statusCode: 500 }, '') this.request.callsArgWith(1, null, { statusCode: 500 }, '')
return this.handler.deleteThread( this.handler.deleteThread(
this.project_id, this.project_id,
this.doc_id, this.doc_id,
this.thread_id, this.thread_id,
@ -767,7 +736,7 @@ describe('DocumentUpdaterHandler', function() {
}) })
it('should return the callback with an error', function() { it('should return the callback with an error', function() {
return this.callback this.callback
.calledWith( .calledWith(
new Error('doc updater returned failure status code: 500') new Error('doc updater returned failure status code: 500')
) )
@ -779,13 +748,13 @@ describe('DocumentUpdaterHandler', function() {
describe('updateProjectStructure ', function() { describe('updateProjectStructure ', function() {
beforeEach(function() { beforeEach(function() {
this.user_id = 1234 this.user_id = 1234
return (this.version = 999) this.version = 999
}) })
describe('with project history disabled', function() { describe('with project history disabled', function() {
beforeEach(function() { beforeEach(function() {
this.settings.apis.project_history.sendProjectStructureOps = false this.settings.apis.project_history.sendProjectStructureOps = false
return this.handler.updateProjectStructure( this.handler.updateProjectStructure(
this.project_id, this.project_id,
this.projectHistoryId, this.projectHistoryId,
this.user_id, this.user_id,
@ -795,11 +764,11 @@ describe('DocumentUpdaterHandler', function() {
}) })
it('does not make a web request', function() { it('does not make a web request', function() {
return this.request.called.should.equal(false) this.request.called.should.equal(false)
}) })
it('calls the callback', function() { it('calls the callback', function() {
return this.callback.called.should.equal(true) this.callback.called.should.equal(true)
}) })
}) })
@ -809,7 +778,7 @@ describe('DocumentUpdaterHandler', function() {
this.url = `${this.settings.apis.documentupdater.url}/project/${ this.url = `${this.settings.apis.documentupdater.url}/project/${
this.project_id this.project_id
}` }`
return this.request.callsArgWith(1, null, { statusCode: 204 }, '') this.request.callsArgWith(1, null, { statusCode: 204 }, '')
}) })
describe('when an entity has changed name', function() { describe('when an entity has changed name', function() {
@ -843,7 +812,7 @@ describe('DocumentUpdaterHandler', function() {
} }
] ]
return this.handler.updateProjectStructure( this.handler.updateProjectStructure(
this.project_id, this.project_id,
this.projectHistoryId, this.projectHistoryId,
this.user_id, this.user_id,
@ -862,7 +831,7 @@ describe('DocumentUpdaterHandler', function() {
} }
}) })
.should.equal(true) .should.equal(true)
return done() done()
} }
) )
}) })
@ -888,7 +857,7 @@ describe('DocumentUpdaterHandler', function() {
} }
] ]
return this.handler.updateProjectStructure( this.handler.updateProjectStructure(
this.project_id, this.project_id,
this.projectHistoryId, this.projectHistoryId,
this.user_id, this.user_id,
@ -907,7 +876,7 @@ describe('DocumentUpdaterHandler', function() {
} }
}) })
.should.equal(true) .should.equal(true)
return done() done()
} }
) )
}) })
@ -937,7 +906,7 @@ describe('DocumentUpdaterHandler', function() {
} }
] ]
return this.handler.updateProjectStructure( this.handler.updateProjectStructure(
this.project_id, this.project_id,
this.projectHistoryId, this.projectHistoryId,
this.user_id, this.user_id,
@ -956,7 +925,7 @@ describe('DocumentUpdaterHandler', function() {
} }
}) })
.should.equal(true) .should.equal(true)
return done() done()
} }
) )
}) })
@ -980,7 +949,7 @@ describe('DocumentUpdaterHandler', function() {
} }
] ]
return this.handler.updateProjectStructure( this.handler.updateProjectStructure(
this.project_id, this.project_id,
this.projectHistoryId, this.projectHistoryId,
this.user_id, this.user_id,
@ -999,7 +968,7 @@ describe('DocumentUpdaterHandler', function() {
} }
}) })
.should.equal(true) .should.equal(true)
return done() done()
} }
) )
}) })
@ -1014,14 +983,6 @@ describe('DocumentUpdaterHandler', function() {
] ]
} }
const docUpdates = [
{
id: this.docId.toString(),
pathname: '/foo',
newPathname: ''
}
]
this.handler.updateProjectStructure( this.handler.updateProjectStructure(
this.project_id, this.project_id,
this.projectHistoryId, this.projectHistoryId,
@ -1032,7 +993,7 @@ describe('DocumentUpdaterHandler', function() {
this.callback.calledWith(new Error()).should.equal(true) this.callback.calledWith(new Error()).should.equal(true)
const firstCallArgs = this.callback.args[0] const firstCallArgs = this.callback.args[0]
return firstCallArgs[0].message.should.equal( firstCallArgs[0].message.should.equal(
'did not receive project version in changes' 'did not receive project version in changes'
) )
}) })

View file

@ -1,68 +1,39 @@
/* eslint-disable
camelcase,
handle-callback-err,
max-len,
no-path-concat,
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
* DS206: Consider reworking classes to avoid initClass
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const spies = require('chai-spies')
const chai = require('chai').use(spies)
const sinon = require('sinon') const sinon = require('sinon')
const should = chai.should() const { expect } = require('chai')
const { expect } = chai
const modulePath = const modulePath =
'../../../../app/src/Features/Project/ProjectCreationHandler.js' '../../../../app/src/Features/Project/ProjectCreationHandler.js'
const SandboxedModule = require('sandboxed-module') const SandboxedModule = require('sandboxed-module')
const Settings = require('settings-sharelatex')
const Path = require('path') const Path = require('path')
const _ = require('underscore')
describe('ProjectCreationHandler', function() { describe('ProjectCreationHandler', function() {
const ownerId = '4eecb1c1bffa66588e0000a1' const ownerId = '4eecb1c1bffa66588e0000a1'
const projectName = 'project name goes here' const projectName = 'project name goes here'
const project_id = '4eecaffcbffa66588e000008' const projectId = '4eecaffcbffa66588e000008'
const docId = '4eecb17ebffa66588e00003f' const docId = '4eecb17ebffa66588e00003f'
const rootFolderId = '234adfa3r2afe' const rootFolderId = '234adfa3r2afe'
beforeEach(function() { beforeEach(function() {
let Folder, Project this.ProjectModel = class Project {
this.ProjectModel = Project = (function() {
Project = class Project {
static initClass() {
this.prototype.save = sinon.stub().callsArg(0)
this.prototype.rootFolder = [
{
_id: rootFolderId,
docs: []
}
]
}
constructor(options) {
if (options == null) {
options = {}
}
this._id = project_id
this.owner_ref = options.owner_ref
this.name = options.name
this.overleaf = { history: {} }
}
}
Project.initClass()
return Project
})()
this.FolderModel = Folder = class Folder {
constructor(options) { constructor(options) {
;({ name: this.name } = options) if (options == null) {
options = {}
}
this._id = projectId
this.owner_ref = options.owner_ref
this.name = options.name
this.overleaf = { history: {} }
}
}
this.ProjectModel.prototype.save = sinon.stub().callsArg(0)
this.ProjectModel.prototype.rootFolder = [
{
_id: rootFolderId,
docs: []
}
]
this.FolderModel = class Folder {
constructor(options) {
this.name = options.name
} }
} }
this.ProjectEntityUpdateHandler = { this.ProjectEntityUpdateHandler = {
@ -88,7 +59,7 @@ describe('ProjectCreationHandler', function() {
this.AnalyticsManager = { recordEvent: sinon.stub() } this.AnalyticsManager = { recordEvent: sinon.stub() }
return (this.handler = SandboxedModule.require(modulePath, { this.handler = SandboxedModule.require(modulePath, {
globals: { globals: {
console: console console: console
}, },
@ -109,107 +80,123 @@ describe('ProjectCreationHandler', function() {
timeAsyncMethod() {} timeAsyncMethod() {}
} }
} }
})) })
}) })
describe('Creating a Blank project', function() { describe('Creating a Blank project', function() {
beforeEach(function() { beforeEach(function() {
this.overleaf_id = 1234 this.overleafId = 1234
this.HistoryManager.initializeProject = sinon this.HistoryManager.initializeProject = sinon
.stub() .stub()
.callsArgWith(0, null, { overleaf_id: this.overleaf_id }) .callsArgWith(0, null, { overleaf_id: this.overleafId })
return (this.ProjectModel.prototype.save = sinon.stub().callsArg(0)) this.ProjectModel.prototype.save = sinon.stub().callsArg(0)
}) })
describe('successfully', function() { describe('successfully', function() {
it('should save the project', function(done) { it('should save the project', function(done) {
return this.handler.createBlankProject(ownerId, projectName, () => { this.handler.createBlankProject(ownerId, projectName, () => {
this.ProjectModel.prototype.save.called.should.equal(true) this.ProjectModel.prototype.save.called.should.equal(true)
return done() done()
}) })
}) })
it('should return the project in the callback', function(done) { it('should return the project in the callback', function(done) {
return this.handler.createBlankProject( this.handler.createBlankProject(
ownerId, ownerId,
projectName, projectName,
(err, project) => { (err, project) => {
if (err != null) {
return done(err)
}
project.name.should.equal(projectName) project.name.should.equal(projectName)
;(project.owner_ref + '').should.equal(ownerId) expect(project.owner_ref + '').to.equal(ownerId)
return done() done()
} }
) )
}) })
it('should initialize the project overleaf if history id not provided', function(done) { it('should initialize the project overleaf if history id not provided', function(done) {
this.handler.createBlankProject(ownerId, projectName, done) this.handler.createBlankProject(ownerId, projectName, done)
return this.HistoryManager.initializeProject this.HistoryManager.initializeProject.calledWith().should.equal(true)
.calledWith()
.should.equal(true)
}) })
it('should set the overleaf id if overleaf id not provided', function(done) { it('should set the overleaf id if overleaf id not provided', function(done) {
return this.handler.createBlankProject( this.handler.createBlankProject(
ownerId, ownerId,
projectName, projectName,
(err, project) => { (err, project) => {
project.overleaf.history.id.should.equal(this.overleaf_id) if (err != null) {
return done() return done(err)
}
project.overleaf.history.id.should.equal(this.overleafId)
done()
} }
) )
}) })
it('should set the overleaf id if overleaf id provided', function(done) { it('should set the overleaf id if overleaf id provided', function(done) {
const overleaf_id = 2345 const overleafId = 2345
const attributes = { const attributes = {
overleaf: { overleaf: {
history: { history: {
id: overleaf_id id: overleafId
} }
} }
} }
return this.handler.createBlankProject( this.handler.createBlankProject(
ownerId, ownerId,
projectName, projectName,
attributes, attributes,
(err, project) => { (err, project) => {
project.overleaf.history.id.should.equal(overleaf_id) if (err != null) {
return done() return done(err)
}
project.overleaf.history.id.should.equal(overleafId)
done()
} }
) )
}) })
it('should set the language from the user', function(done) { it('should set the language from the user', function(done) {
return this.handler.createBlankProject( this.handler.createBlankProject(
ownerId, ownerId,
projectName, projectName,
(err, project) => { (err, project) => {
if (err != null) {
return done(err)
}
project.spellCheckLanguage.should.equal('de') project.spellCheckLanguage.should.equal('de')
return done() done()
} }
) )
}) })
it('should set the imageName to currentImageName if set and no imageName attribute', function(done) { it('should set the imageName to currentImageName if set and no imageName attribute', function(done) {
this.Settings.currentImageName = 'mock-image-name' this.Settings.currentImageName = 'mock-image-name'
return this.handler.createBlankProject( this.handler.createBlankProject(
ownerId, ownerId,
projectName, projectName,
(err, project) => { (err, project) => {
if (err != null) {
return done(err)
}
project.imageName.should.equal(this.Settings.currentImageName) project.imageName.should.equal(this.Settings.currentImageName)
return done() done()
} }
) )
}) })
it('should not set the imageName if no currentImageName', function(done) { it('should not set the imageName if no currentImageName', function(done) {
this.Settings.currentImageName = null this.Settings.currentImageName = null
return this.handler.createBlankProject( this.handler.createBlankProject(
ownerId, ownerId,
projectName, projectName,
(err, project) => { (err, project) => {
if (err != null) {
return done(err)
}
expect(project.imageName).to.not.exist expect(project.imageName).to.not.exist
return done() done()
} }
) )
}) })
@ -217,46 +204,58 @@ describe('ProjectCreationHandler', function() {
it('should set the imageName to the attribute value if set and not overwrite it with the currentImageName', function(done) { it('should set the imageName to the attribute value if set and not overwrite it with the currentImageName', function(done) {
this.Settings.currentImageName = 'mock-image-name' this.Settings.currentImageName = 'mock-image-name'
const attributes = { imageName: 'attribute-image-name' } const attributes = { imageName: 'attribute-image-name' }
return this.handler.createBlankProject( this.handler.createBlankProject(
ownerId, ownerId,
projectName, projectName,
attributes, attributes,
(err, project) => { (err, project) => {
if (err != null) {
return done(err)
}
project.imageName.should.equal(attributes.imageName) project.imageName.should.equal(attributes.imageName)
return done() done()
} }
) )
}) })
it('should not set the overleaf.history.display if not configured in settings', function(done) { it('should not set the overleaf.history.display if not configured in settings', function(done) {
this.Settings.apis.project_history.displayHistoryForNewProjects = false this.Settings.apis.project_history.displayHistoryForNewProjects = false
return this.handler.createBlankProject( this.handler.createBlankProject(
ownerId, ownerId,
projectName, projectName,
(err, project) => { (err, project) => {
if (err != null) {
return done(err)
}
expect(project.overleaf.history.display).to.not.exist expect(project.overleaf.history.display).to.not.exist
return done() done()
} }
) )
}) })
it('should set the overleaf.history.display if configured in settings', function(done) { it('should set the overleaf.history.display if configured in settings', function(done) {
this.Settings.apis.project_history.displayHistoryForNewProjects = true this.Settings.apis.project_history.displayHistoryForNewProjects = true
return this.handler.createBlankProject( this.handler.createBlankProject(
ownerId, ownerId,
projectName, projectName,
(err, project) => { (err, project) => {
if (err != null) {
return done(err)
}
expect(project.overleaf.history.display).to.equal(true) expect(project.overleaf.history.display).to.equal(true)
return done() done()
} }
) )
}) })
it('should send a project-created event to analytics', function(done) { it('should send a project-created event to analytics', function(done) {
return this.handler.createBlankProject( this.handler.createBlankProject(
ownerId, ownerId,
projectName, projectName,
(err, project) => { (err, project) => {
if (err != null) {
return done(err)
}
expect(this.AnalyticsManager.recordEvent.callCount).to.equal(1) expect(this.AnalyticsManager.recordEvent.callCount).to.equal(1)
expect( expect(
this.AnalyticsManager.recordEvent.calledWith( this.AnalyticsManager.recordEvent.calledWith(
@ -264,7 +263,7 @@ describe('ProjectCreationHandler', function() {
'project-created' 'project-created'
) )
).to.equal(true) ).to.equal(true)
return done() done()
} }
) )
}) })
@ -273,11 +272,14 @@ describe('ProjectCreationHandler', function() {
const attributes = { const attributes = {
fromV1TemplateId: 100 fromV1TemplateId: 100
} }
return this.handler.createBlankProject( this.handler.createBlankProject(
ownerId, ownerId,
projectName, projectName,
attributes, attributes,
(err, project) => { (err, project) => {
if (err != null) {
return done(err)
}
expect(this.AnalyticsManager.recordEvent.callCount).to.equal(1) expect(this.AnalyticsManager.recordEvent.callCount).to.equal(1)
expect( expect(
this.AnalyticsManager.recordEvent.calledWith( this.AnalyticsManager.recordEvent.calledWith(
@ -286,7 +288,7 @@ describe('ProjectCreationHandler', function() {
{ projectId: project._id, attributes } { projectId: project._id, attributes }
) )
).to.equal(true) ).to.equal(true)
return done() done()
} }
) )
}) })
@ -299,11 +301,14 @@ describe('ProjectCreationHandler', function() {
} }
} }
} }
return this.handler.createBlankProject( this.handler.createBlankProject(
ownerId, ownerId,
projectName, projectName,
attributes, attributes,
(err, project) => { (err, project) => {
if (err != null) {
return done(err)
}
expect(this.AnalyticsManager.recordEvent.callCount).to.equal(1) expect(this.AnalyticsManager.recordEvent.callCount).to.equal(1)
expect( expect(
this.AnalyticsManager.recordEvent.calledWith( this.AnalyticsManager.recordEvent.calledWith(
@ -311,7 +316,7 @@ describe('ProjectCreationHandler', function() {
'project-imported' 'project-imported'
) )
).to.equal(true) ).to.equal(true)
return done() done()
} }
) )
}) })
@ -322,15 +327,11 @@ describe('ProjectCreationHandler', function() {
this.ProjectModel.prototype.save = sinon this.ProjectModel.prototype.save = sinon
.stub() .stub()
.callsArgWith(0, new Error('something went wrong')) .callsArgWith(0, new Error('something went wrong'))
return this.handler.createBlankProject( this.handler.createBlankProject(ownerId, projectName, this.callback)
ownerId,
projectName,
this.callback
)
}) })
it('should return the error to the callback', function() { it('should return the error to the callback', function() {
return should.exist(this.callback.args[0][0]) expect(this.callback.args[0][0]).to.exist
}) })
}) })
@ -339,19 +340,15 @@ describe('ProjectCreationHandler', function() {
this.ProjectDetailsHandler.validateProjectName = sinon this.ProjectDetailsHandler.validateProjectName = sinon
.stub() .stub()
.yields(new Error('bad name')) .yields(new Error('bad name'))
return this.handler.createBlankProject( this.handler.createBlankProject(ownerId, projectName, this.callback)
ownerId,
projectName,
this.callback
)
}) })
it('should return the error to the callback', function() { it('should return the error to the callback', function() {
return should.exist(this.callback.args[0][0]) expect(this.callback.args[0][0]).to.exist
}) })
it('should not try to create the project', function() { it('should not try to create the project', function() {
return this.ProjectModel.prototype.save.called.should.equal(false) this.ProjectModel.prototype.save.called.should.equal(false)
}) })
}) })
}) })
@ -360,15 +357,15 @@ describe('ProjectCreationHandler', function() {
beforeEach(function() { beforeEach(function() {
this.project = new this.ProjectModel() this.project = new this.ProjectModel()
this.handler._buildTemplate = function( this.handler._buildTemplate = function(
template_name, templateName,
user, user,
project_name, projectName,
callback callback
) { ) {
if (template_name === 'mainbasic.tex') { if (templateName === 'mainbasic.tex') {
return callback(null, ['mainbasic.tex', 'lines']) return callback(null, ['mainbasic.tex', 'lines'])
} }
throw new Error(`unknown template: ${template_name}`) throw new Error(`unknown template: ${templateName}`)
} }
sinon.spy(this.handler, '_buildTemplate') sinon.spy(this.handler, '_buildTemplate')
this.handler.createBlankProject = sinon this.handler.createBlankProject = sinon
@ -377,27 +374,23 @@ describe('ProjectCreationHandler', function() {
this.handler._createRootDoc = sinon this.handler._createRootDoc = sinon
.stub() .stub()
.callsArgWith(3, null, this.project) .callsArgWith(3, null, this.project)
return this.handler.createBasicProject( this.handler.createBasicProject(ownerId, projectName, this.callback)
ownerId,
projectName,
this.callback
)
}) })
it('should create a blank project first', function() { it('should create a blank project first', function() {
return this.handler.createBlankProject this.handler.createBlankProject
.calledWith(ownerId, projectName) .calledWith(ownerId, projectName)
.should.equal(true) .should.equal(true)
}) })
it('should create the root document', function() { it('should create the root document', function() {
return this.handler._createRootDoc this.handler._createRootDoc
.calledWith(this.project, ownerId, ['mainbasic.tex', 'lines']) .calledWith(this.project, ownerId, ['mainbasic.tex', 'lines'])
.should.equal(true) .should.equal(true)
}) })
it('should build the mainbasic.tex template', function() { it('should build the mainbasic.tex template', function() {
return this.handler._buildTemplate this.handler._buildTemplate
.calledWith('mainbasic.tex', ownerId, projectName) .calledWith('mainbasic.tex', ownerId, projectName)
.should.equal(true) .should.equal(true)
}) })
@ -412,7 +405,7 @@ describe('ProjectCreationHandler', function() {
this.handler._createRootDoc = sinon this.handler._createRootDoc = sinon
.stub() .stub()
.callsArgWith(3, null, this.project) .callsArgWith(3, null, this.project)
return this.handler.createProjectFromSnippet( this.handler.createProjectFromSnippet(
ownerId, ownerId,
projectName, projectName,
['snippet line 1', 'snippet line 2'], ['snippet line 1', 'snippet line 2'],
@ -421,13 +414,13 @@ describe('ProjectCreationHandler', function() {
}) })
it('should create a blank project first', function() { it('should create a blank project first', function() {
return this.handler.createBlankProject this.handler.createBlankProject
.calledWith(ownerId, projectName) .calledWith(ownerId, projectName)
.should.equal(true) .should.equal(true)
}) })
it('should create the root document', function() { it('should create the root document', function() {
return this.handler._createRootDoc this.handler._createRootDoc
.calledWith(this.project, ownerId, ['snippet line 1', 'snippet line 2']) .calledWith(this.project, ownerId, ['snippet line 1', 'snippet line 2'])
.should.equal(true) .should.equal(true)
}) })
@ -437,18 +430,18 @@ describe('ProjectCreationHandler', function() {
beforeEach(function() { beforeEach(function() {
this.project = new this.ProjectModel() this.project = new this.ProjectModel()
this.handler._buildTemplate = function( this.handler._buildTemplate = function(
template_name, templateName,
user, user,
project_name, projectName,
callback callback
) { ) {
if (template_name === 'main.tex') { if (templateName === 'main.tex') {
return callback(null, ['main.tex', 'lines']) return callback(null, ['main.tex', 'lines'])
} }
if (template_name === 'references.bib') { if (templateName === 'references.bib') {
return callback(null, ['references.bib', 'lines']) return callback(null, ['references.bib', 'lines'])
} }
throw new Error(`unknown template: ${template_name}`) throw new Error(`unknown template: ${templateName}`)
} }
sinon.spy(this.handler, '_buildTemplate') sinon.spy(this.handler, '_buildTemplate')
this.handler.createBlankProject = sinon this.handler.createBlankProject = sinon
@ -457,29 +450,25 @@ describe('ProjectCreationHandler', function() {
this.handler._createRootDoc = sinon this.handler._createRootDoc = sinon
.stub() .stub()
.callsArgWith(3, null, this.project) .callsArgWith(3, null, this.project)
return this.handler.createExampleProject( this.handler.createExampleProject(ownerId, projectName, this.callback)
ownerId,
projectName,
this.callback
)
}) })
it('should create a blank project first', function() { it('should create a blank project first', function() {
return this.handler.createBlankProject this.handler.createBlankProject
.calledWith(ownerId, projectName) .calledWith(ownerId, projectName)
.should.equal(true) .should.equal(true)
}) })
it('should create the root document', function() { it('should create the root document', function() {
return this.handler._createRootDoc this.handler._createRootDoc
.calledWith(this.project, ownerId, ['main.tex', 'lines']) .calledWith(this.project, ownerId, ['main.tex', 'lines'])
.should.equal(true) .should.equal(true)
}) })
it('should insert references.bib', function() { it('should insert references.bib', function() {
return this.ProjectEntityUpdateHandler.addDoc this.ProjectEntityUpdateHandler.addDoc
.calledWith( .calledWith(
project_id, projectId,
rootFolderId, rootFolderId,
'references.bib', 'references.bib',
['references.bib', 'lines'], ['references.bib', 'lines'],
@ -489,13 +478,16 @@ describe('ProjectCreationHandler', function() {
}) })
it('should insert universe.jpg', function() { it('should insert universe.jpg', function() {
return this.ProjectEntityUpdateHandler.addFile this.ProjectEntityUpdateHandler.addFile
.calledWith( .calledWith(
project_id, projectId,
rootFolderId, rootFolderId,
'universe.jpg', 'universe.jpg',
Path.resolve( Path.resolve(
__dirname + '/../../../../app/templates/project_files/universe.jpg' Path.join(
__dirname,
'../../../../app/templates/project_files/universe.jpg'
)
), ),
null, null,
ownerId ownerId
@ -504,13 +496,13 @@ describe('ProjectCreationHandler', function() {
}) })
it('should build the main.tex template', function() { it('should build the main.tex template', function() {
return this.handler._buildTemplate this.handler._buildTemplate
.calledWith('main.tex', ownerId, projectName) .calledWith('main.tex', ownerId, projectName)
.should.equal(true) .should.equal(true)
}) })
it('should build the references.bib template', function() { it('should build the references.bib template', function() {
return this.handler._buildTemplate this.handler._buildTemplate
.calledWith('references.bib', ownerId, projectName) .calledWith('references.bib', ownerId, projectName)
.should.equal(true) .should.equal(true)
}) })
@ -518,44 +510,47 @@ describe('ProjectCreationHandler', function() {
describe('_buildTemplate', function() { describe('_buildTemplate', function() {
beforeEach(function(done) { beforeEach(function(done) {
return this.handler._buildTemplate( this.handler._buildTemplate(
'main.tex', 'main.tex',
this.user_id, this.user_id,
projectName, projectName,
(err, templateLines) => { (err, templateLines) => {
if (err != null) {
return done(err)
}
this.template = templateLines.reduce( this.template = templateLines.reduce(
(singleLine, line) => `${singleLine}\n${line}` (singleLine, line) => `${singleLine}\n${line}`
) )
return done() done()
} }
) )
}) })
it('should insert the project name into the template', function(done) { it('should insert the project name into the template', function(done) {
this.template.indexOf(projectName).should.not.equal(-1) this.template.indexOf(projectName).should.not.equal(-1)
return done() done()
}) })
it('should insert the users name into the template', function(done) { it('should insert the users name into the template', function(done) {
this.template.indexOf(this.user.first_name).should.not.equal(-1) this.template.indexOf(this.user.first_name).should.not.equal(-1)
this.template.indexOf(this.user.last_name).should.not.equal(-1) this.template.indexOf(this.user.last_name).should.not.equal(-1)
return done() done()
}) })
it('should not have undefined in the template', function(done) { it('should not have undefined in the template', function(done) {
this.template.indexOf('undefined').should.equal(-1) this.template.indexOf('undefined').should.equal(-1)
return done() done()
}) })
it('should not have any underscore brackets in the output', function(done) { it('should not have any underscore brackets in the output', function(done) {
this.template.indexOf('{{').should.equal(-1) this.template.indexOf('{{').should.equal(-1)
this.template.indexOf('<%=').should.equal(-1) this.template.indexOf('<%=').should.equal(-1)
return done() done()
}) })
it('should put the year in', function(done) { it('should put the year in', function(done) {
this.template.indexOf(new Date().getUTCFullYear()).should.not.equal(-1) this.template.indexOf(new Date().getUTCFullYear()).should.not.equal(-1)
return done() done()
}) })
}) })
@ -563,7 +558,7 @@ describe('ProjectCreationHandler', function() {
beforeEach(function(done) { beforeEach(function(done) {
this.project = new this.ProjectModel() this.project = new this.ProjectModel()
return this.handler._createRootDoc( this.handler._createRootDoc(
this.project, this.project,
ownerId, ownerId,
['line 1', 'line 2'], ['line 1', 'line 2'],
@ -572,9 +567,9 @@ describe('ProjectCreationHandler', function() {
}) })
it('should insert main.tex', function() { it('should insert main.tex', function() {
return this.ProjectEntityUpdateHandler.addDoc this.ProjectEntityUpdateHandler.addDoc
.calledWith( .calledWith(
project_id, projectId,
rootFolderId, rootFolderId,
'main.tex', 'main.tex',
['line 1', 'line 2'], ['line 1', 'line 2'],
@ -584,8 +579,8 @@ describe('ProjectCreationHandler', function() {
}) })
it('should set the main doc id', function() { it('should set the main doc id', function() {
return this.ProjectEntityUpdateHandler.setRootDoc this.ProjectEntityUpdateHandler.setRootDoc
.calledWith(project_id, docId) .calledWith(projectId, docId)
.should.equal(true) .should.equal(true)
}) })
}) })

View file

@ -1,30 +1,11 @@
/* eslint-disable const { expect } = require('chai')
camelcase,
handle-callback-err,
max-len,
no-return-assign,
no-use-before-define,
*/
// 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
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const spies = require('chai-spies')
const chai = require('chai').use(spies)
const { assert } = require('chai')
const should = chai.should()
const modulePath = '../../../../app/src/Features/Project/ProjectLocator' const modulePath = '../../../../app/src/Features/Project/ProjectLocator'
const SandboxedModule = require('sandboxed-module') const SandboxedModule = require('sandboxed-module')
const sinon = require('sinon') const sinon = require('sinon')
const Errors = require('../../../../app/src/Features/Errors/Errors') const Errors = require('../../../../app/src/Features/Errors/Errors')
const { expect } = require('chai')
var Project = (Project = class Project {})
const project = { _id: '1234566', rootFolder: [] } const project = { _id: '1234566', rootFolder: [] }
class Project {}
const rootDoc = { name: 'rootDoc', _id: 'das239djd' } const rootDoc = { name: 'rootDoc', _id: 'das239djd' }
const doc1 = { name: 'otherDoc.txt', _id: 'dsad2ddd' } const doc1 = { name: 'otherDoc.txt', _id: 'dsad2ddd' }
const doc2 = { name: 'docname.txt', _id: 'dsad2ddddd' } const doc2 = { name: 'docname.txt', _id: 'dsad2ddddd' }
@ -59,8 +40,8 @@ project.rootDoc_id = rootDoc._id
describe('ProjectLocator', function() { describe('ProjectLocator', function() {
beforeEach(function() { beforeEach(function() {
Project.findById = (project_id, callback) => { Project.findById = (projectId, callback) => {
return callback(null, project) callback(null, project)
} }
this.ProjectGetter = { this.ProjectGetter = {
getProject: sinon.stub().callsArgWith(2, null, project) getProject: sinon.stub().callsArgWith(2, null, project)
@ -68,7 +49,7 @@ describe('ProjectLocator', function() {
this.ProjectHelper = { this.ProjectHelper = {
isArchived: sinon.stub() isArchived: sinon.stub()
} }
return (this.locator = SandboxedModule.require(modulePath, { this.locator = SandboxedModule.require(modulePath, {
globals: { globals: {
console: console console: console
}, },
@ -83,46 +64,52 @@ describe('ProjectLocator', function() {
warn() {} warn() {}
} }
} }
})) })
}) })
describe('finding a doc', function() { describe('finding a doc', function() {
it('finds one at the root level', function(done) { it('finds one at the root level', function(done) {
return this.locator.findElement( this.locator.findElement(
{ project_id: project._id, element_id: doc2._id, type: 'docs' }, { project_id: project._id, element_id: doc2._id, type: 'docs' },
(err, foundElement, path, parentFolder) => { (err, foundElement, path, parentFolder) => {
assert(err == null) if (err != null) {
return done(err)
}
foundElement._id.should.equal(doc2._id) foundElement._id.should.equal(doc2._id)
path.fileSystem.should.equal(`/${doc2.name}`) path.fileSystem.should.equal(`/${doc2.name}`)
parentFolder._id.should.equal(project.rootFolder[0]._id) parentFolder._id.should.equal(project.rootFolder[0]._id)
path.mongo.should.equal('rootFolder.0.docs.1') path.mongo.should.equal('rootFolder.0.docs.1')
return done() done()
} }
) )
}) })
it('when it is nested', function(done) { it('when it is nested', function(done) {
return this.locator.findElement( this.locator.findElement(
{ project_id: project._id, element_id: subSubDoc._id, type: 'doc' }, { project_id: project._id, element_id: subSubDoc._id, type: 'doc' },
(err, foundElement, path, parentFolder) => { (err, foundElement, path, parentFolder) => {
assert(err == null) if (err != null) {
should.equal(foundElement._id, subSubDoc._id) return done(err)
}
expect(foundElement._id).to.equal(subSubDoc._id)
path.fileSystem.should.equal( path.fileSystem.should.equal(
`/${subFolder.name}/${secondSubFolder.name}/${subSubDoc.name}` `/${subFolder.name}/${secondSubFolder.name}/${subSubDoc.name}`
) )
parentFolder._id.should.equal(secondSubFolder._id) parentFolder._id.should.equal(secondSubFolder._id)
path.mongo.should.equal('rootFolder.0.folders.1.folders.0.docs.0') path.mongo.should.equal('rootFolder.0.folders.1.folders.0.docs.0')
return done() done()
} }
) )
}) })
it('should give error if element could not be found', function(done) { it('should give error if element could not be found', function(done) {
return this.locator.findElement( this.locator.findElement(
{ project_id: project._id, element_id: 'ddsd432nj42', type: 'docs' }, { project_id: project._id, element_id: 'ddsd432nj42', type: 'docs' },
(err, foundElement, path, parentFolder) => { (err, foundElement, path, parentFolder) => {
err.should.deep.equal(new Errors.NotFoundError('entity not found')) expect(err).to.deep.equal(
return done() new Errors.NotFoundError('entity not found')
)
done()
} }
) )
}) })
@ -130,46 +117,52 @@ describe('ProjectLocator', function() {
describe('finding a folder', function() { describe('finding a folder', function() {
it('should return root folder when looking for root folder', function(done) { it('should return root folder when looking for root folder', function(done) {
return this.locator.findElement( this.locator.findElement(
{ project_id: project._id, element_id: rootFolder._id, type: 'folder' }, { project_id: project._id, element_id: rootFolder._id, type: 'folder' },
(err, foundElement, path, parentFolder) => { (err, foundElement, path, parentFolder) => {
assert(!err) if (err != null) {
return done(err)
}
foundElement._id.should.equal(rootFolder._id) foundElement._id.should.equal(rootFolder._id)
return done() done()
} }
) )
}) })
it('when at root', function(done) { it('when at root', function(done) {
return this.locator.findElement( this.locator.findElement(
{ project_id: project._id, element_id: subFolder._id, type: 'folder' }, { project_id: project._id, element_id: subFolder._id, type: 'folder' },
(err, foundElement, path, parentFolder) => { (err, foundElement, path, parentFolder) => {
assert(!err) if (err != null) {
return done(err)
}
foundElement._id.should.equal(subFolder._id) foundElement._id.should.equal(subFolder._id)
path.fileSystem.should.equal(`/${subFolder.name}`) path.fileSystem.should.equal(`/${subFolder.name}`)
parentFolder._id.should.equal(rootFolder._id) parentFolder._id.should.equal(rootFolder._id)
path.mongo.should.equal('rootFolder.0.folders.1') path.mongo.should.equal('rootFolder.0.folders.1')
return done() done()
} }
) )
}) })
it('when deeply nested', function(done) { it('when deeply nested', function(done) {
return this.locator.findElement( this.locator.findElement(
{ {
project_id: project._id, project_id: project._id,
element_id: secondSubFolder._id, element_id: secondSubFolder._id,
type: 'folder' type: 'folder'
}, },
(err, foundElement, path, parentFolder) => { (err, foundElement, path, parentFolder) => {
assert(!err) if (err != null) {
return done(err)
}
foundElement._id.should.equal(secondSubFolder._id) foundElement._id.should.equal(secondSubFolder._id)
path.fileSystem.should.equal( path.fileSystem.should.equal(
`/${subFolder.name}/${secondSubFolder.name}` `/${subFolder.name}/${secondSubFolder.name}`
) )
parentFolder._id.should.equal(subFolder._id) parentFolder._id.should.equal(subFolder._id)
path.mongo.should.equal('rootFolder.0.folders.1.folders.0') path.mongo.should.equal('rootFolder.0.folders.1.folders.0')
return done() done()
} }
) )
}) })
@ -177,35 +170,39 @@ describe('ProjectLocator', function() {
describe('finding a file', function() { describe('finding a file', function() {
it('when at root', function(done) { it('when at root', function(done) {
return this.locator.findElement( this.locator.findElement(
{ project_id: project._id, element_id: file1._id, type: 'fileRefs' }, { project_id: project._id, element_id: file1._id, type: 'fileRefs' },
(err, foundElement, path, parentFolder) => { (err, foundElement, path, parentFolder) => {
assert(!err) if (err != null) {
return done(err)
}
foundElement._id.should.equal(file1._id) foundElement._id.should.equal(file1._id)
path.fileSystem.should.equal(`/${file1.name}`) path.fileSystem.should.equal(`/${file1.name}`)
parentFolder._id.should.equal(rootFolder._id) parentFolder._id.should.equal(rootFolder._id)
path.mongo.should.equal('rootFolder.0.fileRefs.0') path.mongo.should.equal('rootFolder.0.fileRefs.0')
return done() done()
} }
) )
}) })
it('when deeply nested', function(done) { it('when deeply nested', function(done) {
return this.locator.findElement( this.locator.findElement(
{ {
project_id: project._id, project_id: project._id,
element_id: subSubFile._id, element_id: subSubFile._id,
type: 'fileRefs' type: 'fileRefs'
}, },
(err, foundElement, path, parentFolder) => { (err, foundElement, path, parentFolder) => {
assert(!err) if (err != null) {
return done(err)
}
foundElement._id.should.equal(subSubFile._id) foundElement._id.should.equal(subSubFile._id)
path.fileSystem.should.equal( path.fileSystem.should.equal(
`/${subFolder.name}/${secondSubFolder.name}/${subSubFile.name}` `/${subFolder.name}/${secondSubFolder.name}/${subSubFile.name}`
) )
parentFolder._id.should.equal(secondSubFolder._id) parentFolder._id.should.equal(secondSubFolder._id)
path.mongo.should.equal('rootFolder.0.folders.1.folders.0.fileRefs.0') path.mongo.should.equal('rootFolder.0.folders.1.folders.0.fileRefs.0')
return done() done()
} }
) )
}) })
@ -213,23 +210,27 @@ describe('ProjectLocator', function() {
describe('finding an element with wrong element type', function() { describe('finding an element with wrong element type', function() {
it('should add an s onto the element type', function(done) { it('should add an s onto the element type', function(done) {
return this.locator.findElement( this.locator.findElement(
{ project_id: project._id, element_id: subSubDoc._id, type: 'doc' }, { project_id: project._id, element_id: subSubDoc._id, type: 'doc' },
(err, foundElement, path, parentFolder) => { (err, foundElement, path, parentFolder) => {
assert(!err) if (err != null) {
return done(err)
}
foundElement._id.should.equal(subSubDoc._id) foundElement._id.should.equal(subSubDoc._id)
return done() done()
} }
) )
}) })
it('should convert file to fileRefs', function(done) { it('should convert file to fileRefs', function(done) {
return this.locator.findElement( this.locator.findElement(
{ project_id: project._id, element_id: file1._id, type: 'fileRefs' }, { project_id: project._id, element_id: file1._id, type: 'fileRefs' },
(err, foundElement, path, parentFolder) => { (err, foundElement, path, parentFolder) => {
assert(!err) if (err != null) {
return done(err)
}
foundElement._id.should.equal(file1._id) foundElement._id.should.equal(file1._id)
return done() done()
} }
) )
}) })
@ -249,15 +250,17 @@ describe('ProjectLocator', function() {
rootFolder: [rootFolder2] rootFolder: [rootFolder2]
} }
it('should find doc in project', function(done) { it('should find doc in project', function(done) {
return this.locator.findElement( this.locator.findElement(
{ project: project2, element_id: doc3._id, type: 'docs' }, { project: project2, element_id: doc3._id, type: 'docs' },
(err, foundElement, path, parentFolder) => { (err, foundElement, path, parentFolder) => {
assert(err == null) if (err != null) {
return done(err)
}
foundElement._id.should.equal(doc3._id) foundElement._id.should.equal(doc3._id)
path.fileSystem.should.equal(`/${doc3.name}`) path.fileSystem.should.equal(`/${doc3.name}`)
parentFolder._id.should.equal(project2.rootFolder[0]._id) parentFolder._id.should.equal(project2.rootFolder[0]._id)
path.mongo.should.equal('rootFolder.0.docs.0') path.mongo.should.equal('rootFolder.0.docs.0')
return done() done()
} }
) )
}) })
@ -265,36 +268,44 @@ describe('ProjectLocator', function() {
describe('finding root doc', function() { describe('finding root doc', function() {
it('should return root doc when passed project', function(done) { it('should return root doc when passed project', function(done) {
return this.locator.findRootDoc(project, (err, doc) => { this.locator.findRootDoc(project, (err, doc) => {
assert(err == null) if (err != null) {
return done(err)
}
doc._id.should.equal(rootDoc._id) doc._id.should.equal(rootDoc._id)
return done() done()
}) })
}) })
it('should return root doc when passed project_id', function(done) { it('should return root doc when passed project_id', function(done) {
return this.locator.findRootDoc(project._id, (err, doc) => { this.locator.findRootDoc(project._id, (err, doc) => {
assert(err == null) if (err != null) {
return done(err)
}
doc._id.should.equal(rootDoc._id) doc._id.should.equal(rootDoc._id)
return done() done()
}) })
}) })
it('should return null when the project has no rootDoc', function(done) { it('should return null when the project has no rootDoc', function(done) {
project.rootDoc_id = null project.rootDoc_id = null
return this.locator.findRootDoc(project, (err, doc) => { this.locator.findRootDoc(project, (err, doc) => {
assert(err == null) if (err != null) {
return done(err)
}
expect(doc).to.equal(null) expect(doc).to.equal(null)
return done() done()
}) })
}) })
it('should return null when the rootDoc_id no longer exists', function(done) { it('should return null when the rootDoc_id no longer exists', function(done) {
project.rootDoc_id = 'doesntexist' project.rootDoc_id = 'doesntexist'
return this.locator.findRootDoc(project, (err, doc) => { this.locator.findRootDoc(project, (err, doc) => {
assert(err == null) if (err != null) {
return done(err)
}
expect(doc).to.equal(null) expect(doc).to.equal(null)
return done() done()
}) })
}) })
}) })
@ -302,48 +313,60 @@ describe('ProjectLocator', function() {
describe('findElementByPath', function() { describe('findElementByPath', function() {
it('should take a doc path and return the element for a root level document', function(done) { it('should take a doc path and return the element for a root level document', function(done) {
const path = `${doc1.name}` const path = `${doc1.name}`
return this.locator.findElementByPath( this.locator.findElementByPath(
{ project, path }, { project, path },
(err, element, type) => { (err, element, type) => {
if (err != null) {
return done(err)
}
element.should.deep.equal(doc1) element.should.deep.equal(doc1)
expect(type).to.equal('doc') expect(type).to.equal('doc')
return done() done()
} }
) )
}) })
it('should take a doc path and return the element for a root level document with a starting slash', function(done) { it('should take a doc path and return the element for a root level document with a starting slash', function(done) {
const path = `/${doc1.name}` const path = `/${doc1.name}`
return this.locator.findElementByPath( this.locator.findElementByPath(
{ project, path }, { project, path },
(err, element, type) => { (err, element, type) => {
if (err != null) {
return done(err)
}
element.should.deep.equal(doc1) element.should.deep.equal(doc1)
expect(type).to.equal('doc') expect(type).to.equal('doc')
return done() done()
} }
) )
}) })
it('should take a doc path and return the element for a nested document', function(done) { it('should take a doc path and return the element for a nested document', function(done) {
const path = `${subFolder.name}/${secondSubFolder.name}/${subSubDoc.name}` const path = `${subFolder.name}/${secondSubFolder.name}/${subSubDoc.name}`
return this.locator.findElementByPath( this.locator.findElementByPath(
{ project, path }, { project, path },
(err, element, type) => { (err, element, type) => {
if (err != null) {
return done(err)
}
element.should.deep.equal(subSubDoc) element.should.deep.equal(subSubDoc)
expect(type).to.equal('doc') expect(type).to.equal('doc')
return done() done()
} }
) )
}) })
it('should take a file path and return the element for a root level document', function(done) { it('should take a file path and return the element for a root level document', function(done) {
const path = `${file1.name}` const path = `${file1.name}`
return this.locator.findElementByPath( this.locator.findElementByPath(
{ project, path }, { project, path },
(err, element, type) => { (err, element, type) => {
if (err != null) {
return done(err)
}
element.should.deep.equal(file1) element.should.deep.equal(file1)
expect(type).to.equal('file') expect(type).to.equal('file')
return done() done()
} }
) )
}) })
@ -352,74 +375,86 @@ describe('ProjectLocator', function() {
const path = `${subFolder.name}/${secondSubFolder.name}/${ const path = `${subFolder.name}/${secondSubFolder.name}/${
subSubFile.name subSubFile.name
}` }`
return this.locator.findElementByPath( this.locator.findElementByPath(
{ project, path }, { project, path },
(err, element, type) => { (err, element, type) => {
if (err != null) {
return done(err)
}
element.should.deep.equal(subSubFile) element.should.deep.equal(subSubFile)
expect(type).to.equal('file') expect(type).to.equal('file')
return done() done()
} }
) )
}) })
it('should take a file path and return the element for a nested document case insenstive', function(done) { it('should take a file path and return the element for a nested document case insenstive', function(done) {
const path = `${subFolder.name.toUpperCase()}/${secondSubFolder.name.toUpperCase()}/${subSubFile.name.toUpperCase()}` const path = `${subFolder.name.toUpperCase()}/${secondSubFolder.name.toUpperCase()}/${subSubFile.name.toUpperCase()}`
return this.locator.findElementByPath( this.locator.findElementByPath(
{ project, path }, { project, path },
(err, element, type) => { (err, element, type) => {
if (err != null) {
return done(err)
}
element.should.deep.equal(subSubFile) element.should.deep.equal(subSubFile)
expect(type).to.equal('file') expect(type).to.equal('file')
return done() done()
} }
) )
}) })
it('should take a file path and return the element for a nested folder', function(done) { it('should take a file path and return the element for a nested folder', function(done) {
const path = `${subFolder.name}/${secondSubFolder.name}` const path = `${subFolder.name}/${secondSubFolder.name}`
return this.locator.findElementByPath( this.locator.findElementByPath(
{ project, path }, { project, path },
(err, element, type) => { (err, element, type) => {
if (err != null) {
return done(err)
}
element.should.deep.equal(secondSubFolder) element.should.deep.equal(secondSubFolder)
expect(type).to.equal('folder') expect(type).to.equal('folder')
return done() done()
} }
) )
}) })
it('should take a file path and return the root folder', function(done) { it('should take a file path and return the root folder', function(done) {
const path = '/' const path = '/'
return this.locator.findElementByPath( this.locator.findElementByPath(
{ project, path }, { project, path },
(err, element, type) => { (err, element, type) => {
if (err != null) {
return done(err)
}
element.should.deep.equal(rootFolder) element.should.deep.equal(rootFolder)
expect(type).to.equal('folder') expect(type).to.equal('folder')
return done() done()
} }
) )
}) })
it('should return an error if the file can not be found inside know folder', function(done) { it('should return an error if the file can not be found inside know folder', function(done) {
const path = `${subFolder.name}/${secondSubFolder.name}/exist.txt` const path = `${subFolder.name}/${secondSubFolder.name}/exist.txt`
return this.locator.findElementByPath( this.locator.findElementByPath(
{ project, path }, { project, path },
(err, element, type) => { (err, element, type) => {
err.should.not.equal(undefined) err.should.not.equal(undefined)
assert.equal(element, undefined) expect(element).to.be.undefined
expect(type).to.be.undefined expect(type).to.be.undefined
return done() done()
} }
) )
}) })
it('should return an error if the file can not be found inside unknown folder', function(done) { it('should return an error if the file can not be found inside unknown folder', function(done) {
const path = 'this/does/not/exist.txt' const path = 'this/does/not/exist.txt'
return this.locator.findElementByPath( this.locator.findElementByPath(
{ project, path }, { project, path },
(err, element, type) => { (err, element, type) => {
err.should.not.equal(undefined) err.should.not.equal(undefined)
assert.equal(element, undefined) expect(element).to.be.undefined
expect(type).to.be.undefined expect(type).to.be.undefined
return done() done()
} }
) )
}) })
@ -440,7 +475,7 @@ describe('ProjectLocator', function() {
docs: [(this.doc = { name: 'main.tex', _id: '456' })], docs: [(this.doc = { name: 'main.tex', _id: '456' })],
fileRefs: [] fileRefs: []
} }
return (this.project = { this.project = {
rootFolder: [ rootFolder: [
{ {
folders: [this.duplicateFolder, this.duplicateFolder], folders: [this.duplicateFolder, this.duplicateFolder],
@ -448,29 +483,27 @@ describe('ProjectLocator', function() {
docs: [] docs: []
} }
] ]
}) }
}) })
it('should not call the callback more than once', function(done) { it('should not call the callback more than once', function(done) {
const path = `${this.duplicateFolder.name}/${this.doc.name}` const path = `${this.duplicateFolder.name}/${this.doc.name}`
return this.locator.findElementByPath( this.locator.findElementByPath({ project: this.project, path }, () =>
{ project: this.project, path }, done()
() => done()
) )
}) // mocha will throw exception if done called multiple times }) // mocha will throw exception if done called multiple times
it('should not call the callback more than once when the path is longer than 1 level below the duplicate level', function(done) { it('should not call the callback more than once when the path is longer than 1 level below the duplicate level', function(done) {
const path = `${this.duplicateFolder.name}/1/main.tex` const path = `${this.duplicateFolder.name}/1/main.tex`
return this.locator.findElementByPath( this.locator.findElementByPath({ project: this.project, path }, () =>
{ project: this.project, path }, done()
() => done()
) )
}) })
}) // mocha will throw exception if done called multiple times }) // mocha will throw exception if done called multiple times
describe('with a null doc', function() { describe('with a null doc', function() {
beforeEach(function() { beforeEach(function() {
return (this.project = { this.project = {
rootFolder: [ rootFolder: [
{ {
folders: [], folders: [],
@ -478,16 +511,19 @@ describe('ProjectLocator', function() {
docs: [{ name: 'main.tex' }, null, { name: 'other.tex' }] docs: [{ name: 'main.tex' }, null, { name: 'other.tex' }]
} }
] ]
}) }
}) })
it('should not crash with a null', function(done) { it('should not crash with a null', function(done) {
const path = '/other.tex' const path = '/other.tex'
return this.locator.findElementByPath( this.locator.findElementByPath(
{ project: this.project, path }, { project: this.project, path },
(err, element) => { (err, element) => {
if (err != null) {
return done(err)
}
element.name.should.equal('other.tex') element.name.should.equal('other.tex')
return done() done()
} }
) )
}) })
@ -495,16 +531,16 @@ describe('ProjectLocator', function() {
describe('with a null project', function() { describe('with a null project', function() {
beforeEach(function() { beforeEach(function() {
return (this.ProjectGetter = { getProject: sinon.stub().callsArg(2) }) this.ProjectGetter = { getProject: sinon.stub().callsArg(2) }
}) })
it('should not crash with a null', function(done) { it('should not crash with a null', function(done) {
const path = '/other.tex' const path = '/other.tex'
return this.locator.findElementByPath( this.locator.findElementByPath(
{ project_id: project._id, path }, { project_id: project._id, path },
(err, element) => { (err, element) => {
expect(err).to.exist expect(err).to.exist
return done() done()
} }
) )
}) })
@ -513,15 +549,18 @@ describe('ProjectLocator', function() {
describe('with a project_id', function() { describe('with a project_id', function() {
it('should take a doc path and return the element for a root level document', function(done) { it('should take a doc path and return the element for a root level document', function(done) {
const path = `${doc1.name}` const path = `${doc1.name}`
return this.locator.findElementByPath( this.locator.findElementByPath(
{ project_id: project._id, path }, { project_id: project._id, path },
(err, element, type) => { (err, element, type) => {
if (err != null) {
return done(err)
}
this.ProjectGetter.getProject this.ProjectGetter.getProject
.calledWith(project._id, { rootFolder: true, rootDoc_id: true }) .calledWith(project._id, { rootFolder: true, rootDoc_id: true })
.should.equal(true) .should.equal(true)
element.should.deep.equal(doc1) element.should.deep.equal(doc1)
expect(type).to.equal('doc') expect(type).to.equal('doc')
return done() done()
} }
) )
}) })
@ -530,7 +569,7 @@ describe('ProjectLocator', function() {
describe('findUsersProjectByName finding a project by user_id and project name', function() { describe('findUsersProjectByName finding a project by user_id and project name', function() {
it('should return the project from an array case insenstive', function(done) { it('should return the project from an array case insenstive', function(done) {
const user_id = '123jojoidns' const userId = '123jojoidns'
const stubbedProject = { name: 'findThis' } const stubbedProject = { name: 'findThis' }
const projects = { const projects = {
owned: [ owned: [
@ -543,18 +582,21 @@ describe('ProjectLocator', function() {
this.ProjectGetter.findAllUsersProjects = sinon this.ProjectGetter.findAllUsersProjects = sinon
.stub() .stub()
.callsArgWith(2, null, projects) .callsArgWith(2, null, projects)
return this.locator.findUsersProjectByName( this.locator.findUsersProjectByName(
user_id, userId,
stubbedProject.name.toLowerCase(), stubbedProject.name.toLowerCase(),
(err, project) => { (err, project) => {
if (err != null) {
return done(err)
}
project.should.equal(stubbedProject) project.should.equal(stubbedProject)
return done() done()
} }
) )
}) })
it('should return the project which is not archived', function(done) { it('should return the project which is not archived', function(done) {
const user_id = '123jojoidns' const userId = '123jojoidns'
const stubbedProject = { name: 'findThis', _id: 12331321 } const stubbedProject = { name: 'findThis', _id: 12331321 }
const projects = { const projects = {
owned: [ owned: [
@ -568,39 +610,42 @@ describe('ProjectLocator', function() {
} }
this.ProjectHelper.isArchived this.ProjectHelper.isArchived
.withArgs(projects.owned[0], user_id) .withArgs(projects.owned[0], userId)
.returns(false) .returns(false)
this.ProjectHelper.isArchived this.ProjectHelper.isArchived
.withArgs(projects.owned[1], user_id) .withArgs(projects.owned[1], userId)
.returns(false) .returns(false)
this.ProjectHelper.isArchived this.ProjectHelper.isArchived
.withArgs(projects.owned[2], user_id) .withArgs(projects.owned[2], userId)
.returns(true) .returns(true)
this.ProjectHelper.isArchived this.ProjectHelper.isArchived
.withArgs(projects.owned[3], user_id) .withArgs(projects.owned[3], userId)
.returns(false) .returns(false)
this.ProjectHelper.isArchived this.ProjectHelper.isArchived
.withArgs(projects.owned[4], user_id) .withArgs(projects.owned[4], userId)
.returns(true) .returns(true)
this.ProjectHelper.isArchived this.ProjectHelper.isArchived
.withArgs(projects.owned[5], user_id) .withArgs(projects.owned[5], userId)
.returns(false) .returns(false)
this.ProjectGetter.findAllUsersProjects = sinon this.ProjectGetter.findAllUsersProjects = sinon
.stub() .stub()
.callsArgWith(2, null, projects) .callsArgWith(2, null, projects)
return this.locator.findUsersProjectByName( this.locator.findUsersProjectByName(
user_id, userId,
stubbedProject.name.toLowerCase(), stubbedProject.name.toLowerCase(),
(err, project) => { (err, project) => {
if (err != null) {
return done(err)
}
project._id.should.equal(stubbedProject._id) project._id.should.equal(stubbedProject._id)
return done() done()
} }
) )
}) })
it('should search collab projects as well', function(done) { it('should search collab projects as well', function(done) {
const user_id = '123jojoidns' const userId = '123jojoidns'
const stubbedProject = { name: 'findThis' } const stubbedProject = { name: 'findThis' }
const projects = { const projects = {
owned: [{ name: 'notThis' }, { name: 'wellll' }, { name: 'Noooo' }], owned: [{ name: 'notThis' }, { name: 'wellll' }, { name: 'Noooo' }],
@ -609,12 +654,15 @@ describe('ProjectLocator', function() {
this.ProjectGetter.findAllUsersProjects = sinon this.ProjectGetter.findAllUsersProjects = sinon
.stub() .stub()
.callsArgWith(2, null, projects) .callsArgWith(2, null, projects)
return this.locator.findUsersProjectByName( this.locator.findUsersProjectByName(
user_id, userId,
stubbedProject.name.toLowerCase(), stubbedProject.name.toLowerCase(),
(err, project) => { (err, project) => {
if (err != null) {
return done(err)
}
project.should.equal(stubbedProject) project.should.equal(stubbedProject)
return done() done()
} }
) )
}) })