From af49ec1c7a521bc95cf7fea589219a24e1019390 Mon Sep 17 00:00:00 2001 From: tim Date: Mon, 19 Jun 2017 14:40:22 +0200 Subject: [PATCH] Adjust HTTP API endpoints and test --- src/connection/index.js | 44 ++++++++------- test/connection/test_connection.js | 90 ++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+), 21 deletions(-) create mode 100644 test/connection/test_connection.js diff --git a/src/connection/index.js b/src/connection/index.js index 26a241d..7248e4a 100644 --- a/src/connection/index.js +++ b/src/connection/index.js @@ -9,15 +9,15 @@ export default class Connection { getApiUrls(endpoints) { // TODO: Use camel case - return { - 'blocks': `${this.path}blocks`, - 'blocks_detail': `${this.path}blocks/%(blockId)s`, - 'outputs': `${this.path}outputs`, - 'statuses': `${this.path}statuses`, - 'transactions': `${this.path}transactions`, - 'transactions_detail': `${this.path}transactions/%(txId)s`, - 'search_assets': `${this.path}assets`, - 'votes': `${this.path}votes` + return this.path + { + 'blocks': 'blocks', + 'blocks_detail': 'blocks/%(blockId)s', + 'outputs': 'outputs', + 'statuses': 'statuses', + 'transactions': 'transactions', + 'transactions_detail': 'transactions/%(transactionId)s', + 'search_assets': 'assets', + 'votes': 'votes' }[endpoints] } @@ -41,37 +41,37 @@ export default class Connection { /** * @public - * @param tx_id + * @param transactionId */ - getStatus(tx_id) { // eslint-disable-line camelcase + getStatus(transactionId) { return this._req(this.getApiUrls('statuses'), { query: { - tx_id + transaction_id: transactionId } }) } /** * @public - * @param txId + * @param transactionId */ - getTransaction(txId) { + getTransaction(transactionId) { return this._req(this.getApiUrls('transactions_detail'), { urlTemplateSpec: { - txId + transaction_id: transactionId } }) } /** * @public - * @param tx_id + * @param transactionId * @param status */ - listBlocks({ tx_id, status }) { + listBlocks({ transactionId, status }) { return this._req(this.getApiUrls('blocks'), { query: { - tx_id, + transaction_id: transactionId, status } }) @@ -83,6 +83,7 @@ export default class Connection { * @param unspent * @param onlyJsonResponse */ + // TODO: Use camel case for parameters listOutputs({ public_key, unspent }, onlyJsonResponse = true) { return this._req(this.getApiUrls('outputs'), { query: { @@ -97,6 +98,7 @@ export default class Connection { * @param asset_id * @param operation */ + // TODO: Use camel case for parameters listTransactions({ asset_id, operation }) { return this._req(this.getApiUrls('transactions'), { query: { @@ -108,12 +110,12 @@ export default class Connection { /** * @public - * @param block_id + * @param blockId */ - listVotes(block_id) { // eslint-disable-line camelcase + listVotes(blockId) { return this._req(this.getApiUrls('votes'), { query: { - block_id + block_id: blockId } }) } diff --git a/test/connection/test_connection.js b/test/connection/test_connection.js new file mode 100644 index 0000000..301515a --- /dev/null +++ b/test/connection/test_connection.js @@ -0,0 +1,90 @@ +import test from 'ava' +import sinon from 'sinon' +import { Connection } from '../../src' + +const API_PATH = 'http://localhost:9984/api/v1/' +const conn = new Connection(API_PATH) + + +test('generate API URLS', t => { + const endpoints = { + 'blocks': 'blocks', + 'blocks_detail': 'blocks/%(blockId)s', + 'outputs': 'outputs', + 'statuses': 'statuses', + 'transactions': 'transactions', + 'transactions_detail': 'transactions/%(transactionId)s', + 'search_assets': 'assets', + } + Object.keys(endpoints).forEach((endpointName) => { + const url = conn.getApiUrls(endpointName) + const expected = API_PATH + endpoints[endpointName] + t.is(url, expected) + }) +}) + + +test('Get status for a transaction', t => { + const expectedPath = 'path' + const transactionId = 'abc' + + conn._req = sinon.spy() + conn.getApiUrls = sinon.stub().returns(expectedPath) + + conn.getStatus(transactionId) + t.truthy(conn._req.calledWith( + expectedPath, + { query: { transaction_id: transactionId } } + )) +}) + + +test('Get transaction for a transaction id', t => { + const expectedPath = 'path' + const transactionId = 'abc' + + conn._req = sinon.spy() + conn.getApiUrls = sinon.stub().returns(expectedPath) + + conn.getTransaction(transactionId) + t.truthy(conn._req.calledWith( + expectedPath, + { urlTemplateSpec: { transaction_id: transactionId } } + )) +}) + + +test('Get list of blocks for a transaction id', t => { + const expectedPath = 'path' + const transactionId = 'abc' + const status = 'status' + + conn._req = sinon.spy() + conn.getApiUrls = sinon.stub().returns(expectedPath) + + conn.listBlocks({ transactionId, status }) + t.truthy(conn._req.calledWith( + expectedPath, + { + query: { + transaction_id: transactionId, + status + } + } + )) +}) + + +test('Get votes for a block id', t => { + const expectedPath = 'path' + const blockId = 'abc' + + conn._req = sinon.spy() + conn.getApiUrls = sinon.stub().returns(expectedPath) + + conn.listVotes(blockId) + t.truthy(conn._req.calledWith( + expectedPath, + { query: { block_id: blockId } } + )) +})