2018-08-10 12:49:26 +02:00
|
|
|
// Copyright BigchainDB GmbH and BigchainDB contributors
|
|
|
|
// SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
|
|
|
|
// Code is Apache-2.0 and docs are CC-BY-4.0
|
|
|
|
|
2017-06-19 14:40:22 +02:00
|
|
|
import test from 'ava'
|
|
|
|
import sinon from 'sinon'
|
2017-06-20 11:03:19 +02:00
|
|
|
|
2018-08-23 17:14:59 +02:00
|
|
|
import {
|
|
|
|
Connection
|
|
|
|
} from '../../src'
|
|
|
|
import {
|
|
|
|
API_PATH
|
|
|
|
} from '../constants'
|
2017-06-19 14:40:22 +02:00
|
|
|
|
|
|
|
const conn = new Connection(API_PATH)
|
|
|
|
|
2018-07-19 08:08:34 +02:00
|
|
|
test('Payload thrown at incorrect API_PATH', async t => {
|
2017-09-27 15:37:49 +02:00
|
|
|
const path = 'http://localhost:9984/api/wrong/'
|
2017-09-27 15:38:52 +02:00
|
|
|
const connection = new Connection(path)
|
2017-09-27 15:37:49 +02:00
|
|
|
const target = {
|
|
|
|
message: 'HTTP Error: Requested page not reachable',
|
|
|
|
status: '404 NOT FOUND',
|
2017-09-30 19:18:50 +02:00
|
|
|
requestURI: 'http://localhost:9984/api/wrong/transactions/transactionId'
|
2017-09-27 15:37:49 +02:00
|
|
|
}
|
2021-03-09 10:57:37 +01:00
|
|
|
const error = await t.throwsAsync(connection.getTransaction('transactionId'), {
|
|
|
|
instanceOf: Error, message: target.message
|
|
|
|
})
|
|
|
|
|
|
|
|
t.is('ResponseError', error.name)
|
|
|
|
t.is(target.status, error.status)
|
|
|
|
t.is(target.requestURI, error.requestURI)
|
2017-09-27 15:37:49 +02:00
|
|
|
})
|
2017-06-19 14:40:22 +02:00
|
|
|
|
2017-09-30 19:18:50 +02:00
|
|
|
test('Generate API URLS', t => {
|
2017-06-19 14:40:22 +02:00
|
|
|
const endpoints = {
|
|
|
|
'blocks': 'blocks',
|
2018-03-21 11:30:54 +01:00
|
|
|
'blocksDetail': 'blocks/%(blockHeight)s',
|
2017-06-19 14:40:22 +02:00
|
|
|
'outputs': 'outputs',
|
|
|
|
'transactions': 'transactions',
|
2018-06-19 16:52:23 +02:00
|
|
|
'transactionsSync': 'transactions?mode=sync',
|
|
|
|
'transactionsAsync': 'transactions?mode=async',
|
|
|
|
'transactionsCommit': 'transactions?mode=commit',
|
2017-06-19 14:56:00 +02:00
|
|
|
'transactionsDetail': 'transactions/%(transactionId)s',
|
2017-06-20 16:17:43 +02:00
|
|
|
'assets': 'assets',
|
2017-06-19 14:40:22 +02:00
|
|
|
}
|
2017-06-21 16:04:35 +02:00
|
|
|
Object.keys(endpoints).forEach(endpointName => {
|
2018-08-22 10:10:09 +02:00
|
|
|
const url = Connection.getApiUrls(endpointName)
|
|
|
|
const expected = endpoints[endpointName]
|
2017-06-19 14:40:22 +02:00
|
|
|
t.is(url, expected)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-08-23 17:14:59 +02:00
|
|
|
test('Normalize node from an object', t => {
|
|
|
|
const headers = {
|
|
|
|
custom: 'headers'
|
|
|
|
}
|
|
|
|
const node = {
|
|
|
|
endpoint: API_PATH,
|
2017-06-19 16:30:00 +02:00
|
|
|
headers: {
|
2018-08-23 17:14:59 +02:00
|
|
|
hello: 'world'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const expectedNode = {
|
|
|
|
'endpoint': API_PATH,
|
|
|
|
'headers': {
|
2017-06-19 16:30:00 +02:00
|
|
|
hello: 'world',
|
|
|
|
custom: 'headers'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 17:14:59 +02:00
|
|
|
t.deepEqual(Connection.normalizeNode(node, headers), expectedNode)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('Normalize node from a string', t => {
|
|
|
|
const headers = {
|
|
|
|
custom: 'headers'
|
|
|
|
}
|
|
|
|
const expectedNode = {
|
|
|
|
'endpoint': API_PATH,
|
|
|
|
'headers': {
|
|
|
|
custom: 'headers'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.deepEqual(Connection.normalizeNode(API_PATH, headers), expectedNode)
|
|
|
|
})
|
2017-06-19 14:40:22 +02:00
|
|
|
|
2017-06-19 16:30:00 +02:00
|
|
|
test('Request with custom headers', t => {
|
2018-08-23 17:14:59 +02:00
|
|
|
const testConn = new Connection(API_PATH, {
|
|
|
|
hello: 'world'
|
|
|
|
})
|
2017-06-19 16:30:00 +02:00
|
|
|
const expectedOptions = {
|
|
|
|
headers: {
|
|
|
|
custom: 'headers'
|
|
|
|
}
|
|
|
|
}
|
2018-08-23 17:14:59 +02:00
|
|
|
const PATH = 'blocks'
|
|
|
|
testConn.transport.forwardRequest = sinon.spy()
|
2017-06-19 16:30:00 +02:00
|
|
|
|
2018-08-23 17:14:59 +02:00
|
|
|
testConn._req(PATH, {
|
|
|
|
headers: {
|
|
|
|
custom: 'headers'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.truthy(testConn.transport.forwardRequest.calledWith(PATH, expectedOptions))
|
2017-06-19 16:30:00 +02:00
|
|
|
})
|
|
|
|
|
2017-06-19 14:56:00 +02:00
|
|
|
test('Get block for a block id', t => {
|
|
|
|
const expectedPath = 'path'
|
2018-03-21 11:30:54 +01:00
|
|
|
const blockHeight = 'abc'
|
2017-06-19 14:56:00 +02:00
|
|
|
|
|
|
|
conn._req = sinon.spy()
|
2018-08-22 10:10:09 +02:00
|
|
|
Connection.getApiUrls = sinon.stub().returns(expectedPath)
|
2017-06-19 14:56:00 +02:00
|
|
|
|
2018-03-21 11:30:54 +01:00
|
|
|
conn.getBlock(blockHeight)
|
2017-06-19 14:56:00 +02:00
|
|
|
t.truthy(conn._req.calledWith(
|
|
|
|
expectedPath,
|
2018-03-21 11:30:54 +01:00
|
|
|
{ urlTemplateSpec: { blockHeight } }
|
2017-06-19 14:40:22 +02:00
|
|
|
))
|
|
|
|
})
|
|
|
|
|
|
|
|
test('Get transaction for a transaction id', t => {
|
|
|
|
const expectedPath = 'path'
|
|
|
|
const transactionId = 'abc'
|
|
|
|
|
|
|
|
conn._req = sinon.spy()
|
2018-08-22 10:10:09 +02:00
|
|
|
Connection.getApiUrls = sinon.stub().returns(expectedPath)
|
2017-06-19 14:40:22 +02:00
|
|
|
|
|
|
|
conn.getTransaction(transactionId)
|
|
|
|
t.truthy(conn._req.calledWith(
|
|
|
|
expectedPath,
|
2017-06-19 14:56:00 +02:00
|
|
|
{ urlTemplateSpec: { transactionId } }
|
2017-06-19 14:40:22 +02:00
|
|
|
))
|
|
|
|
})
|
|
|
|
|
|
|
|
test('Get list of blocks for a transaction id', t => {
|
|
|
|
const expectedPath = 'path'
|
|
|
|
const transactionId = 'abc'
|
|
|
|
|
|
|
|
conn._req = sinon.spy()
|
2018-08-22 10:10:09 +02:00
|
|
|
Connection.getApiUrls = sinon.stub().returns(expectedPath)
|
2017-06-19 14:40:22 +02:00
|
|
|
|
2018-03-21 11:30:54 +01:00
|
|
|
conn.listBlocks(transactionId)
|
2017-06-19 14:40:22 +02:00
|
|
|
t.truthy(conn._req.calledWith(
|
|
|
|
expectedPath,
|
|
|
|
{
|
|
|
|
query: {
|
|
|
|
transaction_id: transactionId,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
))
|
|
|
|
})
|
|
|
|
|
2017-06-21 11:01:28 +02:00
|
|
|
test('Get list of transactions for an asset id', t => {
|
|
|
|
const expectedPath = 'path'
|
|
|
|
const assetId = 'abc'
|
|
|
|
const operation = 'operation'
|
|
|
|
|
|
|
|
conn._req = sinon.spy()
|
2018-08-22 10:10:09 +02:00
|
|
|
Connection.getApiUrls = sinon.stub().returns(expectedPath)
|
2017-06-21 11:01:28 +02:00
|
|
|
|
|
|
|
conn.listTransactions(assetId, operation)
|
|
|
|
t.truthy(conn._req.calledWith(
|
|
|
|
expectedPath,
|
|
|
|
{
|
|
|
|
query: {
|
|
|
|
asset_id: assetId,
|
|
|
|
operation
|
|
|
|
}
|
|
|
|
}
|
|
|
|
))
|
|
|
|
})
|
|
|
|
|
2017-06-20 17:46:25 +02:00
|
|
|
test('Get outputs for a public key and no spent flag', t => {
|
|
|
|
const expectedPath = 'path'
|
|
|
|
const publicKey = 'publicKey'
|
|
|
|
|
|
|
|
conn._req = sinon.spy()
|
2018-08-22 10:10:09 +02:00
|
|
|
Connection.getApiUrls = sinon.stub().returns(expectedPath)
|
2017-06-20 17:46:25 +02:00
|
|
|
|
|
|
|
conn.listOutputs(publicKey)
|
|
|
|
t.truthy(conn._req.calledWith(
|
|
|
|
expectedPath,
|
|
|
|
{ query: { public_key: publicKey } }
|
|
|
|
))
|
|
|
|
})
|
|
|
|
|
|
|
|
test('Get outputs for a public key and spent=false', t => {
|
|
|
|
const expectedPath = 'path'
|
|
|
|
const publicKey = 'publicKey'
|
|
|
|
const spent = false
|
|
|
|
|
|
|
|
conn._req = sinon.spy()
|
2018-08-22 10:10:09 +02:00
|
|
|
Connection.getApiUrls = sinon.stub().returns(expectedPath)
|
2017-06-20 17:46:25 +02:00
|
|
|
|
|
|
|
conn.listOutputs(publicKey, spent)
|
|
|
|
t.truthy(conn._req.calledWith(
|
|
|
|
expectedPath,
|
2017-06-21 15:59:28 +02:00
|
|
|
{ query: { public_key: publicKey, spent: 'false' } }
|
2017-06-20 17:46:25 +02:00
|
|
|
))
|
|
|
|
})
|
|
|
|
|
|
|
|
test('Get outputs for a public key and spent=true', t => {
|
|
|
|
const expectedPath = 'path'
|
|
|
|
const publicKey = 'publicKey'
|
|
|
|
const spent = true
|
|
|
|
|
|
|
|
conn._req = sinon.spy()
|
2018-08-22 10:10:09 +02:00
|
|
|
Connection.getApiUrls = sinon.stub().returns(expectedPath)
|
2017-06-20 17:46:25 +02:00
|
|
|
|
|
|
|
conn.listOutputs(publicKey, spent)
|
|
|
|
t.truthy(conn._req.calledWith(
|
|
|
|
expectedPath,
|
2017-06-21 15:59:28 +02:00
|
|
|
{ query: { public_key: publicKey, spent: 'true' } }
|
2017-06-20 17:46:25 +02:00
|
|
|
))
|
|
|
|
})
|
|
|
|
|
2017-06-20 16:17:43 +02:00
|
|
|
test('Get asset for text', t => {
|
2017-06-19 14:56:00 +02:00
|
|
|
const expectedPath = 'path'
|
2017-06-20 16:17:43 +02:00
|
|
|
const search = 'abc'
|
2017-06-19 14:56:00 +02:00
|
|
|
|
|
|
|
conn._req = sinon.spy()
|
2018-08-22 10:10:09 +02:00
|
|
|
Connection.getApiUrls = sinon.stub().returns(expectedPath)
|
2017-06-19 14:56:00 +02:00
|
|
|
|
2017-06-20 16:17:43 +02:00
|
|
|
conn.searchAssets(search)
|
2017-06-19 14:56:00 +02:00
|
|
|
t.truthy(conn._req.calledWith(
|
|
|
|
expectedPath,
|
2017-06-20 16:17:43 +02:00
|
|
|
{ query: { search } }
|
2017-06-19 14:56:00 +02:00
|
|
|
))
|
|
|
|
})
|
2017-11-21 16:51:31 +01:00
|
|
|
|
|
|
|
test('Get metadata for text', t => {
|
|
|
|
const expectedPath = 'path'
|
|
|
|
const search = 'abc'
|
|
|
|
|
|
|
|
conn._req = sinon.spy()
|
2018-08-22 10:10:09 +02:00
|
|
|
Connection.getApiUrls = sinon.stub().returns(expectedPath)
|
2017-11-21 16:51:31 +01:00
|
|
|
|
|
|
|
conn.searchMetadata(search)
|
|
|
|
t.truthy(conn._req.calledWith(
|
|
|
|
expectedPath,
|
|
|
|
{ query: { search } }
|
|
|
|
))
|
|
|
|
})
|