ProjectDuplicateNameTests.js 20 KB

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