Skip to content

Commit

Permalink
test: add Unauthorized test
Browse files Browse the repository at this point in the history
  • Loading branch information
izayl committed Aug 8, 2022
1 parent 9bb8356 commit 333931e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
26 changes: 23 additions & 3 deletions __tests__/v2/balances.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,24 @@ describe('balances | Gets balances of different supported applications for a spe
mockClient.sendRequest = request
}

describe('Auth', () => {
const parameters = {
networks: [Network.ETHEREUM_MAINNET],
addresses: ['0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D'],
}
it('should throw error when no apiKey is provided', () => {
const client = new V2Client({ apiKey: 'foo' })
client.balances.get(parameters, err => {
expect(err).toBeDefined()
})
})
it('should not throw an error when an apiKey is provided', () => {
client.balances.get(parameters, err => {
expect(err).toBeNull()
})
})
})

describe('GET /v2/apps/{appId}/balance', () => {
beforeEach(resetMock)

Expand Down Expand Up @@ -66,15 +84,15 @@ describe('balances | Gets balances of different supported applications for a spe
addresses: ['0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D'],
}

it('should not use axios client', (done) => {
it('should not use axios client', done => {
mockClient.balances.get(parameters, (err, data) => {
if (err) done(err)
if (data?.type === 'full') done()
})
expect(request).toBeCalledTimes(0)
})

it('should use callback to accept response partially and fully', (done) => {
it('should use callback to accept response partially and fully', done => {
const resp = [] as any[]
const callback: SSECallback<any> = (err, data): void => {
expect(err).toBeNull()
Expand All @@ -99,7 +117,9 @@ describe('balances | Gets balances of different supported applications for a spe
it('should use async/await function to accept fully response', async () => {
const resp1 = await client.balances.get(parameters)
let res: (value: unknown) => void
const p = new Promise((resolve) => { res = resolve })
const p = new Promise(resolve => {
res = resolve
})

const callback: SSECallback<any> = (err, data): void => {
expect(err).toBeNull()
Expand Down
20 changes: 19 additions & 1 deletion __tests__/v2/sse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,26 @@ import { SSEClient } from '../../src/client/sseClient'
import { Network } from '../../src/v2/models'
import { config } from '../config'

jest.setTimeout(15 * 1000)
jest.setTimeout(60 * 1000)
describe('sse client', () => {
it('should response 401 when apiKey not provided', done => {
const balanceSSE = new SSEClient({
pathname: '/v2/balances',
apiKey: 'wrong-api-key',
query: {
addresses: ['0x0d4a11d5eeaac28ec3f61d100daf4d40471f1852'],
network: [Network.ETHEREUM_MAINNET],
},
})

balanceSSE.onError = (evt: any) => {
expect(evt).toBeDefined()
expect(evt.status).toBe(401)
done()
}

balanceSSE.create()
})
it('should received message', done => {
const balanceSSE = new SSEClient({
pathname: '/v2/balances',
Expand Down
2 changes: 1 addition & 1 deletion src/client/sseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class SSEClient {
headers: {
'Content-Type': 'text/event-stream',
'User-Agent': 'Mozilla/5.0',
Authorization: `Basic ${Buffer.from(`${apiKey}: `).toString('base64')}`,
Authorization: `Basic ${Buffer.from(`${apiKey}:`).toString('base64')}`,
...headers,
},
proxy,
Expand Down

0 comments on commit 333931e

Please sign in to comment.