-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Working on buildJWT tests now, made great progress on the protocol cl…
…ients
- Loading branch information
Showing
11 changed files
with
1,633 additions
and
114 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { expect } from 'chai'; | ||
import sinon from 'sinon'; | ||
import * as fs from 'fs'; | ||
import { JWK, JWS } from 'node-jose'; | ||
import { buildJWT } from './index'; | ||
|
||
describe('buildJWT', () => { | ||
let readFileSyncStub: sinon.SinonStub; | ||
let asKeyStub: sinon.SinonStub; | ||
let signStub: sinon.SinonStub; | ||
|
||
beforeEach(() => { | ||
readFileSyncStub = sinon.stub(fs, 'readFileSync'); | ||
asKeyStub = sinon.stub(JWK, 'asKey'); | ||
signStub = sinon.stub(JWS, 'createSign'); | ||
}); | ||
|
||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
|
||
it('should build a JWT with provided API key and private key', async () => { | ||
const url = 'https://api.example.com/resource'; | ||
const method = 'POST'; | ||
const apiKeyName = 'test-api-key'; | ||
const apiPrivateKey = 'test-private-key'; | ||
const pemPrivateKey = | ||
'-----BEGIN EC PRIVATE KEY-----\ntest-private-key\n-----END EC PRIVATE KEY-----'; | ||
const privateKey = { kty: 'EC' }; | ||
|
||
asKeyStub.resolves(privateKey); | ||
signStub.returns({ | ||
update: sinon.stub().returnsThis(), | ||
final: sinon.stub().resolves('signed-jwt'), | ||
}); | ||
|
||
const jwt = await buildJWT(url, method, apiKeyName, apiPrivateKey); | ||
|
||
expect(asKeyStub.calledOnceWithExactly(pemPrivateKey, 'pem')).to.be.true; | ||
expect(signStub.calledOnce).to.be.true; | ||
expect(jwt).to.equal('signed-jwt'); | ||
}); | ||
|
||
it('should build a JWT with API key from file', async () => { | ||
const url = 'https://api.example.com/resource'; | ||
const method = 'POST'; | ||
const apiKey = { | ||
name: 'test-api-key', | ||
privateKey: 'test-private-key', | ||
}; | ||
const pemPrivateKey = | ||
'-----BEGIN EC PRIVATE KEY-----\ntest-private-key\n-----END EC PRIVATE KEY-----'; | ||
const privateKey = { kty: 'EC' }; | ||
|
||
readFileSyncStub.returns(JSON.stringify(apiKey)); | ||
asKeyStub.resolves(privateKey); | ||
signStub.returns({ | ||
update: sinon.stub().returnsThis(), | ||
final: sinon.stub().resolves('signed-jwt'), | ||
}); | ||
|
||
const jwt = await buildJWT(url, method); | ||
|
||
expect( | ||
readFileSyncStub.calledOnceWithExactly('.coinbase_cloud_api_key.json', { | ||
encoding: 'utf8', | ||
}), | ||
).to.be.true; | ||
expect(asKeyStub.calledOnceWithExactly(pemPrivateKey, 'pem')).to.be.true; | ||
expect(signStub.calledOnce).to.be.true; | ||
expect(jwt).to.equal('signed-jwt'); | ||
}); | ||
|
||
it('should throw an error if private key is not EC', async () => { | ||
const url = 'https://api.example.com/resource'; | ||
const method = 'POST'; | ||
const apiKeyName = 'test-api-key'; | ||
const apiPrivateKey = 'test-private-key'; | ||
const pemPrivateKey = | ||
'-----BEGIN EC PRIVATE KEY-----\ntest-private-key\n-----END EC PRIVATE KEY-----'; | ||
const privateKey = { kty: 'RSA' }; | ||
|
||
asKeyStub.resolves(privateKey); | ||
|
||
try { | ||
await buildJWT(url, method, apiKeyName, apiPrivateKey); | ||
expect.fail('Expected buildJWT to throw an error'); | ||
} catch (error) { | ||
expect((error as Error).message).to.equal('Not an EC private key'); | ||
} | ||
|
||
expect(asKeyStub.calledOnceWithExactly(pemPrivateKey, 'pem')).to.be.true; | ||
expect(signStub.notCalled).to.be.true; | ||
}); | ||
|
||
it('should throw an error if private key cannot be parsed', async () => { | ||
const url = 'https://api.example.com/resource'; | ||
const method = 'POST'; | ||
const apiKeyName = 'test-api-key'; | ||
const apiPrivateKey = 'test-private-key'; | ||
const pemPrivateKey = | ||
'-----BEGIN EC PRIVATE KEY-----\ntest-private-key\n-----END EC PRIVATE KEY-----'; | ||
|
||
asKeyStub.rejects(new Error('Invalid key')); | ||
|
||
try { | ||
await buildJWT(url, method, apiKeyName, apiPrivateKey); | ||
expect.fail('Expected buildJWT to throw an error'); | ||
} catch (error) { | ||
expect((error as Error).message).to.include( | ||
'jwt: Could not decode or parse private key. Invalid key', | ||
); | ||
} | ||
|
||
expect(asKeyStub.calledOnceWithExactly(pemPrivateKey, 'pem')).to.be.true; | ||
expect(signStub.notCalled).to.be.true; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { expect } from 'chai'; | ||
import sinon from 'sinon'; | ||
import { Cosmos } from './cosmos-staking'; | ||
import { StakingClient } from '../staking-client'; | ||
import { | ||
ListRewardsRequest, | ||
ListRewardsResponse, | ||
} from '../../gen/coinbase/staking/rewards/v1/reward.pb'; | ||
import { | ||
ListStakesRequest, | ||
ListStakesResponse, | ||
} from '../../gen/coinbase/staking/rewards/v1/stake.pb'; | ||
|
||
describe('Cosmos', () => { | ||
let stakingClientStub: sinon.SinonStubbedInstance<StakingClient>; | ||
let cosmos: Cosmos; | ||
|
||
beforeEach(() => { | ||
stakingClientStub = sinon.createStubInstance(StakingClient); | ||
cosmos = new Cosmos(stakingClientStub); | ||
}); | ||
|
||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
|
||
describe('listRewards', () => { | ||
it('should list rewards with the correct parameters', async () => { | ||
const filter = 'some-filter'; | ||
const pageSize = 50; | ||
const pageToken = 'some-token'; | ||
const req: ListRewardsRequest = { | ||
parent: 'protocols/cosmos', | ||
filter: filter, | ||
pageSize: pageSize, | ||
pageToken: pageToken, | ||
}; | ||
const res: ListRewardsResponse = { | ||
/* response data */ | ||
}; | ||
|
||
stakingClientStub.listRewards.resolves(res); | ||
|
||
const response = await cosmos.listRewards(filter, pageSize, pageToken); | ||
|
||
expect(stakingClientStub.listRewards.calledOnceWithExactly('cosmos', req)) | ||
.to.be.true; | ||
expect(response).to.deep.equal(res); | ||
}); | ||
|
||
it('should use default parameters when not provided', async () => { | ||
const filter = 'some-filter'; | ||
const req: ListRewardsRequest = { | ||
parent: 'protocols/cosmos', | ||
filter: filter, | ||
pageSize: 100, | ||
pageToken: undefined, | ||
}; | ||
const res: ListRewardsResponse = { | ||
/* response data */ | ||
}; | ||
|
||
stakingClientStub.listRewards.resolves(res); | ||
|
||
const response = await cosmos.listRewards(filter); | ||
|
||
expect(stakingClientStub.listRewards.calledOnceWithExactly('cosmos', req)) | ||
.to.be.true; | ||
expect(response).to.deep.equal(res); | ||
}); | ||
}); | ||
|
||
describe('listStakes', () => { | ||
it('should list stakes with the correct parameters', async () => { | ||
const filter = 'some-filter'; | ||
const pageSize = 50; | ||
const pageToken = 'some-token'; | ||
const req: ListStakesRequest = { | ||
parent: 'protocols/cosmos', | ||
filter: filter, | ||
pageSize: pageSize, | ||
pageToken: pageToken, | ||
}; | ||
const res: ListStakesResponse = { | ||
/* response data */ | ||
}; | ||
|
||
stakingClientStub.listStakes.resolves(res); | ||
|
||
const response = await cosmos.listStakes(filter, pageSize, pageToken); | ||
|
||
expect(stakingClientStub.listStakes.calledOnceWithExactly('cosmos', req)) | ||
.to.be.true; | ||
expect(response).to.deep.equal(res); | ||
}); | ||
|
||
it('should use default parameters when not provided', async () => { | ||
const filter = 'some-filter'; | ||
const req: ListStakesRequest = { | ||
parent: 'protocols/cosmos', | ||
filter: filter, | ||
pageSize: 100, | ||
pageToken: undefined, | ||
}; | ||
const res: ListStakesResponse = { | ||
/* response data */ | ||
}; | ||
|
||
stakingClientStub.listStakes.resolves(res); | ||
|
||
const response = await cosmos.listStakes(filter); | ||
|
||
expect(stakingClientStub.listStakes.calledOnceWithExactly('cosmos', req)) | ||
.to.be.true; | ||
expect(response).to.deep.equal(res); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.