| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753 |
- /* eslint-disable
- camelcase,
- handle-callback-err,
- max-len,
- 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
- * DS207: Consider shorter variations of null checks
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
- */
- const async = require('async')
- const { expect } = require('chai')
- const _ = require('underscore')
- const mkdirp = require('mkdirp')
- const Settings = require('settings-sharelatex')
- const MockFileStoreApi = require('./helpers/MockFileStoreApi')
- const request = require('./helpers/request')
- const User = require('./helpers/User')
- const MockClsiApi = require('./helpers/MockClsiApi')
- const express = require('express')
- const LinkedUrlProxy = express()
- LinkedUrlProxy.get('/', (req, res, next) => {
- if (req.query.url === 'http://example.com/foo') {
- return res.send('foo foo foo')
- } else if (req.query.url === 'http://example.com/bar') {
- return res.send('bar bar bar')
- } else {
- return res.sendStatus(404)
- }
- })
- describe('LinkedFiles', function() {
- before(function(done) {
- return LinkedUrlProxy.listen(6543, error => {
- if (error != null) {
- return done(error)
- }
- this.owner = new User()
- return this.owner.login(() => mkdirp(Settings.path.dumpFolder, done))
- })
- })
- describe('creating a project linked file', function() {
- before(function(done) {
- this.source_doc_name = 'test.txt'
- return async.series(
- [
- cb => {
- return this.owner.createProject(
- 'plf-test-one',
- { template: 'blank' },
- (error, project_id) => {
- this.project_one_id = project_id
- return cb(error)
- }
- )
- },
- cb => {
- return this.owner.getProject(
- this.project_one_id,
- (error, project) => {
- this.project_one = project
- this.project_one_root_folder_id = project.rootFolder[0]._id.toString()
- return cb(error)
- }
- )
- },
- cb => {
- return this.owner.createProject(
- 'plf-test-two',
- { template: 'blank' },
- (error, project_id) => {
- this.project_two_id = project_id
- return cb(error)
- }
- )
- },
- cb => {
- return this.owner.getProject(
- this.project_two_id,
- (error, project) => {
- this.project_two = project
- this.project_two_root_folder_id = project.rootFolder[0]._id.toString()
- return cb(error)
- }
- )
- },
- cb => {
- return this.owner.createDocInProject(
- this.project_two_id,
- this.project_two_root_folder_id,
- this.source_doc_name,
- (error, doc_id) => {
- this.source_doc_id = doc_id
- return cb(error)
- }
- )
- },
- cb => {
- return this.owner.createDocInProject(
- this.project_two_id,
- this.project_two_root_folder_id,
- 'some-harmless-doc.txt',
- (error, doc_id) => {
- return cb(error)
- }
- )
- }
- ],
- done
- )
- })
- it('should produce a list of the users projects', function(done) {
- return this.owner.request.get(
- {
- url: '/user/projects',
- json: true
- },
- (err, response, body) => {
- expect(err).to.not.exist
- expect(body).to.deep.equal({
- projects: [
- {
- _id: this.project_one_id,
- name: 'plf-test-one',
- accessLevel: 'owner'
- },
- {
- _id: this.project_two_id,
- name: 'plf-test-two',
- accessLevel: 'owner'
- }
- ]
- })
- return done()
- }
- )
- })
- it('should produce a list of entities in the project', function(done) {
- return this.owner.request.get(
- {
- url: `/project/${this.project_two_id}/entities`,
- json: true
- },
- (err, response, body) => {
- expect(err).to.not.exist
- expect(body).to.deep.equal({
- project_id: this.project_two_id,
- entities: [
- { path: '/main.tex', type: 'doc' },
- { path: '/some-harmless-doc.txt', type: 'doc' },
- { path: '/test.txt', type: 'doc' }
- ]
- })
- return done()
- }
- )
- })
- it('should import a file from the source project', function(done) {
- return this.owner.request.post(
- {
- url: `/project/${this.project_one_id}/linked_file`,
- json: {
- name: 'test-link.txt',
- parent_folder_id: this.project_one_root_folder_id,
- provider: 'project_file',
- data: {
- source_project_id: this.project_two_id,
- source_entity_path: `/${this.source_doc_name}`
- }
- }
- },
- (error, response, body) => {
- expect(response.statusCode).to.equal(200)
- const { new_file_id } = body
- this.existing_file_id = new_file_id
- expect(new_file_id).to.exist
- return this.owner.getProject(
- this.project_one_id,
- (error, project) => {
- if (error != null) {
- return done(error)
- }
- const firstFile = project.rootFolder[0].fileRefs[0]
- expect(firstFile._id.toString()).to.equal(new_file_id.toString())
- expect(firstFile.linkedFileData).to.deep.equal({
- provider: 'project_file',
- source_project_id: this.project_two_id,
- source_entity_path: `/${this.source_doc_name}`
- })
- expect(firstFile.name).to.equal('test-link.txt')
- return done()
- }
- )
- }
- )
- })
- it('should refresh the file', function(done) {
- return this.owner.request.post(
- {
- url: `/project/${this.project_one_id}/linked_file/${
- this.existing_file_id
- }/refresh`,
- json: true
- },
- (error, response, body) => {
- expect(response.statusCode).to.equal(200)
- const { new_file_id } = body
- expect(new_file_id).to.exist
- expect(new_file_id).to.not.equal(this.existing_file_id)
- this.refreshed_file_id = new_file_id
- return this.owner.getProject(
- this.project_one_id,
- (error, project) => {
- if (error != null) {
- return done(error)
- }
- const firstFile = project.rootFolder[0].fileRefs[0]
- expect(firstFile._id.toString()).to.equal(new_file_id.toString())
- expect(firstFile.name).to.equal('test-link.txt')
- return done()
- }
- )
- }
- )
- })
- return it('should not allow to create a linked-file with v1 id', function(done) {
- return this.owner.request.post(
- {
- url: `/project/${this.project_one_id}/linked_file`,
- json: {
- name: 'test-link-should-not-work.txt',
- parent_folder_id: this.project_one_root_folder_id,
- provider: 'project_file',
- data: {
- v1_source_doc_id: 1234,
- source_entity_path: `/${this.source_doc_name}`
- }
- }
- },
- (error, response, body) => {
- expect(response.statusCode).to.equal(403)
- expect(body).to.equal('You do not have access to this project')
- return done()
- }
- )
- })
- })
- describe('with a linked project_file from a v1 project that has not been imported', function() {
- before(function(done) {
- return async.series(
- [
- cb => {
- return this.owner.createProject(
- 'plf-v1-test-one',
- { template: 'blank' },
- (error, project_id) => {
- this.project_one_id = project_id
- return cb(error)
- }
- )
- },
- cb => {
- return this.owner.getProject(
- this.project_one_id,
- (error, project) => {
- this.project_one = project
- this.project_one_root_folder_id = project.rootFolder[0]._id.toString()
- this.project_one.rootFolder[0].fileRefs.push({
- linkedFileData: {
- provider: 'project_file',
- v1_source_doc_id: 9999999, // We won't find this id in the database
- source_entity_path: 'example.jpeg'
- },
- _id: 'abcd',
- rev: 0,
- created: new Date(),
- name: 'example.jpeg'
- })
- return this.owner.saveProject(this.project_one, cb)
- }
- )
- }
- ],
- done
- )
- })
- return it('should refuse to refresh', function(done) {
- return this.owner.request.post(
- {
- url: `/project/${this.project_one_id}/linked_file/abcd/refresh`,
- json: true
- },
- (error, response, body) => {
- expect(response.statusCode).to.equal(409)
- expect(body).to.equal(
- 'Sorry, the source project is not yet imported to Overleaf v2. Please import it to Overleaf v2 to refresh this file'
- )
- return done()
- }
- )
- })
- })
- describe('creating a URL based linked file', function() {
- before(function(done) {
- return this.owner.createProject(
- 'url-linked-files-project',
- { template: 'blank' },
- (error, project_id) => {
- if (error != null) {
- throw error
- }
- this.project_id = project_id
- return this.owner.getProject(project_id, (error, project) => {
- if (error != null) {
- throw error
- }
- this.project = project
- this.root_folder_id = project.rootFolder[0]._id.toString()
- return done()
- })
- }
- )
- })
- it('should download the URL and create a file with the contents and linkedFileData', function(done) {
- return this.owner.request.post(
- {
- url: `/project/${this.project_id}/linked_file`,
- json: {
- provider: 'url',
- data: {
- url: 'http://example.com/foo'
- },
- parent_folder_id: this.root_folder_id,
- name: 'url-test-file-1'
- }
- },
- (error, response, body) => {
- if (error != null) {
- throw error
- }
- expect(response.statusCode).to.equal(200)
- return this.owner.getProject(this.project_id, (error, project) => {
- if (error != null) {
- throw error
- }
- const file = project.rootFolder[0].fileRefs[0]
- expect(file.linkedFileData).to.deep.equal({
- provider: 'url',
- url: 'http://example.com/foo'
- })
- return this.owner.request.get(
- `/project/${this.project_id}/file/${file._id}`,
- function(error, response, body) {
- if (error != null) {
- throw error
- }
- expect(response.statusCode).to.equal(200)
- expect(body).to.equal('foo foo foo')
- return done()
- }
- )
- })
- }
- )
- })
- it('should replace and update a URL based linked file', function(done) {
- return this.owner.request.post(
- {
- url: `/project/${this.project_id}/linked_file`,
- json: {
- provider: 'url',
- data: {
- url: 'http://example.com/foo'
- },
- parent_folder_id: this.root_folder_id,
- name: 'url-test-file-2'
- }
- },
- (error, response, body) => {
- if (error != null) {
- throw error
- }
- expect(response.statusCode).to.equal(200)
- return this.owner.request.post(
- {
- url: `/project/${this.project_id}/linked_file`,
- json: {
- provider: 'url',
- data: {
- url: 'http://example.com/bar'
- },
- parent_folder_id: this.root_folder_id,
- name: 'url-test-file-2'
- }
- },
- (error, response, body) => {
- if (error != null) {
- throw error
- }
- expect(response.statusCode).to.equal(200)
- return this.owner.getProject(
- this.project_id,
- (error, project) => {
- if (error != null) {
- throw error
- }
- const file = project.rootFolder[0].fileRefs[1]
- expect(file.linkedFileData).to.deep.equal({
- provider: 'url',
- url: 'http://example.com/bar'
- })
- return this.owner.request.get(
- `/project/${this.project_id}/file/${file._id}`,
- function(error, response, body) {
- if (error != null) {
- throw error
- }
- expect(response.statusCode).to.equal(200)
- expect(body).to.equal('bar bar bar')
- return done()
- }
- )
- }
- )
- }
- )
- }
- )
- })
- it('should return an error if the URL does not succeed', function(done) {
- return this.owner.request.post(
- {
- url: `/project/${this.project_id}/linked_file`,
- json: {
- provider: 'url',
- data: {
- url: 'http://example.com/does-not-exist'
- },
- parent_folder_id: this.root_folder_id,
- name: 'url-test-file-3'
- }
- },
- (error, response, body) => {
- if (error != null) {
- throw error
- }
- expect(response.statusCode).to.equal(422) // unprocessable
- expect(body).to.equal(
- 'Your URL could not be reached (404 status code). Please check it and try again.'
- )
- return done()
- }
- )
- })
- it('should return an error if the URL is invalid', function(done) {
- return this.owner.request.post(
- {
- url: `/project/${this.project_id}/linked_file`,
- json: {
- provider: 'url',
- data: {
- url: '!^$%'
- },
- parent_folder_id: this.root_folder_id,
- name: 'url-test-file-4'
- }
- },
- (error, response, body) => {
- if (error != null) {
- throw error
- }
- expect(response.statusCode).to.equal(422) // unprocessable
- expect(body).to.equal(
- 'Your URL is not valid. Please check it and try again.'
- )
- return done()
- }
- )
- })
- it('should return an error if the URL uses a non-http protocol', function(done) {
- return this.owner.request.post(
- {
- url: `/project/${this.project_id}/linked_file`,
- json: {
- provider: 'url',
- data: {
- url: 'ftp://localhost'
- },
- parent_folder_id: this.root_folder_id,
- name: 'url-test-file-5'
- }
- },
- (error, response, body) => {
- if (error != null) {
- throw error
- }
- expect(response.statusCode).to.equal(422) // unprocessable
- expect(body).to.equal(
- 'Your URL is not valid. Please check it and try again.'
- )
- return done()
- }
- )
- })
- return it('should accept a URL withuot a leading http://, and add it', function(done) {
- return this.owner.request.post(
- {
- url: `/project/${this.project_id}/linked_file`,
- json: {
- provider: 'url',
- data: {
- url: 'example.com/foo'
- },
- parent_folder_id: this.root_folder_id,
- name: 'url-test-file-6'
- }
- },
- (error, response, body) => {
- if (error != null) {
- throw error
- }
- expect(response.statusCode).to.equal(200)
- return this.owner.getProject(this.project_id, (error, project) => {
- if (error != null) {
- throw error
- }
- const file = _.find(
- project.rootFolder[0].fileRefs,
- file => file.name === 'url-test-file-6'
- )
- expect(file.linkedFileData).to.deep.equal({
- provider: 'url',
- url: 'http://example.com/foo'
- })
- return this.owner.request.get(
- `/project/${this.project_id}/file/${file._id}`,
- function(error, response, body) {
- if (error != null) {
- throw error
- }
- expect(response.statusCode).to.equal(200)
- expect(body).to.equal('foo foo foo')
- return done()
- }
- )
- })
- }
- )
- })
- })
- // TODO: Add test for asking for host that return ENOTFOUND
- // (This will probably end up handled by the proxy)
- describe('creating a linked output file', function() {
- before(function(done) {
- return async.series(
- [
- cb => {
- return this.owner.createProject(
- 'output-test-one',
- { template: 'blank' },
- (error, project_id) => {
- this.project_one_id = project_id
- return cb(error)
- }
- )
- },
- cb => {
- return this.owner.getProject(
- this.project_one_id,
- (error, project) => {
- this.project_one = project
- this.project_one_root_folder_id = project.rootFolder[0]._id.toString()
- return cb(error)
- }
- )
- },
- cb => {
- return this.owner.createProject(
- 'output-test-two',
- { template: 'blank' },
- (error, project_id) => {
- this.project_two_id = project_id
- return cb(error)
- }
- )
- },
- cb => {
- return this.owner.getProject(
- this.project_two_id,
- (error, project) => {
- this.project_two = project
- this.project_two_root_folder_id = project.rootFolder[0]._id.toString()
- return cb(error)
- }
- )
- }
- ],
- done
- )
- })
- it('should import the project.pdf file from the source project', function(done) {
- return this.owner.request.post(
- {
- url: `/project/${this.project_one_id}/linked_file`,
- json: {
- name: 'test.pdf',
- parent_folder_id: this.project_one_root_folder_id,
- provider: 'project_output_file',
- data: {
- source_project_id: this.project_two_id,
- source_output_file_path: 'project.pdf',
- build_id: '1234-abcd'
- }
- }
- },
- (error, response, body) => {
- const { new_file_id } = body
- this.existing_file_id = new_file_id
- expect(new_file_id).to.exist
- return this.owner.getProject(
- this.project_one_id,
- (error, project) => {
- if (error != null) {
- return done(error)
- }
- const firstFile = project.rootFolder[0].fileRefs[0]
- expect(firstFile._id.toString()).to.equal(new_file_id.toString())
- expect(firstFile.linkedFileData).to.deep.equal({
- provider: 'project_output_file',
- source_project_id: this.project_two_id,
- source_output_file_path: 'project.pdf',
- build_id: '1234-abcd'
- })
- expect(firstFile.name).to.equal('test.pdf')
- return done()
- }
- )
- }
- )
- })
- return it('should refresh the file', function(done) {
- return this.owner.request.post(
- {
- url: `/project/${this.project_one_id}/linked_file/${
- this.existing_file_id
- }/refresh`,
- json: true
- },
- (error, response, body) => {
- const { new_file_id } = body
- expect(new_file_id).to.exist
- expect(new_file_id).to.not.equal(this.existing_file_id)
- this.refreshed_file_id = new_file_id
- return this.owner.getProject(
- this.project_one_id,
- (error, project) => {
- if (error != null) {
- return done(error)
- }
- const firstFile = project.rootFolder[0].fileRefs[0]
- expect(firstFile._id.toString()).to.equal(new_file_id.toString())
- expect(firstFile.name).to.equal('test.pdf')
- return done()
- }
- )
- }
- )
- })
- })
- return describe('with a linked project_output_file from a v1 project that has not been imported', function() {
- before(function(done) {
- return async.series(
- [
- cb => {
- return this.owner.createProject(
- 'output-v1-test-one',
- { template: 'blank' },
- (error, project_id) => {
- this.project_one_id = project_id
- return cb(error)
- }
- )
- },
- cb => {
- return this.owner.getProject(
- this.project_one_id,
- (error, project) => {
- this.project_one = project
- this.project_one_root_folder_id = project.rootFolder[0]._id.toString()
- this.project_one.rootFolder[0].fileRefs.push({
- linkedFileData: {
- provider: 'project_output_file',
- v1_source_doc_id: 9999999, // We won't find this id in the database
- source_output_file_path: 'project.pdf'
- },
- _id: 'abcdef',
- rev: 0,
- created: new Date(),
- name: 'whatever.pdf'
- })
- return this.owner.saveProject(this.project_one, cb)
- }
- )
- }
- ],
- done
- )
- })
- return it('should refuse to refresh', function(done) {
- return this.owner.request.post(
- {
- url: `/project/${this.project_one_id}/linked_file/abcdef/refresh`,
- json: true
- },
- (error, response, body) => {
- expect(response.statusCode).to.equal(409)
- expect(body).to.equal(
- 'Sorry, the source project is not yet imported to Overleaf v2. Please import it to Overleaf v2 to refresh this file'
- )
- return done()
- }
- )
- })
- })
- })
|