This repository has been archived by the owner on Oct 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HYP-72: unit tests transactions controller (#73)
* HYP-72: unit tests transactions controller * HYP-72: version bump.
- Loading branch information
Showing
6 changed files
with
140 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 |
---|---|---|
@@ -1,7 +1,36 @@ | ||
import { expect } from 'chai' | ||
|
||
describe('health controller', () => { | ||
it('placeholder', () => { | ||
expect(1).to.equal(1) | ||
import Ipfs from '../../../lib/ipfs.js' | ||
import Database from '../../../lib/db/index.js' | ||
import Identity from '../../../lib/services/identity.js' | ||
import ChainNode from '../../../lib/chainNode.js' | ||
import { HealthController } from '../index.js' | ||
import { Env } from '../../../env.js' | ||
import { ServiceWatcher } from '../../../lib/service-watcher/index.js' | ||
import { ServiceUnavailable } from '../../../lib/error-handler/index.js' | ||
|
||
describe('/health', () => { | ||
let response: any | ||
let controller: HealthController | ||
|
||
const database: Database = new Database() | ||
const identity: Identity = new Identity(new Env()) | ||
const node: ChainNode = new ChainNode(new Env(), database) | ||
const ipfs: Ipfs = new Ipfs(new Env()) | ||
const watcher: ServiceWatcher = new ServiceWatcher(new Env(), node, identity, ipfs) | ||
|
||
before(() => { | ||
controller = new HealthController(watcher) | ||
}) | ||
|
||
describe('get() - GET /', () => { | ||
beforeEach(async () => { | ||
response = await controller.get().catch((err) => err) | ||
}) | ||
|
||
it('throws ServiceUnavailable error', () => { | ||
expect(response).to.be.instanceOf(ServiceUnavailable) | ||
expect(response.code).to.equal(503) | ||
}) | ||
}) | ||
}) |
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,97 @@ | ||
import { expect } from 'chai' | ||
import sinon from 'sinon' | ||
|
||
import Database from '../../../../lib/db/index.js' | ||
import { TransactionController } from '../index.js' | ||
import { TransactionState } from '../../../../models/transaction.js' | ||
import { NotFound } from '../../../../lib/error-handler/index.js' | ||
|
||
export const example = { | ||
id: 'transaction-controller-test', | ||
api_type: 'certificate', | ||
local_id: 'test-cert-1', | ||
state: 'submitted', | ||
transaction_type: 'initiate_cert', | ||
} | ||
|
||
describe('v1/transaction', () => { | ||
let response: any | ||
let controller: TransactionController | ||
|
||
const database: Database = new Database() | ||
const getStub = sinon.stub(database, 'get' as any).callsFake(() => [{ ...example, id: 'test-1' }, example]) | ||
|
||
before(() => { | ||
controller = new TransactionController(database) | ||
}) | ||
|
||
afterEach(() => { | ||
getStub.resetHistory() | ||
}) | ||
|
||
describe('get() - GET /', () => { | ||
describe('query by api_type', () => { | ||
;['certificate'].forEach((api_type: any) => { | ||
it(` - [${api_type}]`, async () => { | ||
response = await controller.get(api_type) | ||
|
||
expect(getStub.lastCall.args[0]).to.equal('transaction') | ||
expect(getStub.lastCall.args[1]).to.to.deep.contain({ api_type }) | ||
expect(response[1]).to.deep.contain(example) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('query by transaction state', () => { | ||
;['submitted', 'inBlock', 'finalised', 'failed'].forEach((state) => { | ||
it(`returns transactions based on state - [${state}]`, async () => { | ||
response = await controller.get(undefined, state as TransactionState) | ||
|
||
expect(getStub.lastCall.args[0]).to.equal('transaction') | ||
expect(response[1]).to.deep.contain(example) | ||
}) | ||
}) | ||
}) | ||
|
||
it('returns transactions by updated_since query param', async () => { | ||
response = await controller.get(undefined, undefined, '2024-01-10') | ||
|
||
expect(getStub.lastCall.args[0]).to.equal('transaction') | ||
}) | ||
|
||
it('returns all transactions without filters', () => { | ||
expect(response.length).to.equal(2) | ||
}) | ||
}) | ||
|
||
describe('getById() - GET /{id}', () => { | ||
beforeEach(async () => { | ||
getStub.callsFake((_, args) => [{ ...example, ...args }]) | ||
response = await controller.getById('test-id').catch((err) => err) | ||
}) | ||
|
||
describe('if transaction can not be found', () => { | ||
beforeEach(async () => { | ||
getStub.callsFake(() => []) | ||
|
||
response = await controller.getById('not-found-transaction-id').catch((err) => err) | ||
}) | ||
|
||
it('throws NotFound error', () => { | ||
expect(response).to.be.instanceOf(NotFound) | ||
expect(response.code).to.equal(404) | ||
expect(response.message).to.equal('transaction [not-found-transaction-id] not found') | ||
}) | ||
}) | ||
|
||
it('returns transaction by id', async () => { | ||
expect(response).to.deep.contain({ | ||
id: 'test-id', | ||
api_type: 'certificate', | ||
local_id: 'test-cert-1', | ||
state: 'submitted', | ||
transaction_type: 'initiate_cert', | ||
}) | ||
}) | ||
}) | ||
}) |
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