ProjectDuplicateNameTests.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. /* eslint-disable
  2. camelcase,
  3. node/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/${this.refBibDoc._id}/rename`,
  312. json: {
  313. name: 'main.tex'
  314. }
  315. },
  316. (err, res, body) => {
  317. this.res = res
  318. return done()
  319. }
  320. )
  321. })
  322. it('should respond with 400 error status', function() {
  323. return expect(this.res.statusCode).to.equal(400)
  324. })
  325. })
  326. describe('trying to rename a folder to the same name', function() {
  327. beforeEach(function(done) {
  328. return this.owner.request.post(
  329. {
  330. uri: `/project/${this.example_project_id}/folder/${this.testFolderId}/rename`,
  331. json: {
  332. name: 'main.tex'
  333. }
  334. },
  335. (err, res, body) => {
  336. this.res = res
  337. return done()
  338. }
  339. )
  340. })
  341. it('should respond with 400 error status', function() {
  342. return expect(this.res.statusCode).to.equal(400)
  343. })
  344. })
  345. describe('trying to rename a file to the same name', function() {
  346. beforeEach(function(done) {
  347. return this.owner.request.post(
  348. {
  349. uri: `/project/${this.example_project_id}/file/${this.imageFile._id}/rename`,
  350. json: {
  351. name: 'main.tex'
  352. }
  353. },
  354. (err, res, body) => {
  355. this.res = res
  356. return done()
  357. }
  358. )
  359. })
  360. it('should respond with failure status', function() {
  361. return expect(this.res.statusCode).to.equal(400)
  362. })
  363. })
  364. })
  365. describe('for an existing file', function() {
  366. describe('trying to rename a doc to the same name', function() {
  367. beforeEach(function(done) {
  368. return this.owner.request.post(
  369. {
  370. uri: `/project/${this.example_project_id}/doc/${this.refBibDoc._id}/rename`,
  371. json: {
  372. name: 'universe.jpg'
  373. }
  374. },
  375. (err, res, body) => {
  376. this.res = res
  377. return done()
  378. }
  379. )
  380. })
  381. it('should respond with 400 error status', function() {
  382. return expect(this.res.statusCode).to.equal(400)
  383. })
  384. })
  385. describe('trying to rename a folder to the same name', function() {
  386. beforeEach(function(done) {
  387. return this.owner.request.post(
  388. {
  389. uri: `/project/${this.example_project_id}/folder/${this.testFolderId}/rename`,
  390. json: {
  391. name: 'universe.jpg'
  392. }
  393. },
  394. (err, res, body) => {
  395. this.res = res
  396. return done()
  397. }
  398. )
  399. })
  400. it('should respond with 400 error status', function() {
  401. return expect(this.res.statusCode).to.equal(400)
  402. })
  403. })
  404. describe('trying to rename a file to the same name', function() {
  405. beforeEach(function(done) {
  406. return this.owner.request.post(
  407. {
  408. uri: `/project/${this.example_project_id}/file/${this.imageFile._id}/rename`,
  409. json: {
  410. name: 'universe.jpg'
  411. }
  412. },
  413. (err, res, body) => {
  414. this.res = res
  415. return done()
  416. }
  417. )
  418. })
  419. it('should respond with failure status', function() {
  420. return expect(this.res.statusCode).to.equal(400)
  421. })
  422. })
  423. })
  424. describe('for an existing folder', function() {
  425. describe('trying to rename a doc to the same name', function() {
  426. beforeEach(function(done) {
  427. return this.owner.request.post(
  428. {
  429. uri: `/project/${this.example_project_id}/doc/${this.refBibDoc._id}/rename`,
  430. json: {
  431. name: 'testfolder'
  432. }
  433. },
  434. (err, res, body) => {
  435. this.res = res
  436. return done()
  437. }
  438. )
  439. })
  440. it('should respond with 400 error status', function() {
  441. return expect(this.res.statusCode).to.equal(400)
  442. })
  443. })
  444. describe('trying to rename a folder to the same name', function() {
  445. beforeEach(function(done) {
  446. return this.owner.request.post(
  447. {
  448. uri: `/project/${this.example_project_id}/folder/${this.testFolderId}/rename`,
  449. json: {
  450. name: 'testfolder'
  451. }
  452. },
  453. (err, res, body) => {
  454. this.res = res
  455. return done()
  456. }
  457. )
  458. })
  459. it('should respond with 400 error status', function() {
  460. return expect(this.res.statusCode).to.equal(400)
  461. })
  462. })
  463. describe('trying to rename a file to the same name', function() {
  464. beforeEach(function(done) {
  465. return this.owner.request.post(
  466. {
  467. uri: `/project/${this.example_project_id}/file/${this.imageFile._id}/rename`,
  468. json: {
  469. name: 'testfolder'
  470. }
  471. },
  472. (err, res, body) => {
  473. this.res = res
  474. return done()
  475. }
  476. )
  477. })
  478. it('should respond with failure status', function() {
  479. return expect(this.res.statusCode).to.equal(400)
  480. })
  481. })
  482. })
  483. describe('for an existing folder with a file with the same name', function() {
  484. beforeEach(function(done) {
  485. return this.owner.request.post(
  486. {
  487. uri: `/project/${this.example_project_id}/doc`,
  488. json: {
  489. name: 'main.tex',
  490. parent_folder_id: this.testFolderId
  491. }
  492. },
  493. (err, res, body) => {
  494. return this.owner.request.post(
  495. {
  496. uri: `/project/${this.example_project_id}/doc`,
  497. json: {
  498. name: 'universe.jpg',
  499. parent_folder_id: this.testFolderId
  500. }
  501. },
  502. (err, res, body) => {
  503. return this.owner.request.post(
  504. {
  505. uri: `/project/${this.example_project_id}/folder`,
  506. json: {
  507. name: 'otherFolder',
  508. parent_folder_id: this.testFolderId
  509. }
  510. },
  511. (err, res, body) => {
  512. this.subFolderId = body._id
  513. return this.owner.request.post(
  514. {
  515. uri: `/project/${this.example_project_id}/folder`,
  516. json: {
  517. name: 'otherFolder',
  518. parent_folder_id: this.rootFolderId
  519. }
  520. },
  521. (err, res, body) => {
  522. this.otherFolderId = body._id
  523. return done()
  524. }
  525. )
  526. }
  527. )
  528. }
  529. )
  530. }
  531. )
  532. })
  533. describe('trying to move a doc into the folder', function() {
  534. beforeEach(function(done) {
  535. return this.owner.request.post(
  536. {
  537. uri: `/project/${this.example_project_id}/doc/${this.mainTexDoc._id}/move`,
  538. json: {
  539. folder_id: this.testFolderId
  540. }
  541. },
  542. (err, res, body) => {
  543. this.res = res
  544. return done()
  545. }
  546. )
  547. })
  548. it('should respond with 400 error status', function() {
  549. return expect(this.res.statusCode).to.equal(400)
  550. })
  551. })
  552. describe('trying to move a file into the folder', function() {
  553. beforeEach(function(done) {
  554. return this.owner.request.post(
  555. {
  556. uri: `/project/${this.example_project_id}/file/${this.imageFile._id}/move`,
  557. json: {
  558. folder_id: this.testFolderId
  559. }
  560. },
  561. (err, res, body) => {
  562. this.res = res
  563. return done()
  564. }
  565. )
  566. })
  567. it('should respond with 400 error status', function() {
  568. return expect(this.res.statusCode).to.equal(400)
  569. })
  570. })
  571. describe('trying to move a folder into the folder', function() {
  572. beforeEach(function(done) {
  573. return this.owner.request.post(
  574. {
  575. uri: `/project/${this.example_project_id}/folder/${this.otherFolderId}/move`,
  576. json: {
  577. folder_id: this.testFolderId
  578. }
  579. },
  580. (err, res, body) => {
  581. this.res = res
  582. return done()
  583. }
  584. )
  585. })
  586. it('should respond with 400 error status', function() {
  587. return expect(this.res.statusCode).to.equal(400)
  588. })
  589. })
  590. describe('trying to move a folder into a subfolder of itself', function() {
  591. beforeEach(function(done) {
  592. return this.owner.request.post(
  593. {
  594. uri: `/project/${this.example_project_id}/folder/${this.testFolderId}/move`,
  595. json: {
  596. folder_id: this.subFolderId
  597. }
  598. },
  599. (err, res, body) => {
  600. this.res = res
  601. return done()
  602. }
  603. )
  604. })
  605. it('should respond with 400 error status', function() {
  606. return expect(this.res.statusCode).to.equal(400)
  607. })
  608. })
  609. })
  610. })
  611. })