ProjectDuplicateNameTests.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. max-len,
  5. mocha/no-identical-title,
  6. no-path-concat,
  7. no-return-assign,
  8. no-unused-vars,
  9. */
  10. // TODO: This file was created by bulk-decaffeinate.
  11. // Fix any style issues and re-enable lint.
  12. /*
  13. * decaffeinate suggestions:
  14. * DS102: Remove unnecessary code created because of implicit returns
  15. * DS207: Consider shorter variations of null checks
  16. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  17. */
  18. const async = require('async')
  19. const { expect } = require('chai')
  20. const sinon = require('sinon')
  21. const mkdirp = require('mkdirp')
  22. const { ObjectId } = require('mongojs')
  23. const Path = require('path')
  24. const fs = require('fs')
  25. const Settings = require('settings-sharelatex')
  26. const _ = require('underscore')
  27. const ProjectGetter = require('../../../app/src/Features/Project/ProjectGetter.js')
  28. const MockDocStoreApi = require('./helpers/MockDocstoreApi')
  29. const MockFileStoreApi = require('./helpers/MockFileStoreApi')
  30. const request = require('./helpers/request')
  31. const User = require('./helpers/User')
  32. describe('ProjectDuplicateNames', function() {
  33. beforeEach(function(done) {
  34. this.owner = new User()
  35. this.owner.login(done)
  36. this.project = {}
  37. return (this.callback = sinon.stub())
  38. })
  39. describe('creating a project from the example template', function() {
  40. beforeEach(function(done) {
  41. return this.owner.createProject(
  42. 'example-project',
  43. { template: 'example' },
  44. (error, project_id) => {
  45. if (error != null) {
  46. throw error
  47. }
  48. this.example_project_id = project_id
  49. return this.owner.getProject(project_id, (error, project) => {
  50. this.project = project
  51. this.mainTexDoc = _.find(
  52. project.rootFolder[0].docs,
  53. doc => doc.name === 'main.tex'
  54. )
  55. this.refBibDoc = _.find(
  56. project.rootFolder[0].docs,
  57. doc => doc.name === 'references.bib'
  58. )
  59. this.imageFile = _.find(
  60. project.rootFolder[0].fileRefs,
  61. file => file.name === 'universe.jpg'
  62. )
  63. this.rootFolderId = project.rootFolder[0]._id.toString()
  64. // create a folder called 'testfolder'
  65. return this.owner.request.post(
  66. {
  67. uri: `/project/${this.example_project_id}/folder`,
  68. json: {
  69. name: 'testfolder',
  70. parent_folder_id: this.rootFolderId
  71. }
  72. },
  73. (err, res, body) => {
  74. this.testFolderId = body._id
  75. return done()
  76. }
  77. )
  78. })
  79. }
  80. )
  81. })
  82. it('should create a project', function() {
  83. expect(this.project.rootFolder[0].docs.length).to.equal(2)
  84. return expect(this.project.rootFolder[0].fileRefs.length).to.equal(1)
  85. })
  86. it('should create two docs in the docstore', function() {
  87. const docs = MockDocStoreApi.docs[this.example_project_id]
  88. return expect(Object.keys(docs).length).to.equal(2)
  89. })
  90. it('should create one file in the filestore', function() {
  91. const files = MockFileStoreApi.files[this.example_project_id]
  92. return expect(Object.keys(files).length).to.equal(1)
  93. })
  94. describe('for an existing doc', function() {
  95. describe('trying to add a doc with the same name', function() {
  96. beforeEach(function(done) {
  97. return this.owner.request.post(
  98. {
  99. uri: `/project/${this.example_project_id}/doc`,
  100. json: {
  101. name: 'main.tex',
  102. parent_folder_id: this.rootFolderId
  103. }
  104. },
  105. (err, res, body) => {
  106. this.res = res
  107. return done()
  108. }
  109. )
  110. })
  111. it('should respond with 400 error status', function() {
  112. return expect(this.res.statusCode).to.equal(400)
  113. })
  114. })
  115. describe('trying to add a folder with the same name', function() {
  116. beforeEach(function(done) {
  117. return this.owner.request.post(
  118. {
  119. uri: `/project/${this.example_project_id}/folder`,
  120. json: {
  121. name: 'main.tex',
  122. parent_folder_id: this.rootFolderId
  123. }
  124. },
  125. (err, res, body) => {
  126. this.res = res
  127. return done()
  128. }
  129. )
  130. })
  131. it('should respond with 400 error status', function() {
  132. return expect(this.res.statusCode).to.equal(400)
  133. })
  134. })
  135. describe('trying to add a folder with the same name', function() {
  136. beforeEach(function(done) {
  137. return this.owner.request.post(
  138. {
  139. uri: `/project/${this.example_project_id}/folder`,
  140. json: {
  141. name: 'main.tex',
  142. parent_folder_id: this.rootFolderId
  143. }
  144. },
  145. (err, res, body) => {
  146. this.res = res
  147. return done()
  148. }
  149. )
  150. })
  151. it('should respond with 400 error status', function() {
  152. return expect(this.res.statusCode).to.equal(400)
  153. })
  154. })
  155. })
  156. describe('for an existing file', function() {
  157. describe('trying to add a doc with the same name', function() {
  158. beforeEach(function(done) {
  159. return this.owner.request.post(
  160. {
  161. uri: `/project/${this.example_project_id}/doc`,
  162. json: {
  163. name: 'universe.jpg',
  164. parent_folder_id: this.rootFolderId
  165. }
  166. },
  167. (err, res, body) => {
  168. this.res = res
  169. return done()
  170. }
  171. )
  172. })
  173. it('should respond with 400 error status', function() {
  174. return expect(this.res.statusCode).to.equal(400)
  175. })
  176. })
  177. describe('trying to add a folder with the same name', function() {
  178. beforeEach(function(done) {
  179. return this.owner.request.post(
  180. {
  181. uri: `/project/${this.example_project_id}/folder`,
  182. json: {
  183. name: 'universe.jpg',
  184. parent_folder_id: this.rootFolderId
  185. }
  186. },
  187. (err, res, body) => {
  188. this.res = res
  189. return done()
  190. }
  191. )
  192. })
  193. it('should respond with 400 error status', function() {
  194. return expect(this.res.statusCode).to.equal(400)
  195. })
  196. })
  197. describe('trying to upload a file with the same name', function() {
  198. beforeEach(function(done) {
  199. return this.owner.request.post(
  200. {
  201. uri: `/project/${this.example_project_id}/upload`,
  202. json: true,
  203. qs: {
  204. folder_id: this.rootFolderId,
  205. qqfilename: 'universe.jpg'
  206. },
  207. formData: {
  208. qqfile: {
  209. value: fs.createReadStream(
  210. Path.resolve(__dirname + '/../files/1pixel.png')
  211. ),
  212. options: {
  213. filename: 'universe.jpg',
  214. contentType: 'image/jpeg'
  215. }
  216. }
  217. }
  218. },
  219. (err, res, body) => {
  220. this.body = body
  221. // update the image id because we have replaced the file
  222. this.imageFile._id = this.body.entity_id
  223. return done()
  224. }
  225. )
  226. })
  227. it('should succeed (overwriting the file)', function() {
  228. return expect(this.body.success).to.equal(true)
  229. })
  230. })
  231. })
  232. // at this point the @imageFile._id has changed
  233. describe('for an existing folder', function() {
  234. describe('trying to add a doc with the same name', function() {
  235. beforeEach(function(done) {
  236. return this.owner.request.post(
  237. {
  238. uri: `/project/${this.example_project_id}/doc`,
  239. json: {
  240. name: 'testfolder',
  241. parent_folder_id: this.rootFolderId
  242. }
  243. },
  244. (err, res, body) => {
  245. this.res = res
  246. return done()
  247. }
  248. )
  249. })
  250. it('should respond with 400 error status', function() {
  251. return expect(this.res.statusCode).to.equal(400)
  252. })
  253. })
  254. describe('trying to add a folder with the same name', function() {
  255. beforeEach(function(done) {
  256. return this.owner.request.post(
  257. {
  258. uri: `/project/${this.example_project_id}/folder`,
  259. json: {
  260. name: 'testfolder',
  261. parent_folder_id: this.rootFolderId
  262. }
  263. },
  264. (err, res, body) => {
  265. this.res = res
  266. return done()
  267. }
  268. )
  269. })
  270. it('should respond with 400 error status', function() {
  271. return expect(this.res.statusCode).to.equal(400)
  272. })
  273. })
  274. describe('trying to upload a file with the same name', function() {
  275. beforeEach(function(done) {
  276. return this.owner.request.post(
  277. {
  278. uri: `/project/${this.example_project_id}/upload`,
  279. json: true,
  280. qs: {
  281. folder_id: this.rootFolderId,
  282. qqfilename: 'universe.jpg'
  283. },
  284. formData: {
  285. qqfile: {
  286. value: fs.createReadStream(
  287. Path.resolve(__dirname + '/../files/1pixel.png')
  288. ),
  289. options: {
  290. filename: 'testfolder',
  291. contentType: 'image/jpeg'
  292. }
  293. }
  294. }
  295. },
  296. (err, res, body) => {
  297. this.body = body
  298. return done()
  299. }
  300. )
  301. })
  302. it('should respond with failure status', function() {
  303. return expect(this.body.success).to.equal(false)
  304. })
  305. })
  306. })
  307. describe('for an existing doc', function() {
  308. describe('trying to rename a doc to the same name', function() {
  309. beforeEach(function(done) {
  310. return this.owner.request.post(
  311. {
  312. uri: `/project/${this.example_project_id}/doc/${
  313. this.refBibDoc._id
  314. }/rename`,
  315. json: {
  316. name: 'main.tex'
  317. }
  318. },
  319. (err, res, body) => {
  320. this.res = res
  321. return done()
  322. }
  323. )
  324. })
  325. it('should respond with 400 error status', function() {
  326. return expect(this.res.statusCode).to.equal(400)
  327. })
  328. })
  329. describe('trying to rename a folder to the same name', function() {
  330. beforeEach(function(done) {
  331. return this.owner.request.post(
  332. {
  333. uri: `/project/${this.example_project_id}/folder/${
  334. this.testFolderId
  335. }/rename`,
  336. json: {
  337. name: 'main.tex'
  338. }
  339. },
  340. (err, res, body) => {
  341. this.res = res
  342. return done()
  343. }
  344. )
  345. })
  346. it('should respond with 400 error status', function() {
  347. return expect(this.res.statusCode).to.equal(400)
  348. })
  349. })
  350. describe('trying to rename a file to the same name', function() {
  351. beforeEach(function(done) {
  352. return this.owner.request.post(
  353. {
  354. uri: `/project/${this.example_project_id}/file/${
  355. this.imageFile._id
  356. }/rename`,
  357. json: {
  358. name: 'main.tex'
  359. }
  360. },
  361. (err, res, body) => {
  362. this.res = res
  363. return done()
  364. }
  365. )
  366. })
  367. it('should respond with failure status', function() {
  368. return expect(this.res.statusCode).to.equal(400)
  369. })
  370. })
  371. })
  372. describe('for an existing file', function() {
  373. describe('trying to rename a doc to the same name', function() {
  374. beforeEach(function(done) {
  375. return this.owner.request.post(
  376. {
  377. uri: `/project/${this.example_project_id}/doc/${
  378. this.refBibDoc._id
  379. }/rename`,
  380. json: {
  381. name: 'universe.jpg'
  382. }
  383. },
  384. (err, res, body) => {
  385. this.res = res
  386. return done()
  387. }
  388. )
  389. })
  390. it('should respond with 400 error status', function() {
  391. return expect(this.res.statusCode).to.equal(400)
  392. })
  393. })
  394. describe('trying to rename a folder to the same name', function() {
  395. beforeEach(function(done) {
  396. return this.owner.request.post(
  397. {
  398. uri: `/project/${this.example_project_id}/folder/${
  399. this.testFolderId
  400. }/rename`,
  401. json: {
  402. name: 'universe.jpg'
  403. }
  404. },
  405. (err, res, body) => {
  406. this.res = res
  407. return done()
  408. }
  409. )
  410. })
  411. it('should respond with 400 error status', function() {
  412. return expect(this.res.statusCode).to.equal(400)
  413. })
  414. })
  415. describe('trying to rename a file to the same name', function() {
  416. beforeEach(function(done) {
  417. return this.owner.request.post(
  418. {
  419. uri: `/project/${this.example_project_id}/file/${
  420. this.imageFile._id
  421. }/rename`,
  422. json: {
  423. name: 'universe.jpg'
  424. }
  425. },
  426. (err, res, body) => {
  427. this.res = res
  428. return done()
  429. }
  430. )
  431. })
  432. it('should respond with failure status', function() {
  433. return expect(this.res.statusCode).to.equal(400)
  434. })
  435. })
  436. })
  437. describe('for an existing folder', function() {
  438. describe('trying to rename a doc to the same name', function() {
  439. beforeEach(function(done) {
  440. return this.owner.request.post(
  441. {
  442. uri: `/project/${this.example_project_id}/doc/${
  443. this.refBibDoc._id
  444. }/rename`,
  445. json: {
  446. name: 'testfolder'
  447. }
  448. },
  449. (err, res, body) => {
  450. this.res = res
  451. return done()
  452. }
  453. )
  454. })
  455. it('should respond with 400 error status', function() {
  456. return expect(this.res.statusCode).to.equal(400)
  457. })
  458. })
  459. describe('trying to rename a folder to the same name', function() {
  460. beforeEach(function(done) {
  461. return this.owner.request.post(
  462. {
  463. uri: `/project/${this.example_project_id}/folder/${
  464. this.testFolderId
  465. }/rename`,
  466. json: {
  467. name: 'testfolder'
  468. }
  469. },
  470. (err, res, body) => {
  471. this.res = res
  472. return done()
  473. }
  474. )
  475. })
  476. it('should respond with 400 error status', function() {
  477. return expect(this.res.statusCode).to.equal(400)
  478. })
  479. })
  480. describe('trying to rename a file to the same name', function() {
  481. beforeEach(function(done) {
  482. return this.owner.request.post(
  483. {
  484. uri: `/project/${this.example_project_id}/file/${
  485. this.imageFile._id
  486. }/rename`,
  487. json: {
  488. name: 'testfolder'
  489. }
  490. },
  491. (err, res, body) => {
  492. this.res = res
  493. return done()
  494. }
  495. )
  496. })
  497. it('should respond with failure status', function() {
  498. return expect(this.res.statusCode).to.equal(400)
  499. })
  500. })
  501. })
  502. describe('for an existing folder with a file with the same name', function() {
  503. beforeEach(function(done) {
  504. return this.owner.request.post(
  505. {
  506. uri: `/project/${this.example_project_id}/doc`,
  507. json: {
  508. name: 'main.tex',
  509. parent_folder_id: this.testFolderId
  510. }
  511. },
  512. (err, res, body) => {
  513. return this.owner.request.post(
  514. {
  515. uri: `/project/${this.example_project_id}/doc`,
  516. json: {
  517. name: 'universe.jpg',
  518. parent_folder_id: this.testFolderId
  519. }
  520. },
  521. (err, res, body) => {
  522. return this.owner.request.post(
  523. {
  524. uri: `/project/${this.example_project_id}/folder`,
  525. json: {
  526. name: 'otherFolder',
  527. parent_folder_id: this.testFolderId
  528. }
  529. },
  530. (err, res, body) => {
  531. this.subFolderId = body._id
  532. return this.owner.request.post(
  533. {
  534. uri: `/project/${this.example_project_id}/folder`,
  535. json: {
  536. name: 'otherFolder',
  537. parent_folder_id: this.rootFolderId
  538. }
  539. },
  540. (err, res, body) => {
  541. this.otherFolderId = body._id
  542. return done()
  543. }
  544. )
  545. }
  546. )
  547. }
  548. )
  549. }
  550. )
  551. })
  552. describe('trying to move a doc into the folder', function() {
  553. beforeEach(function(done) {
  554. return this.owner.request.post(
  555. {
  556. uri: `/project/${this.example_project_id}/doc/${
  557. this.mainTexDoc._id
  558. }/move`,
  559. json: {
  560. folder_id: this.testFolderId
  561. }
  562. },
  563. (err, res, body) => {
  564. this.res = res
  565. return done()
  566. }
  567. )
  568. })
  569. it('should respond with 400 error status', function() {
  570. return expect(this.res.statusCode).to.equal(400)
  571. })
  572. })
  573. describe('trying to move a file into the folder', function() {
  574. beforeEach(function(done) {
  575. return this.owner.request.post(
  576. {
  577. uri: `/project/${this.example_project_id}/file/${
  578. this.imageFile._id
  579. }/move`,
  580. json: {
  581. folder_id: this.testFolderId
  582. }
  583. },
  584. (err, res, body) => {
  585. this.res = res
  586. return done()
  587. }
  588. )
  589. })
  590. it('should respond with 400 error status', function() {
  591. return expect(this.res.statusCode).to.equal(400)
  592. })
  593. })
  594. describe('trying to move a folder into the folder', function() {
  595. beforeEach(function(done) {
  596. return this.owner.request.post(
  597. {
  598. uri: `/project/${this.example_project_id}/folder/${
  599. this.otherFolderId
  600. }/move`,
  601. json: {
  602. folder_id: this.testFolderId
  603. }
  604. },
  605. (err, res, body) => {
  606. this.res = res
  607. return done()
  608. }
  609. )
  610. })
  611. it('should respond with 400 error status', function() {
  612. return expect(this.res.statusCode).to.equal(400)
  613. })
  614. })
  615. describe('trying to move a folder into a subfolder of itself', function() {
  616. beforeEach(function(done) {
  617. return this.owner.request.post(
  618. {
  619. uri: `/project/${this.example_project_id}/folder/${
  620. this.testFolderId
  621. }/move`,
  622. json: {
  623. folder_id: this.subFolderId
  624. }
  625. },
  626. (err, res, body) => {
  627. this.res = res
  628. return done()
  629. }
  630. )
  631. })
  632. it('should respond with 400 error status', function() {
  633. return expect(this.res.statusCode).to.equal(400)
  634. })
  635. })
  636. })
  637. })
  638. })