| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503 |
- import { vi, expect, describe, beforeEach, afterEach, it } from 'vitest'
- import sinon from 'sinon'
- import tk from 'timekeeper'
- import path from 'node:path'
- const modulePath = path.join(
- import.meta.dirname,
- '../../../app/js/RequestParser'
- )
- describe('RequestParser', () => {
- beforeEach(async ctx => {
- tk.freeze()
- ctx.callback = sinon.stub()
- ctx.validResource = {
- path: 'main.tex',
- date: '12:00 01/02/03',
- content: 'Hello world',
- }
- ctx.validRequest = {
- compile: {
- token: 'token-123',
- options: {
- imageName: 'basicImageName/here:2017-1',
- compiler: 'pdflatex',
- timeout: 42,
- },
- resources: [],
- },
- }
- vi.doMock('@overleaf/settings', () => ({
- default: (ctx.settings = {}),
- }))
- vi.doMock('../../../app/js/OutputCacheManager', () => ({
- default: { BUILD_REGEX: /^[0-9a-f]+-[0-9a-f]+$/ },
- }))
- ctx.RequestParser = (await import(modulePath)).default
- })
- afterEach(() => {
- tk.reset()
- })
- describe('without a top level object', () => {
- beforeEach(ctx => {
- ctx.RequestParser.parse([], ctx.callback)
- })
- it('should return an error', ctx => {
- expect(ctx.callback).to.have.been.called
- expect(ctx.callback.args[0][0].message).to.equal(
- 'top level object should have a compile attribute'
- )
- })
- })
- describe('without a compile attribute', () => {
- beforeEach(ctx => {
- ctx.RequestParser.parse({}, ctx.callback)
- })
- it('should return an error', ctx => {
- expect(ctx.callback).to.have.been.called
- expect(ctx.callback.args[0][0].message).to.equal(
- 'top level object should have a compile attribute'
- )
- })
- })
- describe('without a valid compiler', () => {
- beforeEach(ctx => {
- ctx.validRequest.compile.options.compiler = 'not-a-compiler'
- ctx.RequestParser.parse(ctx.validRequest, ctx.callback)
- })
- it('should return an error', ctx => {
- ctx.callback
- .calledWithMatch({
- message:
- 'compiler attribute should be one of: pdflatex, latex, xelatex, lualatex',
- })
- .should.equal(true)
- })
- })
- describe('without a compiler specified', () => {
- beforeEach(async ctx => {
- await new Promise((resolve, reject) => {
- delete ctx.validRequest.compile.options.compiler
- ctx.RequestParser.parse(ctx.validRequest, (error, data) => {
- if (error) return reject(error)
- ctx.data = data
- resolve()
- })
- })
- })
- it('should set the compiler to pdflatex by default', ctx => {
- ctx.data.compiler.should.equal('pdflatex')
- })
- })
- describe('with imageName set', () => {
- beforeEach(async ctx => {
- await new Promise((resolve, reject) => {
- ctx.RequestParser.parse(ctx.validRequest, (error, data) => {
- if (error) return reject(error)
- ctx.data = data
- resolve()
- })
- })
- })
- it('should set the imageName', ctx => {
- ctx.data.imageName.should.equal('basicImageName/here:2017-1')
- })
- })
- describe('when image restrictions are present', () => {
- beforeEach(ctx => {
- ctx.settings.clsi = { docker: {} }
- ctx.settings.clsi.docker.allowedImages = [
- 'repo/name:tag1',
- 'repo/name:tag2',
- ]
- })
- describe('with imageName set to something invalid', () => {
- beforeEach(ctx => {
- const request = ctx.validRequest
- request.compile.options.imageName = 'something/different:latest'
- ctx.RequestParser.parse(request, (error, data) => {
- ctx.error = error
- ctx.data = data
- })
- })
- it('should throw an error for imageName', ctx => {
- expect(String(ctx.error)).to.include(
- 'imageName attribute should be one of'
- )
- })
- })
- describe('with imageName set to something valid', () => {
- beforeEach(ctx => {
- const request = ctx.validRequest
- request.compile.options.imageName = 'repo/name:tag1'
- ctx.RequestParser.parse(request, (error, data) => {
- ctx.error = error
- ctx.data = data
- })
- })
- it('should set the imageName', ctx => {
- ctx.data.imageName.should.equal('repo/name:tag1')
- })
- })
- })
- describe('with flags set', () => {
- beforeEach(async ctx => {
- await new Promise((resolve, reject) => {
- ctx.validRequest.compile.options.flags = ['-file-line-error']
- ctx.RequestParser.parse(ctx.validRequest, (error, data) => {
- if (error) return reject(error)
- ctx.data = data
- resolve()
- })
- })
- })
- it('should set the flags attribute', ctx => {
- expect(ctx.data.flags).to.deep.equal(['-file-line-error'])
- })
- })
- describe('with flags not specified', () => {
- beforeEach(async ctx => {
- await new Promise((resolve, reject) => {
- ctx.RequestParser.parse(ctx.validRequest, (error, data) => {
- if (error) return reject(error)
- ctx.data = data
- resolve()
- })
- })
- })
- it('it should have an empty flags list', ctx => {
- expect(ctx.data.flags).to.deep.equal([])
- })
- })
- describe('without a timeout specified', () => {
- beforeEach(async ctx => {
- await new Promise((resolve, reject) => {
- delete ctx.validRequest.compile.options.timeout
- ctx.RequestParser.parse(ctx.validRequest, (error, data) => {
- if (error) return reject(error)
- ctx.data = data
- resolve()
- })
- })
- })
- it('should set the timeout to MAX_TIMEOUT', ctx => {
- ctx.data.timeout.should.equal(ctx.RequestParser.MAX_TIMEOUT * 1000)
- })
- })
- describe('with a timeout larger than the maximum', () => {
- beforeEach(async ctx => {
- await new Promise((resolve, reject) => {
- ctx.validRequest.compile.options.timeout =
- ctx.RequestParser.MAX_TIMEOUT + 1
- ctx.RequestParser.parse(ctx.validRequest, (error, data) => {
- if (error) return reject(error)
- ctx.data = data
- resolve()
- })
- })
- })
- it('should set the timeout to MAX_TIMEOUT', ctx => {
- ctx.data.timeout.should.equal(ctx.RequestParser.MAX_TIMEOUT * 1000)
- })
- })
- describe('with a timeout', () => {
- beforeEach(async ctx => {
- await new Promise((resolve, reject) => {
- ctx.RequestParser.parse(ctx.validRequest, (error, data) => {
- if (error) return reject(error)
- ctx.data = data
- resolve()
- })
- })
- })
- it('should set the timeout (in milliseconds)', ctx => {
- ctx.data.timeout.should.equal(
- ctx.validRequest.compile.options.timeout * 1000
- )
- })
- })
- describe('with a resource without a path', () => {
- beforeEach(ctx => {
- delete ctx.validResource.path
- ctx.validRequest.compile.resources.push(ctx.validResource)
- ctx.RequestParser.parse(ctx.validRequest, ctx.callback)
- })
- it('should return an error', ctx => {
- ctx.callback
- .calledWithMatch({
- message: 'all resources should have a path attribute',
- })
- .should.equal(true)
- })
- })
- describe('with a resource with a path', () => {
- beforeEach(ctx => {
- ctx.validResource.path = ctx.path = 'test.tex'
- ctx.validRequest.compile.resources.push(ctx.validResource)
- ctx.RequestParser.parse(ctx.validRequest, ctx.callback)
- ctx.data = ctx.callback.args[0][1]
- })
- it('should return the path in the parsed response', ctx => {
- ctx.data.resources[0].path.should.equal(ctx.path)
- })
- })
- describe('with a resource with a malformed modified date', () => {
- beforeEach(ctx => {
- ctx.validResource.modified = 'not-a-date'
- ctx.validRequest.compile.resources.push(ctx.validResource)
- ctx.RequestParser.parse(ctx.validRequest, ctx.callback)
- })
- it('should return an error', ctx => {
- ctx.callback
- .calledWithMatch({
- message:
- 'resource modified date could not be understood: ' +
- ctx.validResource.modified,
- })
- .should.equal(true)
- })
- })
- describe('with a valid buildId', () => {
- beforeEach(async ctx => {
- await new Promise((resolve, reject) => {
- ctx.validRequest.compile.options.buildId =
- '195a4869176-a4ad60bee7bf35e4'
- ctx.RequestParser.parse(ctx.validRequest, (error, data) => {
- if (error) return reject(error)
- ctx.data = data
- resolve()
- })
- })
- })
- it('should return an error', ctx => {
- ctx.data.buildId.should.equal('195a4869176-a4ad60bee7bf35e4')
- })
- })
- describe('with a bad buildId', () => {
- beforeEach(ctx => {
- ctx.validRequest.compile.options.buildId = 'foo/bar'
- ctx.RequestParser.parse(ctx.validRequest, ctx.callback)
- })
- it('should return an error', ctx => {
- ctx.callback
- .calledWithMatch({
- message:
- 'buildId attribute does not match regex /^[0-9a-f]+-[0-9a-f]+$/',
- })
- .should.equal(true)
- })
- })
- describe('with a resource with a valid date', () => {
- beforeEach(ctx => {
- ctx.date = '12:00 01/02/03'
- ctx.validResource.modified = ctx.date
- ctx.validRequest.compile.resources.push(ctx.validResource)
- ctx.RequestParser.parse(ctx.validRequest, ctx.callback)
- ctx.data = ctx.callback.args[0][1]
- })
- it('should return the date as a Javascript Date object', ctx => {
- ;(ctx.data.resources[0].modified instanceof Date).should.equal(true)
- ctx.data.resources[0].modified
- .getTime()
- .should.equal(Date.parse(ctx.date))
- })
- })
- describe('with a resource without either a content or URL attribute', () => {
- beforeEach(ctx => {
- delete ctx.validResource.url
- delete ctx.validResource.content
- ctx.validRequest.compile.resources.push(ctx.validResource)
- ctx.RequestParser.parse(ctx.validRequest, ctx.callback)
- })
- it('should return an error', ctx => {
- ctx.callback
- .calledWithMatch({
- message:
- 'all resources should have either a url or content attribute',
- })
- .should.equal(true)
- })
- })
- describe('with a resource where the content is not a string', () => {
- beforeEach(ctx => {
- ctx.validResource.content = []
- ctx.validRequest.compile.resources.push(ctx.validResource)
- ctx.RequestParser.parse(ctx.validRequest, ctx.callback)
- })
- it('should return an error', ctx => {
- ctx.callback
- .calledWithMatch({ message: 'content attribute should be a string' })
- .should.equal(true)
- })
- })
- describe('with a resource where the url is not a string', () => {
- beforeEach(ctx => {
- ctx.validResource.url = []
- ctx.validRequest.compile.resources.push(ctx.validResource)
- ctx.RequestParser.parse(ctx.validRequest, ctx.callback)
- })
- it('should return an error', ctx => {
- ctx.callback
- .calledWithMatch({ message: 'url attribute should be a string' })
- .should.equal(true)
- })
- })
- describe('with a resource with a url', () => {
- beforeEach(ctx => {
- ctx.validResource.url = ctx.url = 'www.example.com'
- ctx.validRequest.compile.resources.push(ctx.validResource)
- ctx.RequestParser.parse(ctx.validRequest, ctx.callback)
- ctx.data = ctx.callback.args[0][1]
- })
- it('should return the url in the parsed response', ctx => {
- ctx.data.resources[0].url.should.equal(ctx.url)
- })
- })
- describe('with a resource with a content attribute', () => {
- beforeEach(ctx => {
- ctx.validResource.content = ctx.content = 'Hello world'
- ctx.validRequest.compile.resources.push(ctx.validResource)
- ctx.RequestParser.parse(ctx.validRequest, ctx.callback)
- ctx.data = ctx.callback.args[0][1]
- })
- it('should return the content in the parsed response', ctx => {
- ctx.data.resources[0].content.should.equal(ctx.content)
- })
- })
- describe('without a root resource path', () => {
- beforeEach(ctx => {
- delete ctx.validRequest.compile.rootResourcePath
- ctx.RequestParser.parse(ctx.validRequest, ctx.callback)
- ctx.data = ctx.callback.args[0][1]
- })
- it("should set the root resource path to 'main.tex' by default", ctx => {
- ctx.data.rootResourcePath.should.equal('main.tex')
- })
- })
- describe('with a root resource path', () => {
- beforeEach(ctx => {
- ctx.validRequest.compile.rootResourcePath = ctx.path = 'test.tex'
- ctx.RequestParser.parse(ctx.validRequest, ctx.callback)
- ctx.data = ctx.callback.args[0][1]
- })
- it('should return the root resource path in the parsed response', ctx => {
- ctx.data.rootResourcePath.should.equal(ctx.path)
- })
- })
- describe('with a root resource path that is not a string', () => {
- beforeEach(ctx => {
- ctx.validRequest.compile.rootResourcePath = []
- ctx.RequestParser.parse(ctx.validRequest, ctx.callback)
- })
- it('should return an error', ctx => {
- ctx.callback
- .calledWithMatch({
- message: 'rootResourcePath attribute should be a string',
- })
- .should.equal(true)
- })
- })
- describe('with a root resource path that has a relative path', () => {
- beforeEach(ctx => {
- ctx.validRequest.compile.rootResourcePath = 'foo/../../bar.tex'
- ctx.RequestParser.parse(ctx.validRequest, ctx.callback)
- ctx.data = ctx.callback.args[0][1]
- })
- it('should return an error', ctx => {
- ctx.callback
- .calledWithMatch({ message: 'relative path in root resource' })
- .should.equal(true)
- })
- })
- describe('with a root resource path that has unescaped + relative path', () => {
- beforeEach(ctx => {
- ctx.validRequest.compile.rootResourcePath = 'foo/../bar.tex'
- ctx.RequestParser.parse(ctx.validRequest, ctx.callback)
- ctx.data = ctx.callback.args[0][1]
- })
- it('should return an error', ctx => {
- ctx.callback
- .calledWithMatch({ message: 'relative path in root resource' })
- .should.equal(true)
- })
- })
- describe('with an unknown syncType', () => {
- beforeEach(ctx => {
- ctx.validRequest.compile.options.syncType = 'unexpected'
- ctx.RequestParser.parse(ctx.validRequest, ctx.callback)
- ctx.data = ctx.callback.args[0][1]
- })
- it('should return an error', ctx => {
- ctx.callback
- .calledWithMatch({
- message:
- 'syncType attribute should be one of: full, incremental, history-full, history-incremental',
- })
- .should.equal(true)
- })
- })
- })
|