Skip to content
This repository has been archived by the owner on Oct 9, 2024. It is now read-only.

Commit

Permalink
HYP-72: unit tests transactions controller (#73)
Browse files Browse the repository at this point in the history
* HYP-72: unit tests transactions controller

* HYP-72: version bump.
  • Loading branch information
n3op2 authored Jan 11, 2024
1 parent 3371606 commit 9e01425
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 9 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@digicatapult/dscp-hyproof-api",
"version": "0.7.23",
"version": "0.7.24",
"description": "An OpenAPI API service for DSCP",
"main": "src/index.ts",
"type": "module",
Expand Down
35 changes: 32 additions & 3 deletions src/controllers/health/__tests__/index.test.ts
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)
})
})
})
4 changes: 4 additions & 0 deletions src/controllers/v1/certificate/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ describe('v1/certificate', () => {
afterEach(() => {
stubs.submitRunProcess.resetHistory()
stubs.prepareRunProcess.resetHistory()
stubs.build.resetHistory()
stubs.getMemberByAlias.resetHistory()
stubs.getSelfAddress.resetHistory()
stubs.insert.resetHistory()
})

describe('postDraft() - POST /', () => {
Expand Down
97 changes: 97 additions & 0 deletions src/controllers/v1/transaction/__tests__/index.test.ts
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',
})
})
})
})
7 changes: 4 additions & 3 deletions src/controllers/v1/transaction/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class TransactionController extends Controller {
@Response<BadRequest>(400, 'Request was invalid')
@Response<NotFound>(404, 'Item not found')
@Get('/')
public async getAllTransactions(
public async get(
@Query() api_type?: TransactionApiType,
@Query() state?: TransactionState,
@Query() updated_since?: DATE
Expand All @@ -53,8 +53,9 @@ export class TransactionController extends Controller {
*/
@Response<NotFound>(404, 'Item not found')
@Get('{id}')
public async getTransaction(@Path() id: UUID): Promise<GetTransactionResponse> {
const transaction = await this.db.get('transaction', { id }).then((transactions) => transactions[0])
public async getById(@Path() id: UUID): Promise<GetTransactionResponse> {
const [transaction] = await this.db.get('transaction', { id })
if (!transaction) throw new NotFound(`transaction [${id}]`)

return transaction
}
Expand Down

0 comments on commit 9e01425

Please sign in to comment.