From 4305f62b404669a6e8e1bd51c95f0fe5c36c8edc Mon Sep 17 00:00:00 2001 From: James Chartrand Date: Fri, 6 Sep 2024 08:57:49 -0400 Subject: [PATCH] add status list tests and fix 404 --- src/app.js | 11 +++--- src/app.test.js | 23 +++++++++++++ src/test-fixtures/nocks/status_list_nock.js | 34 +++++++++++++++++++ .../nocks/unknown_status_list_nock.js | 7 ++++ 4 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 src/test-fixtures/nocks/status_list_nock.js create mode 100644 src/test-fixtures/nocks/unknown_status_list_nock.js diff --git a/src/app.js b/src/app.js index f9545bf..de8b483 100644 --- a/src/app.js +++ b/src/app.js @@ -134,16 +134,17 @@ export async function build (opts = {}) { }) app.get('/status/:statusCredentialId', async function (req, res, next) { - if (!enableStatusService) return res.status(405).send('The status service has not been enabled.') + if (!enableStatusService) next({ code: 405, message: 'The status service has not been enabled.' }) const statusCredentialId = req.params.statusCredentialId try { const { data: statusCredential } = await axios.get(`http://${statusService}/${statusCredentialId}`) return res.status(200).json(statusCredential) } catch (error) { - next({ - message: error.message, - code: error.code - }) + if (error.response.status === 404) { + next({ code: 404, message: 'No status credential found for that id.' }) + } else { + next(error) + } } return res.status(500).send({ message: 'Server error.' }) }) diff --git a/src/app.test.js b/src/app.test.js index 29ad245..5f0c2eb 100644 --- a/src/app.test.js +++ b/src/app.test.js @@ -7,6 +7,8 @@ import protectedNock from './test-fixtures/nocks/protected_status_signing.js' import unprotectedStatusUpdateNock from './test-fixtures/nocks/unprotected_status_update.js' import unknownStatusIdNock from './test-fixtures/nocks/unknown_status_id_nock.js' import protectedStatusUpdateNock from './test-fixtures/nocks/protected_status_update.js' +import unknownStatusListNock from './test-fixtures/nocks/unknown_status_list_nock.js' +import statusListNock from './test-fixtures/nocks/status_list_nock.js' import { build } from './app.js' @@ -227,4 +229,25 @@ describe('api', () => { expect(response.status).to.equal(200) }) }) + + describe('GET /status/:statusCredentialId', () => { + it('returns 404 for unknown status credential id', async () => { + unknownStatusListNock() + const response = await request(app) + .get('/status/9898u') + expect(response.header['content-type']).to.have.string('json') + expect(response.status).to.equal(404) + }) + + it('returns credential status list from status service', async () => { + statusListNock() + const response = await request(app) + .get('/status/slAwJe6GGR6mBojlGW5U') + expect(response.header['content-type']).to.have.string('json') + expect(response.status).to.equal(200) + const returnedList = JSON.parse(JSON.stringify(response.body)) + // this proof value comes from the nock: + expect(returnedList.proof.proofValue).to.equal('z4y3GawinQg1aCqbYqZM8dmDpbmtFa3kE6tFefdXvLi5iby25dvmVwLNZrfcFPyhpshrhCWB76pdSZchVve3K1Znr') + }) + }) }) diff --git a/src/test-fixtures/nocks/status_list_nock.js b/src/test-fixtures/nocks/status_list_nock.js new file mode 100644 index 0000000..df6725e --- /dev/null +++ b/src/test-fixtures/nocks/status_list_nock.js @@ -0,0 +1,34 @@ +import nock from 'nock' + +const theList = `{ + "@context": [ + "https://www.w3.org/ns/credentials/v2", + "https://w3id.org/security/suites/ed25519-2020/v1" + ], + "id": "https://sincere-bonefish-currently.ngrok-free.app/slAwJe6GGR6mBojlGW5U", + "type": [ + "VerifiableCredential", + "BitstringStatusListCredential" + ], + "credentialSubject": { + "id": "https://sincere-bonefish-currently.ngrok-free.app/slAwJe6GGR6mBojlGW5U#list", + "type": "BitstringStatusList", + "encodedList": "uH4sIAAAAAAAAA-3BIQEAAAACICf4f60vTEADAAAAAAAAAAAAAADwN_wEBkHUMAAA", + "statusPurpose": "revocation" + }, + "issuer": "did:key:z6Mkg165pEHaUPxkY4NxToor7suxzawEmdT1DEWq3e1Nr2VR", + "validFrom": "2024-09-03T15:24:19.685Z", + "proof": { + "type": "Ed25519Signature2020", + "created": "2024-09-03T15:24:19Z", + "verificationMethod": "did:key:z6Mkg165pEHaUPxkY4NxToor7suxzawEmdT1DEWq3e1Nr2VR#z6Mkg165pEHaUPxkY4NxToor7suxzawEmdT1DEWq3e1Nr2VR", + "proofPurpose": "assertionMethod", + "proofValue": "z4y3GawinQg1aCqbYqZM8dmDpbmtFa3kE6tFefdXvLi5iby25dvmVwLNZrfcFPyhpshrhCWB76pdSZchVve3K1Znr" + } +}` + +export default () => { + nock('http://localhost:4008') + .get('/slAwJe6GGR6mBojlGW5U') + .reply(200, theList) +} diff --git a/src/test-fixtures/nocks/unknown_status_list_nock.js b/src/test-fixtures/nocks/unknown_status_list_nock.js new file mode 100644 index 0000000..6bb349a --- /dev/null +++ b/src/test-fixtures/nocks/unknown_status_list_nock.js @@ -0,0 +1,7 @@ +import nock from 'nock' + +export default () => { + nock('http://localhost:4008') + .get('/9898u') + .reply(404, { code: 404, message: 'No status credential found for that id.' }) +}