Skip to content

Commit

Permalink
tests for downloadDocuments, generateEtchSignUrl, getEtchPacket
Browse files Browse the repository at this point in the history
  • Loading branch information
Winggo committed Oct 15, 2020
1 parent 34e3a67 commit dec02fb
Show file tree
Hide file tree
Showing 2 changed files with 180 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,14 @@ Generates an Etch sign URL for an Etch Packet signer. The Etch Packet and its si

##### downloadDocuments(documentGroupEid[, options])

Returns the PassThrough stream of the document group specified by the documentGroupEid in Zip file format.
Returns a Buffer or Stream of the document group specified by the documentGroupEid in Zip file format.
* `documentGroupEid` (string) - the eid of the document group to download
* `options` (Object) - _optional_ Any additional options for the request
* `dataType` (Enum[String]) - _optional_ Set the type of the `data` value that is returned in the resolved `Promise`. Defaults to `'buffer'`, but `'stream'` is also supported.
* Returns a `Promise` that resolves to an `Object`
* `statusCode` (Number) - the HTTP status code, `200` is success
* `response` (Object) - the Response object resulting from the client's request to the Anvil app
* `data` (object) - a PassThrough Stream object containing the document group's data in Zip file format
* `data` (Buffer | Stream) - The raw binary data of the downloaded document if success. Will be in the format of either a Buffer or a Stream, depending on `dataType` option supplied to the request.
* `errors` (Array of Objects) - Will be present if status >= 400. See Errors
* `message` (String)

Expand Down
178 changes: 178 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function mockNodeFetchResponse (options = {}) {
json,
buffer,
headers,
body,
} = options

const mock = {
Expand All @@ -27,6 +28,10 @@ function mockNodeFetchResponse (options = {}) {
mock.buffer = () => buffer
}

if (body) {
mock.body = body
}

if (headers) {
mock.headers = {
get: (header) => headers[header],
Expand Down Expand Up @@ -212,6 +217,80 @@ describe('Anvil API Client', function () {
})
})
})

describe('downloadDocuments', function () {
def('statusCode', 200)
def('buffer', 'This would be Zip file data buffer...')
def('body', 'This would be Zip file data stream')
def('nodeFetchResponse', () => mockNodeFetchResponse({
status: $.statusCode,
buffer: $.buffer,
body: $.body,
json: $.json,
}))

beforeEach(async function () {
client._request.callsFake((url, options) => {
return Promise.resolve($.nodeFetchResponse)
})
})

context('everything goes well', function () {
it('returns data as buffer', async function () {
const { statusCode, response, data, errors } = await client.downloadDocuments('docGroupEid123')
expect(statusCode).to.eql(200)
expect(response).to.deep.eql($.nodeFetchResponse)
expect(data).to.eql($.buffer)
expect(errors).to.be.undefined
})

it('returns data as stream', async function () {
const { statusCode, response, data, errors } = await client.downloadDocuments('docGroupEid123', { dataType: 'stream' })
expect(statusCode).to.eql(200)
expect(response).to.deep.eql($.nodeFetchResponse)
expect(data).to.eql($.body)
expect(errors).to.be.undefined
})
})

context('unsupported options', function () {
it('returns data as buffer', async function () {
try {
await client.downloadDocuments('docGroupEid123', { dataType: 'json' })
} catch (e) {
expect(e.message).to.eql('dataType must be one of: stream|buffer')
}
})
})

context('server 400s with errors array in JSON', function () {
const errors = [{ message: 'problem' }]
def('statusCode', 400)
def('json', { errors })

it('finds errors and puts them in response', async function () {
const { statusCode, errors } = await client.downloadDocuments('docGroupEid123')

expect(client._request).to.have.been.calledOnce
expect(statusCode).to.eql(400)
expect(errors).to.eql(errors)
})
})

context('server 401s with single error in response', function () {
const error = { name: 'AuthorizationError', message: 'problem' }
def('statusCode', 401)
def('json', error)

it('finds error and puts it in the response', async function () {
const { statusCode, errors } = await client.downloadDocuments('docGroupEid123')

expect(client._request).to.have.been.calledOnce
expect(statusCode).to.eql(401)
expect(errors).to.eql([error])
})
})
})
})

describe('GraphQL', function () {
Expand Down Expand Up @@ -442,5 +521,104 @@ describe('Anvil API Client', function () {
})
})
})

describe('generateEtchSignUrl', function () {
def('statusCode', 200)
beforeEach(async function () {
sinon.stub(client, '_request')
client._request.callsFake((url, options) => {
return Promise.resolve($.nodeFetchResponse)
})
})
afterEach(function () {
client._request.restore()
})

context('everything goes well', function () {
def('data', {
data: {
generateEtchSignURL: 'http://www.testing.com',
},
})
def('nodeFetchResponse', () => mockNodeFetchResponse({
status: $.statusCode,
json: $.data,
}))

it('returns url successfully', async function () {
const variables = { clientUserId: 'foo', signerEid: 'bar' }
const { statusCode, url, errors } = await client.generateEtchSignUrl({ variables })
expect(statusCode).to.eql(200)
expect(url).to.be.eql($.data.data.generateEtchSignURL)
expect(errors).to.be.undefined
})
})

context('generate URL failures', function () {
def('data', {
data: {},
})
def('nodeFetchResponse', () => mockNodeFetchResponse({
status: $.statusCode,
json: $.data,
}))

it('returns undefined url', async function () {
const variables = { clientUserId: 'foo', signerEid: 'bar' }
const { statusCode, url, errors } = await client.generateEtchSignUrl({ variables })
expect(statusCode).to.eql(200)
expect(url).to.be.undefined
expect(errors).to.be.undefined
})
})
})

describe('getEtchPacket', function () {
def('variables', { eid: 'etchPacketEid123' })
beforeEach(function () {
sinon.stub(client, 'requestGraphQL')
})

afterEach(function () {
client.requestGraphQL.restore()
})

context('no responseQuery specified', function () {
it('calls requestGraphQL with default responseQuery', async function () {
client.getEtchPacket({ variables: $.variables })

expect(client.requestGraphQL).to.have.been.calledOnce
const [options, clientOptions] = client.requestGraphQL.lastCall.args

const {
query,
variables: variablesReceived,
} = options

expect($.variables).to.eql(variablesReceived)
expect(query).to.include('documentGroup {')
expect(clientOptions).to.eql({ dataType: 'json' })
})
})

context('responseQuery specified', function () {
it('calls requestGraphQL with overridden responseQuery', async function () {
const responseQuery = 'myCustomResponseQuery'
client.getEtchPacket({ variables: $.variables, responseQuery })

expect(client.requestGraphQL).to.have.been.calledOnce
const [options, clientOptions] = client.requestGraphQL.lastCall.args

const {
query,
variables: variablesReceived,
} = options

expect($.variables).to.eql(variablesReceived)
expect(query).to.include(responseQuery)
expect(clientOptions).to.eql({ dataType: 'json' })
})
})
})
})
})

0 comments on commit dec02fb

Please sign in to comment.