ProjectLocatorTests.coffee 12 KB

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