Skip to content

Commit

Permalink
add coverage, and prettying and linting on precommit
Browse files Browse the repository at this point in the history
  • Loading branch information
jchartrand committed May 26, 2024
1 parent 358066f commit 21c5532
Show file tree
Hide file tree
Showing 10 changed files with 280 additions and 204 deletions.
1 change: 1 addition & 0 deletions .husky/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_
5 changes: 5 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx lint-staged
npm test
6 changes: 6 additions & 0 deletions .lintstagedrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"*.js": [
"prettier --write",
"eslint"
]
}
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build
coverage
node_modules
7 changes: 7 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
trailingComma: 'none',
tabWidth: 2,
semi: false,
singleQuote: true,
bracketSpacing: true
}
11 changes: 11 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import globals from 'globals'
import pluginJs from '@eslint/js'
import eslintConfigPrettier from 'eslint-config-prettier'
import mochaPlugin from 'eslint-plugin-mocha'

export default [
{ languageOptions: { globals: globals.node } },
mochaPlugin.configs.flat.recommended,
pluginJs.configs.recommended,
eslintConfigPrettier
]
21 changes: 19 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
"scripts": {
"start": "node -r dotenv/config server.js",
"dev": "nodemon -r dotenv/config server.js",
"test": "NODE_OPTIONS=--experimental-vm-modules npx mocha --timeout 10000 -r dotenv/config dotenv_config_path=src/test-fixtures/.env.testing src/app.test.js "
"dev-noenv": "nodemon server.js",
"test": "NODE_OPTIONS=--experimental-vm-modules npx c8 mocha --timeout 10000 -r dotenv/config dotenv_config_path=src/test-fixtures/.env.testing src/app.test.js ",
"coveralls": "npm run test; c8 report --reporter=text-lcov | coveralls",
"prepare": "test -d node_modules/husky && husky install || echo \"husky is not installed\"",
"lint": "eslint",
"lint-fix": "eslint --fix"
},
"dependencies": {
"@digitalbazaar/did-io": "^2.0.0",
Expand All @@ -30,9 +35,17 @@
"morgan": "~1.9.1"
},
"devDependencies": {
"@eslint/js": "^9.3.0",
"chai": "^4.3.7",
"eslint": "^9.3.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-mocha": "^10.4.3",
"globals": "^15.3.0",
"husky": "^9.0.11",
"lint-staged": "^15.2.5",
"mocha": "^10.2.0",
"nodemon": "^2.0.21",
"prettier": "3.2.5",
"supertest": "^6.3.3"
},
"keywords": [
Expand All @@ -51,5 +64,9 @@
"url": "https://github.com/digitalcredentials/transaction-manager-service"
},
"homepage": "https://github.com/digitalcredentials/transaction-manager-service",
"bugs": "https://github.com/digitalcredentials/transaction-manager-service/issues"
"bugs": "https://github.com/digitalcredentials/transaction-manager-service/issues",
"lint-staged": {
"*.js": "eslint --cache --fix",
"*.{js,css,md}": "prettier --write"
}
}
183 changes: 97 additions & 86 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,68 @@
import express, { request } from 'express';
import logger from 'morgan';
import cors from 'cors';
import express from 'express'
import logger from 'morgan'
import cors from 'cors'
import axios from 'axios'
import { initializeTransactionManager, setupExchange, retrieveStoredData, getVPR } from './transactionManager.js';
import { getDataForExchangeSetupPost } from './test-fixtures/testData.js';
import { getSignedDIDAuth } from './didAuth.js';
import TransactionException from './TransactionException.js';
import {
initializeTransactionManager,
setupExchange,
retrieveStoredData,
getVPR
} from './transactionManager.js'
import { getDataForExchangeSetupPost } from './test-fixtures/testData.js'
import { getSignedDIDAuth } from './didAuth.js'
import TransactionException from './TransactionException.js'

