ProjectLocatorTests.coffee 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. spies = require('chai-spies')
  2. chai = require('chai').use(spies)
  3. assert = require('chai').assert
  4. should = chai.should()
  5. modulePath = "../../../../app/js/Features/Project/ProjectLocator"
  6. SandboxedModule = require('sandboxed-module')
  7. sinon = require('sinon')
  8. Errors = require "../../../../app/js/errors"
  9. expect = require("chai").expect
  10. Project = class Project
  11. project = _id : "1234566", rootFolder:[]
  12. rootDoc = name:"rootDoc", _id:"das239djd"
  13. doc1 = name:"otherDoc.txt", _id:"dsad2ddd"
  14. doc2 = name:"docname.txt", _id:"dsad2ddddd"
  15. file1 = name:"file1", _id:"dsa9lkdsad"
  16. subSubFile = name:"subSubFile", _id:"d1d2dk"
  17. subSubDoc = name:"subdoc.txt", _id:"321dmdwi"
  18. secondSubFolder = name:"secondSubFolder", _id:"dsa3e23", docs:[subSubDoc], fileRefs:[subSubFile], folders:[]
  19. subFolder = name:"subFolder", _id:"dsadsa93", folders:[secondSubFolder], docs:[], fileRefs:[]
  20. subFolder1 = name:"subFolder1", _id:"123asdjoij"
  21. rootFolder =
  22. _id : "123sdskd"
  23. docs:[doc1, doc2, null, rootDoc]
  24. fileRefs:[file1]
  25. folders:[subFolder1, subFolder]
  26. project.rootFolder[0] = rootFolder
  27. project.rootDoc_id = rootDoc._id
  28. describe 'ProjectLocator', ->
  29. beforeEach ->
  30. Project.getProject = (project_id, fields, callback)=>
  31. callback(null, project)
  32. Project.findById = (project_id, callback)=>
  33. callback(null, project)
  34. @locator = SandboxedModule.require modulePath, requires:
  35. '../../models/Project':{Project:Project}
  36. '../../models/User':{User:@User}
  37. './ProjectGetter': @ProjectGetter = {}
  38. 'logger-sharelatex':
  39. log:->
  40. err:->
  41. warn: ->
  42. describe 'finding a doc', ->
  43. it 'finds one at the root level', (done)->
  44. @locator.findElement {project_id:project._id, element_id:doc2._id, type:"docs"}, (err, foundElement, path, parentFolder)->
  45. assert(!err?)
  46. foundElement._id.should.equal doc2._id
  47. path.fileSystem.should.equal "/#{doc2.name}"
  48. parentFolder._id.should.equal project.rootFolder[0]._id
  49. path.mongo.should.equal "rootFolder.0.docs.1"
  50. done()
  51. it 'when it is nested', (done)->
  52. @locator.findElement {project_id:project._id, element_id:subSubDoc._id, type:"doc"}, (err, foundElement, path, parentFolder)->
  53. assert(!err?)
  54. should.equal foundElement._id, subSubDoc._id
  55. path.fileSystem.should.equal "/#{subFolder.name}/#{secondSubFolder.name}/#{subSubDoc.name}"
  56. parentFolder._id.should.equal secondSubFolder._id
  57. path.mongo.should.equal "rootFolder.0.folders.1.folders.0.docs.0"
  58. done()
  59. it 'should give error if element could not be found', (done)->
  60. @locator.findElement {project_id:project._id, element_id:"ddsd432nj42", type:"docs"}, (err, foundElement, path, parentFolder)->
  61. err.should.deep.equal new Errors.NotFoundError("entity not found")
  62. done()
  63. describe 'finding a folder', ->
  64. it 'should return root folder when looking for root folder', (done)->
  65. @locator.findElement {project_id:project._id, element_id:rootFolder._id, type:"folder"}, (err, foundElement, path, parentFolder)->
  66. assert(!err)
  67. foundElement._id.should.equal rootFolder._id
  68. done()
  69. it 'when at root', (done)->
  70. @locator.findElement {project_id:project._id, element_id:subFolder._id, type:"folder"}, (err, foundElement, path, parentFolder)->
  71. assert(!err)
  72. foundElement._id.should.equal subFolder._id
  73. path.fileSystem.should.equal "/#{subFolder.name}"
  74. parentFolder._id.should.equal rootFolder._id
  75. path.mongo.should.equal "rootFolder.0.folders.1"
  76. done()
  77. it 'when deeply nested', (done)->
  78. @locator.findElement {project_id:project._id, element_id:secondSubFolder._id, type:"folder"}, (err, foundElement, path, parentFolder)->
  79. assert(!err)
  80. foundElement._id.should.equal secondSubFolder._id
  81. path.fileSystem.should.equal "/#{subFolder.name}/#{secondSubFolder.name}"
  82. parentFolder._id.should.equal subFolder._id
  83. path.mongo.should.equal "rootFolder.0.folders.1.folders.0"
  84. done()
  85. describe 'finding a file', ->
  86. it 'when at root', (done)->
  87. @locator.findElement {project_id:project._id, element_id:file1._id, type:"fileRefs"}, (err, foundElement, path, parentFolder)->
  88. assert(!err)
  89. foundElement._id.should.equal file1._id
  90. path.fileSystem.should.equal "/#{file1.name}"
  91. parentFolder._id.should.equal rootFolder._id
  92. path.mongo.should.equal "rootFolder.0.fileRefs.0"
  93. done()
  94. it 'when deeply nested', (done)->
  95. @locator.findElement {project_id:project._id, element_id:subSubFile._id, type:"fileRefs"}, (err, foundElement, path, parentFolder)->
  96. assert(!err)
  97. foundElement._id.should.equal subSubFile._id
  98. path.fileSystem.should.equal "/#{subFolder.name}/#{secondSubFolder.name}/#{subSubFile.name}"
  99. parentFolder._id.should.equal secondSubFolder._id
  100. path.mongo.should.equal "rootFolder.0.folders.1.folders.0.fileRefs.0"
  101. done()
  102. describe 'finding an element with wrong element type', ->
  103. it 'should add an s onto the element type', (done)->
  104. @locator.findElement {project_id:project._id, element_id:subSubDoc._id, type:"doc"}, (err, foundElement, path, parentFolder)->
  105. assert(!err)
  106. foundElement._id.should.equal subSubDoc._id
  107. done()
  108. it 'should convert file to fileRefs', (done)->
  109. @locator.findElement {project_id:project._id, element_id:file1._id, type:"fileRefs"}, (err, foundElement, path, parentFolder)->
  110. assert(!err)
  111. foundElement._id.should.equal file1._id
  112. done()
  113. describe 'should be able to take actual project as well as id', ->
  114. doc3 =
  115. _id:"123dsdj3"
  116. name:"doc3"
  117. rootFolder2 =
  118. _id : "123sddedskd"
  119. docs:[doc3]
  120. project2 =
  121. _id : "1234566"
  122. rootFolder:[rootFolder2]
  123. it 'should find doc in project', (done)->
  124. @locator.findElement {project:project2, element_id:doc3._id, type:"docs"}, (err, foundElement, path, parentFolder)->
  125. assert(!err?)
  126. foundElement._id.should.equal doc3._id
  127. path.fileSystem.should.equal "/#{doc3.name}"
  128. parentFolder._id.should.equal project2.rootFolder[0]._id
  129. path.mongo.should.equal "rootFolder.0.docs.0"
  130. done()
  131. describe 'finding root doc', ->
  132. it 'should return root doc when passed project', (done)->
  133. @locator.findRootDoc project, (err, doc)->
  134. assert !err?
  135. doc._id.should.equal rootDoc._id
  136. done()
  137. it 'should return root doc when passed project_id', (done)->
  138. @locator.findRootDoc project._id, (err, doc)->
  139. assert !err?
  140. doc._id.should.equal rootDoc._id
  141. done()
  142. it 'should return null when the project has no rootDoc', (done) ->
  143. project.rootDoc_id = null
  144. @locator.findRootDoc project, (err, doc)->
  145. assert !err?
  146. expect(doc).to.equal null
  147. done()
  148. describe 'findElementByPath', ->
  149. it 'should take a doc path and return the element for a root level document', (done)->
  150. path = "#{doc1.name}"
  151. @locator.findElementByPath project._id, path, (err, element)->
  152. element.should.deep.equal doc1
  153. done()
  154. it 'should take a doc path and return the element for a root level document with a starting slash', (done)->
  155. path = "/#{doc1.name}"
  156. @locator.findElementByPath project._id, path, (err, element)->
  157. element.should.deep.equal doc1
  158. done()
  159. it 'should take a doc path and return the element for a nested document', (done)->
  160. path = "#{subFolder.name}/#{secondSubFolder.name}/#{subSubDoc.name}"
  161. @locator.findElementByPath project._id, path, (err, element)->
  162. element.should.deep.equal subSubDoc
  163. done()
  164. it 'should take a file path and return the element for a root level document', (done)->
  165. path = "#{file1.name}"
  166. @locator.findElementByPath project._id, path, (err, element)->
  167. element.should.deep.equal file1
  168. done()
  169. it 'should take a file path and return the element for a nested document', (done)->
  170. path = "#{subFolder.name}/#{secondSubFolder.name}/#{subSubFile.name}"
  171. @locator.findElementByPath project._id, path, (err, element)->
  172. element.should.deep.equal subSubFile
  173. done()
  174. it 'should take a file path and return the element for a nested document case insenstive', (done)->
  175. path = "#{subFolder.name.toUpperCase()}/#{secondSubFolder.name.toUpperCase()}/#{subSubFile.name.toUpperCase()}"
  176. @locator.findElementByPath project._id, path, (err, element)->
  177. element.should.deep.equal subSubFile
  178. done()
  179. it 'should take a file path and return the element for a nested folder', (done)->
  180. path = "#{subFolder.name}/#{secondSubFolder.name}"
  181. @locator.findElementByPath project._id, path, (err, element)->
  182. element.should.deep.equal secondSubFolder
  183. done()
  184. it 'should take a file path and return the root folder', (done)->
  185. @locator.findElementByPath project._id, "/", (err, element)->
  186. element.should.deep.equal rootFolder
  187. done()
  188. it 'should return an error if the file can not be found inside know folder', (done)->
  189. @locator.findElementByPath project._id, "#{subFolder.name}/#{secondSubFolder.name}/exist.txt", (err, element)->
  190. err.should.not.equal undefined
  191. assert.equal element, undefined
  192. done()
  193. it 'should return an error if the file can not be found inside unknown folder', (done)->
  194. @locator.findElementByPath project._id, "this/does/not/exist.txt", (err, element)->
  195. err.should.not.equal undefined
  196. assert.equal element, undefined
  197. done()
  198. describe "where duplicate folder exists", ->
  199. beforeEach ->
  200. @duplicateFolder = {name:"duplicate1", _id:"1234", folders:[{
  201. name: "1"
  202. docs:[{name:"main.tex", _id:"456"}]
  203. folders: []
  204. fileRefs: []
  205. }], docs:[@doc = {name:"main.tex", _id:"456"}], fileRefs:[]}
  206. @project =
  207. rootFolder:[
  208. folders: [@duplicateFolder, @duplicateFolder]
  209. fileRefs: []
  210. docs: []
  211. ]
  212. Project.getProject = sinon.stub()
  213. Project.getProject.callsArgWith(2, null, @project)
  214. it "should not call the callback more than once", (done)->
  215. @locator.findElementByPath project._id, "#{@duplicateFolder.name}/#{@doc.name}", ->
  216. done() #mocha will throw exception if done called multiple times
  217. it "should not call the callback more than once when the path is longer than 1 level below the duplicate level", (done)->
  218. @locator.findElementByPath project._id, "#{@duplicateFolder.name}/1/main.tex", ->
  219. done() #mocha will throw exception if done called multiple times
  220. describe "with a null doc", ->
  221. beforeEach ->
  222. @project =
  223. rootFolder:[
  224. folders: []
  225. fileRefs: []
  226. docs: [{name:"main.tex"}, null, {name:"other.tex"}]
  227. ]
  228. Project.getProject = sinon.stub()
  229. Project.getProject.callsArgWith(2, null, @project)
  230. it "should not crash with a null", (done)->
  231. callback = sinon.stub()
  232. @locator.findElementByPath project._id, "/other.tex", (err, element)->
  233. element.name.should.equal "other.tex"
  234. done()
  235. describe "with a null project", ->
  236. beforeEach ->
  237. @project =
  238. rootFolder:[
  239. folders: []
  240. fileRefs: []
  241. docs: [{name:"main.tex"}, null, {name:"other.tex"}]
  242. ]
  243. Project.getProject = sinon.stub()
  244. Project.getProject.callsArgWith(2, null)
  245. it "should not crash with a null", (done)->
  246. callback = sinon.stub()
  247. @locator.findElementByPath project._id, "/other.tex", (err, element)->
  248. expect(err).to.exist
  249. done()
  250. describe 'findUsersProjectByName finding a project by user_id and project name', ()->
  251. it 'should return the project from an array case insenstive', (done)->
  252. user_id = "123jojoidns"
  253. stubbedProject = {name:"findThis"}
  254. projects = [{name:"notThis"}, {name:"wellll"}, stubbedProject, {name:"Noooo"}]
  255. @ProjectGetter.findAllUsersProjects = sinon.stub().callsArgWith(2, null, projects)
  256. @locator.findUsersProjectByName user_id, stubbedProject.name.toLowerCase(), (err, project)->
  257. project.should.equal stubbedProject
  258. done()
  259. it 'should return the project which is not archived', (done)->
  260. user_id = "123jojoidns"
  261. stubbedProject = {name:"findThis", _id:12331321}
  262. projects = [{name:"notThis"}, {name:"wellll"}, {name:"findThis",archived:true}, stubbedProject, {name:"findThis",archived:true}, {name:"Noooo"}]
  263. @ProjectGetter.findAllUsersProjects = sinon.stub().callsArgWith(2, null, projects)
  264. @locator.findUsersProjectByName user_id, stubbedProject.name.toLowerCase(), (err, project)->
  265. project._id.should.equal stubbedProject._id
  266. done()
  267. it 'should search collab projects as well', (done)->
  268. user_id = "123jojoidns"
  269. stubbedProject = {name:"findThis"}
  270. projects = [{name:"notThis"}, {name:"wellll"}, {name:"Noooo"}]
  271. @ProjectGetter.findAllUsersProjects = sinon.stub().callsArgWith(2, null, projects, [stubbedProject])
  272. @locator.findUsersProjectByName user_id, stubbedProject.name.toLowerCase(), (err, project)->
  273. project.should.equal stubbedProject
  274. done()