ClsiManagerTests.js 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  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('when the resources fail the precompile check', function() {
  204. beforeEach(function() {
  205. this.ClsiFormatChecker.checkRecoursesForProblems = sinon
  206. .stub()
  207. .callsArgWith(1, new Error('failed'))
  208. this.ClsiManager._postToClsi = sinon.stub().callsArgWith(4, null, {
  209. compile: {
  210. status: (this.status = 'failure')
  211. }
  212. })
  213. this.ClsiManager.sendRequest(
  214. this.project_id,
  215. this.user_id,
  216. {},
  217. this.callback
  218. )
  219. })
  220. it('should call the callback only once', function() {
  221. this.callback.calledOnce.should.equal(true)
  222. })
  223. it('should call the callback with an error', function() {
  224. this.callback.should.have.been.calledWith(sinon.match.instanceOf(Error))
  225. })
  226. })
  227. })
  228. describe('sendExternalRequest', function() {
  229. beforeEach(function() {
  230. this.submission_id = 'submission-id'
  231. this.clsi_request = 'mock-request'
  232. this.ClsiCookieManager._getServerId.callsArgWith(1, null, 'clsi3')
  233. })
  234. describe('with a successful compile', function() {
  235. beforeEach(function() {
  236. this.ClsiManager._postToClsi = sinon.stub().callsArgWith(4, null, {
  237. compile: {
  238. status: (this.status = 'success'),
  239. outputFiles: [
  240. {
  241. url: `${this.settings.apis.clsi.url}/project/${
  242. this.submission_id
  243. }/build/1234/output/output.pdf`,
  244. path: 'output.pdf',
  245. type: 'pdf',
  246. build: 1234
  247. },
  248. {
  249. url: `${this.settings.apis.clsi.url}/project/${
  250. this.submission_id
  251. }/build/1234/output/output.log`,
  252. path: 'output.log',
  253. type: 'log',
  254. build: 1234
  255. }
  256. ]
  257. }
  258. })
  259. this.ClsiManager.sendExternalRequest(
  260. this.submission_id,
  261. this.clsi_request,
  262. { compileGroup: 'standard' },
  263. this.callback
  264. )
  265. })
  266. it('should send the request to the CLSI', function() {
  267. this.ClsiManager._postToClsi
  268. .calledWith(this.submission_id, null, this.clsi_request, 'standard')
  269. .should.equal(true)
  270. })
  271. it('should call the callback with the status and output files', function() {
  272. const outputFiles = [
  273. {
  274. url: `/project/${this.submission_id}/build/1234/output/output.pdf`,
  275. path: 'output.pdf',
  276. type: 'pdf',
  277. build: 1234
  278. },
  279. {
  280. url: `/project/${this.submission_id}/build/1234/output/output.log`,
  281. path: 'output.log',
  282. type: 'log',
  283. build: 1234
  284. }
  285. ]
  286. this.callback
  287. .calledWith(null, this.status, outputFiles)
  288. .should.equal(true)
  289. })
  290. })
  291. describe('with a failed compile', function() {
  292. beforeEach(function() {
  293. this.ClsiManager._postToClsi = sinon.stub().callsArgWith(4, null, {
  294. compile: {
  295. status: (this.status = 'failure')
  296. }
  297. })
  298. this.ClsiManager.sendExternalRequest(
  299. this.submission_id,
  300. this.clsi_request,
  301. {},
  302. this.callback
  303. )
  304. })
  305. it('should call the callback with a failure status', function() {
  306. this.callback.calledWith(null, this.status).should.equal(true)
  307. })
  308. })
  309. describe('when the resources fail the precompile check', function() {
  310. beforeEach(function() {
  311. this.ClsiFormatChecker.checkRecoursesForProblems = sinon
  312. .stub()
  313. .callsArgWith(1, new Error('failed'))
  314. this.ClsiManager._postToClsi = sinon.stub().callsArgWith(4, null, {
  315. compile: {
  316. status: (this.status = 'failure')
  317. }
  318. })
  319. this.ClsiManager.sendExternalRequest(
  320. this.submission_id,
  321. this.clsi_request,
  322. {},
  323. this.callback
  324. )
  325. })
  326. it('should call the callback only once', function() {
  327. this.callback.calledOnce.should.equal(true)
  328. })
  329. it('should call the callback with an error', function() {
  330. this.callback.should.have.been.calledWith(sinon.match.instanceOf(Error))
  331. })
  332. })
  333. })
  334. describe('deleteAuxFiles', function() {
  335. beforeEach(function() {
  336. this.ClsiManager._makeRequest = sinon.stub().callsArg(2)
  337. this.DocumentUpdaterHandler.clearProjectState = sinon.stub().callsArg(1)
  338. })
  339. describe('with the standard compileGroup', function() {
  340. beforeEach(function() {
  341. this.ClsiManager.deleteAuxFiles(
  342. this.project_id,
  343. this.user_id,
  344. { compileGroup: 'standard' },
  345. this.callback
  346. )
  347. })
  348. it('should call the delete method in the standard CLSI', function() {
  349. this.ClsiManager._makeRequest
  350. .calledWith(this.project_id, {
  351. method: 'DELETE',
  352. url: `${this.settings.apis.clsi.url}/project/${
  353. this.project_id
  354. }/user/${this.user_id}`
  355. })
  356. .should.equal(true)
  357. })
  358. it('should clear the project state from the docupdater', function() {
  359. this.DocumentUpdaterHandler.clearProjectState
  360. .calledWith(this.project_id)
  361. .should.equal(true)
  362. })
  363. it('should call the callback', function() {
  364. this.callback.called.should.equal(true)
  365. })
  366. })
  367. })
  368. describe('_buildRequest', function() {
  369. beforeEach(function() {
  370. this.project = {
  371. _id: this.project_id,
  372. compiler: (this.compiler = 'latex'),
  373. rootDoc_id: 'mock-doc-id-1',
  374. imageName: (this.image = 'mock-image-name')
  375. }
  376. this.docs = {
  377. '/main.tex': (this.doc_1 = {
  378. name: 'main.tex',
  379. _id: 'mock-doc-id-1',
  380. lines: ['Hello', 'world']
  381. }),
  382. '/chapters/chapter1.tex': (this.doc_2 = {
  383. name: 'chapter1.tex',
  384. _id: 'mock-doc-id-2',
  385. lines: ['Chapter 1']
  386. })
  387. }
  388. this.files = {
  389. '/images/image.png': (this.file_1 = {
  390. name: 'image.png',
  391. _id: 'mock-file-id-1',
  392. created: new Date()
  393. })
  394. }
  395. this.Project.findById = sinon.stub().callsArgWith(2, null, this.project)
  396. this.ProjectEntityHandler.getAllDocs = sinon
  397. .stub()
  398. .callsArgWith(1, null, this.docs)
  399. this.ProjectEntityHandler.getAllFiles = sinon
  400. .stub()
  401. .callsArgWith(1, null, this.files)
  402. this.ProjectGetter.getProject = sinon
  403. .stub()
  404. .callsArgWith(2, null, this.project)
  405. this.DocumentUpdaterHandler.flushProjectToMongo = sinon
  406. .stub()
  407. .callsArgWith(1, null)
  408. })
  409. describe('with a valid project', function() {
  410. beforeEach(function(done) {
  411. this.ClsiManager._buildRequest(
  412. this.project_id,
  413. { timeout: 100 },
  414. (err, request) => {
  415. if (err != null) {
  416. return done(err)
  417. }
  418. this.request = request
  419. done()
  420. }
  421. )
  422. })
  423. it('should get the project with the required fields', function() {
  424. this.ProjectGetter.getProject
  425. .calledWith(this.project_id, {
  426. compiler: 1,
  427. rootDoc_id: 1,
  428. imageName: 1,
  429. rootFolder: 1
  430. })
  431. .should.equal(true)
  432. })
  433. it('should flush the project to the database', function() {
  434. this.DocumentUpdaterHandler.flushProjectToMongo
  435. .calledWith(this.project_id)
  436. .should.equal(true)
  437. })
  438. it('should get all the docs', function() {
  439. this.ProjectEntityHandler.getAllDocs
  440. .calledWith(this.project_id)
  441. .should.equal(true)
  442. })
  443. it('should get all the files', function() {
  444. this.ProjectEntityHandler.getAllFiles
  445. .calledWith(this.project_id)
  446. .should.equal(true)
  447. })
  448. it('should build up the CLSI request', function() {
  449. expect(this.request).to.deep.equal({
  450. compile: {
  451. options: {
  452. compiler: this.compiler,
  453. timeout: 100,
  454. imageName: this.image,
  455. draft: false,
  456. check: undefined,
  457. syncType: undefined, // "full"
  458. syncState: undefined
  459. }, // "01234567890abcdef"
  460. rootResourcePath: 'main.tex',
  461. resources: [
  462. {
  463. path: 'main.tex',
  464. content: this.doc_1.lines.join('\n')
  465. },
  466. {
  467. path: 'chapters/chapter1.tex',
  468. content: this.doc_2.lines.join('\n')
  469. },
  470. {
  471. path: 'images/image.png',
  472. url: `${this.settings.apis.filestore.url}/project/${
  473. this.project_id
  474. }/file/${this.file_1._id}`,
  475. modified: this.file_1.created.getTime()
  476. }
  477. ]
  478. }
  479. })
  480. })
  481. })
  482. describe('with the incremental compile option', function() {
  483. beforeEach(function(done) {
  484. this.ClsiStateManager.computeHash = sinon
  485. .stub()
  486. .callsArgWith(
  487. 2,
  488. null,
  489. (this.project_state_hash = '01234567890abcdef')
  490. )
  491. this.DocumentUpdaterHandler.getProjectDocsIfMatch = sinon
  492. .stub()
  493. .callsArgWith(2, null, [
  494. { _id: this.doc_1._id, lines: this.doc_1.lines, v: 123 }
  495. ])
  496. this.ProjectEntityHandler.getAllDocPathsFromProject = sinon
  497. .stub()
  498. .callsArgWith(1, null, { 'mock-doc-id-1': 'main.tex' })
  499. this.ClsiManager._buildRequest(
  500. this.project_id,
  501. { timeout: 100, incrementalCompilesEnabled: true },
  502. (err, request) => {
  503. if (err != null) {
  504. return done(err)
  505. }
  506. this.request = request
  507. done()
  508. }
  509. )
  510. })
  511. it('should get the project with the required fields', function() {
  512. this.ProjectGetter.getProject
  513. .calledWith(this.project_id, {
  514. compiler: 1,
  515. rootDoc_id: 1,
  516. imageName: 1,
  517. rootFolder: 1
  518. })
  519. .should.equal(true)
  520. })
  521. it('should not explicitly flush the project to the database', function() {
  522. this.DocumentUpdaterHandler.flushProjectToMongo
  523. .calledWith(this.project_id)
  524. .should.equal(false)
  525. })
  526. it('should get only the live docs from the docupdater with a background flush in docupdater', function() {
  527. this.DocumentUpdaterHandler.getProjectDocsIfMatch
  528. .calledWith(this.project_id)
  529. .should.equal(true)
  530. })
  531. it('should not get any of the files', function() {
  532. this.ProjectEntityHandler.getAllFiles.called.should.equal(false)
  533. })
  534. it('should build up the CLSI request', function() {
  535. expect(this.request).to.deep.equal({
  536. compile: {
  537. options: {
  538. compiler: this.compiler,
  539. timeout: 100,
  540. imageName: this.image,
  541. draft: false,
  542. check: undefined,
  543. syncType: 'incremental',
  544. syncState: '01234567890abcdef'
  545. },
  546. rootResourcePath: 'main.tex',
  547. resources: [
  548. {
  549. path: 'main.tex',
  550. content: this.doc_1.lines.join('\n')
  551. }
  552. ]
  553. }
  554. })
  555. })
  556. describe('when the root doc is set and not in the docupdater', function() {
  557. beforeEach(function(done) {
  558. this.ClsiStateManager.computeHash = sinon
  559. .stub()
  560. .callsArgWith(
  561. 2,
  562. null,
  563. (this.project_state_hash = '01234567890abcdef')
  564. )
  565. this.DocumentUpdaterHandler.getProjectDocsIfMatch = sinon
  566. .stub()
  567. .callsArgWith(2, null, [
  568. { _id: this.doc_1._id, lines: this.doc_1.lines, v: 123 }
  569. ])
  570. this.ProjectEntityHandler.getAllDocPathsFromProject = sinon
  571. .stub()
  572. .callsArgWith(1, null, {
  573. 'mock-doc-id-1': 'main.tex',
  574. 'mock-doc-id-2': '/chapters/chapter1.tex'
  575. })
  576. this.ClsiManager._buildRequest(
  577. this.project_id,
  578. {
  579. timeout: 100,
  580. incrementalCompilesEnabled: true,
  581. rootDoc_id: 'mock-doc-id-2'
  582. },
  583. (err, request) => {
  584. if (err != null) {
  585. return done(err)
  586. }
  587. this.request = request
  588. done()
  589. }
  590. )
  591. })
  592. it('should still change the root path', function() {
  593. this.request.compile.rootResourcePath.should.equal(
  594. 'chapters/chapter1.tex'
  595. )
  596. })
  597. })
  598. })
  599. describe('when root doc override is valid', function() {
  600. beforeEach(function(done) {
  601. this.ClsiManager._buildRequest(
  602. this.project_id,
  603. { rootDoc_id: 'mock-doc-id-2' },
  604. (err, request) => {
  605. if (err != null) {
  606. return done(err)
  607. }
  608. this.request = request
  609. done()
  610. }
  611. )
  612. })
  613. it('should change root path', function() {
  614. this.request.compile.rootResourcePath.should.equal(
  615. 'chapters/chapter1.tex'
  616. )
  617. })
  618. })
  619. describe('when root doc override is invalid', function() {
  620. beforeEach(function(done) {
  621. this.ClsiManager._buildRequest(
  622. this.project_id,
  623. { rootDoc_id: 'invalid-id' },
  624. (err, request) => {
  625. if (err != null) {
  626. return done(err)
  627. }
  628. this.request = request
  629. done()
  630. }
  631. )
  632. })
  633. it('should fallback to default root doc', function() {
  634. this.request.compile.rootResourcePath.should.equal('main.tex')
  635. })
  636. })
  637. describe('when the project has an invalid compiler', function() {
  638. beforeEach(function(done) {
  639. this.project.compiler = 'context'
  640. this.ClsiManager._buildRequest(this.project, null, (err, request) => {
  641. if (err != null) {
  642. return done(err)
  643. }
  644. this.request = request
  645. done()
  646. })
  647. })
  648. it('should set the compiler to pdflatex', function() {
  649. this.request.compile.options.compiler.should.equal('pdflatex')
  650. })
  651. })
  652. describe('when there is no valid root document', function() {
  653. beforeEach(function(done) {
  654. this.project.rootDoc_id = 'not-valid'
  655. this.ClsiManager._buildRequest(this.project, null, (error, request) => {
  656. this.error = error
  657. this.request = request
  658. done()
  659. })
  660. })
  661. it('should set to main.tex', function() {
  662. this.request.compile.rootResourcePath.should.equal('main.tex')
  663. })
  664. })
  665. describe('when there is no valid root document and no main.tex document', function() {
  666. beforeEach(function() {
  667. this.project.rootDoc_id = 'not-valid'
  668. this.docs = {
  669. '/other.tex': (this.doc_1 = {
  670. name: 'other.tex',
  671. _id: 'mock-doc-id-1',
  672. lines: ['Hello', 'world']
  673. }),
  674. '/chapters/chapter1.tex': (this.doc_2 = {
  675. name: 'chapter1.tex',
  676. _id: 'mock-doc-id-2',
  677. lines: ['Chapter 1']
  678. })
  679. }
  680. this.ProjectEntityHandler.getAllDocs = sinon
  681. .stub()
  682. .callsArgWith(1, null, this.docs)
  683. this.ClsiManager._buildRequest(this.project, null, this.callback)
  684. })
  685. it('should report an error', function() {
  686. this.callback.should.have.been.calledWith(sinon.match.instanceOf(Error))
  687. })
  688. })
  689. describe('when there is no valid root document and a single document which is not main.tex', function() {
  690. beforeEach(function(done) {
  691. this.project.rootDoc_id = 'not-valid'
  692. this.docs = {
  693. '/other.tex': (this.doc_1 = {
  694. name: 'other.tex',
  695. _id: 'mock-doc-id-1',
  696. lines: ['Hello', 'world']
  697. })
  698. }
  699. this.ProjectEntityHandler.getAllDocs = sinon
  700. .stub()
  701. .callsArgWith(1, null, this.docs)
  702. this.ClsiManager._buildRequest(this.project, null, (error, request) => {
  703. this.error = error
  704. this.request = request
  705. done()
  706. })
  707. })
  708. it('should set io to the only file', function() {
  709. this.request.compile.rootResourcePath.should.equal('other.tex')
  710. })
  711. })
  712. describe('with the draft option', function() {
  713. it('should add the draft option into the request', function(done) {
  714. this.ClsiManager._buildRequest(
  715. this.project_id,
  716. { timeout: 100, draft: true },
  717. (err, request) => {
  718. if (err != null) {
  719. return done(err)
  720. }
  721. request.compile.options.draft.should.equal(true)
  722. done()
  723. }
  724. )
  725. })
  726. })
  727. })
  728. describe('_postToClsi', function() {
  729. beforeEach(function() {
  730. this.req = { mock: 'req', compile: {} }
  731. })
  732. describe('successfully', function() {
  733. beforeEach(function() {
  734. this.ClsiManager._makeRequest = sinon
  735. .stub()
  736. .callsArgWith(
  737. 2,
  738. null,
  739. { statusCode: 204 },
  740. (this.body = { mock: 'foo' })
  741. )
  742. this.ClsiManager._postToClsi(
  743. this.project_id,
  744. this.user_id,
  745. this.req,
  746. 'standard',
  747. this.callback
  748. )
  749. })
  750. it('should send the request to the CLSI', function() {
  751. const url = `${this.settings.apis.clsi.url}/project/${
  752. this.project_id
  753. }/user/${this.user_id}/compile`
  754. this.ClsiManager._makeRequest
  755. .calledWith(this.project_id, {
  756. method: 'POST',
  757. url,
  758. json: this.req
  759. })
  760. .should.equal(true)
  761. })
  762. it('should call the callback with the body and no error', function() {
  763. this.callback.calledWith(null, this.body).should.equal(true)
  764. })
  765. })
  766. describe('when the CLSI returns an error', function() {
  767. beforeEach(function() {
  768. this.ClsiManager._makeRequest = sinon
  769. .stub()
  770. .callsArgWith(
  771. 2,
  772. null,
  773. { statusCode: 500 },
  774. (this.body = { mock: 'foo' })
  775. )
  776. this.ClsiManager._postToClsi(
  777. this.project_id,
  778. this.user_id,
  779. this.req,
  780. 'standard',
  781. this.callback
  782. )
  783. })
  784. it('should call the callback with an error', function() {
  785. this.callback.should.have.been.calledWith(sinon.match.instanceOf(Error))
  786. })
  787. })
  788. })
  789. describe('wordCount', function() {
  790. beforeEach(function() {
  791. this.ClsiManager._makeRequest = sinon
  792. .stub()
  793. .callsArgWith(
  794. 2,
  795. null,
  796. { statusCode: 200 },
  797. (this.body = { mock: 'foo' })
  798. )
  799. this.ClsiManager._buildRequest = sinon.stub().callsArgWith(
  800. 2,
  801. null,
  802. (this.req = {
  803. compile: { rootResourcePath: 'rootfile.text', options: {} }
  804. })
  805. )
  806. })
  807. describe('with root file', function() {
  808. beforeEach(function() {
  809. this.ClsiManager.wordCount(
  810. this.project_id,
  811. this.user_id,
  812. false,
  813. {},
  814. this.callback
  815. )
  816. })
  817. it('should call wordCount with root file', function() {
  818. this.ClsiManager._makeRequest
  819. .calledWith(this.project_id, {
  820. method: 'GET',
  821. url: `http://clsi.example.com/project/${this.project_id}/user/${
  822. this.user_id
  823. }/wordcount`,
  824. qs: { file: 'rootfile.text', image: undefined }
  825. })
  826. .should.equal(true)
  827. })
  828. it('should call the callback', function() {
  829. this.callback.called.should.equal(true)
  830. })
  831. })
  832. describe('with param file', function() {
  833. beforeEach(function() {
  834. this.ClsiManager.wordCount(
  835. this.project_id,
  836. this.user_id,
  837. 'main.tex',
  838. {},
  839. this.callback
  840. )
  841. })
  842. it('should call wordCount with param file', function() {
  843. this.ClsiManager._makeRequest
  844. .calledWith(this.project_id, {
  845. method: 'GET',
  846. url: `http://clsi.example.com/project/${this.project_id}/user/${
  847. this.user_id
  848. }/wordcount`,
  849. qs: { file: 'main.tex', image: undefined }
  850. })
  851. .should.equal(true)
  852. })
  853. })
  854. describe('with image', function() {
  855. beforeEach(function() {
  856. this.req.compile.options.imageName = this.image =
  857. 'example.com/mock/image'
  858. this.ClsiManager.wordCount(
  859. this.project_id,
  860. this.user_id,
  861. 'main.tex',
  862. {},
  863. this.callback
  864. )
  865. })
  866. it('should call wordCount with file and image', function() {
  867. this.ClsiManager._makeRequest
  868. .calledWith(this.project_id, {
  869. method: 'GET',
  870. url: `http://clsi.example.com/project/${this.project_id}/user/${
  871. this.user_id
  872. }/wordcount`,
  873. qs: { file: 'main.tex', image: this.image }
  874. })
  875. .should.equal(true)
  876. })
  877. })
  878. })
  879. describe('_makeRequest', function() {
  880. beforeEach(function() {
  881. this.response = { there: 'something' }
  882. this.request.callsArgWith(1, null, this.response)
  883. this.opts = {
  884. method: 'SOMETHIGN',
  885. url: 'http://a place on the web'
  886. }
  887. })
  888. it('should process a request with a cookie jar', function(done) {
  889. this.ClsiManager._makeRequest(this.project_id, this.opts, () => {
  890. const args = this.request.args[0]
  891. args[0].method.should.equal(this.opts.method)
  892. args[0].url.should.equal(this.opts.url)
  893. args[0].jar.should.equal(this.jar)
  894. done()
  895. })
  896. })
  897. it('should set the cookie again on response as it might have changed', function(done) {
  898. this.ClsiManager._makeRequest(this.project_id, this.opts, () => {
  899. this.ClsiCookieManager.setServerId
  900. .calledWith(this.project_id, this.response)
  901. .should.equal(true)
  902. done()
  903. })
  904. })
  905. })
  906. describe('_makeGoogleCloudRequest', function() {
  907. beforeEach(function() {
  908. this.settings.apis.clsi_new = { url: 'https://compiles.somewhere.test' }
  909. this.response = { there: 'something' }
  910. this.request.callsArgWith(1, null, this.response)
  911. this.opts = {
  912. url: this.ClsiManager._getCompilerUrl(null, this.project_id)
  913. }
  914. })
  915. it('should change the domain on the url', function(done) {
  916. this.ClsiManager._makeNewBackendRequest(
  917. this.project_id,
  918. this.opts,
  919. () => {
  920. const args = this.request.args[0]
  921. args[0].url.should.equal(
  922. `https://compiles.somewhere.test/project/${this.project_id}`
  923. )
  924. done()
  925. }
  926. )
  927. })
  928. it('should not make a request if there is not clsi_new url', function(done) {
  929. this.settings.apis.clsi_new = undefined
  930. this.ClsiManager._makeNewBackendRequest(
  931. this.project_id,
  932. this.opts,
  933. err => {
  934. expect(err).to.equal(undefined)
  935. this.request.callCount.should.equal(0)
  936. done()
  937. }
  938. )
  939. })
  940. })
  941. })