ProjectDuplicateNameTests.js 20 KB

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