ProjectDuplicateNameTests.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. const { expect } = require('chai')
  2. const sinon = require('sinon')
  3. const Path = require('path')
  4. const fs = require('fs')
  5. const _ = require('lodash')
  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. name: 'frog.jpg',
  176. qqfile: {
  177. value: fs.createReadStream(
  178. Path.join(__dirname, '/../files/1pixel.png')
  179. ),
  180. options: {
  181. filename: 'frog.jpg',
  182. contentType: 'image/jpeg',
  183. },
  184. },
  185. },
  186. },
  187. (err, res, body) => {
  188. expect(err).to.not.exist
  189. this.body = body
  190. // update the image id because we have replaced the file
  191. this.imageFile._id = this.body.entity_id
  192. done()
  193. }
  194. )
  195. })
  196. it('should succeed (overwriting the file)', function () {
  197. expect(this.body.success).to.equal(true)
  198. })
  199. })
  200. })
  201. // at this point the @imageFile._id has changed
  202. describe('for an existing folder', function () {
  203. describe('trying to add a doc with the same name', function () {
  204. beforeEach(function (done) {
  205. this.owner.request.post(
  206. {
  207. uri: `/project/${this.example_project_id}/doc`,
  208. json: {
  209. name: 'testfolder',
  210. parent_folder_id: this.rootFolderId,
  211. },
  212. },
  213. (err, res, body) => {
  214. expect(err).to.not.exist
  215. this.res = res
  216. done()
  217. }
  218. )
  219. })
  220. it('should respond with 400 error status', function () {
  221. expect(this.res.statusCode).to.equal(400)
  222. })
  223. })
  224. describe('trying to add a folder with the same name', function () {
  225. beforeEach(function (done) {
  226. this.owner.request.post(
  227. {
  228. uri: `/project/${this.example_project_id}/folder`,
  229. json: {
  230. name: 'testfolder',
  231. parent_folder_id: this.rootFolderId,
  232. },
  233. },
  234. (err, res, body) => {
  235. expect(err).to.not.exist
  236. this.res = res
  237. done()
  238. }
  239. )
  240. })
  241. it('should respond with 400 error status', function () {
  242. expect(this.res.statusCode).to.equal(400)
  243. })
  244. })
  245. describe('trying to upload a file with the same name', function () {
  246. beforeEach(function (done) {
  247. this.owner.request.post(
  248. {
  249. uri: `/project/${this.example_project_id}/upload`,
  250. json: true,
  251. qs: {
  252. folder_id: this.rootFolderId,
  253. qqfilename: 'frog.jpg',
  254. },
  255. formData: {
  256. qqfile: {
  257. value: fs.createReadStream(
  258. Path.join(__dirname, '/../files/1pixel.png')
  259. ),
  260. options: {
  261. filename: 'testfolder',
  262. contentType: 'image/jpeg',
  263. },
  264. },
  265. },
  266. },
  267. (err, res, body) => {
  268. expect(err).to.not.exist
  269. this.body = body
  270. done()
  271. }
  272. )
  273. })
  274. it('should respond with failure status', function () {
  275. expect(this.body.success).to.equal(false)
  276. })
  277. })
  278. })
  279. describe('rename for an existing doc', function () {
  280. describe('trying to rename a doc to the same name', function () {
  281. beforeEach(function (done) {
  282. this.owner.request.post(
  283. {
  284. uri: `/project/${this.example_project_id}/doc/${this.refBibDoc._id}/rename`,
  285. json: {
  286. name: 'main.tex',
  287. },
  288. },
  289. (err, res, body) => {
  290. expect(err).to.not.exist
  291. this.res = res
  292. done()
  293. }
  294. )
  295. })
  296. it('should respond with 400 error status', function () {
  297. expect(this.res.statusCode).to.equal(400)
  298. })
  299. })
  300. describe('trying to rename a folder to the same name', function () {
  301. beforeEach(function (done) {
  302. this.owner.request.post(
  303. {
  304. uri: `/project/${this.example_project_id}/folder/${this.testFolderId}/rename`,
  305. json: {
  306. name: 'main.tex',
  307. },
  308. },
  309. (err, res, body) => {
  310. expect(err).to.not.exist
  311. this.res = res
  312. done()
  313. }
  314. )
  315. })
  316. it('should respond with 400 error status', function () {
  317. expect(this.res.statusCode).to.equal(400)
  318. })
  319. })
  320. describe('trying to rename a file to the same name', function () {
  321. beforeEach(function (done) {
  322. this.owner.request.post(
  323. {
  324. uri: `/project/${this.example_project_id}/file/${this.imageFile._id}/rename`,
  325. json: {
  326. name: 'main.tex',
  327. },
  328. },
  329. (err, res, body) => {
  330. expect(err).to.not.exist
  331. this.res = res
  332. done()
  333. }
  334. )
  335. })
  336. it('should respond with failure status', function () {
  337. expect(this.res.statusCode).to.equal(400)
  338. })
  339. })
  340. })
  341. describe('rename for an existing file', function () {
  342. describe('trying to rename a doc to the same name', function () {
  343. beforeEach(function (done) {
  344. this.owner.request.post(
  345. {
  346. uri: `/project/${this.example_project_id}/doc/${this.refBibDoc._id}/rename`,
  347. json: {
  348. name: 'frog.jpg',
  349. },
  350. },
  351. (err, res, body) => {
  352. expect(err).to.not.exist
  353. this.res = res
  354. done()
  355. }
  356. )
  357. })
  358. it('should respond with 400 error status', function () {
  359. expect(this.res.statusCode).to.equal(400)
  360. })
  361. })
  362. describe('trying to rename a folder to the same name', function () {
  363. beforeEach(function (done) {
  364. this.owner.request.post(
  365. {
  366. uri: `/project/${this.example_project_id}/folder/${this.testFolderId}/rename`,
  367. json: {
  368. name: 'frog.jpg',
  369. },
  370. },
  371. (err, res, body) => {
  372. expect(err).to.not.exist
  373. this.res = res
  374. done()
  375. }
  376. )
  377. })
  378. it('should respond with 400 error status', function () {
  379. expect(this.res.statusCode).to.equal(400)
  380. })
  381. })
  382. describe('trying to rename a file to the same name', function () {
  383. beforeEach(function (done) {
  384. this.owner.request.post(
  385. {
  386. uri: `/project/${this.example_project_id}/file/${this.imageFile._id}/rename`,
  387. json: {
  388. name: 'frog.jpg',
  389. },
  390. },
  391. (err, res, body) => {
  392. expect(err).to.not.exist
  393. this.res = res
  394. done()
  395. }
  396. )
  397. })
  398. it('should respond with failure status', function () {
  399. expect(this.res.statusCode).to.equal(400)
  400. })
  401. })
  402. })
  403. describe('rename for an existing folder', function () {
  404. describe('trying to rename a doc to the same name', function () {
  405. beforeEach(function (done) {
  406. this.owner.request.post(
  407. {
  408. uri: `/project/${this.example_project_id}/doc/${this.refBibDoc._id}/rename`,
  409. json: {
  410. name: 'testfolder',
  411. },
  412. },
  413. (err, res, body) => {
  414. expect(err).to.not.exist
  415. this.res = res
  416. done()
  417. }
  418. )
  419. })
  420. it('should respond with 400 error status', function () {
  421. expect(this.res.statusCode).to.equal(400)
  422. })
  423. })
  424. describe('trying to rename a folder to the same name', function () {
  425. beforeEach(function (done) {
  426. this.owner.request.post(
  427. {
  428. uri: `/project/${this.example_project_id}/folder/${this.testFolderId}/rename`,
  429. json: {
  430. name: 'testfolder',
  431. },
  432. },
  433. (err, res, body) => {
  434. expect(err).to.not.exist
  435. this.res = res
  436. done()
  437. }
  438. )
  439. })
  440. it('should respond with 400 error status', function () {
  441. expect(this.res.statusCode).to.equal(400)
  442. })
  443. })
  444. describe('trying to rename a file to the same name', function () {
  445. beforeEach(function (done) {
  446. this.owner.request.post(
  447. {
  448. uri: `/project/${this.example_project_id}/file/${this.imageFile._id}/rename`,
  449. json: {
  450. name: 'testfolder',
  451. },
  452. },
  453. (err, res, body) => {
  454. expect(err).to.not.exist
  455. this.res = res
  456. done()
  457. }
  458. )
  459. })
  460. it('should respond with failure status', function () {
  461. expect(this.res.statusCode).to.equal(400)
  462. })
  463. })
  464. })
  465. describe('for an existing folder with a file with the same name', function () {
  466. beforeEach(function (done) {
  467. this.owner.request.post(
  468. {
  469. uri: `/project/${this.example_project_id}/doc`,
  470. json: {
  471. name: 'main.tex',
  472. parent_folder_id: this.testFolderId,
  473. },
  474. },
  475. (err, res, body) => {
  476. if (err) {
  477. throw err
  478. }
  479. this.owner.request.post(
  480. {
  481. uri: `/project/${this.example_project_id}/doc`,
  482. json: {
  483. name: 'frog.jpg',
  484. parent_folder_id: this.testFolderId,
  485. },
  486. },
  487. (err, res, body) => {
  488. if (err) {
  489. throw err
  490. }
  491. this.owner.request.post(
  492. {
  493. uri: `/project/${this.example_project_id}/folder`,
  494. json: {
  495. name: 'otherFolder',
  496. parent_folder_id: this.testFolderId,
  497. },
  498. },
  499. (err, res, body) => {
  500. if (err) {
  501. throw err
  502. }
  503. this.subFolderId = body._id
  504. this.owner.request.post(
  505. {
  506. uri: `/project/${this.example_project_id}/folder`,
  507. json: {
  508. name: 'otherFolder',
  509. parent_folder_id: this.rootFolderId,
  510. },
  511. },
  512. (err, res, body) => {
  513. if (err) {
  514. throw err
  515. }
  516. this.otherFolderId = body._id
  517. done()
  518. }
  519. )
  520. }
  521. )
  522. }
  523. )
  524. }
  525. )
  526. })
  527. describe('trying to move a doc into the folder', function () {
  528. beforeEach(function (done) {
  529. this.owner.request.post(
  530. {
  531. uri: `/project/${this.example_project_id}/doc/${this.mainTexDoc._id}/move`,
  532. json: {
  533. folder_id: this.testFolderId,
  534. },
  535. },
  536. (err, res, body) => {
  537. expect(err).to.not.exist
  538. this.res = res
  539. done()
  540. }
  541. )
  542. })
  543. it('should respond with 400 error status', function () {
  544. expect(this.res.statusCode).to.equal(400)
  545. })
  546. })
  547. describe('trying to move a file into the folder', function () {
  548. beforeEach(function (done) {
  549. this.owner.request.post(
  550. {
  551. uri: `/project/${this.example_project_id}/file/${this.imageFile._id}/move`,
  552. json: {
  553. folder_id: this.testFolderId,
  554. },
  555. },
  556. (err, res, body) => {
  557. expect(err).to.not.exist
  558. this.res = res
  559. done()
  560. }
  561. )
  562. })
  563. it('should respond with 400 error status', function () {
  564. expect(this.res.statusCode).to.equal(400)
  565. })
  566. })
  567. describe('trying to move a folder into the folder', function () {
  568. beforeEach(function (done) {
  569. this.owner.request.post(
  570. {
  571. uri: `/project/${this.example_project_id}/folder/${this.otherFolderId}/move`,
  572. json: {
  573. folder_id: this.testFolderId,
  574. },
  575. },
  576. (err, res, body) => {
  577. expect(err).to.not.exist
  578. this.res = res
  579. done()
  580. }
  581. )
  582. })
  583. it('should respond with 400 error status', function () {
  584. expect(this.res.statusCode).to.equal(400)
  585. })
  586. })
  587. describe('trying to move a folder into a subfolder of itself', function () {
  588. beforeEach(function (done) {
  589. this.owner.request.post(
  590. {
  591. uri: `/project/${this.example_project_id}/folder/${this.testFolderId}/move`,
  592. json: {
  593. folder_id: this.subFolderId,
  594. },
  595. },
  596. (err, res, body) => {
  597. expect(err).to.not.exist
  598. this.res = res
  599. done()
  600. }
  601. )
  602. })
  603. it('should respond with 400 error status', function () {
  604. expect(this.res.statusCode).to.equal(400)
  605. })
  606. })
  607. })
  608. })
  609. describe('regex characters in title', function () {
  610. let response, userHelper
  611. beforeEach(async function () {
  612. userHelper = new UserHelper()
  613. userHelper = await UserHelper.createUser()
  614. userHelper = await UserHelper.loginUser(
  615. userHelper.getDefaultEmailPassword()
  616. )
  617. })
  618. it('should handle characters that would cause an invalid regular expression', async function () {
  619. const projectName = 'Example (test'
  620. response = await userHelper.fetch('/project/new', {
  621. method: 'POST',
  622. body: new URLSearchParams([['projectName', projectName]]),
  623. })
  624. const body = await response.json()
  625. expect(response.status).to.equal(200) // can create project
  626. response = await userHelper.fetch(`/project/${body.project_id}`)
  627. expect(response.status).to.equal(200) // can open project
  628. })
  629. })
  630. })