export async function build(opts = {}) {
export async function build() {
await initializeTransactionManager()

await initializeTransactionManager()
var app = express()

var app = express();
app.use(logger('dev'))
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.use(cors())

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cors())
app.get('/', function (req, res) {
res.send({ message: 'transaction-service server status: ok.' })
})

app.get('/', function (req, res, next) {
res.send({ message: 'transaction-service server status: ok.' })
});

app.get('/healthz', async function (req, res) {

const baseURL = `${req.protocol}://${req.headers.host}`
const testData = getDataForExchangeSetupPost('test', baseURL)
const exchangeURL = `${baseURL}/exchange`
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 } = await axios.post(requestURI, didAuth)
const { tenantName, vc: unSignedVC } = data
if (!tenantName === 'test' || ! unSignedVC.name === "A Simply Wonderful Course") {
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('/healthz', async function (req, res) {
const baseURL = `${req.protocol}://${req.headers.host}`
const testData = getDataForExchangeSetupPost('test', baseURL)
const exchangeURL = `${baseURL}/exchange`
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 } = await axios.post(requestURI, didAuth)
const { tenantName, vc: unSignedVC } = data
if (
!tenantName === 'test' ||
!unSignedVC.name === 'A Simply Wonderful Course'
) {
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
})
})

/*
/*
This is step 1 in an exchange.
Creates a new exchange and stores the provided data
for later use in the exchange, in particular the subject data
Expand All @@ -62,55 +71,57 @@ export async function build(opts = {}) {
with which to trigger wallet selection that in turn
will trigger the exchange when the wallet opens.
*/
app.post("/exchange",
async (req, res) => {
try {
const data = req.body;
if (!data || !Object.keys(data).length) return res.status(400).send({ code: 400, message: 'No data was provided in the body.' })
const walletQuerys = await setupExchange(data)
return res.json(walletQuerys)
} catch (error) {
console.log(error);
return res.status(error.code || 500).json(error);
}
})
app.post('/exchange', async (req, res) => {
try {
const data = req.body
if (!data || !Object.keys(data).length)
return res
.status(400)
.send({ code: 400, message: 'No data was provided in the body.' })
const walletQuerys = await setupExchange(data)
return res.json(walletQuerys)
} catch (error) {
console.log(error)
return res.status(error.code || 500).json(error)
}
})

/*
/*
This is step 2 in an exchange, where the wallet
has asked to initiate the exchange, and we reply
here with a Verifiable Presentation Request, asking
for a DIDAuth. Note that in some scenarios the wallet
may skip this step and directly present the DIDAuth.
*/
app.post("/exchange/:exchangeId",
async (req, res) => {
try {
const vpr = await getVPR(req.params.exchangeId)
return res.json(vpr)
} catch (error) {
console.log(error);
return res.status(error.code || 500).json(error);
}
})
app.post('/exchange/:exchangeId', async (req, res) => {
try {
const vpr = await getVPR(req.params.exchangeId)
return res.json(vpr)
} catch (error) {
console.log(error)
return res.status(error.code || 500).json(error)
}
})

/*
/*
This is step 3 in an exchange, where we verify the
supplied DIDAuth, and if verified we return the previously
stored data for the exchange.
*/
app.post("/exchange/:exchangeId/:transactionId",
async (req, res) => {
try {
const didAuth = req.body
const data = await retrieveStoredData(req.params.exchangeId, req.params.transactionId, didAuth)
return res.json(data)
} catch (error) {
console.log(error);
return res.status(error.code || 500).json(error);
}
})


return app;
app.post('/exchange/:exchangeId/:transactionId', async (req, res) => {
try {
const didAuth = req.body
const data = await retrieveStoredData(
req.params.exchangeId,
req.params.transactionId,
didAuth
)
return res.json(data)
} catch (error) {
console.log(error)
return res.status(error.code || 500).json(error)
}
})

return app
}
Loading

0 comments on commit 21c5532

Please sign in to comment.