ClsiManagerTests.js 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  1. const sinon = require('sinon')
  2. const chai = require('chai')
  3. const { expect } = chai
  4. const modulePath = '../../../../app/src/Features/Compile/ClsiManager.js'
  5. const SandboxedModule = require('sandboxed-module')
  6. describe('ClsiManager', function() {
  7. beforeEach(function() {
  8. this.jar = { cookie: 'stuff' }
  9. this.ClsiCookieManager = {
  10. getCookieJar: sinon.stub().callsArgWith(1, null, this.jar),
  11. setServerId: sinon.stub().callsArgWith(2),
  12. _getServerId: sinon.stub()
  13. }
  14. this.ClsiStateManager = {
  15. computeHash: sinon.stub().callsArgWith(2, null, '01234567890abcdef')
  16. }
  17. this.ClsiFormatChecker = {
  18. checkRecoursesForProblems: sinon.stub().callsArgWith(1)
  19. }
  20. this.Project = {}
  21. this.ProjectEntityHandler = {}
  22. this.ProjectGetter = {}
  23. this.DocumentUpdaterHandler = {
  24. getProjectDocsIfMatch: sinon.stub().callsArgWith(2, null, null)
  25. }
  26. this.logger = {
  27. log: sinon.stub(),
  28. error: sinon.stub(),
  29. err: sinon.stub(),
  30. warn: sinon.stub()
  31. }
  32. this.request = sinon.stub()
  33. this.Metrics = {
  34. Timer: class Metrics {
  35. constructor() {
  36. this.done = sinon.stub()
  37. }
  38. },
  39. inc: sinon.stub()
  40. }
  41. this.ClsiManager = SandboxedModule.require(modulePath, {
  42. globals: {
  43. console: console
  44. },
  45. requires: {
  46. 'settings-sharelatex': (this.settings = {
  47. apis: {
  48. filestore: {
  49. url: 'filestore.example.com',
  50. secret: 'secret'
  51. },
  52. clsi: {
  53. url: 'http://clsi.example.com'
  54. },
  55. clsi_priority: {
  56. url: 'https://clsipremium.example.com'
  57. }
  58. }
  59. }),
  60. '../../models/Project': {
  61. Project: this.Project
  62. },
  63. '../Project/ProjectEntityHandler': this.ProjectEntityHandler,
  64. '../Project/ProjectGetter': this.ProjectGetter,
  65. '../DocumentUpdater/DocumentUpdaterHandler': this
  66. .DocumentUpdaterHandler,
  67. './ClsiCookieManager': () => this.ClsiCookieManager,
  68. './ClsiStateManager': this.ClsiStateManager,
  69. 'logger-sharelatex': this.logger,
  70. request: this.request,
  71. './ClsiFormatChecker': this.ClsiFormatChecker,
  72. 'metrics-sharelatex': this.Metrics
  73. }
  74. })
  75. this.project_id = 'project-id'
  76. this.user_id = 'user-id'
  77. this.callback = sinon.stub()
  78. })
  79. describe('sendRequest', function() {
  80. beforeEach(function() {
  81. this.ClsiManager._buildRequest = sinon
  82. .stub()
  83. .callsArgWith(2, null, (this.request = 'mock-request'))
  84. this.ClsiCookieManager._getServerId.callsArgWith(1, null, 'clsi3')
  85. })
  86. describe('with a successful compile', function() {
  87. beforeEach(function() {
  88. this.ClsiManager._postToClsi = sinon.stub().callsArgWith(4, null, {
  89. compile: {
  90. status: (this.status = 'success'),
  91. outputFiles: [
  92. {
  93. url: `${this.settings.apis.clsi.url}/project/${
  94. this.project_id
  95. }/user/${this.user_id}/build/1234/output/output.pdf`,
  96. path: 'output.pdf',
  97. type: 'pdf',
  98. build: 1234
  99. },
  100. {
  101. url: `${this.settings.apis.clsi.url}/project/${
  102. this.project_id
  103. }/user/${this.user_id}/build/1234/output/output.log`,
  104. path: 'output.log',
  105. type: 'log',
  106. build: 1234
  107. }
  108. ]
  109. }
  110. })
  111. this.ClsiManager.sendRequest(
  112. this.project_id,
  113. this.user_id,
  114. { compileGroup: 'standard' },
  115. this.callback
  116. )
  117. })
  118. it('should build the request', function() {
  119. this.ClsiManager._buildRequest
  120. .calledWith(this.project_id)
  121. .should.equal(true)
  122. })
  123. it('should send the request to the CLSI', function() {
  124. this.ClsiManager._postToClsi
  125. .calledWith(this.project_id, this.user_id, this.request, 'standard')
  126. .should.equal(true)
  127. })
  128. it('should call the callback with the status and output files', function() {
  129. const outputFiles = [
  130. {
  131. url: `/project/${this.project_id}/user/${
  132. this.user_id
  133. }/build/1234/output/output.pdf`,
  134. path: 'output.pdf',
  135. type: 'pdf',
  136. build: 1234
  137. },
  138. {
  139. url: `/project/${this.project_id}/user/${
  140. this.user_id
  141. }/build/1234/output/output.log`,
  142. path: 'output.log',
  143. type: 'log',
  144. build: 1234
  145. }
  146. ]
  147. this.callback
  148. .calledWith(null, this.status, outputFiles)
  149. .should.equal(true)
  150. })
  151. })
  152. describe('with a failed compile', function() {
  153. beforeEach(function() {
  154. this.ClsiManager._postToClsi = sinon.stub().callsArgWith(4, null, {
  155. compile: {
  156. status: (this.status = 'failure')
  157. }
  158. })
  159. this.ClsiManager.sendRequest(
  160. this.project_id,
  161. this.user_id,
  162. {},
  163. this.callback
  164. )
  165. })
  166. it('should call the callback with a failure status', function() {
  167. this.callback.calledWith(null, this.status).should.equal(true)
  168. })
  169. })
  170. describe('with a sync conflict', function() {
  171. beforeEach(function() {
  172. this.ClsiManager.sendRequestOnce = sinon.stub()
  173. this.ClsiManager.sendRequestOnce
  174. .withArgs(this.project_id, this.user_id, { syncType: 'full' })
  175. .callsArgWith(3, null, (this.status = 'success'))
  176. this.ClsiManager.sendRequestOnce
  177. .withArgs(this.project_id, this.user_id, {})
  178. .callsArgWith(3, null, 'conflict')
  179. this.ClsiManager.sendRequest(
  180. this.project_id,
  181. this.user_id,
  182. {},
  183. this.callback
  184. )
  185. })
  186. it('should call the sendRequestOnce method twice', function() {
  187. this.ClsiManager.sendRequestOnce.calledTwice.should.equal(true)
  188. })
  189. it('should call the sendRequestOnce method with syncType:full', function() {
  190. this.ClsiManager.sendRequestOnce
  191. .calledWith(this.project_id, this.user_id, { syncType: 'full' })
  192. .should.equal(true)
  193. })
  194. it('should call the sendRequestOnce method without syncType:full', function() {
  195. this.ClsiManager.sendRequestOnce
  196. .calledWith(this.project_id, this.user_id, {})
  197. .should.equal(true)
  198. })
  199. it('should call the callback with a success status', function() {
  200. this.callback.calledWith(null, this.status).should.equal(true)
  201. })
  202. })
  203. describe('with an unavailable response', function() {
  204. beforeEach(function() {
  205. this.ClsiManager.sendRequestOnce = sinon.stub()
  206. this.ClsiManager.sendRequestOnce
  207. .withArgs(this.project_id, this.user_id, {
  208. syncType: 'full',
  209. forceNewClsiServer: true
  210. })
  211. .callsArgWith(3, null, (this.status = 'success'))
  212. this.ClsiManager.sendRequestOnce
  213. .withArgs(this.project_id, this.user_id, {})
  214. .callsArgWith(3, null, 'unavailable')
  215. this.ClsiManager.sendRequest(
  216. this.project_id,
  217. this.user_id,
  218. {},
  219. this.callback
  220. )
  221. })
  222. it('should call the sendRequestOnce method twice', function() {
  223. this.ClsiManager.sendRequestOnce.calledTwice.should.equal(true)
  224. })
  225. it('should call the sendRequestOnce method with forceNewClsiServer:true', function() {
  226. this.ClsiManager.sendRequestOnce
  227. .calledWith(this.project_id, this.user_id, {
  228. forceNewClsiServer: true,
  229. syncType: 'full'
  230. })
  231. .should.equal(true)
  232. })
  233. it('should call the sendRequestOnce method without forceNewClsiServer:true', function() {
  234. this.ClsiManager.sendRequestOnce
  235. .calledWith(this.project_id, this.user_id, {})
  236. .should.equal(true)
  237. })
  238. it('should call the callback with a success status', function() {
  239. this.callback.calledWith(null, this.status).should.equal(true)
  240. })
  241. })
  242. describe('when the resources fail the precompile check', function() {
  243. beforeEach(function() {
  244. this.ClsiFormatChecker.checkRecoursesForProblems = sinon
  245. .stub()
  246. .callsArgWith(1, new Error('failed'))
  247. this.ClsiManager._postToClsi = sinon.stub().callsArgWith(4, null, {
  248. compile: {
  249. status: (this.status = 'failure')
  250. }
  251. })
  252. this.ClsiManager.sendRequest(
  253. this.project_id,
  254. this.user_id,
  255. {},
  256. this.callback
  257. )
  258. })
  259. it('should call the callback only once', function() {
  260. this.callback.calledOnce.should.equal(true)
  261. })
  262. it('should call the callback with an error', function() {
  263. this.callback.should.have.been.calledWith(sinon.match.instanceOf(Error))
  264. })
  265. })
  266. })
  267. describe('sendExternalRequest', function() {
  268. beforeEach(function() {
  269. this.submission_id = 'submission-id'
  270. this.clsi_request = 'mock-request'
  271. this.ClsiCookieManager._getServerId.callsArgWith(1, null, 'clsi3')
  272. })
  273. describe('with a successful compile', function() {
  274. beforeEach(function() {
  275. this.ClsiManager._postToClsi = sinon.stub().callsArgWith(4, null, {
  276. compile: {
  277. status: (this.status = 'success'),
  278. outputFiles: [
  279. {
  280. url: `${this.settings.apis.clsi.url}/project/${
  281. this.submission_id
  282. }/build/1234/output/output.pdf`,
  283. path: 'output.pdf',
  284. type: 'pdf',
  285. build: 1234
  286. },
  287. {
  288. url: `${this.settings.apis.clsi.url}/project/${
  289. this.submission_id
  290. }/build/1234/output/output.log`,
  291. path: 'output.log',
  292. type: 'log',
  293. build: 1234
  294. }
  295. ]
  296. }
  297. })
  298. this.ClsiManager.sendExternalRequest(
  299. this.submission_id,
  300. this.clsi_request,
  301. { compileGroup: 'standard' },
  302. this.callback
  303. )
  304. })
  305. it('should send the request to the CLSI', function() {
  306. this.ClsiManager._postToClsi
  307. .calledWith(this.submission_id, null, this.clsi_request, 'standard')
  308. .should.equal(true)
  309. })
  310. it('should call the callback with the status and output files', function() {
  311. const outputFiles = [
  312. {
  313. url: `/project/${this.submission_id}/build/1234/output/output.pdf`,
  314. path: 'output.pdf',
  315. type: 'pdf',
  316. build: 1234
  317. },
  318. {
  319. url: `/project/${this.submission_id}/build/1234/output/output.log`,
  320. path: 'output.log',
  321. type: 'log',
  322. build: 1234
  323. }
  324. ]
  325. this.callback
  326. .calledWith(null, this.status, outputFiles)
  327. .should.equal(true)
  328. })
  329. })
  330. describe('with a failed compile', function() {
  331. beforeEach(function() {
  332. this.ClsiManager._postToClsi = sinon.stub().callsArgWith(4, null, {
  333. compile: {
  334. status: (this.status = 'failure')
  335. }
  336. })
  337. this.ClsiManager.sendExternalRequest(
  338. this.submission_id,
  339. this.clsi_request,
  340. {},
  341. this.callback
  342. )
  343. })
  344. it('should call the callback with a failure status', function() {
  345. this.callback.calledWith(null, this.status).should.equal(true)
  346. })
  347. })
  348. describe('when the resources fail the precompile check', function() {
  349. beforeEach(function() {
  350. this.ClsiFormatChecker.checkRecoursesForProblems = sinon
  351. .stub()
  352. .callsArgWith(1, new Error('failed'))
  353. this.ClsiManager._postToClsi = sinon.stub().callsArgWith(4, null, {
  354. compile: {
  355. status: (this.status = 'failure')
  356. }
  357. })
  358. this.ClsiManager.sendExternalRequest(
  359. this.submission_id,
  360. this.clsi_request,
  361. {},
  362. this.callback
  363. )
  364. })
  365. it('should call the callback only once', function() {
  366. this.callback.calledOnce.should.equal(true)
  367. })
  368. it('should call the callback with an error', function() {
  369. this.callback.should.have.been.calledWith(sinon.match.instanceOf(Error))
  370. })
  371. })
  372. })
  373. describe('deleteAuxFiles', function() {
  374. beforeEach(function() {
  375. this.ClsiManager._makeRequest = sinon.stub().callsArg(2)
  376. this.DocumentUpdaterHandler.clearProjectState = sinon.stub().callsArg(1)
  377. })
  378. describe('with the standard compileGroup', function() {
  379. beforeEach(function() {
  380. this.ClsiManager.deleteAuxFiles(
  381. this.project_id,
  382. this.user_id,
  383. { compileGroup: 'standard' },
  384. this.callback
  385. )
  386. })
  387. it('should call the delete method in the standard CLSI', function() {
  388. this.ClsiManager._makeRequest
  389. .calledWith(this.project_id, {
  390. method: 'DELETE',
  391. url: `${this.settings.apis.clsi.url}/project/${
  392. this.project_id
  393. }/user/${this.user_id}`
  394. })
  395. .should.equal(true)
  396. })
  397. it('should clear the project state from the docupdater', function() {
  398. this.DocumentUpdaterHandler.clearProjectState
  399. .calledWith(this.project_id)
  400. .should.equal(true)
  401. })
  402. it('should call the callback', function() {
  403. this.callback.called.should.equal(true)
  404. })
  405. })
  406. })
  407. describe('_buildRequest', function() {
  408. beforeEach(function() {
  409. this.project = {
  410. _id: this.project_id,
  411. compiler: (this.compiler = 'latex'),
  412. rootDoc_id: 'mock-doc-id-1',
  413. imageName: (this.image = 'mock-image-name')
  414. }
  415. this.docs = {
  416. '/main.tex': (this.doc_1 = {
  417. name: 'main.tex',
  418. _id: 'mock-doc-id-1',
  419. lines: ['Hello', 'world']
  420. }),
  421. '/chapters/chapter1.tex': (this.doc_2 = {
  422. name: 'chapter1.tex',
  423. _id: 'mock-doc-id-2',
  424. lines: ['Chapter 1']
  425. })
  426. }
  427. this.files = {
  428. '/images/image.png': (this.file_1 = {
  429. name: 'image.png',
  430. _id: 'mock-file-id-1',
  431. created: new Date()
  432. })
  433. }
  434. this.Project.findById = sinon.stub().callsArgWith(2, null, this.project)
  435. this.ProjectEntityHandler.getAllDocs = sinon
  436. .stub()
  437. .callsArgWith(1, null, this.docs)
  438. this.ProjectEntityHandler.getAllFiles = sinon
  439. .stub()
  440. .callsArgWith(1, null, this.files)
  441. this.ProjectGetter.getProject = sinon
  442. .stub()
  443. .callsArgWith(2, null, this.project)
  444. this.DocumentUpdaterHandler.flushProjectToMongo = sinon
  445. .stub()
  446. .callsArgWith(1, null)
  447. })
  448. describe('with a valid project', function() {
  449. beforeEach(function(done) {
  450. this.ClsiManager._buildRequest(
  451. this.project_id,
  452. { timeout: 100 },
  453. (err, request) => {
  454. if (err != null) {
  455. return done(err)
  456. }
  457. this.request = request
  458. done()
  459. }
  460. )
  461. })
  462. it('should get the project with the required fields', function() {
  463. this.ProjectGetter.getProject
  464. .calledWith(this.project_id, {
  465. compiler: 1,
  466. rootDoc_id: 1,
  467. imageName: 1,
  468. rootFolder: 1
  469. })
  470. .should.equal(true)
  471. })
  472. it('should flush the project to the database', function() {
  473. this.DocumentUpdaterHandler.flushProjectToMongo
  474. .calledWith(this.project_id)
  475. .should.equal(true)
  476. })
  477. it('should get all the docs', function() {
  478. this.ProjectEntityHandler.getAllDocs
  479. .calledWith(this.project_id)
  480. .should.equal(true)
  481. })
  482. it('should get all the files', function() {
  483. this.ProjectEntityHandler.getAllFiles
  484. .calledWith(this.project_id)
  485. .should.equal(true)
  486. })
  487. it('should build up the CLSI request', function() {
  488. expect(this.request).to.deep.equal({
  489. compile: {
  490. options: {
  491. compiler: this.compiler,
  492. timeout: 100,
  493. imageName: this.image,
  494. draft: false,
  495. check: undefined,
  496. syncType: undefined, // "full"
  497. syncState: undefined
  498. }, // "01234567890abcdef"
  499. rootResourcePath: 'main.tex',
  500. resources: [
  501. {
  502. path: 'main.tex',
  503. content: this.doc_1.lines.join('\n')
  504. },
  505. {
  506. path: 'chapters/chapter1.tex',
  507. content: this.doc_2.lines.join('\n')
  508. },
  509. {
  510. path: 'images/image.png',
  511. url: `${this.settings.apis.filestore.url}/project/${
  512. this.project_id
  513. }/file/${this.file_1._id}`,
  514. modified: this.file_1.created.getTime()
  515. }
  516. ]
  517. }
  518. })
  519. })
  520. })
  521. describe('with the incremental compile option', function() {
  522. beforeEach(function(done) {
  523. this.ClsiStateManager.computeHash = sinon
  524. .stub()
  525. .callsArgWith(
  526. 2,
  527. null,
  528. (this.project_state_hash = '01234567890abcdef')
  529. )
  530. this.DocumentUpdaterHandler.getProjectDocsIfMatch = sinon
  531. .stub()
  532. .callsArgWith(2, null, [
  533. { _id: this.doc_1._id, lines: this.doc_1.lines, v: 123 }
  534. ])
  535. this.ProjectEntityHandler.getAllDocPathsFromProject = sinon
  536. .stub()
  537. .callsArgWith(1, null, { 'mock-doc-id-1': 'main.tex' })
  538. this.ClsiManager._buildRequest(
  539. this.project_id,
  540. { timeout: 100, incrementalCompilesEnabled: true },
  541. (err, request) => {
  542. if (err != null) {
  543. return done(err)
  544. }
  545. this.request = request
  546. done()
  547. }
  548. )
  549. })
  550. it('should get the project with the required fields', function() {
  551. this.ProjectGetter.getProject
  552. .calledWith(this.project_id, {
  553. compiler: 1,
  554. rootDoc_id: 1,
  555. imageName: 1,
  556. rootFolder: 1
  557. })
  558. .should.equal(true)
  559. })
  560. it('should not explicitly flush the project to the database', function() {
  561. this.DocumentUpdaterHandler.flushProjectToMongo
  562. .calledWith(this.project_id)
  563. .should.equal(false)
  564. })
  565. it('should get only the live docs from the docupdater with a background flush in docupdater', function() {
  566. this.DocumentUpdaterHandler.getProjectDocsIfMatch
  567. .calledWith(this.project_id)
  568. .should.equal(true)
  569. })
  570. it('should not get any of the files', function() {
  571. this.ProjectEntityHandler.getAllFiles.called.should.equal(false)
  572. })
  573. it('should build up the CLSI request', function() {
  574. expect(this.request).to.deep.equal({
  575. compile: {
  576. options: {
  577. compiler: this.compiler,
  578. timeout: 100,
  579. imageName: this.image,
  580. draft: false,
  581. check: undefined,
  582. syncType: 'incremental',
  583. syncState: '01234567890abcdef'
  584. },
  585. rootResourcePath: 'main.tex',
  586. resources: [
  587. {
  588. path: 'main.tex',
  589. content: this.doc_1.lines.join('\n')
  590. }
  591. ]
  592. }
  593. })
  594. })
  595. describe('when the root doc is set and not in the docupdater', function() {
  596. beforeEach(function(done) {
  597. this.ClsiStateManager.computeHash = sinon
  598. .stub()
  599. .callsArgWith(
  600. 2,
  601. null,
  602. (this.project_state_hash = '01234567890abcdef')
  603. )
  604. this.DocumentUpdaterHandler.getProjectDocsIfMatch = sinon
  605. .stub()
  606. .callsArgWith(2, null, [
  607. { _id: this.doc_1._id, lines: this.doc_1.lines, v: 123 }
  608. ])
  609. this.ProjectEntityHandler.getAllDocPathsFromProject = sinon
  610. .stub()
  611. .callsArgWith(1, null, {
  612. 'mock-doc-id-1': 'main.tex',
  613. 'mock-doc-id-2': '/chapters/chapter1.tex'
  614. })
  615. this.ClsiManager._buildRequest(
  616. this.project_id,
  617. {
  618. timeout: 100,
  619. incrementalCompilesEnabled: true,
  620. rootDoc_id: 'mock-doc-id-2'
  621. },
  622. (err, request) => {
  623. if (err != null) {
  624. return done(err)
  625. }
  626. this.request = request
  627. done()
  628. }
  629. )
  630. })
  631. it('should still change the root path', function() {
  632. this.request.compile.rootResourcePath.should.equal(
  633. 'chapters/chapter1.tex'
  634. )
  635. })
  636. })
  637. })
  638. describe('when root doc override is valid', function() {
  639. beforeEach(function(done) {
  640. this.ClsiManager._buildRequest(
  641. this.project_id,
  642. { rootDoc_id: 'mock-doc-id-2' },
  643. (err, request) => {
  644. if (err != null) {
  645. return done(err)
  646. }
  647. this.request = request
  648. done()
  649. }
  650. )
  651. })
  652. it('should change root path', function() {
  653. this.request.compile.rootResourcePath.should.equal(
  654. 'chapters/chapter1.tex'
  655. )
  656. })
  657. })
  658. describe('when root doc override is invalid', function() {
  659. beforeEach(function(done) {
  660. this.ClsiManager._buildRequest(
  661. this.project_id,
  662. { rootDoc_id: 'invalid-id' },
  663. (err, request) => {
  664. if (err != null) {
  665. return done(err)
  666. }
  667. this.request = request
  668. done()
  669. }
  670. )
  671. })
  672. it('should fallback to default root doc', function() {
  673. this.request.compile.rootResourcePath.should.equal('main.tex')
  674. })
  675. })
  676. describe('when the project has an invalid compiler', function() {
  677. beforeEach(function(done) {
  678. this.project.compiler = 'context'
  679. this.ClsiManager._buildRequest(this.project, null, (err, request) => {
  680. if (err != null) {
  681. return done(err)
  682. }
  683. this.request = request
  684. done()
  685. })
  686. })
  687. it('should set the compiler to pdflatex', function() {
  688. this.request.compile.options.compiler.should.equal('pdflatex')
  689. })
  690. })
  691. describe('when there is no valid root document', function() {
  692. beforeEach(function(done) {
  693. this.project.rootDoc_id = 'not-valid'
  694. this.ClsiManager._buildRequest(this.project, null, (error, request) => {
  695. this.error = error
  696. this.request = request
  697. done()
  698. })
  699. })
  700. it('should set to main.tex', function() {
  701. this.request.compile.rootResourcePath.should.equal('main.tex')
  702. })
  703. })
  704. describe('when there is no valid root document and no main.tex document', function() {
  705. beforeEach(function() {
  706. this.project.rootDoc_id = 'not-valid'
  707. this.docs = {
  708. '/other.tex': (this.doc_1 = {
  709. name: 'other.tex',
  710. _id: 'mock-doc-id-1',
  711. lines: ['Hello', 'world']
  712. }),
  713. '/chapters/chapter1.tex': (this.doc_2 = {
  714. name: 'chapter1.tex',
  715. _id: 'mock-doc-id-2',
  716. lines: ['Chapter 1']
  717. })
  718. }
  719. this.ProjectEntityHandler.getAllDocs = sinon
  720. .stub()
  721. .callsArgWith(1, null, this.docs)
  722. this.ClsiManager._buildRequest(this.project, null, this.callback)
  723. })
  724. it('should report an error', function() {
  725. this.callback.should.have.been.calledWith(sinon.match.instanceOf(Error))
  726. })
  727. })
  728. describe('when there is no valid root document and a single document which is not main.tex', function() {
  729. beforeEach(function(done) {
  730. this.project.rootDoc_id = 'not-valid'
  731. this.docs = {
  732. '/other.tex': (this.doc_1 = {
  733. name: 'other.tex',
  734. _id: 'mock-doc-id-1',
  735. lines: ['Hello', 'world']
  736. })
  737. }
  738. this.ProjectEntityHandler.getAllDocs = sinon
  739. .stub()
  740. .callsArgWith(1, null, this.docs)
  741. this.ClsiManager._buildRequest(this.project, null, (error, request) => {
  742. this.error = error
  743. this.request = request
  744. done()
  745. })
  746. })
  747. it('should set io to the only file', function() {
  748. this.request.compile.rootResourcePath.should.equal('other.tex')
  749. })
  750. })
  751. describe('with the draft option', function() {
  752. it('should add the draft option into the request', function(done) {
  753. this.ClsiManager._buildRequest(
  754. this.project_id,
  755. { timeout: 100, draft: true },
  756. (err, request) => {
  757. if (err != null) {
  758. return done(err)
  759. }
  760. request.compile.options.draft.should.equal(true)
  761. done()
  762. }
  763. )
  764. })
  765. })
  766. })
  767. describe('_postToClsi', function() {
  768. beforeEach(function() {
  769. this.req = { mock: 'req', compile: {} }
  770. })
  771. describe('successfully', function() {
  772. beforeEach(function() {
  773. this.ClsiManager._makeRequest = sinon
  774. .stub()
  775. .callsArgWith(
  776. 2,
  777. null,
  778. { statusCode: 204 },
  779. (this.body = { mock: 'foo' })
  780. )
  781. this.ClsiManager._postToClsi(
  782. this.project_id,
  783. this.user_id,
  784. this.req,
  785. 'standard',
  786. this.callback
  787. )
  788. })
  789. it('should send the request to the CLSI', function() {
  790. const url = `${this.settings.apis.clsi.url}/project/${
  791. this.project_id
  792. }/user/${this.user_id}/compile`
  793. this.ClsiManager._makeRequest
  794. .calledWith(this.project_id, {
  795. method: 'POST',
  796. url,
  797. json: this.req
  798. })
  799. .should.equal(true)
  800. })
  801. it('should call the callback with the body and no error', function() {
  802. this.callback.calledWith(null, this.body).should.equal(true)
  803. })
  804. })
  805. describe('when the CLSI returns an error', function() {
  806. beforeEach(function() {
  807. this.ClsiManager._makeRequest = sinon
  808. .stub()
  809. .callsArgWith(
  810. 2,
  811. null,
  812. { statusCode: 500 },
  813. (this.body = { mock: 'foo' })
  814. )
  815. this.ClsiManager._postToClsi(
  816. this.project_id,
  817. this.user_id,
  818. this.req,
  819. 'standard',
  820. this.callback
  821. )
  822. })
  823. it('should call the callback with an error', function() {
  824. this.callback.should.have.been.calledWith(sinon.match.instanceOf(Error))
  825. })
  826. })
  827. })
  828. describe('wordCount', function() {
  829. beforeEach(function() {
  830. this.ClsiManager._makeRequest = sinon
  831. .stub()
  832. .callsArgWith(
  833. 2,
  834. null,
  835. { statusCode: 200 },
  836. (this.body = { mock: 'foo' })
  837. )
  838. this.ClsiManager._buildRequest = sinon.stub().callsArgWith(
  839. 2,
  840. null,
  841. (this.req = {
  842. compile: { rootResourcePath: 'rootfile.text', options: {} }
  843. })
  844. )
  845. })
  846. describe('with root file', function() {
  847. beforeEach(function() {
  848. this.ClsiManager.wordCount(
  849. this.project_id,
  850. this.user_id,
  851. false,
  852. {},
  853. this.callback
  854. )
  855. })
  856. it('should call wordCount with root file', function() {
  857. this.ClsiManager._makeRequest
  858. .calledWith(this.project_id, {
  859. method: 'GET',
  860. url: `http://clsi.example.com/project/${this.project_id}/user/${
  861. this.user_id
  862. }/wordcount`,
  863. qs: { file: 'rootfile.text', image: undefined }
  864. })
  865. .should.equal(true)
  866. })
  867. it('should call the callback', function() {
  868. this.callback.called.should.equal(true)
  869. })
  870. })
  871. describe('with param file', function() {
  872. beforeEach(function() {
  873. this.ClsiManager.wordCount(
  874. this.project_id,
  875. this.user_id,
  876. 'main.tex',
  877. {},
  878. this.callback
  879. )
  880. })
  881. it('should call wordCount with param file', function() {
  882. this.ClsiManager._makeRequest
  883. .calledWith(this.project_id, {
  884. method: 'GET',
  885. url: `http://clsi.example.com/project/${this.project_id}/user/${
  886. this.user_id
  887. }/wordcount`,
  888. qs: { file: 'main.tex', image: undefined }
  889. })
  890. .should.equal(true)
  891. })
  892. })
  893. describe('with image', function() {
  894. beforeEach(function() {
  895. this.req.compile.options.imageName = this.image =
  896. 'example.com/mock/image'
  897. this.ClsiManager.wordCount(
  898. this.project_id,
  899. this.user_id,
  900. 'main.tex',
  901. {},
  902. this.callback
  903. )
  904. })
  905. it('should call wordCount with file and image', function() {
  906. this.ClsiManager._makeRequest
  907. .calledWith(this.project_id, {
  908. method: 'GET',
  909. url: `http://clsi.example.com/project/${this.project_id}/user/${
  910. this.user_id
  911. }/wordcount`,
  912. qs: { file: 'main.tex', image: this.image }
  913. })
  914. .should.equal(true)
  915. })
  916. })
  917. })
  918. describe('_makeRequest', function() {
  919. beforeEach(function() {
  920. this.response = { there: 'something' }
  921. this.request.callsArgWith(1, null, this.response)
  922. this.opts = {
  923. method: 'SOMETHIGN',
  924. url: 'http://a place on the web'
  925. }
  926. })
  927. it('should process a request with a cookie jar', function(done) {
  928. this.ClsiManager._makeRequest(this.project_id, this.opts, () => {
  929. const args = this.request.args[0]
  930. args[0].method.should.equal(this.opts.method)
  931. args[0].url.should.equal(this.opts.url)
  932. args[0].jar.should.equal(this.jar)
  933. done()
  934. })
  935. })
  936. it('should set the cookie again on response as it might have changed', function(done) {
  937. this.ClsiManager._makeRequest(this.project_id, this.opts, () => {
  938. this.ClsiCookieManager.setServerId
  939. .calledWith(this.project_id, this.response)
  940. .should.equal(true)
  941. done()
  942. })
  943. })
  944. })
  945. describe('_makeGoogleCloudRequest', function() {
  946. beforeEach(function() {
  947. this.settings.apis.clsi_new = { url: 'https://compiles.somewhere.test' }
  948. this.response = { there: 'something' }
  949. this.request.callsArgWith(1, null, this.response)
  950. this.opts = {
  951. url: this.ClsiManager._getCompilerUrl(null, this.project_id)
  952. }
  953. })
  954. it('should change the domain on the url', function(done) {
  955. this.ClsiManager._makeNewBackendRequest(
  956. this.project_id,
  957. this.opts,
  958. () => {
  959. const args = this.request.args[0]
  960. args[0].url.should.equal(
  961. `https://compiles.somewhere.test/project/${this.project_id}`
  962. )
  963. done()
  964. }
  965. )
  966. })
  967. it('should not make a request if there is not clsi_new url', function(done) {
  968. this.settings.apis.clsi_new = undefined
  969. this.ClsiManager._makeNewBackendRequest(
  970. this.project_id,
  971. this.opts,
  972. err => {
  973. expect(err).to.equal(undefined)
  974. this.request.callCount.should.equal(0)
  975. done()
  976. }
  977. )
  978. })
  979. })
  980. })