ProjectDuplicateNameTests.coffee 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. async = require "async"
  2. expect = require("chai").expect
  3. sinon = require "sinon"
  4. mkdirp = require "mkdirp"
  5. ObjectId = require("mongojs").ObjectId
  6. Path = require "path"
  7. fs = require "fs"
  8. Settings = require "settings-sharelatex"
  9. _ = require "underscore"
  10. ProjectGetter = require "../../../app/js/Features/Project/ProjectGetter.js"
  11. MockDocStoreApi = require './helpers/MockDocstoreApi'
  12. MockFileStoreApi = require './helpers/MockFileStoreApi'
  13. request = require "./helpers/request"
  14. User = require "./helpers/User"
  15. describe "ProjectDuplicateNames", ->
  16. before (done) ->
  17. @owner = new User()
  18. @owner.login done
  19. @project = {}
  20. @callback = sinon.stub()
  21. describe "creating a project from the example template", ->
  22. before (done) ->
  23. @owner.createProject "example-project", {template: "example"}, (error, project_id) =>
  24. throw error if error?
  25. @example_project_id = project_id
  26. @owner.getProject project_id, (error, project) =>
  27. @project = project
  28. @mainTexDoc = _.find(project.rootFolder[0].docs, (doc) -> doc.name is 'main.tex')
  29. @refBibDoc = _.find(project.rootFolder[0].docs, (doc) -> doc.name is 'references.bib')
  30. @imageFile = _.find(project.rootFolder[0].fileRefs, (file) -> file.name is 'universe.jpg')
  31. @rootFolderId = project.rootFolder[0]._id.toString()
  32. # create a folder called 'testfolder'
  33. @owner.request.post {
  34. uri: "/project/#{@example_project_id}/folder"
  35. json:
  36. name: "testfolder"
  37. parent_folder_id: @rootFolderId
  38. }, (err, res, body) =>
  39. @testFolderId = body._id
  40. done()
  41. it "should create a project", ->
  42. expect(@project.rootFolder[0].docs.length).to.equal(2)
  43. expect(@project.rootFolder[0].fileRefs.length).to.equal(1)
  44. it "should create two docs in the docstore", ->
  45. docs = MockDocStoreApi.docs[@example_project_id]
  46. expect(Object.keys(docs).length).to.equal(2)
  47. it "should create one file in the filestore", ->
  48. files = MockFileStoreApi.files[@example_project_id]
  49. expect(Object.keys(files).length).to.equal(1)
  50. describe "for an existing doc", ->
  51. describe "trying to add a doc with the same name", ->
  52. before (done) ->
  53. @owner.request.post {
  54. uri: "/project/#{@example_project_id}/doc"
  55. json:
  56. name: "main.tex"
  57. parent_folder_id: @rootFolderId
  58. }, (err, res, body) =>
  59. @res = res
  60. done()
  61. it "should respond with 400 error status", ->
  62. expect(@res.statusCode).to.equal 400
  63. describe "trying to add a folder with the same name", ->
  64. before (done) ->
  65. @owner.request.post {
  66. uri: "/project/#{@example_project_id}/folder"
  67. json:
  68. name: "main.tex"
  69. parent_folder_id: @rootFolderId
  70. }, (err, res, body) =>
  71. @res = res
  72. done()
  73. it "should respond with 400 error status", ->
  74. expect(@res.statusCode).to.equal 400
  75. describe "trying to upload a file with the same name", ->
  76. before (done) ->
  77. @owner.request.post
  78. uri: "/project/#{@example_project_id}/upload"
  79. json: true
  80. qs:
  81. folder_id: @rootFolderId
  82. qqfilename: "main.tex"
  83. formData:
  84. qqfile:
  85. value: fs.createReadStream Path.resolve(__dirname + '/../files/1pixel.png')
  86. options:
  87. filename: 'main.tex',
  88. contentType: 'image/png'
  89. , (err, res, body) =>
  90. @body = body
  91. done()
  92. it "should respond with failure status", ->
  93. expect(@body.success).to.equal false
  94. describe "trying to add a folder with the same name", ->
  95. before (done) ->
  96. @owner.request.post {
  97. uri: "/project/#{@example_project_id}/folder"
  98. json:
  99. name: "main.tex"
  100. parent_folder_id: @rootFolderId
  101. }, (err, res, body) =>
  102. @res = res
  103. done()
  104. it "should respond with 400 error status", ->
  105. expect(@res.statusCode).to.equal 400
  106. describe "trying to upload a file with the same name", ->
  107. before (done) ->
  108. @owner.request.post
  109. uri: "/project/#{@example_project_id}/upload"
  110. json: true
  111. qs:
  112. folder_id: @rootFolderId
  113. qqfilename: "main.tex"
  114. formData:
  115. qqfile:
  116. value: fs.createReadStream Path.resolve(__dirname + '/../files/1pixel.png')
  117. options:
  118. filename: 'main.tex',
  119. contentType: 'image/png'
  120. , (err, res, body) =>
  121. @body = body
  122. done()
  123. it "should respond with failure status", ->
  124. expect(@body.success).to.equal false
  125. describe "for an existing file", ->
  126. describe "trying to add a doc with the same name", ->
  127. before (done) ->
  128. @owner.request.post {
  129. uri: "/project/#{@example_project_id}/doc"
  130. json:
  131. name: "universe.jpg"
  132. parent_folder_id: @rootFolderId
  133. }, (err, res, body) =>
  134. @res = res
  135. done()
  136. it "should respond with 400 error status", ->
  137. expect(@res.statusCode).to.equal 400
  138. describe "trying to add a folder with the same name", ->
  139. before (done) ->
  140. @owner.request.post {
  141. uri: "/project/#{@example_project_id}/folder"
  142. json:
  143. name: "universe.jpg"
  144. parent_folder_id: @rootFolderId
  145. }, (err, res, body) =>
  146. @res = res
  147. done()
  148. it "should respond with 400 error status", ->
  149. expect(@res.statusCode).to.equal 400
  150. describe "trying to upload a file with the same name", ->
  151. before (done) ->
  152. @owner.request.post
  153. uri: "/project/#{@example_project_id}/upload"
  154. json: true
  155. qs:
  156. folder_id: @rootFolderId
  157. qqfilename: "universe.jpg"
  158. formData:
  159. qqfile:
  160. value: fs.createReadStream Path.resolve(__dirname + '/../files/1pixel.png')
  161. options:
  162. filename: 'universe.jpg',
  163. contentType: 'image/jpeg'
  164. , (err, res, body) =>
  165. @body = body
  166. # update the image id because we have replaced the file
  167. @imageFile._id = @body.entity_id
  168. done()
  169. it "should succeed (overwriting the file)", ->
  170. expect(@body.success).to.equal true
  171. # at this point the @imageFile._id has changed
  172. describe "for an existing folder", ->
  173. describe "trying to add a doc with the same name", ->
  174. before (done) ->
  175. @owner.request.post {
  176. uri: "/project/#{@example_project_id}/doc"
  177. json:
  178. name: "testfolder"
  179. parent_folder_id: @rootFolderId
  180. }, (err, res, body) =>
  181. @res = res
  182. done()
  183. it "should respond with 400 error status", ->
  184. expect(@res.statusCode).to.equal 400
  185. describe "trying to add a folder with the same name", ->
  186. before (done) ->
  187. @owner.request.post {
  188. uri: "/project/#{@example_project_id}/folder"
  189. json:
  190. name: "testfolder"
  191. parent_folder_id: @rootFolderId
  192. }, (err, res, body) =>
  193. @res = res
  194. done()
  195. it "should respond with 400 error status", ->
  196. expect(@res.statusCode).to.equal 400
  197. describe "trying to upload a file with the same name", ->
  198. before (done) ->
  199. @owner.request.post
  200. uri: "/project/#{@example_project_id}/upload"
  201. json: true
  202. qs:
  203. folder_id: @rootFolderId
  204. qqfilename: "universe.jpg"
  205. formData:
  206. qqfile:
  207. value: fs.createReadStream Path.resolve(__dirname + '/../files/1pixel.png')
  208. options:
  209. filename: 'testfolder',
  210. contentType: 'image/jpeg'
  211. , (err, res, body) =>
  212. @body = body
  213. done()
  214. it "should respond with failure status", ->
  215. expect(@body.success).to.equal false
  216. describe "for an existing doc", ->
  217. describe "trying to rename a doc to the same name", ->
  218. before (done) ->
  219. @owner.request.post {
  220. uri: "/project/#{@example_project_id}/doc/#{@refBibDoc._id}/rename"
  221. json:
  222. name: "main.tex"
  223. }, (err, res, body) =>
  224. @res = res
  225. done()
  226. it "should respond with 400 error status", ->
  227. expect(@res.statusCode).to.equal 400
  228. describe "trying to rename a folder to the same name", ->
  229. before (done) ->
  230. @owner.request.post {
  231. uri: "/project/#{@example_project_id}/folder/#{@testFolderId}/rename"
  232. json:
  233. name: "main.tex"
  234. }, (err, res, body) =>
  235. @res = res
  236. done()
  237. it "should respond with 400 error status", ->
  238. expect(@res.statusCode).to.equal 400
  239. describe "trying to rename a file to the same name", ->
  240. before (done) ->
  241. @owner.request.post {
  242. uri: "/project/#{@example_project_id}/file/#{@imageFile._id}/rename"
  243. json:
  244. name: "main.tex"
  245. }, (err, res, body) =>
  246. @res = res
  247. done()
  248. it "should respond with failure status", ->
  249. expect(@res.statusCode).to.equal 400
  250. describe "for an existing file", ->
  251. describe "trying to rename a doc to the same name", ->
  252. before (done) ->
  253. @owner.request.post {
  254. uri: "/project/#{@example_project_id}/doc/#{@refBibDoc._id}/rename"
  255. json:
  256. name: "universe.jpg"
  257. }, (err, res, body) =>
  258. @res = res
  259. done()
  260. it "should respond with 400 error status", ->
  261. expect(@res.statusCode).to.equal 400
  262. describe "trying to rename a folder to the same name", ->
  263. before (done) ->
  264. @owner.request.post {
  265. uri: "/project/#{@example_project_id}/folder/#{@testFolderId}/rename"
  266. json:
  267. name: "universe.jpg"
  268. }, (err, res, body) =>
  269. @res = res
  270. done()
  271. it "should respond with 400 error status", ->
  272. expect(@res.statusCode).to.equal 400
  273. describe "trying to rename a file to the same name", ->
  274. before (done) ->
  275. @owner.request.post {
  276. uri: "/project/#{@example_project_id}/file/#{@imageFile._id}/rename"
  277. json:
  278. name: "universe.jpg"
  279. }, (err, res, body) =>
  280. @res = res
  281. done()
  282. it "should respond with failure status", ->
  283. expect(@res.statusCode).to.equal 400
  284. describe "for an existing folder", ->
  285. describe "trying to rename a doc to the same name", ->
  286. before (done) ->
  287. @owner.request.post {
  288. uri: "/project/#{@example_project_id}/doc/#{@refBibDoc._id}/rename"
  289. json:
  290. name: "testfolder"
  291. }, (err, res, body) =>
  292. @res = res
  293. done()
  294. it "should respond with 400 error status", ->
  295. expect(@res.statusCode).to.equal 400
  296. describe "trying to rename a folder to the same name", ->
  297. before (done) ->
  298. @owner.request.post {
  299. uri: "/project/#{@example_project_id}/folder/#{@testFolderId}/rename"
  300. json:
  301. name: "testfolder"
  302. }, (err, res, body) =>
  303. @res = res
  304. done()
  305. it "should respond with 400 error status", ->
  306. expect(@res.statusCode).to.equal 400
  307. describe "trying to rename a file to the same name", ->
  308. before (done) ->
  309. @owner.request.post {
  310. uri: "/project/#{@example_project_id}/file/#{@imageFile._id}/rename"
  311. json:
  312. name: "testfolder"
  313. }, (err, res, body) =>
  314. @res = res
  315. done()
  316. it "should respond with failure status", ->
  317. expect(@res.statusCode).to.equal 400
  318. describe "for an existing folder with a file with the same name", ->
  319. before (done) ->
  320. @owner.request.post {
  321. uri: "/project/#{@example_project_id}/doc"
  322. json:
  323. name: "main.tex"
  324. parent_folder_id: @testFolderId
  325. }, (err, res, body) =>
  326. @owner.request.post {
  327. uri: "/project/#{@example_project_id}/doc"
  328. json:
  329. name: "universe.jpg"
  330. parent_folder_id: @testFolderId
  331. }, (err, res, body) =>
  332. @owner.request.post {
  333. uri: "/project/#{@example_project_id}/folder"
  334. json:
  335. name: "otherFolder"
  336. parent_folder_id: @testFolderId
  337. }, (err, res, body) =>
  338. @subFolderId = body._id
  339. @owner.request.post {
  340. uri: "/project/#{@example_project_id}/folder"
  341. json:
  342. name: "otherFolder"
  343. parent_folder_id: @rootFolderId
  344. }, (err, res, body) =>
  345. @otherFolderId = body._id
  346. done()
  347. describe "trying to move a doc into the folder", ->
  348. before (done) ->
  349. @owner.request.post {
  350. uri: "/project/#{@example_project_id}/doc/#{@mainTexDoc._id}/move"
  351. json:
  352. folder_id: @testFolderId
  353. }, (err, res, body) =>
  354. @res = res
  355. done()
  356. it "should respond with 400 error status", ->
  357. expect(@res.statusCode).to.equal 400
  358. describe "trying to move a file into the folder", ->
  359. before (done) ->
  360. @owner.request.post {
  361. uri: "/project/#{@example_project_id}/file/#{@imageFile._id}/move"
  362. json:
  363. folder_id: @testFolderId
  364. }, (err, res, body) =>
  365. @res = res
  366. done()
  367. it "should respond with 400 error status", ->
  368. expect(@res.statusCode).to.equal 400
  369. describe "trying to move a folder into the folder", ->
  370. before (done) ->
  371. @owner.request.post {
  372. uri: "/project/#{@example_project_id}/folder/#{@otherFolderId}/move"
  373. json:
  374. folder_id: @testFolderId
  375. }, (err, res, body) =>
  376. @res = res
  377. done()
  378. it "should respond with 400 error status", ->
  379. expect(@res.statusCode).to.equal 400
  380. describe "trying to move a folder into a subfolder of itself", ->
  381. before (done) ->
  382. @owner.request.post {
  383. uri: "/project/#{@example_project_id}/folder/#{@testFolderId}/move"
  384. json:
  385. folder_id: @subFolderId
  386. }, (err, res, body) =>
  387. @res = res
  388. done()
  389. it "should respond with 400 error status", ->
  390. expect(@res.statusCode).to.equal 400