ProjectEntityHandlerTests.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. const { expect } = require('chai')
  2. const sinon = require('sinon')
  3. const modulePath = '../../../../app/src/Features/Project/ProjectEntityHandler'
  4. const SandboxedModule = require('sandboxed-module')
  5. const Errors = require('../../../../app/src/Features/Errors/Errors')
  6. describe('ProjectEntityHandler', function () {
  7. const projectId = '4eecb1c1bffa66588e0000a1'
  8. const docId = '4eecb1c1bffa66588e0000a2'
  9. beforeEach(function () {
  10. this.TpdsUpdateSender = {
  11. addDoc: sinon.stub().callsArg(1),
  12. addFile: sinon.stub().callsArg(1),
  13. }
  14. this.ProjectModel = class Project {
  15. constructor(options) {
  16. this._id = projectId
  17. this.name = 'project_name_here'
  18. this.rev = 0
  19. this.rootFolder = [this.rootFolder]
  20. }
  21. }
  22. this.project = new this.ProjectModel()
  23. this.ProjectLocator = { findElement: sinon.stub() }
  24. this.DocumentUpdaterHandler = {
  25. updateProjectStructure: sinon.stub().yields(),
  26. }
  27. this.callback = sinon.stub()
  28. this.ProjectEntityHandler = SandboxedModule.require(modulePath, {
  29. requires: {
  30. '../Docstore/DocstoreManager': (this.DocstoreManager = {
  31. promises: {},
  32. }),
  33. '../../Features/DocumentUpdater/DocumentUpdaterHandler':
  34. this.DocumentUpdaterHandler,
  35. '../../models/Project': {
  36. Project: this.ProjectModel,
  37. },
  38. './ProjectLocator': this.ProjectLocator,
  39. './ProjectGetter': (this.ProjectGetter = { promises: {} }),
  40. '../ThirdPartyDataStore/TpdsUpdateSender': this.TpdsUpdateSender,
  41. },
  42. })
  43. })
  44. describe('getting folders, docs and files', function () {
  45. beforeEach(function () {
  46. this.project.rootFolder = [
  47. {
  48. docs: [
  49. (this.doc1 = {
  50. name: 'doc1',
  51. _id: 'doc1_id',
  52. }),
  53. ],
  54. fileRefs: [
  55. (this.file1 = {
  56. rev: 1,
  57. _id: 'file1_id',
  58. name: 'file1',
  59. }),
  60. ],
  61. folders: [
  62. (this.folder1 = {
  63. name: 'folder1',
  64. docs: [
  65. (this.doc2 = {
  66. name: 'doc2',
  67. _id: 'doc2_id',
  68. }),
  69. ],
  70. fileRefs: [
  71. (this.file2 = {
  72. rev: 2,
  73. name: 'file2',
  74. _id: 'file2_id',
  75. }),
  76. ],
  77. folders: [],
  78. }),
  79. ],
  80. },
  81. ]
  82. this.ProjectGetter.promises.getProjectWithoutDocLines = sinon
  83. .stub()
  84. .resolves(this.project)
  85. })
  86. describe('getAllDocs', function () {
  87. let fetchedDocs
  88. beforeEach(async function () {
  89. this.docs = [
  90. {
  91. _id: this.doc1._id,
  92. lines: (this.lines1 = ['one']),
  93. rev: (this.rev1 = 1),
  94. },
  95. {
  96. _id: this.doc2._id,
  97. lines: (this.lines2 = ['two']),
  98. rev: (this.rev2 = 2),
  99. },
  100. ]
  101. this.DocstoreManager.promises.getAllDocs = sinon
  102. .stub()
  103. .resolves(this.docs)
  104. fetchedDocs =
  105. await this.ProjectEntityHandler.promises.getAllDocs(projectId)
  106. })
  107. it('should get the doc lines and rev from the docstore', function () {
  108. this.DocstoreManager.promises.getAllDocs
  109. .calledWith(projectId)
  110. .should.equal(true)
  111. })
  112. it('should call the callback with the docs with the lines and rev included', function () {
  113. expect(fetchedDocs).to.deep.equal({
  114. '/doc1': {
  115. _id: this.doc1._id,
  116. lines: this.lines1,
  117. name: this.doc1.name,
  118. rev: this.rev1,
  119. folder: this.project.rootFolder[0],
  120. },
  121. '/folder1/doc2': {
  122. _id: this.doc2._id,
  123. lines: this.lines2,
  124. name: this.doc2.name,
  125. rev: this.rev2,
  126. folder: this.folder1,
  127. },
  128. })
  129. })
  130. })
  131. describe('getAllFiles', function () {
  132. let allFiles
  133. beforeEach(async function () {
  134. this.callback = sinon.stub()
  135. allFiles = await this.ProjectEntityHandler.promises.getAllFiles(
  136. projectId,
  137. this.callback
  138. )
  139. })
  140. it('should call the callback with the files', function () {
  141. expect(allFiles).to.deep.equal({
  142. '/file1': { ...this.file1, folder: this.project.rootFolder[0] },
  143. '/folder1/file2': { ...this.file2, folder: this.folder1 },
  144. })
  145. })
  146. })
  147. describe('getAllDocPathsFromProject', function () {
  148. beforeEach(function () {
  149. this.docs = [
  150. {
  151. _id: this.doc1._id,
  152. lines: (this.lines1 = ['one']),
  153. rev: (this.rev1 = 1),
  154. },
  155. {
  156. _id: this.doc2._id,
  157. lines: (this.lines2 = ['two']),
  158. rev: (this.rev2 = 2),
  159. },
  160. ]
  161. })
  162. it('should call the callback with the path for each docId', function () {
  163. const expected = {
  164. [this.doc1._id]: `/${this.doc1.name}`,
  165. [this.doc2._id]: `/folder1/${this.doc2.name}`,
  166. }
  167. expect(
  168. this.ProjectEntityHandler.getAllDocPathsFromProject(
  169. this.project,
  170. this.callback
  171. )
  172. ).to.deep.equal(expected)
  173. })
  174. })
  175. describe('getDocPathByProjectIdAndDocId', function () {
  176. it('should call the callback with the path for an existing doc id at the root level', async function () {
  177. const path =
  178. await this.ProjectEntityHandler.promises.getDocPathByProjectIdAndDocId(
  179. projectId,
  180. this.doc1._id
  181. )
  182. expect(path).to.deep.equal(`/${this.doc1.name}`)
  183. })
  184. it('should call the callback with the path for an existing doc id nested within a folder', async function () {
  185. const path =
  186. await this.ProjectEntityHandler.promises.getDocPathByProjectIdAndDocId(
  187. projectId,
  188. this.doc2._id
  189. )
  190. expect(path).to.deep.equal(`/folder1/${this.doc2.name}`)
  191. })
  192. it('should call the callback with a NotFoundError for a non-existing doc', async function () {
  193. await expect(
  194. this.ProjectEntityHandler.promises.getDocPathByProjectIdAndDocId(
  195. projectId,
  196. 'non-existing-id'
  197. )
  198. ).to.be.rejectedWith(Errors.NotFoundError)
  199. })
  200. it('should call the callback with a NotFoundError for an existing file', async function () {
  201. await expect(
  202. this.ProjectEntityHandler.promises.getDocPathByProjectIdAndDocId(
  203. projectId,
  204. this.file1._id
  205. )
  206. ).to.be.rejectedWith(Errors.NotFoundError)
  207. })
  208. })
  209. describe('_getAllFolders', async function () {
  210. let folders
  211. beforeEach(async function () {
  212. this.callback = sinon.stub()
  213. folders =
  214. await this.ProjectEntityHandler.promises._getAllFolders(projectId)
  215. })
  216. it('should get the project without the docs lines', function () {
  217. this.ProjectGetter.promises.getProjectWithoutDocLines
  218. .calledWith(projectId)
  219. .should.equal(true)
  220. })
  221. it('should call the callback with the folders', function () {
  222. expect(folders).to.deep.equal([
  223. { path: '/', folder: this.project.rootFolder[0] },
  224. { path: '/folder1', folder: this.folder1 },
  225. ])
  226. })
  227. })
  228. describe('_getAllFoldersFromProject', function () {
  229. it('should return the folders', function () {
  230. expect(
  231. this.ProjectEntityHandler._getAllFoldersFromProject(this.project)
  232. ).to.deep.equal([
  233. { path: '/', folder: this.project.rootFolder[0] },
  234. { path: '/folder1', folder: this.folder1 },
  235. ])
  236. })
  237. })
  238. })
  239. describe('with an invalid file tree', function () {
  240. beforeEach(function () {
  241. this.project.rootFolder = [
  242. {
  243. docs: [
  244. (this.doc1 = {
  245. name: null, // invalid doc name
  246. _id: 'doc1_id',
  247. }),
  248. ],
  249. fileRefs: [
  250. (this.file1 = {
  251. rev: 1,
  252. _id: 'file1_id',
  253. name: null, // invalid file name
  254. }),
  255. ],
  256. folders: [
  257. (this.folder1 = {
  258. name: null, // invalid folder name
  259. docs: [
  260. (this.doc2 = {
  261. name: 'doc2',
  262. _id: 'doc2_id',
  263. }),
  264. ],
  265. fileRefs: [
  266. (this.file2 = {
  267. rev: 2,
  268. name: 'file2',
  269. _id: 'file2_id',
  270. }),
  271. ],
  272. folders: null,
  273. }),
  274. null, // invalid folder
  275. ],
  276. },
  277. ]
  278. this.ProjectGetter.promises.getProjectWithoutDocLines = sinon
  279. .stub()
  280. .resolves(this.project)
  281. })
  282. describe('getAllDocs', function () {
  283. beforeEach(async function () {
  284. this.docs = [
  285. {
  286. _id: this.doc1._id,
  287. lines: (this.lines1 = ['one']),
  288. rev: (this.rev1 = 1),
  289. },
  290. {
  291. _id: this.doc2._id,
  292. lines: (this.lines2 = ['two']),
  293. rev: (this.rev2 = 2),
  294. },
  295. ]
  296. this.DocstoreManager.promises.getAllDocs = sinon
  297. .stub()
  298. .resolves(this.docs)
  299. })
  300. it('should call the callback with an error', async function () {
  301. await expect(this.ProjectEntityHandler.promises.getAllDocs(projectId))
  302. .to.be.rejected
  303. })
  304. })
  305. describe('getAllFiles', function () {
  306. it('should call the callback with and error', async function () {
  307. await expect(this.ProjectEntityHandler.promises.getAllFiles(projectId))
  308. .to.be.rejected
  309. })
  310. })
  311. describe('getDocPathByProjectIdAndDocId', function () {
  312. it('should call the callback with an error for an existing doc id at the root level', async function () {
  313. await expect(
  314. this.ProjectEntityHandler.promises.getDocPathByProjectIdAndDocId(
  315. projectId,
  316. this.doc1._id
  317. )
  318. ).to.be.rejectedWith(Error)
  319. })
  320. it('should call the callback with an error for an existing doc id nested within a folder', async function () {
  321. await expect(
  322. this.ProjectEntityHandler.promises.getDocPathByProjectIdAndDocId(
  323. projectId,
  324. this.doc2._id
  325. )
  326. ).to.be.rejectedWith(Error)
  327. })
  328. it('should call the callback with an error for a non-existing doc', async function () {
  329. await expect(
  330. this.ProjectEntityHandler.promises.getDocPathByProjectIdAndDocId(
  331. projectId,
  332. 'non-existing-id'
  333. )
  334. ).to.be.rejectedWith(Error)
  335. })
  336. it('should call the callback with an error for an existing file', async function () {
  337. await expect(
  338. this.ProjectEntityHandler.promises.getDocPathByProjectIdAndDocId(
  339. projectId,
  340. this.file1._id
  341. )
  342. ).to.be.rejectedWith(Error)
  343. })
  344. })
  345. describe('_getAllFolders', function () {
  346. it('should call the callback with an error', async function () {
  347. await expect(
  348. this.ProjectEntityHandler.promises._getAllFolders(projectId)
  349. ).to.be.rejected
  350. })
  351. })
  352. describe('getAllEntities', function () {
  353. beforeEach(function () {
  354. this.ProjectGetter.promises.getProject = sinon
  355. .stub()
  356. .resolves(this.project)
  357. })
  358. it('should call the callback with an error', async function () {
  359. await expect(
  360. this.ProjectEntityHandler.promises.getAllEntities(projectId)
  361. ).to.be.rejected
  362. })
  363. })
  364. describe('getAllDocPathsFromProjectById', function () {
  365. it('should call the callback with an error', async function () {
  366. await expect(
  367. this.ProjectEntityHandler.promises.getAllDocPathsFromProjectById(
  368. projectId
  369. )
  370. ).to.be.rejected
  371. })
  372. })
  373. describe('getDocPathFromProjectByDocId', function () {
  374. it('should call the callback with an error', async function () {
  375. await expect(
  376. this.ProjectEntityHandler.promises.getDocPathFromProjectByDocId(
  377. projectId,
  378. this.doc1._id
  379. )
  380. ).to.be.rejected
  381. })
  382. })
  383. })
  384. describe('getDoc', function () {
  385. beforeEach(function () {
  386. this.lines = ['mock', 'doc', 'lines']
  387. this.rev = 5
  388. this.version = 42
  389. this.ranges = { mock: 'ranges' }
  390. this.callback = sinon.stub()
  391. this.DocstoreManager.promises.getDoc = sinon.stub().resolves({
  392. lines: this.lines,
  393. rev: this.rev,
  394. version: this.version,
  395. ranges: this.ranges,
  396. })
  397. })
  398. it('should call the callback with the lines, version and rev', async function () {
  399. const doc = await this.ProjectEntityHandler.promises.getDoc(
  400. projectId,
  401. docId
  402. )
  403. this.DocstoreManager.promises.getDoc
  404. .calledWith(projectId, docId)
  405. .should.equal(true)
  406. expect(doc).to.exist
  407. })
  408. })
  409. describe('promises.getDoc', function () {
  410. let result
  411. beforeEach(async function () {
  412. this.lines = ['mock', 'doc', 'lines']
  413. this.rev = 5
  414. this.version = 42
  415. this.ranges = { mock: 'ranges' }
  416. this.DocstoreManager.promises.getDoc = sinon.stub().resolves({
  417. lines: this.lines,
  418. rev: this.rev,
  419. version: this.version,
  420. ranges: this.ranges,
  421. })
  422. result = await this.ProjectEntityHandler.promises.getDoc(projectId, docId)
  423. })
  424. it('should call the docstore', function () {
  425. this.DocstoreManager.promises.getDoc
  426. .calledWith(projectId, docId)
  427. .should.equal(true)
  428. })
  429. it('should return the lines, rev, version and ranges', function () {
  430. expect(result.lines).to.equal(this.lines)
  431. expect(result.rev).to.equal(this.rev)
  432. expect(result.version).to.equal(this.version)
  433. expect(result.ranges).to.equal(this.ranges)
  434. })
  435. })
  436. })