From 72c2e8e32336fe3cbfafd30a79e6102a6785a22e Mon Sep 17 00:00:00 2001 From: James Chartrand Date: Mon, 27 May 2024 17:12:00 -0400 Subject: [PATCH] add healthz endpoint and test --- src/app.js | 38 +++++++++++++++++++++++++++++++++++++- src/app.test.js | 24 ++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/src/app.js b/src/app.js index b949d12..eb25e77 100644 --- a/src/app.js +++ b/src/app.js @@ -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) { @@ -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 { diff --git a/src/app.test.js b/src/app.test.js index 5de4d3d..a925a77 100644 --- a/src/app.test.js +++ b/src/app.test.js @@ -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() + }) + }) + }) \ No newline at end of file