ProjectLocatorTests.coffee 13 KB

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