ProjectLocatorTests.coffee 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. Project = class Project
  10. project = _id : "1234566", rootFolder:[]
  11. rootDoc = name:"rootDoc", _id:"das239djd"
  12. doc1 = name:"otherDoc.txt", _id:"dsad2ddd"
  13. doc2 = name:"docname.txt", _id:"dsad2ddddd"
  14. file1 = name:"file1", _id:"dsa9lkdsad"
  15. subSubFile = name:"subSubFile", _id:"d1d2dk"
  16. subSubDoc = name:"subdoc.txt", _id:"321dmdwi"
  17. secondSubFolder = name:"secondSubFolder", _id:"dsa3e23", docs:[subSubDoc], fileRefs:[subSubFile], folders:[]
  18. subFolder = name:"subFolder", _id:"dsadsa93", folders:[secondSubFolder], docs:[], fileRefs:[]
  19. subFolder1 = name:"subFolder1", _id:"123asdjoij"
  20. rootFolder =
  21. _id : "123sdskd"
  22. docs:[doc1, doc2, null, rootDoc]
  23. fileRefs:[file1]
  24. folders:[subFolder1, subFolder]
  25. project.rootFolder[0] = rootFolder
  26. project.rootDoc_id = rootDoc._id
  27. describe 'project model', ->
  28. beforeEach ->
  29. Project.getProject = (project_id, fields, callback)=>
  30. callback(null, project)
  31. Project.findById = (project_id, callback)=>
  32. callback(null, project)
  33. @locator = SandboxedModule.require modulePath, requires:
  34. '../../models/Project':{Project:Project}
  35. '../../models/User':{User:@User}
  36. 'logger-sharelatex':
  37. log:->
  38. err:->
  39. warn: ->
  40. describe 'finding a doc', ->
  41. it 'finds one at the root level', (done)->
  42. @locator.findElement {project_id:project._id, element_id:doc2._id, type:"docs"}, (err, foundElement, path, parentFolder)->
  43. assert(!err?)
  44. foundElement._id.should.equal doc2._id
  45. path.fileSystem.should.equal "/#{doc2.name}"
  46. parentFolder._id.should.equal project.rootFolder[0]._id
  47. path.mongo.should.equal "rootFolder.0.docs.1"
  48. done()
  49. it 'when it is nested', (done)->
  50. @locator.findElement {project_id:project._id, element_id:subSubDoc._id, type:"doc"}, (err, foundElement, path, parentFolder)->
  51. assert(!err?)
  52. should.equal foundElement._id, subSubDoc._id
  53. path.fileSystem.should.equal "/#{subFolder.name}/#{secondSubFolder.name}/#{subSubDoc.name}"
  54. parentFolder._id.should.equal secondSubFolder._id
  55. path.mongo.should.equal "rootFolder.0.folders.1.folders.0.docs.0"
  56. done()
  57. it 'should give error if element could not be found', (done)->
  58. @locator.findElement {project_id:project._id, element_id:"ddsd432nj42", type:"docs"}, (err, foundElement, path, parentFolder)->
  59. err.should.deep.equal new Errors.NotFoundError("entity not found")
  60. done()
  61. describe 'finding a folder', ->
  62. it 'should return root folder when looking for root folder', (done)->
  63. @locator.findElement {project_id:project._id, element_id:rootFolder._id, type:"folder"}, (err, foundElement, path, parentFolder)->
  64. assert(!err)
  65. foundElement._id.should.equal rootFolder._id
  66. done()
  67. it 'when at root', (done)->
  68. @locator.findElement {project_id:project._id, element_id:subFolder._id, type:"folder"}, (err, foundElement, path, parentFolder)->
  69. assert(!err)
  70. foundElement._id.should.equal subFolder._id
  71. path.fileSystem.should.equal "/#{subFolder.name}"
  72. parentFolder._id.should.equal rootFolder._id
  73. path.mongo.should.equal "rootFolder.0.folders.1"
  74. done()
  75. it 'when deeply nested', (done)->
  76. @locator.findElement {project_id:project._id, element_id:secondSubFolder._id, type:"folder"}, (err, foundElement, path, parentFolder)->
  77. assert(!err)
  78. foundElement._id.should.equal secondSubFolder._id
  79. path.fileSystem.should.equal "/#{subFolder.name}/#{secondSubFolder.name}"
  80. parentFolder._id.should.equal subFolder._id
  81. path.mongo.should.equal "rootFolder.0.folders.1.folders.0"
  82. done()
  83. describe 'finding a file', ->
  84. it 'when at root', (done)->
  85. @locator.findElement {project_id:project._id, element_id:file1._id, type:"fileRefs"}, (err, foundElement, path, parentFolder)->
  86. assert(!err)
  87. foundElement._id.should.equal file1._id
  88. path.fileSystem.should.equal "/#{file1.name}"
  89. parentFolder._id.should.equal rootFolder._id
  90. path.mongo.should.equal "rootFolder.0.fileRefs.0"
  91. done()
  92. it 'when deeply nested', (done)->
  93. @locator.findElement {project_id:project._id, element_id:subSubFile._id, type:"fileRefs"}, (err, foundElement, path, parentFolder)->
  94. assert(!err)
  95. foundElement._id.should.equal subSubFile._id
  96. path.fileSystem.should.equal "/#{subFolder.name}/#{secondSubFolder.name}/#{subSubFile.name}"
  97. parentFolder._id.should.equal secondSubFolder._id
  98. path.mongo.should.equal "rootFolder.0.folders.1.folders.0.fileRefs.0"
  99. done()
  100. describe 'finding an element with wrong element type', ->
  101. it 'should add an s onto the element type', (done)->
  102. @locator.findElement {project_id:project._id, element_id:subSubDoc._id, type:"doc"}, (err, foundElement, path, parentFolder)->
  103. assert(!err)
  104. foundElement._id.should.equal subSubDoc._id
  105. done()
  106. it 'should convert file to fileRefs', (done)->
  107. @locator.findElement {project_id:project._id, element_id:file1._id, type:"fileRefs"}, (err, foundElement, path, parentFolder)->
  108. assert(!err)
  109. foundElement._id.should.equal file1._id
  110. done()
  111. describe 'should be able to take actual project as well as id', ->
  112. doc3 =
  113. _id:"123dsdj3"
  114. name:"doc3"
  115. rootFolder2 =
  116. _id : "123sddedskd"
  117. docs:[doc3]
  118. project2 =
  119. _id : "1234566"
  120. rootFolder:[rootFolder2]
  121. it 'should find doc in project', (done)->
  122. @locator.findElement {project:project2, element_id:doc3._id, type:"docs"}, (err, foundElement, path, parentFolder)->
  123. assert(!err?)
  124. foundElement._id.should.equal doc3._id
  125. path.fileSystem.should.equal "/#{doc3.name}"
  126. parentFolder._id.should.equal project2.rootFolder[0]._id
  127. path.mongo.should.equal "rootFolder.0.docs.0"
  128. done()
  129. describe 'finding root doc', ->
  130. it 'should return root doc when passed project', (done)->
  131. @locator.findRootDoc project, (err, doc)->
  132. assert !err?
  133. doc._id.should.equal rootDoc._id
  134. done()
  135. it 'should return root doc when passed project_id', (done)->
  136. @locator.findRootDoc project._id, (err, doc)->
  137. assert !err?
  138. doc._id.should.equal rootDoc._id
  139. done()
  140. describe 'finding an entity by path', (done)->
  141. it 'should take a doc path and return the element for a root level document', (done)->
  142. path = "#{doc1.name}"
  143. @locator.findElementByPath project._id, path, (err, element)->
  144. element.should.deep.equal doc1
  145. done()
  146. it 'should take a doc path and return the element for a root level document with a starting slash', (done)->
  147. path = "/#{doc1.name}"
  148. @locator.findElementByPath project._id, path, (err, element)->
  149. element.should.deep.equal doc1
  150. done()
  151. it 'should take a doc path and return the element for a nested document', (done)->
  152. path = "#{subFolder.name}/#{secondSubFolder.name}/#{subSubDoc.name}"
  153. @locator.findElementByPath project._id, path, (err, element)->
  154. element.should.deep.equal subSubDoc
  155. done()
  156. it 'should take a file path and return the element for a root level document', (done)->
  157. path = "#{file1.name}"
  158. @locator.findElementByPath project._id, path, (err, element)->
  159. element.should.deep.equal file1
  160. done()
  161. it 'should take a file path and return the element for a nested document', (done)->
  162. path = "#{subFolder.name}/#{secondSubFolder.name}/#{subSubFile.name}"
  163. @locator.findElementByPath project._id, path, (err, element)->
  164. element.should.deep.equal subSubFile
  165. done()
  166. it 'should take a file path and return the element for a nested document case insenstive', (done)->
  167. path = "#{subFolder.name.toUpperCase()}/#{secondSubFolder.name.toUpperCase()}/#{subSubFile.name.toUpperCase()}"
  168. @locator.findElementByPath project._id, path, (err, element)->
  169. element.should.deep.equal subSubFile
  170. done()
  171. it 'should take a file path and return the element for a nested folder', (done)->
  172. path = "#{subFolder.name}/#{secondSubFolder.name}"
  173. @locator.findElementByPath project._id, path, (err, element)->
  174. element.should.deep.equal secondSubFolder
  175. done()
  176. it 'should take a file path and return the root folder', (done)->
  177. @locator.findElementByPath project._id, "/", (err, element)->
  178. element.should.deep.equal rootFolder
  179. done()
  180. it 'should return an error if the file can not be found inside know folder', (done)->
  181. @locator.findElementByPath project._id, "#{subFolder.name}/#{secondSubFolder.name}/exist.txt", (err, element)->
  182. err.should.not.equal undefined
  183. assert.equal element, undefined
  184. done()
  185. it 'should return an error if the file can not be found inside unknown folder', (done)->
  186. @locator.findElementByPath project._id, "this/does/not/exist.txt", (err, element)->
  187. err.should.not.equal undefined
  188. assert.equal element, undefined
  189. done()
  190. describe 'finding a project by user_id and project name', ()->
  191. it 'should return the projet from an array case insenstive', (done)->
  192. user_id = "123jojoidns"
  193. stubbedProject = {name:"findThis"}
  194. projects = [{name:"notThis"}, {name:"wellll"}, stubbedProject, {name:"Noooo"}]
  195. Project.findAllUsersProjects = sinon.stub().callsArgWith(2, null, projects)
  196. @locator.findUsersProjectByName user_id, stubbedProject.name.toLowerCase(), (err, project)->
  197. project.should.equal stubbedProject
  198. done()
  199. it 'should search collab projects as well', (done)->
  200. user_id = "123jojoidns"
  201. stubbedProject = {name:"findThis"}
  202. projects = [{name:"notThis"}, {name:"wellll"}, {name:"Noooo"}]
  203. Project.findAllUsersProjects = sinon.stub().callsArgWith(2, null, projects, [stubbedProject])
  204. @locator.findUsersProjectByName user_id, stubbedProject.name.toLowerCase(), (err, project)->
  205. project.should.equal stubbedProject
  206. done()