EditorController.test.mjs 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  1. import { beforeEach, describe, expect, it, vi } from 'vitest'
  2. import sinon from 'sinon'
  3. import OError from '@overleaf/o-error'
  4. import mongodb from 'mongodb-legacy'
  5. const modulePath = '../../../../app/src/Features/Editor/EditorController'
  6. const { ObjectId } = mongodb
  7. describe('EditorController', function () {
  8. beforeEach(async function (ctx) {
  9. ctx.project_id = 'test-project-id'
  10. ctx.source = 'dropbox'
  11. ctx.user_id = new ObjectId()
  12. ctx.doc = { _id: (ctx.doc_id = 'test-doc-id') }
  13. ctx.docName = 'doc.tex'
  14. ctx.docLines = ['1234', 'dskl']
  15. ctx.file = { _id: (ctx.file_id = 'dasdkjk') }
  16. ctx.fileName = 'file.png'
  17. ctx.fsPath = '/folder/file.png'
  18. ctx.linkedFileData = { provider: 'url' }
  19. ctx.newFile = { _id: 'new-file-id' }
  20. ctx.folder_id = '123ksajdn'
  21. ctx.folder = { _id: ctx.folder_id }
  22. ctx.folderName = 'folder'
  23. ctx.callback = sinon.stub()
  24. vi.doMock(
  25. '../../../../app/src/Features/Project/ProjectEntityUpdateHandler',
  26. () => ({
  27. default: (ctx.ProjectEntityUpdateHandler = {}),
  28. })
  29. )
  30. vi.doMock(
  31. '../../../../app/src/Features/Project/ProjectOptionsHandler',
  32. () => ({
  33. default: (ctx.ProjectOptionsHandler = {
  34. setCompiler: sinon.stub().yields(),
  35. setImageName: sinon.stub().yields(),
  36. setSpellCheckLanguage: sinon.stub().yields(),
  37. }),
  38. })
  39. )
  40. vi.doMock(
  41. '../../../../app/src/Features/Project/ProjectDetailsHandler',
  42. () => ({
  43. default: (ctx.ProjectDetailsHandler = {
  44. setProjectDescription: sinon.stub().yields(),
  45. renameProject: sinon.stub().yields(),
  46. setPublicAccessLevel: sinon.stub().yields(),
  47. }),
  48. })
  49. )
  50. vi.doMock('../../../../app/src/Features/Project/ProjectDeleter', () => ({
  51. default: (ctx.ProjectDeleter = {
  52. deleteProject: sinon.stub(),
  53. }),
  54. }))
  55. vi.doMock(
  56. '../../../../app/src/Features/DocumentUpdater/DocumentUpdaterHandler',
  57. () => ({
  58. default: (ctx.DocumentUpdaterHandler = {
  59. flushDocToMongo: sinon.stub().yields(),
  60. setDocument: sinon.stub().yields(),
  61. }),
  62. })
  63. )
  64. vi.doMock(
  65. '../../../../app/src/Features/Editor/EditorRealTimeController',
  66. () => ({
  67. default: (ctx.EditorRealTimeController = {
  68. emitToRoom: sinon.stub(),
  69. }),
  70. })
  71. )
  72. ctx.EditorController = (await import(modulePath)).default
  73. })
  74. describe('addDoc', function () {
  75. beforeEach(function (ctx) {
  76. ctx.ProjectEntityUpdateHandler.addDocWithRanges = sinon
  77. .stub()
  78. .yields(null, ctx.doc, ctx.folder_id)
  79. return ctx.EditorController.addDoc(
  80. ctx.project_id,
  81. ctx.folder_id,
  82. ctx.docName,
  83. ctx.docLines,
  84. ctx.source,
  85. ctx.user_id,
  86. ctx.callback
  87. )
  88. })
  89. it('should add the doc using the project entity handler', function (ctx) {
  90. return ctx.ProjectEntityUpdateHandler.addDocWithRanges
  91. .calledWith(
  92. ctx.project_id,
  93. ctx.folder_id,
  94. ctx.docName,
  95. ctx.docLines,
  96. {},
  97. ctx.user_id,
  98. ctx.source
  99. )
  100. .should.equal(true)
  101. })
  102. it('should send the update out to the users in the project', function (ctx) {
  103. return ctx.EditorRealTimeController.emitToRoom
  104. .calledWith(
  105. ctx.project_id,
  106. 'reciveNewDoc',
  107. ctx.folder_id,
  108. ctx.doc,
  109. ctx.source,
  110. ctx.user_id
  111. )
  112. .should.equal(true)
  113. })
  114. it('calls the callback', function (ctx) {
  115. return ctx.callback.calledWith(null, ctx.doc).should.equal(true)
  116. })
  117. })
  118. describe('addFile', function () {
  119. beforeEach(function (ctx) {
  120. ctx.ProjectEntityUpdateHandler.addFile = sinon
  121. .stub()
  122. .yields(null, ctx.file, ctx.folder_id)
  123. return ctx.EditorController.addFile(
  124. ctx.project_id,
  125. ctx.folder_id,
  126. ctx.fileName,
  127. ctx.fsPath,
  128. ctx.linkedFileData,
  129. ctx.source,
  130. ctx.user_id,
  131. ctx.callback
  132. )
  133. })
  134. it('should add the folder using the project entity handler', function (ctx) {
  135. return ctx.ProjectEntityUpdateHandler.addFile
  136. .calledWith(
  137. ctx.project_id,
  138. ctx.folder_id,
  139. ctx.fileName,
  140. ctx.fsPath,
  141. ctx.linkedFileData,
  142. ctx.user_id,
  143. ctx.source
  144. )
  145. .should.equal(true)
  146. })
  147. it('should send the update of a new folder out to the users in the project', function (ctx) {
  148. return ctx.EditorRealTimeController.emitToRoom
  149. .calledWith(
  150. ctx.project_id,
  151. 'reciveNewFile',
  152. ctx.folder_id,
  153. ctx.file,
  154. ctx.source,
  155. ctx.linkedFileData,
  156. ctx.user_id
  157. )
  158. .should.equal(true)
  159. })
  160. it('calls the callback', function (ctx) {
  161. return ctx.callback.calledWith(null, ctx.file).should.equal(true)
  162. })
  163. })
  164. describe('upsertDoc', function () {
  165. beforeEach(function (ctx) {
  166. ctx.ProjectEntityUpdateHandler.upsertDoc = sinon
  167. .stub()
  168. .yields(null, ctx.doc, false)
  169. return ctx.EditorController.upsertDoc(
  170. ctx.project_id,
  171. ctx.folder_id,
  172. ctx.docName,
  173. ctx.docLines,
  174. ctx.source,
  175. ctx.user_id,
  176. ctx.callback
  177. )
  178. })
  179. it('upserts the doc using the project entity handler', function (ctx) {
  180. return ctx.ProjectEntityUpdateHandler.upsertDoc
  181. .calledWith(
  182. ctx.project_id,
  183. ctx.folder_id,
  184. ctx.docName,
  185. ctx.docLines,
  186. ctx.source
  187. )
  188. .should.equal(true)
  189. })
  190. it('returns the doc', function (ctx) {
  191. return ctx.callback.calledWith(null, ctx.doc).should.equal(true)
  192. })
  193. describe('doc does not exist', function () {
  194. beforeEach(function (ctx) {
  195. ctx.ProjectEntityUpdateHandler.upsertDoc = sinon
  196. .stub()
  197. .yields(null, ctx.doc, true)
  198. return ctx.EditorController.upsertDoc(
  199. ctx.project_id,
  200. ctx.folder_id,
  201. ctx.docName,
  202. ctx.docLines,
  203. ctx.source,
  204. ctx.user_id,
  205. ctx.callback
  206. )
  207. })
  208. it('sends an update out to users in the project', function (ctx) {
  209. return ctx.EditorRealTimeController.emitToRoom
  210. .calledWith(
  211. ctx.project_id,
  212. 'reciveNewDoc',
  213. ctx.folder_id,
  214. ctx.doc,
  215. ctx.source,
  216. ctx.user_id
  217. )
  218. .should.equal(true)
  219. })
  220. })
  221. })
  222. describe('upsertFile', function () {
  223. beforeEach(function (ctx) {
  224. ctx.ProjectEntityUpdateHandler.upsertFile = sinon
  225. .stub()
  226. .yields(null, ctx.newFile, false, ctx.file)
  227. return ctx.EditorController.upsertFile(
  228. ctx.project_id,
  229. ctx.folder_id,
  230. ctx.fileName,
  231. ctx.fsPath,
  232. ctx.linkedFileData,
  233. ctx.source,
  234. ctx.user_id,
  235. ctx.callback
  236. )
  237. })
  238. it('upserts the file using the project entity handler', function (ctx) {
  239. return ctx.ProjectEntityUpdateHandler.upsertFile
  240. .calledWith(
  241. ctx.project_id,
  242. ctx.folder_id,
  243. ctx.fileName,
  244. ctx.fsPath,
  245. ctx.linkedFileData,
  246. ctx.user_id,
  247. ctx.source
  248. )
  249. .should.equal(true)
  250. })
  251. it('returns the file', function (ctx) {
  252. return ctx.callback.calledWith(null, ctx.newFile).should.equal(true)
  253. })
  254. describe('file does not exist', function () {
  255. beforeEach(function (ctx) {
  256. ctx.ProjectEntityUpdateHandler.upsertFile = sinon
  257. .stub()
  258. .yields(null, ctx.file, true)
  259. return ctx.EditorController.upsertFile(
  260. ctx.project_id,
  261. ctx.folder_id,
  262. ctx.fileName,
  263. ctx.fsPath,
  264. ctx.linkedFileData,
  265. ctx.source,
  266. ctx.user_id,
  267. ctx.callback
  268. )
  269. })
  270. it('should send the update out to users in the project', function (ctx) {
  271. return ctx.EditorRealTimeController.emitToRoom
  272. .calledWith(
  273. ctx.project_id,
  274. 'reciveNewFile',
  275. ctx.folder_id,
  276. ctx.file,
  277. ctx.source,
  278. ctx.linkedFileData,
  279. ctx.user_id
  280. )
  281. .should.equal(true)
  282. })
  283. })
  284. })
  285. describe('upsertDocWithPath', function () {
  286. beforeEach(function (ctx) {
  287. ctx.docPath = '/folder/doc'
  288. ctx.ProjectEntityUpdateHandler.upsertDocWithPath = sinon
  289. .stub()
  290. .yields(null, ctx.doc, false, [], ctx.folder)
  291. return ctx.EditorController.upsertDocWithPath(
  292. ctx.project_id,
  293. ctx.docPath,
  294. ctx.docLines,
  295. ctx.source,
  296. ctx.user_id,
  297. ctx.callback
  298. )
  299. })
  300. it('upserts the doc using the project entity handler', function (ctx) {
  301. return ctx.ProjectEntityUpdateHandler.upsertDocWithPath
  302. .calledWith(ctx.project_id, ctx.docPath, ctx.docLines, ctx.source)
  303. .should.equal(true)
  304. })
  305. describe('doc does not exist', function () {
  306. beforeEach(function (ctx) {
  307. ctx.ProjectEntityUpdateHandler.upsertDocWithPath = sinon
  308. .stub()
  309. .yields(null, ctx.doc, true, [], ctx.folder)
  310. return ctx.EditorController.upsertDocWithPath(
  311. ctx.project_id,
  312. ctx.docPath,
  313. ctx.docLines,
  314. ctx.source,
  315. ctx.user_id,
  316. ctx.callback
  317. )
  318. })
  319. it('should send the update for the doc out to users in the project', function (ctx) {
  320. return ctx.EditorRealTimeController.emitToRoom
  321. .calledWith(
  322. ctx.project_id,
  323. 'reciveNewDoc',
  324. ctx.folder_id,
  325. ctx.doc,
  326. ctx.source,
  327. ctx.user_id
  328. )
  329. .should.equal(true)
  330. })
  331. })
  332. describe('folders required for doc do not exist', function () {
  333. beforeEach(function (ctx) {
  334. const folders = [
  335. (ctx.folderA = { _id: 2, parentFolder_id: 1 }),
  336. (ctx.folderB = { _id: 3, parentFolder_id: 2 }),
  337. ]
  338. ctx.ProjectEntityUpdateHandler.upsertDocWithPath = sinon
  339. .stub()
  340. .yields(null, ctx.doc, true, folders, ctx.folderB)
  341. return ctx.EditorController.upsertDocWithPath(
  342. ctx.project_id,
  343. ctx.docPath,
  344. ctx.docLines,
  345. ctx.source,
  346. ctx.user_id,
  347. ctx.callback
  348. )
  349. })
  350. it('should send the update for each folder to users in the project', function (ctx) {
  351. ctx.EditorRealTimeController.emitToRoom
  352. .calledWith(
  353. ctx.project_id,
  354. 'reciveNewFolder',
  355. ctx.folderA.parentFolder_id,
  356. ctx.folderA
  357. )
  358. .should.equal(true)
  359. return ctx.EditorRealTimeController.emitToRoom
  360. .calledWith(
  361. ctx.project_id,
  362. 'reciveNewFolder',
  363. ctx.folderB.parentFolder_id,
  364. ctx.folderB
  365. )
  366. .should.equal(true)
  367. })
  368. })
  369. })
  370. describe('upsertFileWithPath', function () {
  371. beforeEach(function (ctx) {
  372. ctx.filePath = '/folder/file'
  373. ctx.ProjectEntityUpdateHandler.upsertFileWithPath = sinon
  374. .stub()
  375. .yields(null, ctx.newFile, false, ctx.file, [], ctx.folder)
  376. return ctx.EditorController.upsertFileWithPath(
  377. ctx.project_id,
  378. ctx.filePath,
  379. ctx.fsPath,
  380. ctx.linkedFileData,
  381. ctx.source,
  382. ctx.user_id,
  383. ctx.callback
  384. )
  385. })
  386. it('upserts the file using the project entity handler', function (ctx) {
  387. return ctx.ProjectEntityUpdateHandler.upsertFileWithPath
  388. .calledWith(
  389. ctx.project_id,
  390. ctx.filePath,
  391. ctx.fsPath,
  392. ctx.linkedFileData,
  393. ctx.user_id,
  394. ctx.source
  395. )
  396. .should.equal(true)
  397. })
  398. describe('file does not exist', function () {
  399. beforeEach(function (ctx) {
  400. ctx.ProjectEntityUpdateHandler.upsertFileWithPath = sinon
  401. .stub()
  402. .yields(null, ctx.file, true, undefined, [], ctx.folder)
  403. return ctx.EditorController.upsertFileWithPath(
  404. ctx.project_id,
  405. ctx.filePath,
  406. ctx.fsPath,
  407. ctx.linkedFileData,
  408. ctx.source,
  409. ctx.user_id,
  410. ctx.callback
  411. )
  412. })
  413. it('should send the update for the file out to users in the project', function (ctx) {
  414. return ctx.EditorRealTimeController.emitToRoom
  415. .calledWith(
  416. ctx.project_id,
  417. 'reciveNewFile',
  418. ctx.folder_id,
  419. ctx.file,
  420. ctx.source,
  421. ctx.linkedFileData,
  422. ctx.user_id
  423. )
  424. .should.equal(true)
  425. })
  426. })
  427. describe('folders required for file do not exist', function () {
  428. beforeEach(function (ctx) {
  429. const folders = [
  430. (ctx.folderA = { _id: 2, parentFolder_id: 1 }),
  431. (ctx.folderB = { _id: 3, parentFolder_id: 2 }),
  432. ]
  433. ctx.ProjectEntityUpdateHandler.upsertFileWithPath = sinon
  434. .stub()
  435. .yields(null, ctx.file, true, undefined, folders, ctx.folderB)
  436. return ctx.EditorController.upsertFileWithPath(
  437. ctx.project_id,
  438. ctx.filePath,
  439. ctx.fsPath,
  440. ctx.linkedFileData,
  441. ctx.source,
  442. ctx.user_id,
  443. ctx.callback
  444. )
  445. })
  446. it('should send the update for each folder to users in the project', function (ctx) {
  447. ctx.EditorRealTimeController.emitToRoom
  448. .calledWith(
  449. ctx.project_id,
  450. 'reciveNewFolder',
  451. ctx.folderA.parentFolder_id,
  452. ctx.folderA
  453. )
  454. .should.equal(true)
  455. return ctx.EditorRealTimeController.emitToRoom
  456. .calledWith(
  457. ctx.project_id,
  458. 'reciveNewFolder',
  459. ctx.folderB.parentFolder_id,
  460. ctx.folderB
  461. )
  462. .should.equal(true)
  463. })
  464. })
  465. })
  466. describe('addFolder', function () {
  467. beforeEach(function (ctx) {
  468. ctx.EditorController._notifyProjectUsersOfNewFolder = sinon
  469. .stub()
  470. .yields()
  471. ctx.ProjectEntityUpdateHandler.addFolder = sinon
  472. .stub()
  473. .yields(null, ctx.folder, ctx.folder_id)
  474. return ctx.EditorController.addFolder(
  475. ctx.project_id,
  476. ctx.folder_id,
  477. ctx.folderName,
  478. ctx.source,
  479. ctx.user_id,
  480. ctx.callback
  481. )
  482. })
  483. it('should add the folder using the project entity handler', function (ctx) {
  484. return ctx.ProjectEntityUpdateHandler.addFolder
  485. .calledWith(ctx.project_id, ctx.folder_id, ctx.folderName, ctx.user_id)
  486. .should.equal(true)
  487. })
  488. it('should notifyProjectUsersOfNewFolder', function (ctx) {
  489. return ctx.EditorController._notifyProjectUsersOfNewFolder
  490. .calledWith(ctx.project_id, ctx.folder_id, ctx.folder, ctx.user_id)
  491. .should.equal(true)
  492. })
  493. it('should return the folder in the callback', function (ctx) {
  494. return ctx.callback.calledWith(null, ctx.folder).should.equal(true)
  495. })
  496. })
  497. describe('mkdirp', function () {
  498. beforeEach(function (ctx) {
  499. ctx.path = 'folder1/folder2'
  500. ctx.folders = [
  501. (ctx.folderA = { _id: 2, parentFolder_id: 1 }),
  502. (ctx.folderB = { _id: 3, parentFolder_id: 2 }),
  503. ]
  504. ctx.userId = new ObjectId().toString()
  505. ctx.EditorController._notifyProjectUsersOfNewFolders = sinon
  506. .stub()
  507. .yields()
  508. ctx.ProjectEntityUpdateHandler.mkdirp = sinon
  509. .stub()
  510. .yields(null, ctx.folders, ctx.folder)
  511. return ctx.EditorController.mkdirp(
  512. ctx.project_id,
  513. ctx.path,
  514. ctx.userId,
  515. ctx.callback
  516. )
  517. })
  518. it('should create the folder using the project entity handler', function (ctx) {
  519. return ctx.ProjectEntityUpdateHandler.mkdirp
  520. .calledWith(ctx.project_id, ctx.path, ctx.userId)
  521. .should.equal(true)
  522. })
  523. it('should notifyProjectUsersOfNewFolder', function (ctx) {
  524. return ctx.EditorController._notifyProjectUsersOfNewFolders.calledWith(
  525. ctx.project_id,
  526. ctx.folders
  527. )
  528. })
  529. it('should return the folder in the callback', function (ctx) {
  530. return ctx.callback
  531. .calledWith(null, ctx.folders, ctx.folder)
  532. .should.equal(true)
  533. })
  534. })
  535. describe('deleteEntity', function () {
  536. beforeEach(function (ctx) {
  537. ctx.entity_id = 'entity_id_here'
  538. ctx.type = 'doc'
  539. ctx.ProjectEntityUpdateHandler.deleteEntity = sinon.stub().yields()
  540. return ctx.EditorController.deleteEntity(
  541. ctx.project_id,
  542. ctx.entity_id,
  543. ctx.type,
  544. ctx.source,
  545. ctx.user_id,
  546. ctx.callback
  547. )
  548. })
  549. it('should delete the folder using the project entity handler', function (ctx) {
  550. return ctx.ProjectEntityUpdateHandler.deleteEntity
  551. .calledWith(
  552. ctx.project_id,
  553. ctx.entity_id,
  554. ctx.type,
  555. ctx.user_id,
  556. ctx.source
  557. )
  558. .should.equal(true)
  559. })
  560. it('notify users an entity has been deleted', function (ctx) {
  561. return ctx.EditorRealTimeController.emitToRoom
  562. .calledWith(ctx.project_id, 'removeEntity', ctx.entity_id, ctx.source)
  563. .should.equal(true)
  564. })
  565. })
  566. describe('deleteEntityWithPath', function () {
  567. beforeEach(function (ctx) {
  568. ctx.entity_id = 'entity_id_here'
  569. ctx.ProjectEntityUpdateHandler.deleteEntityWithPath = sinon
  570. .stub()
  571. .yields(null, ctx.entity_id)
  572. ctx.path = 'folder1/folder2'
  573. return ctx.EditorController.deleteEntityWithPath(
  574. ctx.project_id,
  575. ctx.path,
  576. ctx.source,
  577. ctx.user_id,
  578. ctx.callback
  579. )
  580. })
  581. it('should delete the folder using the project entity handler', function (ctx) {
  582. return ctx.ProjectEntityUpdateHandler.deleteEntityWithPath
  583. .calledWith(ctx.project_id, ctx.path, ctx.user_id, ctx.source)
  584. .should.equal(true)
  585. })
  586. it('notify users an entity has been deleted', function (ctx) {
  587. return ctx.EditorRealTimeController.emitToRoom
  588. .calledWith(ctx.project_id, 'removeEntity', ctx.entity_id, ctx.source)
  589. .should.equal(true)
  590. })
  591. })
  592. describe('updateProjectDescription', function () {
  593. beforeEach(function (ctx) {
  594. ctx.description = 'new description'
  595. return ctx.EditorController.updateProjectDescription(
  596. ctx.project_id,
  597. ctx.description,
  598. ctx.callback
  599. )
  600. })
  601. it('should send the new description to the project details handler', function (ctx) {
  602. return ctx.ProjectDetailsHandler.setProjectDescription
  603. .calledWith(ctx.project_id, ctx.description)
  604. .should.equal(true)
  605. })
  606. it('should notify the other clients about the updated description', function (ctx) {
  607. return ctx.EditorRealTimeController.emitToRoom
  608. .calledWith(
  609. ctx.project_id,
  610. 'projectDescriptionUpdated',
  611. ctx.description
  612. )
  613. .should.equal(true)
  614. })
  615. })
  616. describe('deleteProject', function () {
  617. beforeEach(function (ctx) {
  618. ctx.err = 'errro'
  619. ctx.ProjectDeleter.deleteProject.callsArgWith(2, ctx.err)
  620. })
  621. it('should call the project handler', async function (ctx) {
  622. await new Promise(resolve => {
  623. ctx.EditorController.deleteProject(ctx.project_id, err => {
  624. err.should.equal(ctx.err)
  625. ctx.ProjectDeleter.deleteProject
  626. .calledWith(ctx.project_id, {
  627. deletedReason: 'user',
  628. })
  629. .should.equal(true)
  630. resolve()
  631. })
  632. })
  633. })
  634. })
  635. describe('renameEntity', function () {
  636. beforeEach(async function (ctx) {
  637. await new Promise(resolve => {
  638. ctx.entity_id = 'entity_id_here'
  639. ctx.entityType = 'doc'
  640. ctx.newName = 'bobsfile.tex'
  641. ctx.ProjectEntityUpdateHandler.renameEntity = sinon.stub().yields()
  642. return ctx.EditorController.renameEntity(
  643. ctx.project_id,
  644. ctx.entity_id,
  645. ctx.entityType,
  646. ctx.newName,
  647. ctx.user_id,
  648. ctx.source,
  649. resolve
  650. )
  651. })
  652. })
  653. it('should call the project handler', function (ctx) {
  654. return ctx.ProjectEntityUpdateHandler.renameEntity
  655. .calledWith(
  656. ctx.project_id,
  657. ctx.entity_id,
  658. ctx.entityType,
  659. ctx.newName,
  660. ctx.user_id,
  661. ctx.source
  662. )
  663. .should.equal(true)
  664. })
  665. it('should emit the update to the room', function (ctx) {
  666. return ctx.EditorRealTimeController.emitToRoom
  667. .calledWith(
  668. ctx.project_id,
  669. 'reciveEntityRename',
  670. ctx.entity_id,
  671. ctx.newName
  672. )
  673. .should.equal(true)
  674. })
  675. })
  676. describe('moveEntity', function () {
  677. beforeEach(function (ctx) {
  678. ctx.entity_id = 'entity_id_here'
  679. ctx.entityType = 'doc'
  680. ctx.ProjectEntityUpdateHandler.moveEntity = sinon.stub().yields()
  681. return ctx.EditorController.moveEntity(
  682. ctx.project_id,
  683. ctx.entity_id,
  684. ctx.folder_id,
  685. ctx.entityType,
  686. ctx.user_id,
  687. ctx.source,
  688. ctx.callback
  689. )
  690. })
  691. it('should call the ProjectEntityUpdateHandler', function (ctx) {
  692. return ctx.ProjectEntityUpdateHandler.moveEntity
  693. .calledWith(
  694. ctx.project_id,
  695. ctx.entity_id,
  696. ctx.folder_id,
  697. ctx.entityType,
  698. ctx.user_id,
  699. ctx.source
  700. )
  701. .should.equal(true)
  702. })
  703. it('should emit the update to the room', function (ctx) {
  704. return ctx.EditorRealTimeController.emitToRoom
  705. .calledWith(
  706. ctx.project_id,
  707. 'reciveEntityMove',
  708. ctx.entity_id,
  709. ctx.folder_id
  710. )
  711. .should.equal(true)
  712. })
  713. it('calls the callback', function (ctx) {
  714. return ctx.callback.called.should.equal(true)
  715. })
  716. })
  717. describe('renameProject', function () {
  718. beforeEach(function (ctx) {
  719. ctx.err = 'errro'
  720. ctx.newName = 'new name here'
  721. return ctx.EditorController.renameProject(
  722. ctx.project_id,
  723. ctx.newName,
  724. ctx.callback
  725. )
  726. })
  727. it('should call the EditorController', function (ctx) {
  728. return ctx.ProjectDetailsHandler.renameProject
  729. .calledWith(ctx.project_id, ctx.newName)
  730. .should.equal(true)
  731. })
  732. it('should emit the update to the room', function (ctx) {
  733. return ctx.EditorRealTimeController.emitToRoom
  734. .calledWith(ctx.project_id, 'projectNameUpdated', ctx.newName)
  735. .should.equal(true)
  736. })
  737. })
  738. describe('setCompiler', function () {
  739. beforeEach(function (ctx) {
  740. ctx.compiler = 'latex'
  741. return ctx.EditorController.setCompiler(
  742. ctx.project_id,
  743. ctx.compiler,
  744. ctx.callback
  745. )
  746. })
  747. it('should send the new compiler and project id to the project options handler', function (ctx) {
  748. ctx.ProjectOptionsHandler.setCompiler
  749. .calledWith(ctx.project_id, ctx.compiler)
  750. .should.equal(true)
  751. return ctx.EditorRealTimeController.emitToRoom
  752. .calledWith(ctx.project_id, 'compilerUpdated', ctx.compiler)
  753. .should.equal(true)
  754. })
  755. })
  756. describe('setImageName', function () {
  757. beforeEach(function (ctx) {
  758. ctx.imageName = 'texlive-1234.5'
  759. return ctx.EditorController.setImageName(
  760. ctx.project_id,
  761. ctx.imageName,
  762. ctx.callback
  763. )
  764. })
  765. it('should send the new imageName and project id to the project options handler', function (ctx) {
  766. ctx.ProjectOptionsHandler.setImageName
  767. .calledWith(ctx.project_id, ctx.imageName)
  768. .should.equal(true)
  769. return ctx.EditorRealTimeController.emitToRoom
  770. .calledWith(ctx.project_id, 'imageNameUpdated', ctx.imageName)
  771. .should.equal(true)
  772. })
  773. })
  774. describe('setSpellCheckLanguage', function () {
  775. beforeEach(function (ctx) {
  776. ctx.languageCode = 'fr'
  777. return ctx.EditorController.setSpellCheckLanguage(
  778. ctx.project_id,
  779. ctx.languageCode,
  780. ctx.callback
  781. )
  782. })
  783. it('should send the new languageCode and project id to the project options handler', function (ctx) {
  784. ctx.ProjectOptionsHandler.setSpellCheckLanguage
  785. .calledWith(ctx.project_id, ctx.languageCode)
  786. .should.equal(true)
  787. return ctx.EditorRealTimeController.emitToRoom
  788. .calledWith(
  789. ctx.project_id,
  790. 'spellCheckLanguageUpdated',
  791. ctx.languageCode
  792. )
  793. .should.equal(true)
  794. })
  795. })
  796. describe('setPublicAccessLevel', function () {
  797. describe('when setting to private', function () {
  798. beforeEach(function (ctx) {
  799. ctx.newAccessLevel = 'private'
  800. ctx.ProjectDetailsHandler.ensureTokensArePresent = sinon.stub().yields()
  801. return ctx.EditorController.setPublicAccessLevel(
  802. ctx.project_id,
  803. ctx.newAccessLevel,
  804. ctx.callback
  805. )
  806. })
  807. it('should set the access level', function (ctx) {
  808. return ctx.ProjectDetailsHandler.setPublicAccessLevel
  809. .calledWith(ctx.project_id, ctx.newAccessLevel)
  810. .should.equal(true)
  811. })
  812. it('should broadcast the access level change', function (ctx) {
  813. return ctx.EditorRealTimeController.emitToRoom
  814. .calledWith(ctx.project_id, 'project:publicAccessLevel:changed')
  815. .should.equal(true)
  816. })
  817. it('should not ensure tokens are present for project', function (ctx) {
  818. return ctx.ProjectDetailsHandler.ensureTokensArePresent
  819. .calledWith(ctx.project_id)
  820. .should.equal(false)
  821. })
  822. })
  823. describe('when setting to tokenBased', function () {
  824. beforeEach(function (ctx) {
  825. ctx.newAccessLevel = 'tokenBased'
  826. ctx.tokens = { readOnly: 'aaa', readAndWrite: '42bbb' }
  827. ctx.ProjectDetailsHandler.ensureTokensArePresent = sinon.stub().yields()
  828. return ctx.EditorController.setPublicAccessLevel(
  829. ctx.project_id,
  830. ctx.newAccessLevel,
  831. ctx.callback
  832. )
  833. })
  834. it('should set the access level', function (ctx) {
  835. return ctx.ProjectDetailsHandler.setPublicAccessLevel
  836. .calledWith(ctx.project_id, ctx.newAccessLevel)
  837. .should.equal(true)
  838. })
  839. it('should broadcast the access level change', function (ctx) {
  840. return ctx.EditorRealTimeController.emitToRoom
  841. .calledWith(ctx.project_id, 'project:publicAccessLevel:changed')
  842. .should.equal(true)
  843. })
  844. it('should ensure tokens are present for project', function (ctx) {
  845. return ctx.ProjectDetailsHandler.ensureTokensArePresent
  846. .calledWith(ctx.project_id)
  847. .should.equal(true)
  848. })
  849. })
  850. })
  851. describe('setRootDoc', function () {
  852. beforeEach(function (ctx) {
  853. ctx.newRootDocID = '21312321321'
  854. ctx.ProjectEntityUpdateHandler.setRootDoc = sinon.stub().yields()
  855. return ctx.EditorController.setRootDoc(
  856. ctx.project_id,
  857. ctx.newRootDocID,
  858. ctx.callback
  859. )
  860. })
  861. it('should call the ProjectEntityUpdateHandler', function (ctx) {
  862. return ctx.ProjectEntityUpdateHandler.setRootDoc
  863. .calledWith(ctx.project_id, ctx.newRootDocID)
  864. .should.equal(true)
  865. })
  866. it('should emit the update to the room', function (ctx) {
  867. return ctx.EditorRealTimeController.emitToRoom
  868. .calledWith(ctx.project_id, 'rootDocUpdated', ctx.newRootDocID)
  869. .should.equal(true)
  870. })
  871. })
  872. describe('setMainBibliographyDoc', function () {
  873. describe('on success', function () {
  874. beforeEach(async function (ctx) {
  875. await new Promise(resolve => {
  876. ctx.mainBibliographyId = 'bib-doc-id'
  877. ctx.ProjectEntityUpdateHandler.setMainBibliographyDoc = sinon
  878. .stub()
  879. .yields()
  880. ctx.callback = sinon.stub().callsFake(resolve)
  881. ctx.EditorController.setMainBibliographyDoc(
  882. ctx.project_id,
  883. ctx.mainBibliographyId,
  884. ctx.callback
  885. )
  886. })
  887. })
  888. it('should forward the call to the ProjectEntityUpdateHandler', function (ctx) {
  889. expect(
  890. ctx.ProjectEntityUpdateHandler.setMainBibliographyDoc
  891. ).to.have.been.calledWith(ctx.project_id, ctx.mainBibliographyId)
  892. })
  893. it('should emit the update to the room', function (ctx) {
  894. expect(ctx.EditorRealTimeController.emitToRoom).to.have.been.calledWith(
  895. ctx.project_id,
  896. 'mainBibliographyDocUpdated',
  897. ctx.mainBibliographyId
  898. )
  899. })
  900. it('should return nothing', function (ctx) {
  901. expect(ctx.callback).to.have.been.calledWithExactly()
  902. })
  903. })
  904. describe('on error', function () {
  905. beforeEach(async function (ctx) {
  906. await new Promise(resolve => {
  907. ctx.mainBibliographyId = 'bib-doc-id'
  908. ctx.error = new Error('oh no')
  909. ctx.ProjectEntityUpdateHandler.setMainBibliographyDoc = sinon
  910. .stub()
  911. .yields(ctx.error)
  912. ctx.callback = sinon.stub().callsFake(() => resolve())
  913. ctx.EditorController.setMainBibliographyDoc(
  914. ctx.project_id,
  915. ctx.mainBibliographyId,
  916. ctx.callback
  917. )
  918. })
  919. })
  920. it('should forward the call to the ProjectEntityUpdateHandler', function (ctx) {
  921. expect(
  922. ctx.ProjectEntityUpdateHandler.setMainBibliographyDoc
  923. ).to.have.been.calledWith(ctx.project_id, ctx.mainBibliographyId)
  924. })
  925. it('should return the error', function (ctx) {
  926. expect(ctx.callback).to.have.been.calledWithExactly(ctx.error)
  927. })
  928. it('should not emit the update to the room', function (ctx) {
  929. expect(ctx.EditorRealTimeController.emitToRoom).to.not.have.been.called
  930. })
  931. })
  932. })
  933. describe('appendToDoc', function () {
  934. describe('on success', function () {
  935. beforeEach(function (ctx) {
  936. ctx.docId = 'doc-1'
  937. ctx.ProjectEntityUpdateHandler.appendToDoc = sinon
  938. .stub()
  939. .yields(null, { rev: '1' })
  940. ctx.EditorController.appendToDoc(
  941. ctx.project_id,
  942. ctx.docId,
  943. ctx.docLines,
  944. ctx.source,
  945. ctx.user_id,
  946. ctx.callback
  947. )
  948. })
  949. it('appends to the doc using the project entity handler', function (ctx) {
  950. ctx.ProjectEntityUpdateHandler.appendToDoc
  951. .calledWith(ctx.project_id, ctx.docId, ctx.docLines, ctx.source)
  952. .should.equal(true)
  953. })
  954. })
  955. describe('on error', function () {
  956. beforeEach(function (ctx) {
  957. ctx.docId = 'doc-1'
  958. ctx.ProjectEntityUpdateHandler.appendToDoc = sinon
  959. .stub()
  960. .yields(new Error('foo'))
  961. ctx.EditorController.appendToDoc(
  962. ctx.project_id,
  963. ctx.docId,
  964. ctx.docLines,
  965. ctx.source,
  966. ctx.user_id,
  967. ctx.callback
  968. )
  969. })
  970. it('tries to append to the doc using the project entity handler', function (ctx) {
  971. ctx.ProjectEntityUpdateHandler.appendToDoc
  972. .calledWith(ctx.project_id, ctx.docId, ctx.docLines, ctx.source)
  973. .should.equal(true)
  974. })
  975. it('tags the error', function (ctx) {
  976. ctx.callback.calledWith(sinon.match.instanceOf(OError))
  977. })
  978. })
  979. })
  980. })