From bfb4331d3e00aab9420b919f46336e0d4894a8c0 Mon Sep 17 00:00:00 2001 From: Jerko J <83344666+JJ-Cro@users.noreply.github.com> Date: Thu, 19 Sep 2024 13:42:22 +0200 Subject: [PATCH] feat(): added all client tests --- test/CBExchangeClient/private.test.ts | 83 ++++++++++++++++++++++ test/CBExchangeClient/public.test.ts | 2 +- test/CBInternationalClient/private.test.ts | 78 ++++++++++++++++++++ test/CBInternationalClient/public.test.ts | 2 +- test/CBPrimeClient/private.test.ts | 70 ++++++++++++++++++ 5 files changed, 233 insertions(+), 2 deletions(-) create mode 100644 test/CBExchangeClient/private.test.ts create mode 100644 test/CBInternationalClient/private.test.ts create mode 100644 test/CBPrimeClient/private.test.ts diff --git a/test/CBExchangeClient/private.test.ts b/test/CBExchangeClient/private.test.ts new file mode 100644 index 0000000..bcfab43 --- /dev/null +++ b/test/CBExchangeClient/private.test.ts @@ -0,0 +1,83 @@ +import { CBExchangeClient } from '../../src/index.js'; + +describe('CBExchangeClient PRIVATE', () => { + const account = { + key: process.env.API_KEY_NAME, + secret: process.env.API_PRIVATE_KEY, + passphrase: process.env.API_PASSPHRASE, + }; + + const rest = new CBExchangeClient({ + apiKey: account.key, + apiSecret: account.secret, + apiPassphrase: account.passphrase, + }); + + it('should have credentials to test with', () => { + expect(account.key).toBeDefined(); + expect(account.secret).toBeDefined(); + }); + + describe('public endpoints', () => { + it('should succeed making a GET request', async () => { + const res = await rest.getProductBook({ product_id: 'BTC-USDT' }); + //console.log(res); + expect(res).toMatchObject({ + bids: expect.any(Array), + asks: expect.any(Array), + sequence: expect.any(Number), + auction_mode: expect.any(Boolean), + auction: expect.any(Object), + time: expect.any(String), + }); + }); + }); + + describe('private endpoints', () => { + describe('GET requests', () => { + test('without params', async () => { + const res = await rest.getFees(); + expect(res).toMatchObject({ + taker_fee_rate: expect.any(String), + maker_fee_rate: expect.any(String), + usd_volume: expect.any(String), + }); + }); + + test('with params', async () => { + const res = await rest.getNewLoanPreview({ + currency: 'USDC', + native_amount: '100', + }); + //console.log('res with params', res); + expect(res).toMatchObject({ + overview: expect.any(Object), + loans: expect.any(Array), + }); + }); + }); + + describe('POST requests', () => { + test('with params as request body', async () => { + try { + const res = await rest.createProfile({ + name: 'testProfile', + }); + + console.log(`res "${expect.getState().currentTestName}"`, res); + expect(res).toMatchObject({ + whatever: true, + }); + } catch (e: any) { + // These are deliberatly restricted API keys. If the response is a permission error, it confirms the sign + request was OK and permissions were denied. + // console.log(`err "${expect.getState().currentTestName}"`, e?.body); + console.log('Error, CBExchange, post req:', e); + const responseBody = e?.body; + expect(responseBody).toMatchObject({ + message: 'Unauthorized.', + }); + } + }); + }); + }); +}); diff --git a/test/CBExchangeClient/public.test.ts b/test/CBExchangeClient/public.test.ts index c330c03..093f2aa 100644 --- a/test/CBExchangeClient/public.test.ts +++ b/test/CBExchangeClient/public.test.ts @@ -6,7 +6,7 @@ describe('CBExchangeClient PUBLIC', () => { describe('public endpoints', () => { it('should succeed making a GET request', async () => { const res = await rest.getProductBook({ product_id: 'BTC-USDT' }); - console.log(res); + //console.log(res); expect(res).toMatchObject({ bids: expect.any(Array), asks: expect.any(Array), diff --git a/test/CBInternationalClient/private.test.ts b/test/CBInternationalClient/private.test.ts new file mode 100644 index 0000000..a2a6926 --- /dev/null +++ b/test/CBInternationalClient/private.test.ts @@ -0,0 +1,78 @@ +import { CBInternationalClient } from '../../src/index.js'; + +describe('CBInternationalClient PRIVATE', () => { + const account = { + key: process.env.API_KEY_NAME, + secret: process.env.API_PRIVATE_KEY, + passphrase: process.env.API_PASSPHRASE, + }; + + const rest = new CBInternationalClient({ + apiKey: account.key, + apiSecret: account.secret, + apiPassphrase: account.passphrase, + }); + + it('should have credentials to test with', () => { + expect(account.key).toBeDefined(); + expect(account.secret).toBeDefined(); + }); + + describe('public endpoints', () => { + it('should succeed making a GET request', async () => { + const res = await rest.getHistoricalFundingRates({ + instrument: 'BTC-PERP', + }); + //console.log(res); + expect(res).toMatchObject({ + pagination: expect.any(Object), + results: expect.any(Array), + }); + }); + }); + + describe('private endpoints', () => { + describe('GET requests', () => { + test('without params', async () => { + const res = await rest.getOpenOrders({}); + expect(res).toMatchObject({ + pagination: expect.any(Object), + results: expect.any(Array), + }); + }); + + test('with params', async () => { + const res = await rest.getRankings({ instrument_type: 'spot' }); + expect(res).toMatchObject({ + last_updated: expect.any(String), + statistics: expect.any(Object), + }); + }); + }); + + describe('POST requests', () => { + test('with params as request body', async () => { + try { + const res = await rest.createPortfolio({ + name: 'testPorftolio', + }); + + console.log(`res "${expect.getState().currentTestName}"`, res); + expect(res).toMatchObject({ + whatever: true, + }); + } catch (e: any) { + // These are deliberatly restricted API keys. If the response is a permission error, it confirms the sign + request was OK and permissions were denied. + // console.log(`err "${expect.getState().currentTestName}"`, e?.body); + console.log('Error, CBINTX, post req:', e); + const responseBody = e?.body; + expect(responseBody).toMatchObject({ + title: 'Unauthorized', + status: 401, + error: 'UNAUTHORIZED', + }); + } + }); + }); + }); +}); diff --git a/test/CBInternationalClient/public.test.ts b/test/CBInternationalClient/public.test.ts index 4ff15a9..94a84c5 100644 --- a/test/CBInternationalClient/public.test.ts +++ b/test/CBInternationalClient/public.test.ts @@ -8,7 +8,7 @@ describe('CBInternationalClient PUBLIC', () => { const res = await rest.getHistoricalFundingRates({ instrument: 'BTC-PERP', }); - console.log(res); + //console.log(res); expect(res).toMatchObject({ pagination: expect.any(Object), results: expect.any(Array), diff --git a/test/CBPrimeClient/private.test.ts b/test/CBPrimeClient/private.test.ts new file mode 100644 index 0000000..a31f78b --- /dev/null +++ b/test/CBPrimeClient/private.test.ts @@ -0,0 +1,70 @@ +import { CBPrimeClient } from '../../src/index.js'; + +describe('CBPrimeClient PRIVATE', () => { + const account = { + key: process.env.API_KEY_NAME, + secret: process.env.API_PRIVATE_KEY, + passphrase: process.env.API_PASSPHRASE, + }; + + const rest = new CBPrimeClient({ + apiKey: account.key, + apiSecret: account.secret, + apiPassphrase: account.passphrase, + }); + + let portfolioId: string; + + it('should have credentials to test with', () => { + expect(account.key).toBeDefined(); + expect(account.secret).toBeDefined(); + }); + + describe('private endpoints', () => { + describe('GET requests', () => { + test('without params', async () => { + const res = await rest.getPortfolios(); + expect(res).toMatchObject({ + portfolios: expect.any(Array), + }); + portfolioId = res.portfolios[0].id; + }); + + test('with params', async () => { + const res = await rest.getPortfolioById({ portfolio_id: portfolioId }); + expect(res).toMatchObject({ + portfolio: expect.any(Object), + }); + }); + }); + + describe('POST requests', () => { + test('with params as request body', async () => { + try { + const res = await rest.getOrderPreview({ + portfolio_id: portfolioId, + side: 'BUY', + product_id: 'BTC-USDT', + type: 'MARKET', + quote_value: '1', + }); + + console.log(`res "${expect.getState().currentTestName}"`, res); + expect(res).toMatchObject({ + whatever: true, + }); + } catch (e: any) { + // These are deliberatly restricted API keys. If the response is a permission error, it confirms the sign + request was OK and permissions were denied. + // console.log(`err "${expect.getState().currentTestName}"`, e?.body); + console.log('Error, CBPrime, post req:', e); + const responseBody = e?.body; + expect(responseBody).toMatchObject({ + title: 'Unauthorized', + status: 401, + error: 'UNAUTHORIZED', + }); + } + }); + }); + }); +});