ProjectEntityHandlerTests.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /* eslint-disable
  2. camelcase,
  3. max-len,
  4. no-return-assign,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS205: Consider reworking code to avoid use of IIFEs
  13. * DS206: Consider reworking classes to avoid initClass
  14. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  15. */
  16. const chai = require('chai')
  17. const { assert } = require('chai')
  18. const should = chai.should()
  19. const { expect } = chai
  20. const sinon = require('sinon')
  21. const modulePath = '../../../../app/src/Features/Project/ProjectEntityHandler'
  22. const SandboxedModule = require('sandboxed-module')
  23. const { ObjectId } = require('mongoose').Types
  24. const Errors = require('../../../../app/src/Features/Errors/Errors')
  25. describe('ProjectEntityHandler', function() {
  26. const project_id = '4eecb1c1bffa66588e0000a1'
  27. const doc_id = '4eecb1c1bffa66588e0000a2'
  28. const folder_id = '4eecaffcbffa66588e000008'
  29. const rootFolderId = '4eecaffcbffa66588e000007'
  30. const userId = 1234
  31. beforeEach(function() {
  32. let Project
  33. this.TpdsUpdateSender = {
  34. addDoc: sinon.stub().callsArg(1),
  35. addFile: sinon.stub().callsArg(1)
  36. }
  37. this.ProjectModel = Project = (function() {
  38. Project = class Project {
  39. static initClass() {
  40. this.prototype.rootFolder = [this.rootFolder]
  41. }
  42. constructor(options) {
  43. this._id = project_id
  44. this.name = 'project_name_here'
  45. this.rev = 0
  46. }
  47. }
  48. Project.initClass()
  49. return Project
  50. })()
  51. this.project = new this.ProjectModel()
  52. this.ProjectLocator = { findElement: sinon.stub() }
  53. this.DocumentUpdaterHandler = {
  54. updateProjectStructure: sinon.stub().yields()
  55. }
  56. this.callback = sinon.stub()
  57. return (this.ProjectEntityHandler = SandboxedModule.require(modulePath, {
  58. globals: {
  59. console: console
  60. },
  61. requires: {
  62. 'logger-sharelatex': (this.logger = {
  63. log: sinon.stub(),
  64. error: sinon.stub(),
  65. err() {}
  66. }),
  67. '../Docstore/DocstoreManager': (this.DocstoreManager = {}),
  68. '../../Features/DocumentUpdater/DocumentUpdaterHandler': this
  69. .DocumentUpdaterHandler,
  70. '../../models/Project': {
  71. Project: this.ProjectModel
  72. },
  73. '../Errors/Errors': Errors,
  74. './ProjectLocator': this.ProjectLocator,
  75. './ProjectGetter': (this.ProjectGetter = {}),
  76. '../ThirdPartyDataStore/TpdsUpdateSender': this.TpdsUpdateSender
  77. }
  78. }))
  79. })
  80. describe('getting folders, docs and files', function() {
  81. beforeEach(function() {
  82. this.project.rootFolder = [
  83. {
  84. docs: [
  85. (this.doc1 = {
  86. name: 'doc1',
  87. _id: 'doc1_id'
  88. })
  89. ],
  90. fileRefs: [
  91. (this.file1 = {
  92. rev: 1,
  93. _id: 'file1_id',
  94. name: 'file1'
  95. })
  96. ],
  97. folders: [
  98. (this.folder1 = {
  99. name: 'folder1',
  100. docs: [
  101. (this.doc2 = {
  102. name: 'doc2',
  103. _id: 'doc2_id'
  104. })
  105. ],
  106. fileRefs: [
  107. (this.file2 = {
  108. rev: 2,
  109. name: 'file2',
  110. _id: 'file2_id'
  111. })
  112. ],
  113. folders: []
  114. })
  115. ]
  116. }
  117. ]
  118. this.ProjectGetter.getProjectWithoutDocLines = sinon
  119. .stub()
  120. .yields(null, this.project)
  121. })
  122. describe('getAllDocs', function() {
  123. beforeEach(function() {
  124. this.docs = [
  125. {
  126. _id: this.doc1._id,
  127. lines: (this.lines1 = ['one']),
  128. rev: (this.rev1 = 1)
  129. },
  130. {
  131. _id: this.doc2._id,
  132. lines: (this.lines2 = ['two']),
  133. rev: (this.rev2 = 2)
  134. }
  135. ]
  136. this.DocstoreManager.getAllDocs = sinon
  137. .stub()
  138. .callsArgWith(1, null, this.docs)
  139. this.ProjectEntityHandler.getAllDocs(project_id, this.callback)
  140. })
  141. it('should get the doc lines and rev from the docstore', function() {
  142. this.DocstoreManager.getAllDocs
  143. .calledWith(project_id)
  144. .should.equal(true)
  145. })
  146. it('should call the callback with the docs with the lines and rev included', function() {
  147. this.callback
  148. .calledWith(null, {
  149. '/doc1': {
  150. _id: this.doc1._id,
  151. lines: this.lines1,
  152. name: this.doc1.name,
  153. rev: this.rev1
  154. },
  155. '/folder1/doc2': {
  156. _id: this.doc2._id,
  157. lines: this.lines2,
  158. name: this.doc2.name,
  159. rev: this.rev2
  160. }
  161. })
  162. .should.equal(true)
  163. })
  164. })
  165. describe('getAllFiles', function() {
  166. beforeEach(function() {
  167. this.callback = sinon.stub()
  168. this.ProjectEntityHandler.getAllFiles(project_id, this.callback)
  169. })
  170. it('should call the callback with the files', function() {
  171. this.callback
  172. .calledWith(null, {
  173. '/file1': this.file1,
  174. '/folder1/file2': this.file2
  175. })
  176. .should.equal(true)
  177. })
  178. })
  179. describe('getAllDocPathsFromProject', function() {
  180. beforeEach(function() {
  181. this.docs = [
  182. {
  183. _id: this.doc1._id,
  184. lines: (this.lines1 = ['one']),
  185. rev: (this.rev1 = 1)
  186. },
  187. {
  188. _id: this.doc2._id,
  189. lines: (this.lines2 = ['two']),
  190. rev: (this.rev2 = 2)
  191. }
  192. ]
  193. this.callback = sinon.stub()
  194. this.ProjectEntityHandler.getAllDocPathsFromProject(
  195. this.project,
  196. this.callback
  197. )
  198. })
  199. it('should call the callback with the path for each doc_id', function() {
  200. this.expected = {}
  201. this.expected[this.doc1._id] = `/${this.doc1.name}`
  202. this.expected[this.doc2._id] = `/folder1/${this.doc2.name}`
  203. this.callback.calledWith(null, this.expected).should.equal(true)
  204. })
  205. })
  206. describe('getDocPathByProjectIdAndDocId', function() {
  207. beforeEach(function() {
  208. this.callback = sinon.stub()
  209. })
  210. it('should call the callback with the path for an existing doc id at the root level', function() {
  211. this.ProjectEntityHandler.getDocPathByProjectIdAndDocId(
  212. project_id,
  213. this.doc1._id,
  214. this.callback
  215. )
  216. this.callback.calledWith(null, `/${this.doc1.name}`).should.equal(true)
  217. })
  218. it('should call the callback with the path for an existing doc id nested within a folder', function() {
  219. this.ProjectEntityHandler.getDocPathByProjectIdAndDocId(
  220. project_id,
  221. this.doc2._id,
  222. this.callback
  223. )
  224. this.callback
  225. .calledWith(null, `/folder1/${this.doc2.name}`)
  226. .should.equal(true)
  227. })
  228. it('should call the callback with a NotFoundError for a non-existing doc', function() {
  229. this.ProjectEntityHandler.getDocPathByProjectIdAndDocId(
  230. project_id,
  231. 'non-existing-id',
  232. this.callback
  233. )
  234. expect(this.callback.firstCall.args[0]).to.be.an.instanceof(
  235. Errors.NotFoundError
  236. )
  237. })
  238. it('should call the callback with a NotFoundError for an existing file', function() {
  239. this.ProjectEntityHandler.getDocPathByProjectIdAndDocId(
  240. project_id,
  241. this.file1._id,
  242. this.callback
  243. )
  244. expect(this.callback.firstCall.args[0]).to.be.an.instanceof(
  245. Errors.NotFoundError
  246. )
  247. })
  248. })
  249. describe('_getAllFolders', function() {
  250. beforeEach(function() {
  251. this.callback = sinon.stub()
  252. this.ProjectEntityHandler._getAllFolders(project_id, this.callback)
  253. })
  254. it('should get the project without the docs lines', function() {
  255. this.ProjectGetter.getProjectWithoutDocLines
  256. .calledWith(project_id)
  257. .should.equal(true)
  258. })
  259. it('should call the callback with the folders', function() {
  260. this.callback
  261. .calledWith(null, {
  262. '/': this.project.rootFolder[0],
  263. '/folder1': this.folder1
  264. })
  265. .should.equal(true)
  266. })
  267. })
  268. describe('_getAllFoldersFromProject', function() {
  269. beforeEach(function() {
  270. this.callback = sinon.stub()
  271. this.ProjectEntityHandler._getAllFoldersFromProject(
  272. this.project,
  273. this.callback
  274. )
  275. })
  276. it('should call the callback with the folders', function() {
  277. this.callback
  278. .calledWith(null, {
  279. '/': this.project.rootFolder[0],
  280. '/folder1': this.folder1
  281. })
  282. .should.equal(true)
  283. })
  284. })
  285. })
  286. describe('getDoc', function() {
  287. beforeEach(function() {
  288. this.lines = ['mock', 'doc', 'lines']
  289. this.rev = 5
  290. this.version = 42
  291. this.ranges = { mock: 'ranges' }
  292. this.DocstoreManager.getDoc = sinon
  293. .stub()
  294. .callsArgWith(3, null, this.lines, this.rev, this.version, this.ranges)
  295. this.ProjectEntityHandler.getDoc(project_id, doc_id, this.callback)
  296. })
  297. it('should call the docstore', function() {
  298. this.DocstoreManager.getDoc
  299. .calledWith(project_id, doc_id)
  300. .should.equal(true)
  301. })
  302. it('should call the callback with the lines, version and rev', function() {
  303. this.callback
  304. .calledWith(null, this.lines, this.rev, this.version, this.ranges)
  305. .should.equal(true)
  306. })
  307. })
  308. describe('promises.getDoc', function() {
  309. let result
  310. beforeEach(async function() {
  311. this.lines = ['mock', 'doc', 'lines']
  312. this.rev = 5
  313. this.version = 42
  314. this.ranges = { mock: 'ranges' }
  315. this.DocstoreManager.getDoc = sinon
  316. .stub()
  317. .callsArgWith(3, null, this.lines, this.rev, this.version, this.ranges)
  318. result = await this.ProjectEntityHandler.promises.getDoc(
  319. project_id,
  320. doc_id
  321. )
  322. })
  323. it('should call the docstore', function() {
  324. this.DocstoreManager.getDoc
  325. .calledWith(project_id, doc_id)
  326. .should.equal(true)
  327. })
  328. it('should return the lines, rev, version and ranges', function() {
  329. expect(result.lines).to.equal(this.lines)
  330. expect(result.rev).to.equal(this.rev)
  331. expect(result.version).to.equal(this.version)
  332. expect(result.ranges).to.equal(this.ranges)
  333. })
  334. })
  335. })