Skip to content

Commit

Permalink
add unhealthy test
Browse files Browse the repository at this point in the history
  • Loading branch information
jchartrand committed May 24, 2024
1 parent 8bc1061 commit 358066f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
17 changes: 16 additions & 1 deletion src/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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")
Expand All @@ -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()

})


})

Expand Down
29 changes: 24 additions & 5 deletions src/transactionManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 => {
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 358066f

Please sign in to comment.