ProjectDuplicateNameTests.coffee 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 add a folder with the same name", ->
  76. before (done) ->
  77. @owner.request.post {
  78. uri: "/project/#{@example_project_id}/folder"
  79. json:
  80. name: "main.tex"
  81. parent_folder_id: @rootFolderId
  82. }, (err, res, body) =>
  83. @res = res
  84. done()
  85. it "should respond with 400 error status", ->
  86. expect(@res.statusCode).to.equal 400
  87. describe "for an existing file", ->
  88. describe "trying to add a doc with the same name", ->
  89. before (done) ->
  90. @owner.request.post {
  91. uri: "/project/#{@example_project_id}/doc"
  92. json:
  93. name: "universe.jpg"
  94. parent_folder_id: @rootFolderId
  95. }, (err, res, body) =>
  96. @res = res
  97. done()
  98. it "should respond with 400 error status", ->
  99. expect(@res.statusCode).to.equal 400
  100. describe "trying to add a folder with the same name", ->
  101. before (done) ->
  102. @owner.request.post {
  103. uri: "/project/#{@example_project_id}/folder"
  104. json:
  105. name: "universe.jpg"
  106. parent_folder_id: @rootFolderId
  107. }, (err, res, body) =>
  108. @res = res
  109. done()
  110. it "should respond with 400 error status", ->
  111. expect(@res.statusCode).to.equal 400
  112. describe "trying to upload a file with the same name", ->
  113. before (done) ->
  114. @owner.request.post
  115. uri: "/project/#{@example_project_id}/upload"
  116. json: true
  117. qs:
  118. folder_id: @rootFolderId
  119. qqfilename: "universe.jpg"
  120. formData:
  121. qqfile:
  122. value: fs.createReadStream Path.resolve(__dirname + '/../files/1pixel.png')
  123. options:
  124. filename: 'universe.jpg',
  125. contentType: 'image/jpeg'
  126. , (err, res, body) =>
  127. @body = body
  128. # update the image id because we have replaced the file
  129. @imageFile._id = @body.entity_id
  130. done()
  131. it "should succeed (overwriting the file)", ->
  132. expect(@body.success).to.equal true
  133. # at this point the @imageFile._id has changed
  134. describe "for an existing folder", ->
  135. describe "trying to add a doc with the same name", ->
  136. before (done) ->
  137. @owner.request.post {
  138. uri: "/project/#{@example_project_id}/doc"
  139. json:
  140. name: "testfolder"
  141. parent_folder_id: @rootFolderId
  142. }, (err, res, body) =>
  143. @res = res
  144. done()
  145. it "should respond with 400 error status", ->
  146. expect(@res.statusCode).to.equal 400
  147. describe "trying to add a folder with the same name", ->
  148. before (done) ->
  149. @owner.request.post {
  150. uri: "/project/#{@example_project_id}/folder"
  151. json:
  152. name: "testfolder"
  153. parent_folder_id: @rootFolderId
  154. }, (err, res, body) =>
  155. @res = res
  156. done()
  157. it "should respond with 400 error status", ->
  158. expect(@res.statusCode).to.equal 400
  159. describe "trying to upload a file with the same name", ->
  160. before (done) ->
  161. @owner.request.post
  162. uri: "/project/#{@example_project_id}/upload"
  163. json: true
  164. qs:
  165. folder_id: @rootFolderId
  166. qqfilename: "universe.jpg"
  167. formData:
  168. qqfile:
  169. value: fs.createReadStream Path.resolve(__dirname + '/../files/1pixel.png')
  170. options:
  171. filename: 'testfolder',
  172. contentType: 'image/jpeg'
  173. , (err, res, body) =>
  174. @body = body
  175. done()
  176. it "should respond with failure status", ->
  177. expect(@body.success).to.equal false
  178. describe "for an existing doc", ->
  179. describe "trying to rename a doc to the same name", ->
  180. before (done) ->
  181. @owner.request.post {
  182. uri: "/project/#{@example_project_id}/doc/#{@refBibDoc._id}/rename"
  183. json:
  184. name: "main.tex"
  185. }, (err, res, body) =>
  186. @res = res
  187. done()
  188. it "should respond with 400 error status", ->
  189. expect(@res.statusCode).to.equal 400
  190. describe "trying to rename a folder to the same name", ->
  191. before (done) ->
  192. @owner.request.post {
  193. uri: "/project/#{@example_project_id}/folder/#{@testFolderId}/rename"
  194. json:
  195. name: "main.tex"
  196. }, (err, res, body) =>
  197. @res = res
  198. done()
  199. it "should respond with 400 error status", ->
  200. expect(@res.statusCode).to.equal 400
  201. describe "trying to rename a file to the same name", ->
  202. before (done) ->
  203. @owner.request.post {
  204. uri: "/project/#{@example_project_id}/file/#{@imageFile._id}/rename"
  205. json:
  206. name: "main.tex"
  207. }, (err, res, body) =>
  208. @res = res
  209. done()
  210. it "should respond with failure status", ->
  211. expect(@res.statusCode).to.equal 400
  212. describe "for an existing file", ->
  213. describe "trying to rename a doc to the same name", ->
  214. before (done) ->
  215. @owner.request.post {
  216. uri: "/project/#{@example_project_id}/doc/#{@refBibDoc._id}/rename"
  217. json:
  218. name: "universe.jpg"
  219. }, (err, res, body) =>
  220. @res = res
  221. done()
  222. it "should respond with 400 error status", ->
  223. expect(@res.statusCode).to.equal 400
  224. describe "trying to rename a folder to the same name", ->
  225. before (done) ->
  226. @owner.request.post {
  227. uri: "/project/#{@example_project_id}/folder/#{@testFolderId}/rename"
  228. json:
  229. name: "universe.jpg"
  230. }, (err, res, body) =>
  231. @res = res
  232. done()
  233. it "should respond with 400 error status", ->
  234. expect(@res.statusCode).to.equal 400
  235. describe "trying to rename a file to the same name", ->
  236. before (done) ->
  237. @owner.request.post {
  238. uri: "/project/#{@example_project_id}/file/#{@imageFile._id}/rename"
  239. json:
  240. name: "universe.jpg"
  241. }, (err, res, body) =>
  242. @res = res
  243. done()
  244. it "should respond with failure status", ->
  245. expect(@res.statusCode).to.equal 400
  246. describe "for an existing folder", ->
  247. describe "trying to rename a doc to the same name", ->
  248. before (done) ->
  249. @owner.request.post {
  250. uri: "/project/#{@example_project_id}/doc/#{@refBibDoc._id}/rename"
  251. json:
  252. name: "testfolder"
  253. }, (err, res, body) =>
  254. @res = res
  255. done()
  256. it "should respond with 400 error status", ->
  257. expect(@res.statusCode).to.equal 400
  258. describe "trying to rename a folder to the same name", ->
  259. before (done) ->
  260. @owner.request.post {
  261. uri: "/project/#{@example_project_id}/folder/#{@testFolderId}/rename"
  262. json:
  263. name: "testfolder"
  264. }, (err, res, body) =>
  265. @res = res
  266. done()
  267. it "should respond with 400 error status", ->
  268. expect(@res.statusCode).to.equal 400
  269. describe "trying to rename a file to the same name", ->
  270. before (done) ->
  271. @owner.request.post {
  272. uri: "/project/#{@example_project_id}/file/#{@imageFile._id}/rename"
  273. json:
  274. name: "testfolder"
  275. }, (err, res, body) =>
  276. @res = res
  277. done()
  278. it "should respond with failure status", ->
  279. expect(@res.statusCode).to.equal 400
  280. describe "for an existing folder with a file with the same name", ->
  281. before (done) ->
  282. @owner.request.post {
  283. uri: "/project/#{@example_project_id}/doc"
  284. json:
  285. name: "main.tex"
  286. parent_folder_id: @testFolderId
  287. }, (err, res, body) =>
  288. @owner.request.post {
  289. uri: "/project/#{@example_project_id}/doc"
  290. json:
  291. name: "universe.jpg"
  292. parent_folder_id: @testFolderId
  293. }, (err, res, body) =>
  294. @owner.request.post {
  295. uri: "/project/#{@example_project_id}/folder"
  296. json:
  297. name: "otherFolder"
  298. parent_folder_id: @testFolderId
  299. }, (err, res, body) =>
  300. @subFolderId = body._id
  301. @owner.request.post {
  302. uri: "/project/#{@example_project_id}/folder"
  303. json:
  304. name: "otherFolder"
  305. parent_folder_id: @rootFolderId
  306. }, (err, res, body) =>
  307. @otherFolderId = body._id
  308. done()
  309. describe "trying to move a doc into the folder", ->
  310. before (done) ->
  311. @owner.request.post {
  312. uri: "/project/#{@example_project_id}/doc/#{@mainTexDoc._id}/move"
  313. json:
  314. folder_id: @testFolderId
  315. }, (err, res, body) =>
  316. @res = res
  317. done()
  318. it "should respond with 400 error status", ->
  319. expect(@res.statusCode).to.equal 400
  320. describe "trying to move a file into the folder", ->
  321. before (done) ->
  322. @owner.request.post {
  323. uri: "/project/#{@example_project_id}/file/#{@imageFile._id}/move"
  324. json:
  325. folder_id: @testFolderId
  326. }, (err, res, body) =>
  327. @res = res
  328. done()
  329. it "should respond with 400 error status", ->
  330. expect(@res.statusCode).to.equal 400
  331. describe "trying to move a folder into the folder", ->
  332. before (done) ->
  333. @owner.request.post {
  334. uri: "/project/#{@example_project_id}/folder/#{@otherFolderId}/move"
  335. json:
  336. folder_id: @testFolderId
  337. }, (err, res, body) =>
  338. @res = res
  339. done()
  340. it "should respond with 400 error status", ->
  341. expect(@res.statusCode).to.equal 400
  342. describe "trying to move a folder into a subfolder of itself", ->
  343. before (done) ->
  344. @owner.request.post {
  345. uri: "/project/#{@example_project_id}/folder/#{@testFolderId}/move"
  346. json:
  347. folder_id: @subFolderId
  348. }, (err, res, body) =>
  349. @res = res
  350. done()
  351. it "should respond with 400 error status", ->
  352. expect(@res.statusCode).to.equal 400