ProjectDuplicateNameTests.js 20 KB

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