-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/improvingTest' into develop
- Loading branch information
Showing
24 changed files
with
692 additions
and
178 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
25 changes: 25 additions & 0 deletions
25
src/__tests__/acceptance/broadcast.controller.acceptance.ts
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,25 @@ | ||
import {Client} from '@loopback/testlab'; | ||
import {TwpapiApplication} from '../..'; | ||
import {setupApplication} from './test-helper'; | ||
|
||
describe('Broadcast Controller', () => { | ||
let app: TwpapiApplication; | ||
let client: Client; | ||
|
||
before('setupApplication', async () => { | ||
({app, client} = await setupApplication()); | ||
}); | ||
|
||
after(async () => { | ||
await app.stop(); | ||
}); | ||
|
||
it('invokes POST /broadcast with value on hex', async () => { | ||
await client | ||
.post('/broadcast') | ||
.send({ | ||
data: 'value on hex', | ||
}) | ||
.expect(400); | ||
}); | ||
}); |
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
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,28 @@ | ||
import {expect} from '@loopback/testlab'; | ||
import {AccountBalance} from '../../models'; | ||
import { | ||
getLegacyAddressList, | ||
getNativeSegwitAddressList, | ||
getSewitAddressList, | ||
} from '../helper'; | ||
import * as constants from '../../constants'; | ||
|
||
describe('Account Balance Model ', () => { | ||
it('Validate the account type given an address', () => { | ||
getLegacyAddressList().forEach(walletAddress => { | ||
expect(AccountBalance.getAccountType(walletAddress.address)).to.eql( | ||
constants.BITCOIN_LEGACY_ADDRESS, | ||
); | ||
}); | ||
getNativeSegwitAddressList().forEach(walletAddress => { | ||
expect(AccountBalance.getAccountType(walletAddress.address)).to.eql( | ||
constants.BITCOIN_NATIVE_SEGWIT_ADDRESS, | ||
); | ||
}); | ||
getSewitAddressList().forEach(walletAddress => { | ||
expect(AccountBalance.getAccountType(walletAddress.address)).to.eql( | ||
constants.BITCOIN_SEGWIT_ADDRESS, | ||
); | ||
}); | ||
}); | ||
}); |
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,61 @@ | ||
import { | ||
createStubInstance, | ||
expect, | ||
StubbedInstanceWithSinonAccessor, | ||
} from '@loopback/testlab'; | ||
import {SessionRepository} from '../../repositories'; | ||
import {TxFeeController} from '../../controllers'; | ||
import {FeeLevel} from '../../services'; | ||
import {sinon} from '@loopback/testlab/dist/sinon'; | ||
import {getMockInputs, getUtxoList} from '../helper'; | ||
import {FeeRequestData} from '../../models'; | ||
import * as constants from '../../constants'; | ||
import {config} from 'dotenv'; | ||
|
||
config(); | ||
|
||
describe('Session Repository', () => { | ||
let controller: TxFeeController; | ||
let feeLevelService: FeeLevel; | ||
let feeProvider: sinon.SinonStub; | ||
let findAccountUtxos: sinon.SinonStub; | ||
let getAccountInputs: sinon.SinonStub; | ||
let sessionRepository: StubbedInstanceWithSinonAccessor<SessionRepository>; | ||
beforeEach(resetRepositories); | ||
|
||
function resetRepositories() { | ||
feeLevelService = {feeProvider: sinon.stub()}; | ||
feeProvider = feeLevelService.feeProvider as sinon.SinonStub; | ||
sessionRepository = createStubInstance(SessionRepository); | ||
findAccountUtxos = sessionRepository.findAccountUtxos as sinon.SinonStub; | ||
getAccountInputs = sessionRepository.getAccountInputs as sinon.SinonStub; | ||
controller = new TxFeeController(sessionRepository, feeLevelService); | ||
} | ||
|
||
it('should return the tx fee given provided utxos', async () => { | ||
const fast: number = process.env.FAST_MINING_BLOCK | ||
? +process.env.FAST_MINING_BLOCK | ||
: 1; | ||
const average: number = process.env.AVERAGE_MINING_BLOCK | ||
? +process.env.AVERAGE_MINING_BLOCK | ||
: 6; | ||
const slow: number = process.env.LOW_MINING_BLOCK | ||
? +process.env.LOW_MINING_BLOCK | ||
: 12; | ||
feeProvider.withArgs(fast).resolves(['0.00003']); | ||
feeProvider.withArgs(average).resolves(['0.00002']); | ||
feeProvider.withArgs(slow).resolves(['0.00001']); | ||
findAccountUtxos.resolves(getUtxoList()); | ||
getAccountInputs.resolves(getMockInputs()); | ||
const sessionId = 'test_id'; | ||
const request = new FeeRequestData({ | ||
sessionId, | ||
amount: 0.00004, | ||
accountType: constants.BITCOIN_LEGACY_ADDRESS, | ||
}); | ||
const feeAmount = await controller.getTxFee(request); | ||
expect(feeAmount.slow).to.eql(338000.00000000006); | ||
expect(feeAmount.average).to.eql(676000.0000000001); | ||
expect(feeAmount.fast).to.eql(1014000); | ||
}); | ||
}); |
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,42 @@ | ||
import {inject} from '@loopback/core'; | ||
import {Broadcast} from '../services'; | ||
import {post, getModelSchemaRef, requestBody} from '@loopback/rest'; | ||
import {BroadcastRequest, BroadcastResponse} from '../models'; | ||
|
||
export class BroadcastController { | ||
constructor( | ||
@inject('services.Broadcast') | ||
protected broadcastProvider: Broadcast, | ||
) {} | ||
|
||
@post('/broadcast', { | ||
responses: { | ||
'201': { | ||
description: 'a Tx Broadcast', | ||
content: { | ||
'application/json': { | ||
schema: getModelSchemaRef(BroadcastResponse), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
sendTx( | ||
@requestBody({schema: getModelSchemaRef(BroadcastRequest)}) | ||
req: BroadcastRequest, | ||
): Promise<BroadcastResponse> { | ||
return new Promise<BroadcastResponse>((resolve, reject) => { | ||
this.broadcastProvider | ||
.broadcast(req.data) | ||
.then(([txStatus]) => { | ||
resolve( | ||
new BroadcastResponse({ | ||
txId: txStatus.result ?? '', | ||
error: txStatus.error ?? undefined, | ||
}), | ||
); | ||
}) | ||
.catch(reject); | ||
}); | ||
} | ||
} |
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
Oops, something went wrong.