ClsiManagerTests.js 34 KB

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