diff --git a/src/app.test.js b/src/app.test.js index b56e2ea..06a7dba 100644 --- a/src/app.test.js +++ b/src/app.test.js @@ -2,6 +2,7 @@ import { expect } from 'chai' import request from 'supertest'; import { build } from './app.js'; import { getDataForExchangeSetupPost } from './test-fixtures/testData.js'; +import { clearKeyv, initializeTransactionManager } from './transactionManager.js'; let app @@ -54,7 +55,7 @@ describe('api', () => { describe('GET /healthz', () => { - it.only('returns 200 if running', async () => { + it('returns 200 if healthy', async () => { const response = await request(app) .get("/healthz") @@ -64,6 +65,20 @@ describe('api', () => { expect(response.body).to.eql({ message: 'transaction-service server status: ok.', healthy: true }) }) + + it('returns 503 if not healthy', async () => { + // 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() + + }) + }) diff --git a/src/transactionManager.js b/src/transactionManager.js index a7eb996..77fd6ce 100644 --- a/src/transactionManager.js +++ b/src/transactionManager.js @@ -11,7 +11,9 @@ const defaultTimeToLive = process.env.DEFAULT_TTL = 1000 * 60 * 10; // keyv entr let keyv; - +/** + * Intializes the keyv store either in-memory or in file system, according to env. + */ export const initializeTransactionManager = () => { if (!keyv) { if (persistToFile) { @@ -83,9 +85,13 @@ export const getDIDAuthVPR = async (exchangeId) => { } } +/** + * @param {string} exchangeHost + * @param {string} tenantName + * @returns a function for processing incoming records, bound to the specific exchangeHost and tenant + */ const bindProcessRecordFnToExchangeHostAndTenant = (exchangeHost, tenantName) => { - // returns a function for processing incoming records, bound to the specific exchangeHost and tenant - return async (record) => { + return async (record) => { record.tenantName = tenantName record.exchangeHost = exchangeHost record.transactionId = crypto.randomUUID() @@ -154,7 +160,7 @@ export const retrieveStoredData = async (exchangeId, transactionId, didAuthVP) = /** * @param {string} exchangeId - * @throws {ExchangeIdError} Unknown exchangeID + * @throws {ExchangeError} Unknown exchangeID * @returns returns stored data if exchangeId exists */ const getExchangeData = async exchangeId => { @@ -163,6 +169,19 @@ const getExchangeData = async exchangeId => { return storedData } +/** + * This is meant for testing failures. It deletes the keyv store entirely. + * @throws {ExchangeError} Unknown error + */ +export const clearKeyv = () => { + try { + keyv = null; + } catch (e) { + throw new ExchangeError("Clear failed") + } + +} + /** * @param {Array} exchangeData Array of data items, one per credential, with data needed for the exchange * @param {Object} [exchangeData.data[].vc] optional - an unsigned populated VC @@ -172,7 +191,7 @@ const getExchangeData = async exchangeId => { * @param {string} exchangeData.batchId batch to which cred belongs; also determines vc template * @param {string} exchangeData.data[].retrievalId an identifer for ech record, e.g., the recipient's email address * @param {Object} exchangeData.data[].metadata anything else we want to store in the record for later use - * @throws {ExchangeIdError} Unknown exchangeID + * @throws {ExchangError} Unknown exchangeID */ const verifyExchangeData = exchangeData => { const batchId = exchangeData.batchId