Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add healthz endpoint and test #15

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import verifyAuthHeader from './verifyAuthHeader.js'
import { getConfig } from './config.js'
import testVC from './testVC.js';
import CoordinatorException from './CoordinatorException.js';
import { getSignedDIDAuth, verifyDIDAuth } from './didAuth.js';
import { getSignedDIDAuth } from './didAuth.js';
import { getDataForExchangeSetupPost } from './test-fixtures/vc.js';

async function callService(endpoint, body) {

Expand All @@ -33,6 +34,41 @@ export async function build(opts = {}) {
app.use(express.urlencoded({ extended: false }));
app.use(cors())

app.get('/healthz', async function(req, res) {
const baseURL = `${req.protocol}://${req.headers.host}`
const testData = getDataForExchangeSetupPost('test')
const exchangeURL = `${baseURL}/exchange/setup`
try {
const response = await axios.post(exchangeURL, testData)
const { data: walletQuerys } = response
const walletQuery = walletQuerys.find((q) => q.retrievalId === 'someId')
const parsedDeepLink = new URL(walletQuery.directDeepLink)
const requestURI = parsedDeepLink.searchParams.get('vc_request_url')
const challenge = parsedDeepLink.searchParams.get('challenge')
const didAuth = await getSignedDIDAuth('did:ex:223234', challenge)
const { data: vc } = await axios.post(requestURI, didAuth)

if (
!vc.proof
) {
throw new TransactionException(
503,
'transaction-service healthz failed'
)
}
} catch (e) {
console.log(`exception in healthz: ${JSON.stringify(e)}`)
return res.status(503).json({
error: `transaction-service healthz check failed with error: ${e}`,
healthy: false
})
}
res.send({
message: 'transaction-service server status: ok.',
healthy: true
})
})

app.get('/', async function (req, res, next) {
if (enableStatusService) {
try {
Expand Down
24 changes: 24 additions & 0 deletions src/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,4 +327,28 @@ describe('api', () => {
})


describe('GET /healthz', function () {
it.only('returns 200 if healthy', async function () {
const response = await request(app).get('/healthz')

expect(response.header['content-type']).to.have.string('json')
expect(response.status).to.eql(200)
expect(response.body).to.eql({
message: 'transaction-service server status: ok.',
healthy: true
})
})

it('returns 503 if not healthy', async function () {
// we delete the keyv store to force an error
//clearKeyv()
const response = await request(app).get('/healthz')

expect(response.header['content-type']).to.have.string('json')
expect(response.status).to.eql(503)
expect(response.body).to.have.property('healthy', false)
initializeTransactionManager()
})
})

})
Loading