|
| 1 | +/*! |
| 2 | + * Copyright (c) 2025 Digital Bazaar, Inc. All rights reserved. |
| 3 | + */ |
| 4 | +import * as helpers from './helpers.js'; |
| 5 | +import { |
| 6 | + AsymmetricKey, |
| 7 | + CapabilityAgent, |
| 8 | + KmsClient |
| 9 | +} from '@digitalbazaar/webkms-client'; |
| 10 | +import {config} from '@bedrock/core'; |
| 11 | +// apisauce is a wrapper around axios that provides improved error handling |
| 12 | +import {create} from 'apisauce'; |
| 13 | +import {Ed25519Signature2020} from '@digitalbazaar/ed25519-signature-2020'; |
| 14 | +import https from 'node:https'; |
| 15 | +import {httpsAgent} from '@bedrock/https-agent'; |
| 16 | +import {mockData} from './mock.data.js'; |
| 17 | +import {ZcapClient} from '@digitalbazaar/ezcap'; |
| 18 | + |
| 19 | +let accounts; |
| 20 | +let api; |
| 21 | + |
| 22 | +const baseURL = `https://${config.server.host}`; |
| 23 | + |
| 24 | +describe('policies', () => { |
| 25 | + // mock session authentication for delegations endpoint |
| 26 | + let passportStub; |
| 27 | + let capabilityAgent; |
| 28 | + let zcapClient; |
| 29 | + const urls = {}; |
| 30 | + before(async () => { |
| 31 | + await helpers.prepareDatabase(mockData); |
| 32 | + passportStub = helpers.stubPassport(); |
| 33 | + accounts = mockData.accounts; |
| 34 | + api = create({ |
| 35 | + baseURL, |
| 36 | + headers: {Accept: 'application/ld+json, application/json'}, |
| 37 | + httpsAgent: new https.Agent({rejectUnauthorized: false}) |
| 38 | + }); |
| 39 | + |
| 40 | + // create local ephemeral capability agent |
| 41 | + const secret = crypto.randomUUID(); |
| 42 | + const handle = 'test'; |
| 43 | + capabilityAgent = await CapabilityAgent.fromSecret({secret, handle}); |
| 44 | + |
| 45 | + // delegate profile root zcap to capability agent |
| 46 | + const {account: {id: account}} = accounts['[email protected]']; |
| 47 | + const {data: {id: profileId}} = await api.post('/profiles', |
| 48 | + {account, didMethod: 'key'}); |
| 49 | + const {data} = await api.get(`/profile-agents/?account=${account}` + |
| 50 | + `&profile=${profileId}`); |
| 51 | + const [{profileAgent}] = data; |
| 52 | + const {id: profileAgentId} = profileAgent; |
| 53 | + const zcap = profileAgent.zcaps.profileCapabilityInvocationKey; |
| 54 | + const result = await api.post( |
| 55 | + `/profile-agents/${profileAgentId}/capabilities/delegate`, { |
| 56 | + controller: capabilityAgent.id, account, zcap |
| 57 | + }); |
| 58 | + |
| 59 | + // create `invocationSigner` interface for acting as profile |
| 60 | + const profileSigner = await AsymmetricKey.fromCapability({ |
| 61 | + capability: result.data.zcap, |
| 62 | + invocationSigner: capabilityAgent.getSigner(), |
| 63 | + kmsClient: new KmsClient({httpsAgent}) |
| 64 | + }); |
| 65 | + zcapClient = new ZcapClient({ |
| 66 | + agent: httpsAgent, |
| 67 | + invocationSigner: profileSigner, |
| 68 | + delegationSigner: profileSigner, |
| 69 | + SuiteClass: Ed25519Signature2020 |
| 70 | + }); |
| 71 | + |
| 72 | + // create test "delegates" for whom the policies will be about |
| 73 | + const delegates = []; |
| 74 | + for(let i = 0; i < 2; ++i) { |
| 75 | + const secret = crypto.randomUUID(); |
| 76 | + const handle = 'test'; |
| 77 | + const delegate = await CapabilityAgent.fromSecret({secret, handle}); |
| 78 | + delegates.push(delegate); |
| 79 | + } |
| 80 | + |
| 81 | + // setup policy urls |
| 82 | + const profilePath = `${baseURL}/profiles/${encodeURIComponent(profileId)}`; |
| 83 | + const zcapsPath = `${profilePath}/zcaps`; |
| 84 | + urls.policies = `${zcapsPath}/policies`; |
| 85 | + urls.refresh = `${zcapsPath}/refresh`; |
| 86 | + urls.viewablePolicy = `${urls.refresh}/policy`; |
| 87 | + }); |
| 88 | + after(async () => { |
| 89 | + passportStub.restore(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('fails to create a new policy with bad post data', async () => { |
| 93 | + should.exist(zcapClient); |
| 94 | + |
| 95 | + let err; |
| 96 | + let result; |
| 97 | + try { |
| 98 | + result = await zcapClient.write({ |
| 99 | + url: urls.policies, |
| 100 | + json: {foo: {}, policy: {}} |
| 101 | + }); |
| 102 | + } catch(e) { |
| 103 | + err = e; |
| 104 | + } |
| 105 | + should.exist(err); |
| 106 | + should.not.exist(result); |
| 107 | + err.status.should.equal(400); |
| 108 | + err.data.details.errors.should.have.length(1); |
| 109 | + const [error] = err.data.details.errors; |
| 110 | + error.name.should.equal('ValidationError'); |
| 111 | + error.message.should.contain('should NOT have additional properties'); |
| 112 | + }); |
| 113 | +}); |
0 commit comments