From bbdf6edad924284a0eb08470fb3a593e7c4ef023 Mon Sep 17 00:00:00 2001 From: tiagosiebler Date: Thu, 24 Jun 2021 00:41:21 +0100 Subject: [PATCH] cleaning and expand public api tests --- src/inverse-futures-client.ts | 3 +- src/linear-client.ts | 5 ++- test/inverse-futures/public.test.ts | 70 +++++++++++++++++++++++++++++ test/inverse/public.test.ts | 6 +-- test/linear/public.test.ts | 70 +++++++++++++++++++++++++++++ test/response.util.ts | 4 ++ 6 files changed, 152 insertions(+), 6 deletions(-) create mode 100644 test/inverse-futures/public.test.ts create mode 100644 test/linear/public.test.ts diff --git a/src/inverse-futures-client.ts b/src/inverse-futures-client.ts index fcd4cd0..9605e19 100644 --- a/src/inverse-futures-client.ts +++ b/src/inverse-futures-client.ts @@ -22,7 +22,8 @@ export class InverseFuturesClient extends SharedEndpoints { restClientOptions: RestClientOptions = {}, requestOptions: AxiosRequestConfig = {} ) { - super() + super(); + this.requestWrapper = new RequestWrapper( key, secret, diff --git a/src/linear-client.ts b/src/linear-client.ts index 04b6b0f..09c86ca 100644 --- a/src/linear-client.ts +++ b/src/linear-client.ts @@ -7,7 +7,7 @@ export class LinearClient extends SharedEndpoints { protected requestWrapper: RequestWrapper; /** - * @public Creates an instance of the inverse REST API client. + * @public Creates an instance of the linear REST API client. * * @param {string} key - your API key * @param {string} secret - your API secret @@ -22,7 +22,8 @@ export class LinearClient extends SharedEndpoints { restClientOptions: RestClientOptions = {}, requestOptions: AxiosRequestConfig = {} ) { - super() + super(); + this.requestWrapper = new RequestWrapper( key, secret, diff --git a/test/inverse-futures/public.test.ts b/test/inverse-futures/public.test.ts new file mode 100644 index 0000000..a0fa5f7 --- /dev/null +++ b/test/inverse-futures/public.test.ts @@ -0,0 +1,70 @@ +import { InverseFuturesClient } from "../../src/inverse-futures-client"; +import { notAuthenticatedError, successResponseList, successResponseObject } from "../response.util"; + +describe('Public Inverse Futures REST API Endpoints', () => { + const useLivenet = true; + const api = new InverseFuturesClient(undefined, undefined, useLivenet); + + const symbol = 'BTCUSD'; + const interval = '15'; + const timestampOneHourAgo = (new Date().getTime() / 1000) - (1000 * 60 * 60); + const from = Number(timestampOneHourAgo.toFixed(0)); + + describe('Inverse-Futures only endpoints', () => { + it('should throw for unauthenticated private calls', async () => { + expect(() => api.getPosition()).rejects.toMatchObject(notAuthenticatedError()); + }); + + it('getKline()', async () => { + expect( + await api.getKline({ symbol, interval, from }) + ).toMatchObject(successResponseList()); + }); + + it('getTrades()', async () => { + expect(await api.getTrades({ symbol })).toMatchObject(successResponseList()); + }); + + it('getIndexPriceKline()', async () => { + expect(await api.getIndexPriceKline({ symbol, interval, from })).toMatchObject(successResponseList()); + }); + + it('getPremiumIndexKline()', async () => { + expect(await api.getPremiumIndexKline({ symbol, interval, from })).toMatchObject(successResponseList()); + }); + + it('getLastFundingRate()', async () => { + expect(await api.getLastFundingRate({ symbol })).toMatchObject(successResponseObject()); + }); + }); + + describe('Shared endpoints', () => { + it('should throw for unauthenticated private calls', async () => { + expect(() => api.getApiKeyInfo()).rejects.toMatchObject(notAuthenticatedError()); + }); + + it('getOrderBook()', async () => { + expect(await api.getOrderBook({ symbol })).toMatchObject(successResponseList()); + }); + + it('getTickers()', async () => { + expect(await api.getTickers()).toMatchObject(successResponseList()); + }); + + it('getSymbols()', async () => { + expect(await api.getSymbols()).toMatchObject(successResponseList()); + }); + + it('getLiquidations()', async () => { + expect(await api.getLiquidations({ symbol })).toMatchObject(successResponseList()); + }); + + it('getServerTime()', async () => { + expect(await api.getServerTime()).toMatchObject(successResponseObject()); + }); + + it('getApiAnnouncements()', async () => { + expect(await api.getApiAnnouncements()).toMatchObject(successResponseList()); + }); + }); +}); \ No newline at end of file diff --git a/test/inverse/public.test.ts b/test/inverse/public.test.ts index 75af062..3712c93 100644 --- a/test/inverse/public.test.ts +++ b/test/inverse/public.test.ts @@ -1,5 +1,5 @@ import { InverseClient } from "../../src/inverse-client"; -import { successResponseList, successResponseObject } from "../response.util"; +import { notAuthenticatedError, successResponseList, successResponseObject } from "../response.util"; describe('Public Inverse REST API Endpoints', () => { const useLivenet = true; @@ -12,7 +12,7 @@ describe('Public Inverse REST API Endpoints', () => { describe('Inverse only endpoints', () => { it('should throw for unauthenticated private calls', async () => { - expect(() => api.getPosition()).rejects.toMatchObject(new Error('Private endpoints require api and private keys set')); + expect(() => api.getPosition()).rejects.toMatchObject(notAuthenticatedError()); }); it('getKline()', async () => { @@ -40,7 +40,7 @@ describe('Public Inverse REST API Endpoints', () => { describe('Shared endpoints', () => { it('should throw for unauthenticated private calls', async () => { - expect(() => api.getApiKeyInfo()).rejects.toMatchObject(new Error('Private endpoints require api and private keys set')); + expect(() => api.getApiKeyInfo()).rejects.toMatchObject(notAuthenticatedError()); }); it('getOrderBook()', async () => { diff --git a/test/linear/public.test.ts b/test/linear/public.test.ts new file mode 100644 index 0000000..b401d31 --- /dev/null +++ b/test/linear/public.test.ts @@ -0,0 +1,70 @@ +import { LinearClient } from "../../src/linear-client"; +import { notAuthenticatedError, successResponseList, successResponseObject } from "../response.util"; + +describe('Public Linear REST API Endpoints', () => { + const useLivenet = true; + const api = new LinearClient(undefined, undefined, useLivenet); + + const symbol = 'BTCUSDT'; + const interval = '15'; + const timestampOneHourAgo = (new Date().getTime() / 1000) - (1000 * 60 * 60); + const from = Number(timestampOneHourAgo.toFixed(0)); + + describe('Linear only endpoints', () => { + it('should throw for unauthenticated private calls', async () => { + expect(() => api.getPosition()).rejects.toMatchObject(notAuthenticatedError()); + }); + + it('getKline()', async () => { + expect( + await api.getKline({ symbol, interval, from }) + ).toMatchObject(successResponseList()); + }); + + it('getTrades()', async () => { + expect(await api.getTrades({ symbol })).toMatchObject(successResponseList()); + }); + + it('getIndexPriceKline()', async () => { + expect(await api.getIndexPriceKline({ symbol, interval, from })).toMatchObject(successResponseList()); + }); + + it('getPremiumIndexKline()', async () => { + expect(await api.getPremiumIndexKline({ symbol, interval, from })).toMatchObject(successResponseList()); + }); + + it('getLastFundingRate()', async () => { + expect(await api.getLastFundingRate({ symbol })).toMatchObject(successResponseObject()); + }); + }); + + describe('Shared endpoints', () => { + it('should throw for unauthenticated private calls', async () => { + expect(() => api.getApiKeyInfo()).rejects.toMatchObject(notAuthenticatedError()); + }); + + it('getOrderBook()', async () => { + expect(await api.getOrderBook({ symbol })).toMatchObject(successResponseList()); + }); + + it('getTickers()', async () => { + expect(await api.getTickers()).toMatchObject(successResponseList()); + }); + + it('getSymbols()', async () => { + expect(await api.getSymbols()).toMatchObject(successResponseList()); + }); + + it('getLiquidations()', async () => { + expect(await api.getLiquidations({ symbol })).toMatchObject(successResponseList()); + }); + + it('getServerTime()', async () => { + expect(await api.getServerTime()).toMatchObject(successResponseObject()); + }); + + it('getApiAnnouncements()', async () => { + expect(await api.getApiAnnouncements()).toMatchObject(successResponseList()); + }); + }); +}); \ No newline at end of file diff --git a/test/response.util.ts b/test/response.util.ts index bf189d0..6bc3974 100644 --- a/test/response.util.ts +++ b/test/response.util.ts @@ -20,3 +20,7 @@ export function successResponseObject() { "time_now": expect.any(String), }; }; + +export function notAuthenticatedError() { + return new Error('Private endpoints require api and private keys set'); +};