ProjectDuplicateNameTests.coffee 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. done()
  167. it "should succeed (overwriting the file)", ->
  168. expect(@body.success).to.equal true
  169. describe "for an existing folder", ->
  170. describe "trying to add a doc with the same name", ->
  171. before (done) ->
  172. @owner.request.post {
  173. uri: "/project/#{@example_project_id}/doc"
  174. json:
  175. name: "testfolder"
  176. parent_folder_id: @rootFolderId
  177. }, (err, res, body) =>
  178. @res = res
  179. done()
  180. it "should respond with 400 error status", ->
  181. expect(@res.statusCode).to.equal 400
  182. describe "trying to add a folder with the same name", ->
  183. before (done) ->
  184. @owner.request.post {
  185. uri: "/project/#{@example_project_id}/folder"
  186. json:
  187. name: "testfolder"
  188. parent_folder_id: @rootFolderId
  189. }, (err, res, body) =>
  190. @res = res
  191. done()
  192. it "should respond with 400 error status", ->
  193. expect(@res.statusCode).to.equal 400
  194. describe "trying to upload a file with the same name", ->
  195. before (done) ->
  196. @owner.request.post
  197. uri: "/project/#{@example_project_id}/upload"
  198. json: true
  199. qs:
  200. folder_id: @rootFolderId
  201. qqfilename: "universe.jpg"
  202. formData:
  203. qqfile:
  204. value: fs.createReadStream Path.resolve(__dirname + '/../files/1pixel.png')
  205. options:
  206. filename: 'testfolder',
  207. contentType: 'image/jpeg'
  208. , (err, res, body) =>
  209. @body = body
  210. done()
  211. it "should respond with failure status", ->
  212. expect(@body.success).to.equal false
  213. describe "for an existing doc", ->
  214. describe "trying to rename a doc to the same name", ->
  215. before (done) ->
  216. @owner.request.post {
  217. uri: "/project/#{@example_project_id}/doc/#{@refBibDoc._id}/rename"
  218. json:
  219. name: "main.tex"
  220. }, (err, res, body) =>
  221. @res = res
  222. done()
  223. it "should respond with 400 error status", ->
  224. expect(@res.statusCode).to.equal 400
  225. describe "trying to rename a folder to the same name", ->
  226. before (done) ->
  227. @owner.request.post {
  228. uri: "/project/#{@example_project_id}/folder/#{@testFolderId}/rename"
  229. json:
  230. name: "main.tex"
  231. }, (err, res, body) =>
  232. @res = res
  233. done()
  234. it "should respond with 400 error status", ->
  235. expect(@res.statusCode).to.equal 400
  236. describe "trying to rename a file to the same name", ->
  237. before (done) ->
  238. @owner.request.post {
  239. uri: "/project/#{@example_project_id}/file/#{@imageFile._id}/rename"
  240. json:
  241. name: "main.tex"
  242. }, (err, res, body) =>
  243. @res = res
  244. done()
  245. it "should respond with failure status", ->
  246. expect(@res.statusCode).to.equal 400
  247. describe "for an existing file", ->
  248. describe "trying to rename a doc to the same name", ->
  249. before (done) ->
  250. @owner.request.post {
  251. uri: "/project/#{@example_project_id}/doc/#{@refBibDoc._id}/rename"
  252. json:
  253. name: "universe.jpg"
  254. }, (err, res, body) =>
  255. @res = res
  256. done()
  257. it "should respond with 400 error status", ->
  258. expect(@res.statusCode).to.equal 400
  259. describe "trying to rename a folder to the same name", ->
  260. before (done) ->
  261. @owner.request.post {
  262. uri: "/project/#{@example_project_id}/folder/#{@testFolderId}/rename"
  263. json:
  264. name: "universe.jpg"
  265. }, (err, res, body) =>
  266. @res = res
  267. done()
  268. it "should respond with 400 error status", ->
  269. expect(@res.statusCode).to.equal 400
  270. describe "trying to rename a file to the same name", ->
  271. before (done) ->
  272. @owner.request.post {
  273. uri: "/project/#{@example_project_id}/file/#{@imageFile._id}/rename"
  274. json:
  275. name: "universe.jpg"
  276. }, (err, res, body) =>
  277. @res = res
  278. done()
  279. it "should respond with failure status", ->
  280. expect(@res.statusCode).to.equal 400
  281. describe "for an existing folder", ->
  282. describe "trying to rename a doc to the same name", ->
  283. before (done) ->
  284. @owner.request.post {
  285. uri: "/project/#{@example_project_id}/doc/#{@refBibDoc._id}/rename"
  286. json:
  287. name: "testfolder"
  288. }, (err, res, body) =>
  289. @res = res
  290. done()
  291. it "should respond with 400 error status", ->
  292. expect(@res.statusCode).to.equal 400
  293. describe "trying to rename a folder to the same name", ->
  294. before (done) ->
  295. @owner.request.post {
  296. uri: "/project/#{@example_project_id}/folder/#{@testFolderId}/rename"
  297. json:
  298. name: "testfolder"
  299. }, (err, res, body) =>
  300. @res = res
  301. done()
  302. it "should respond with 400 error status", ->
  303. expect(@res.statusCode).to.equal 400
  304. describe "trying to rename a file to the same name", ->
  305. before (done) ->
  306. @owner.request.post {
  307. uri: "/project/#{@example_project_id}/file/#{@imageFile._id}/rename"
  308. json:
  309. name: "testfolder"
  310. }, (err, res, body) =>
  311. @res = res
  312. done()
  313. it "should respond with failure status", ->
  314. expect(@res.statusCode).to.equal 400
  315. describe "for an existing folder with a file with the same name", ->
  316. before (done) ->
  317. @owner.request.post {
  318. uri: "/project/#{@example_project_id}/doc"
  319. json:
  320. name: "main.tex"
  321. parent_folder_id: @testFolderId
  322. }, (err, res, body) =>
  323. @owner.request.post {
  324. uri: "/project/#{@example_project_id}/doc"
  325. json:
  326. name: "universe.jpg"
  327. parent_folder_id: @testFolderId
  328. }, (err, res, body) =>
  329. @owner.request.post {
  330. uri: "/project/#{@example_project_id}/folder"
  331. json:
  332. name: "otherFolder"
  333. parent_folder_id: @testFolderId
  334. }, (err, res, body) =>
  335. @subFolderId = body._id
  336. @owner.request.post {
  337. uri: "/project/#{@example_project_id}/folder"
  338. json:
  339. name: "otherFolder"
  340. parent_folder_id: @rootFolderId
  341. }, (err, res, body) =>
  342. @otherFolderId = body._id
  343. done()
  344. describe "trying to move a doc into the folder", ->
  345. before (done) ->
  346. @owner.request.post {
  347. uri: "/project/#{@example_project_id}/doc/#{@mainTexDoc._id}/move"
  348. json:
  349. folder_id: @testFolderId
  350. }, (err, res, body) =>
  351. @res = res
  352. done()
  353. it "should respond with 400 error status", ->
  354. expect(@res.statusCode).to.equal 400
  355. describe "trying to move a file into the folder", ->
  356. before (done) ->
  357. @owner.request.post {
  358. uri: "/project/#{@example_project_id}/file/#{@imageFile._id}/move"
  359. json:
  360. folder_id: @testFolderId
  361. }, (err, res, body) =>
  362. @res = res
  363. done()
  364. it "should respond with 400 error status", ->
  365. expect(@res.statusCode).to.equal 400
  366. describe "trying to move a folder into the folder", ->
  367. before (done) ->
  368. @owner.request.post {
  369. uri: "/project/#{@example_project_id}/folder/#{@otherFolderId}/move"
  370. json:
  371. folder_id: @testFolderId
  372. }, (err, res, body) =>
  373. @res = res
  374. done()
  375. it "should respond with 400 error status", ->
  376. expect(@res.statusCode).to.equal 400
  377. describe "trying to move a folder into a subfolder of itself", ->
  378. before (done) ->
  379. @owner.request.post {
  380. uri: "/project/#{@example_project_id}/folder/#{@testFolderId}/move"
  381. json:
  382. folder_id: @subFolderId
  383. }, (err, res, body) =>
  384. @res = res
  385. done()
  386. it "should respond with 400 error status", ->
  387. expect(@res.statusCode).to.equal 400