ExportsHandlerTests.mjs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. // TODO: This file was created by bulk-decaffeinate.
  2. // Fix any style issues and re-enable lint.
  3. /*
  4. * decaffeinate suggestions:
  5. * DS102: Remove unnecessary code created because of implicit returns
  6. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  7. */
  8. import sinon from 'sinon'
  9. import esmock from 'esmock'
  10. import { expect } from 'chai'
  11. const modulePath = '../../../../app/src/Features/Exports/ExportsHandler.mjs'
  12. describe('ExportsHandler', function () {
  13. beforeEach(async function () {
  14. this.stubRequest = {}
  15. this.request = {
  16. defaults: () => {
  17. return this.stubRequest
  18. },
  19. }
  20. this.ExportsHandler = await esmock.strict(modulePath, {
  21. '../../../../app/src/Features/Project/ProjectGetter':
  22. (this.ProjectGetter = {}),
  23. '../../../../app/src/Features/Project/ProjectHistoryHandler':
  24. (this.ProjectHistoryHandler = {}),
  25. '../../../../app/src/Features/Project/ProjectLocator':
  26. (this.ProjectLocator = {}),
  27. '../../../../app/src/Features/Project/ProjectRootDocManager':
  28. (this.ProjectRootDocManager = {}),
  29. '../../../../app/src/Features/User/UserGetter': (this.UserGetter = {}),
  30. '@overleaf/settings': (this.settings = {}),
  31. request: this.request,
  32. })
  33. this.project_id = 'project-id-123'
  34. this.project_history_id = 987
  35. this.user_id = 'user-id-456'
  36. this.brand_variation_id = 789
  37. this.title = 'title'
  38. this.description = 'description'
  39. this.author = 'author'
  40. this.license = 'other'
  41. this.show_source = true
  42. this.export_params = {
  43. project_id: this.project_id,
  44. brand_variation_id: this.brand_variation_id,
  45. user_id: this.user_id,
  46. title: this.title,
  47. description: this.description,
  48. author: this.author,
  49. license: this.license,
  50. show_source: this.show_source,
  51. }
  52. return (this.callback = sinon.stub())
  53. })
  54. describe('exportProject', function () {
  55. beforeEach(function () {
  56. this.export_data = { iAmAnExport: true }
  57. this.response_body = { iAmAResponseBody: true }
  58. this.ExportsHandler._buildExport = sinon
  59. .stub()
  60. .yields(null, this.export_data)
  61. return (this.ExportsHandler._requestExport = sinon
  62. .stub()
  63. .yields(null, this.response_body))
  64. })
  65. describe('when all goes well', function () {
  66. beforeEach(function (done) {
  67. return this.ExportsHandler.exportProject(
  68. this.export_params,
  69. (error, exportData) => {
  70. this.callback(error, exportData)
  71. return done()
  72. }
  73. )
  74. })
  75. it('should build the export', function () {
  76. return this.ExportsHandler._buildExport
  77. .calledWith(this.export_params)
  78. .should.equal(true)
  79. })
  80. it('should request the export', function () {
  81. return this.ExportsHandler._requestExport
  82. .calledWith(this.export_data)
  83. .should.equal(true)
  84. })
  85. it('should return the export', function () {
  86. return this.callback
  87. .calledWith(null, this.export_data)
  88. .should.equal(true)
  89. })
  90. })
  91. describe("when request can't be built", function () {
  92. beforeEach(function (done) {
  93. this.ExportsHandler._buildExport = sinon
  94. .stub()
  95. .yields(new Error('cannot export project without root doc'))
  96. return this.ExportsHandler.exportProject(
  97. this.export_params,
  98. (error, exportData) => {
  99. this.callback(error, exportData)
  100. return done()
  101. }
  102. )
  103. })
  104. it('should return an error', function () {
  105. return (this.callback.args[0][0] instanceof Error).should.equal(true)
  106. })
  107. })
  108. describe('when export request returns an error to forward to the user', function () {
  109. beforeEach(function (done) {
  110. this.error_json = { status: 422, message: 'nope' }
  111. this.ExportsHandler._requestExport = sinon
  112. .stub()
  113. .yields(null, { forwardResponse: this.error_json })
  114. return this.ExportsHandler.exportProject(
  115. this.export_params,
  116. (error, exportData) => {
  117. this.callback(error, exportData)
  118. return done()
  119. }
  120. )
  121. })
  122. it('should return success and the response to forward', function () {
  123. ;(this.callback.args[0][0] instanceof Error).should.equal(false)
  124. return this.callback.calledWith(null, {
  125. forwardResponse: this.error_json,
  126. })
  127. })
  128. })
  129. })
  130. describe('_buildExport', function () {
  131. beforeEach(function (done) {
  132. this.project = {
  133. id: this.project_id,
  134. rootDoc_id: 'doc1_id',
  135. compiler: 'pdflatex',
  136. imageName: 'mock-image-name',
  137. overleaf: {
  138. id: this.project_history_id, // for projects imported from v1
  139. history: {
  140. id: this.project_history_id,
  141. },
  142. },
  143. }
  144. this.user = {
  145. id: this.user_id,
  146. first_name: 'Arthur',
  147. last_name: 'Author',
  148. email: 'arthur.author@arthurauthoring.org',
  149. overleaf: {
  150. id: 876,
  151. },
  152. }
  153. this.rootDocPath = 'main.tex'
  154. this.historyVersion = 777
  155. this.ProjectGetter.getProject = sinon.stub().yields(null, this.project)
  156. this.ProjectHistoryHandler.ensureHistoryExistsForProject = sinon
  157. .stub()
  158. .yields(null)
  159. this.ProjectLocator.findRootDoc = sinon
  160. .stub()
  161. .yields(null, [null, { fileSystem: 'main.tex' }])
  162. this.ProjectRootDocManager.ensureRootDocumentIsValid = sinon
  163. .stub()
  164. .callsArgWith(1, null)
  165. this.UserGetter.getUser = sinon.stub().yields(null, this.user)
  166. this.ExportsHandler._requestVersion = sinon
  167. .stub()
  168. .yields(null, this.historyVersion)
  169. return done()
  170. })
  171. describe('when all goes well', function () {
  172. beforeEach(function (done) {
  173. return this.ExportsHandler._buildExport(
  174. this.export_params,
  175. (error, exportData) => {
  176. this.callback(error, exportData)
  177. return done()
  178. }
  179. )
  180. })
  181. it('should ensure the project has history', function () {
  182. return this.ProjectHistoryHandler.ensureHistoryExistsForProject.called.should.equal(
  183. true
  184. )
  185. })
  186. it('should request the project history version', function () {
  187. return this.ExportsHandler._requestVersion.called.should.equal(true)
  188. })
  189. it('should return export data', function () {
  190. const expectedExportData = {
  191. project: {
  192. id: this.project_id,
  193. rootDocPath: this.rootDocPath,
  194. historyId: this.project_history_id,
  195. historyVersion: this.historyVersion,
  196. v1ProjectId: this.project_history_id,
  197. metadata: {
  198. compiler: 'pdflatex',
  199. imageName: 'mock-image-name',
  200. title: this.title,
  201. description: this.description,
  202. author: this.author,
  203. license: this.license,
  204. showSource: this.show_source,
  205. },
  206. },
  207. user: {
  208. id: this.user_id,
  209. firstName: this.user.first_name,
  210. lastName: this.user.last_name,
  211. email: this.user.email,
  212. orcidId: null,
  213. v1UserId: 876,
  214. },
  215. destination: {
  216. brandVariationId: this.brand_variation_id,
  217. },
  218. options: {
  219. callbackUrl: null,
  220. },
  221. }
  222. return this.callback
  223. .calledWith(null, expectedExportData)
  224. .should.equal(true)
  225. })
  226. })
  227. describe('when we send replacement user first and last name', function () {
  228. beforeEach(function (done) {
  229. this.custom_first_name = 'FIRST'
  230. this.custom_last_name = 'LAST'
  231. this.export_params.first_name = this.custom_first_name
  232. this.export_params.last_name = this.custom_last_name
  233. return this.ExportsHandler._buildExport(
  234. this.export_params,
  235. (error, exportData) => {
  236. this.callback(error, exportData)
  237. return done()
  238. }
  239. )
  240. })
  241. it('should send the data from the user input', function () {
  242. const expectedExportData = {
  243. project: {
  244. id: this.project_id,
  245. rootDocPath: this.rootDocPath,
  246. historyId: this.project_history_id,
  247. historyVersion: this.historyVersion,
  248. v1ProjectId: this.project_history_id,
  249. metadata: {
  250. compiler: 'pdflatex',
  251. imageName: 'mock-image-name',
  252. title: this.title,
  253. description: this.description,
  254. author: this.author,
  255. license: this.license,
  256. showSource: this.show_source,
  257. },
  258. },
  259. user: {
  260. id: this.user_id,
  261. firstName: this.custom_first_name,
  262. lastName: this.custom_last_name,
  263. email: this.user.email,
  264. orcidId: null,
  265. v1UserId: 876,
  266. },
  267. destination: {
  268. brandVariationId: this.brand_variation_id,
  269. },
  270. options: {
  271. callbackUrl: null,
  272. },
  273. }
  274. return this.callback
  275. .calledWith(null, expectedExportData)
  276. .should.equal(true)
  277. })
  278. })
  279. describe('when project is not found', function () {
  280. beforeEach(function (done) {
  281. this.ProjectGetter.getProject = sinon
  282. .stub()
  283. .yields(new Error('project not found'))
  284. return this.ExportsHandler._buildExport(
  285. this.export_params,
  286. (error, exportData) => {
  287. this.callback(error, exportData)
  288. return done()
  289. }
  290. )
  291. })
  292. it('should return an error', function () {
  293. return (this.callback.args[0][0] instanceof Error).should.equal(true)
  294. })
  295. })
  296. describe('when project has no root doc', function () {
  297. describe('when a root doc can be set automatically', function () {
  298. beforeEach(function (done) {
  299. this.project.rootDoc_id = null
  300. this.ProjectLocator.findRootDoc = sinon
  301. .stub()
  302. .yields(null, [null, { fileSystem: 'other.tex' }])
  303. return this.ExportsHandler._buildExport(
  304. this.export_params,
  305. (error, exportData) => {
  306. this.callback(error, exportData)
  307. return done()
  308. }
  309. )
  310. })
  311. it('should set a root doc', function () {
  312. return this.ProjectRootDocManager.ensureRootDocumentIsValid.called.should.equal(
  313. true
  314. )
  315. })
  316. it('should return export data', function () {
  317. const expectedExportData = {
  318. project: {
  319. id: this.project_id,
  320. rootDocPath: 'other.tex',
  321. historyId: this.project_history_id,
  322. historyVersion: this.historyVersion,
  323. v1ProjectId: this.project_history_id,
  324. metadata: {
  325. compiler: 'pdflatex',
  326. imageName: 'mock-image-name',
  327. title: this.title,
  328. description: this.description,
  329. author: this.author,
  330. license: this.license,
  331. showSource: this.show_source,
  332. },
  333. },
  334. user: {
  335. id: this.user_id,
  336. firstName: this.user.first_name,
  337. lastName: this.user.last_name,
  338. email: this.user.email,
  339. orcidId: null,
  340. v1UserId: 876,
  341. },
  342. destination: {
  343. brandVariationId: this.brand_variation_id,
  344. },
  345. options: {
  346. callbackUrl: null,
  347. },
  348. }
  349. return this.callback
  350. .calledWith(null, expectedExportData)
  351. .should.equal(true)
  352. })
  353. })
  354. })
  355. describe('when project has an invalid root doc', function () {
  356. describe('when a new root doc can be set automatically', function () {
  357. beforeEach(function (done) {
  358. this.fakeDoc_id = '1a2b3c4d5e6f'
  359. this.project.rootDoc_id = this.fakeDoc_id
  360. this.ProjectLocator.findRootDoc = sinon
  361. .stub()
  362. .yields(null, [null, { fileSystem: 'other.tex' }])
  363. return this.ExportsHandler._buildExport(
  364. this.export_params,
  365. (error, exportData) => {
  366. this.callback(error, exportData)
  367. return done()
  368. }
  369. )
  370. })
  371. it('should set a valid root doc', function () {
  372. return this.ProjectRootDocManager.ensureRootDocumentIsValid.called.should.equal(
  373. true
  374. )
  375. })
  376. it('should return export data', function () {
  377. const expectedExportData = {
  378. project: {
  379. id: this.project_id,
  380. rootDocPath: 'other.tex',
  381. historyId: this.project_history_id,
  382. historyVersion: this.historyVersion,
  383. v1ProjectId: this.project_history_id,
  384. metadata: {
  385. compiler: 'pdflatex',
  386. imageName: 'mock-image-name',
  387. title: this.title,
  388. description: this.description,
  389. author: this.author,
  390. license: this.license,
  391. showSource: this.show_source,
  392. },
  393. },
  394. user: {
  395. id: this.user_id,
  396. firstName: this.user.first_name,
  397. lastName: this.user.last_name,
  398. email: this.user.email,
  399. orcidId: null,
  400. v1UserId: 876,
  401. },
  402. destination: {
  403. brandVariationId: this.brand_variation_id,
  404. },
  405. options: {
  406. callbackUrl: null,
  407. },
  408. }
  409. return this.callback
  410. .calledWith(null, expectedExportData)
  411. .should.equal(true)
  412. })
  413. })
  414. describe('when no root doc can be identified', function () {
  415. beforeEach(function (done) {
  416. this.ProjectLocator.findRootDoc = sinon
  417. .stub()
  418. .yields(null, [null, null])
  419. return this.ExportsHandler._buildExport(
  420. this.export_params,
  421. (error, exportData) => {
  422. this.callback(error, exportData)
  423. return done()
  424. }
  425. )
  426. })
  427. it('should return an error', function () {
  428. return (this.callback.args[0][0] instanceof Error).should.equal(true)
  429. })
  430. })
  431. })
  432. describe('when user is not found', function () {
  433. beforeEach(function (done) {
  434. this.UserGetter.getUser = sinon
  435. .stub()
  436. .yields(new Error('user not found'))
  437. return this.ExportsHandler._buildExport(
  438. this.export_params,
  439. (error, exportData) => {
  440. this.callback(error, exportData)
  441. return done()
  442. }
  443. )
  444. })
  445. it('should return an error', function () {
  446. return (this.callback.args[0][0] instanceof Error).should.equal(true)
  447. })
  448. })
  449. describe('when project history request fails', function () {
  450. beforeEach(function (done) {
  451. this.ExportsHandler._requestVersion = sinon
  452. .stub()
  453. .yields(new Error('project history call failed'))
  454. return this.ExportsHandler._buildExport(
  455. this.export_params,
  456. (error, exportData) => {
  457. this.callback(error, exportData)
  458. return done()
  459. }
  460. )
  461. })
  462. it('should return an error', function () {
  463. return (this.callback.args[0][0] instanceof Error).should.equal(true)
  464. })
  465. })
  466. })
  467. describe('_requestExport', function () {
  468. beforeEach(function (done) {
  469. this.settings.apis = {
  470. v1: {
  471. url: 'http://127.0.0.1:5000',
  472. user: 'overleaf',
  473. pass: 'pass',
  474. timeout: 15000,
  475. },
  476. }
  477. this.export_data = { iAmAnExport: true }
  478. this.export_id = 4096
  479. this.stubPost = sinon
  480. .stub()
  481. .yields(null, { statusCode: 200 }, { exportId: this.export_id })
  482. return done()
  483. })
  484. describe('when all goes well', function () {
  485. beforeEach(function (done) {
  486. this.stubRequest.post = this.stubPost
  487. return this.ExportsHandler._requestExport(
  488. this.export_data,
  489. (error, exportV1Id) => {
  490. this.callback(error, exportV1Id)
  491. return done()
  492. }
  493. )
  494. })
  495. it('should issue the request', function () {
  496. return expect(this.stubPost.getCall(0).args[0]).to.deep.equal({
  497. url: this.settings.apis.v1.url + '/api/v1/overleaf/exports',
  498. auth: {
  499. user: this.settings.apis.v1.user,
  500. pass: this.settings.apis.v1.pass,
  501. },
  502. json: this.export_data,
  503. timeout: 15000,
  504. })
  505. })
  506. it('should return the body with v1 export id', function () {
  507. return this.callback
  508. .calledWith(null, { exportId: this.export_id })
  509. .should.equal(true)
  510. })
  511. })
  512. describe('when the request fails', function () {
  513. beforeEach(function (done) {
  514. this.stubRequest.post = sinon
  515. .stub()
  516. .yields(new Error('export request failed'))
  517. return this.ExportsHandler._requestExport(
  518. this.export_data,
  519. (error, exportV1Id) => {
  520. this.callback(error, exportV1Id)
  521. return done()
  522. }
  523. )
  524. })
  525. it('should return an error', function () {
  526. return (this.callback.args[0][0] instanceof Error).should.equal(true)
  527. })
  528. })
  529. describe('when the request returns an error response to forward', function () {
  530. beforeEach(function (done) {
  531. this.error_code = 422
  532. this.error_json = { status: this.error_code, message: 'nope' }
  533. this.stubRequest.post = sinon
  534. .stub()
  535. .yields(null, { statusCode: this.error_code }, this.error_json)
  536. return this.ExportsHandler._requestExport(
  537. this.export_data,
  538. (error, exportV1Id) => {
  539. this.callback(error, exportV1Id)
  540. return done()
  541. }
  542. )
  543. })
  544. it('should return success and the response to forward', function () {
  545. ;(this.callback.args[0][0] instanceof Error).should.equal(false)
  546. return this.callback.calledWith(null, {
  547. forwardResponse: this.error_json,
  548. })
  549. })
  550. })
  551. })
  552. describe('fetchExport', function () {
  553. beforeEach(function (done) {
  554. this.settings.apis = {
  555. v1: {
  556. url: 'http://127.0.0.1:5000',
  557. user: 'overleaf',
  558. pass: 'pass',
  559. timeout: 15000,
  560. },
  561. }
  562. this.export_id = 897
  563. this.body = '{"id":897, "status_summary":"completed"}'
  564. this.stubGet = sinon
  565. .stub()
  566. .yields(null, { statusCode: 200 }, { body: this.body })
  567. return done()
  568. })
  569. describe('when all goes well', function () {
  570. beforeEach(function (done) {
  571. this.stubRequest.get = this.stubGet
  572. return this.ExportsHandler.fetchExport(
  573. this.export_id,
  574. (error, body) => {
  575. this.callback(error, body)
  576. return done()
  577. }
  578. )
  579. })
  580. it('should issue the request', function () {
  581. return expect(this.stubGet.getCall(0).args[0]).to.deep.equal({
  582. url:
  583. this.settings.apis.v1.url +
  584. '/api/v1/overleaf/exports/' +
  585. this.export_id,
  586. auth: {
  587. user: this.settings.apis.v1.user,
  588. pass: this.settings.apis.v1.pass,
  589. },
  590. timeout: 15000,
  591. })
  592. })
  593. it('should return the v1 export id', function () {
  594. return this.callback
  595. .calledWith(null, { body: this.body })
  596. .should.equal(true)
  597. })
  598. })
  599. })
  600. describe('fetchDownload', function () {
  601. beforeEach(function (done) {
  602. this.settings.apis = {
  603. v1: {
  604. url: 'http://127.0.0.1:5000',
  605. user: 'overleaf',
  606. pass: 'pass',
  607. timeout: 15000,
  608. },
  609. }
  610. this.export_id = 897
  611. this.body =
  612. 'https://writelatex-conversions-dev.s3.amazonaws.com/exports/ieee_latexqc/tnb/2912/xggmprcrpfwbsnqzqqmvktddnrbqkqkr.zip?X-Amz-Expires=14400&X-Amz-Date=20180730T181003Z&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAJDGDIJFGLNVGZH6A/20180730/us-east-1/s3/aws4_request&X-Amz-SignedHeaders=host&X-Amz-Signature=dec990336913cef9933f0e269afe99722d7ab2830ebf2c618a75673ee7159fee'
  613. this.stubGet = sinon
  614. .stub()
  615. .yields(null, { statusCode: 200 }, { body: this.body })
  616. return done()
  617. })
  618. describe('when all goes well', function () {
  619. beforeEach(function (done) {
  620. this.stubRequest.get = this.stubGet
  621. return this.ExportsHandler.fetchDownload(
  622. this.export_id,
  623. 'zip',
  624. (error, body) => {
  625. this.callback(error, body)
  626. return done()
  627. }
  628. )
  629. })
  630. it('should issue the request', function () {
  631. return expect(this.stubGet.getCall(0).args[0]).to.deep.equal({
  632. url:
  633. this.settings.apis.v1.url +
  634. '/api/v1/overleaf/exports/' +
  635. this.export_id +
  636. '/zip_url',
  637. auth: {
  638. user: this.settings.apis.v1.user,
  639. pass: this.settings.apis.v1.pass,
  640. },
  641. timeout: 15000,
  642. })
  643. })
  644. it('should return the v1 export id', function () {
  645. return this.callback
  646. .calledWith(null, { body: this.body })
  647. .should.equal(true)
  648. })
  649. })
  650. })
  651. })