ProjectDuplicateNameTests.coffee 13 KB

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