Skip to content

Commit

Permalink
modify tests to avoid creating new did dht dids each run
Browse files Browse the repository at this point in the history
  • Loading branch information
LiranCohen committed Oct 16, 2024
1 parent e353b8d commit c3f971f
Showing 1 changed file with 164 additions and 99 deletions.
263 changes: 164 additions & 99 deletions packages/agent/tests/identity-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { TestAgent } from './utils/test-agent.js';
import { AgentIdentityApi } from '../src/identity-api.js';
import { PlatformAgentTestHarness } from '../src/test-harness.js';
import { PortableIdentity } from '../src/index.js';
import { BearerDid, PortableDid } from '@web5/dids';
import { BearerDid, PortableDid, UniversalResolver } from '@web5/dids';

describe('AgentIdentityApi', () => {

Expand Down Expand Up @@ -222,88 +222,143 @@ describe('AgentIdentityApi', () => {
});

describe('setDwnEndpoints()', () => {
it('should set the DWN endpoints for a DID', async () => {
const initialEndpoints = ['https://example.com/dwn'];
// create a new identity
const identity = await testHarness.agent.identity.create({
didMethod : 'dht',
didOptions : {
services: [
{
id : 'dwn',
type : 'DecentralizedWebNode',
serviceEndpoint : initialEndpoints,
enc : '#enc',
sig : '#sig',
const testPortableDid: PortableDid = {
uri : 'did:dht:d71hju6wjeu5j7r5sbujqkubktds1kbtei8imkj859jr4hw77hdy',
document : {
id : 'did:dht:d71hju6wjeu5j7r5sbujqkubktds1kbtei8imkj859jr4hw77hdy',
verificationMethod : [
{
id : 'did:dht:d71hju6wjeu5j7r5sbujqkubktds1kbtei8imkj859jr4hw77hdy#0',
type : 'JsonWebKey',
controller : 'did:dht:d71hju6wjeu5j7r5sbujqkubktds1kbtei8imkj859jr4hw77hdy',
publicKeyJwk : {
crv : 'Ed25519',
kty : 'OKP',
x : 'H2XEz9RKJ7T0m7BmlyphVEdpKDFFT1WpJ9_STXKd7wY',
kid : '-2bXX6F3hvTHV5EBFX6oyKq11s7gtJdzUjjwdeUyBVA',
alg : 'EdDSA'
}
},
{
id : 'did:dht:d71hju6wjeu5j7r5sbujqkubktds1kbtei8imkj859jr4hw77hdy#sig',
type : 'JsonWebKey',
controller : 'did:dht:d71hju6wjeu5j7r5sbujqkubktds1kbtei8imkj859jr4hw77hdy',
publicKeyJwk : {
crv : 'Ed25519',
kty : 'OKP',
x : 'T2rdfCxGubY_zta8Gy6SVxypcchfmZKJhbXB9Ia9xlg',
kid : 'Ogpmsy5VR3SET9WC0WZD9r5p1WAKdCt1fxT0GNSLE5c',
alg : 'EdDSA'
}
],
verificationMethods: [
{
algorithm : 'Ed25519',
id : 'sig',
purposes : ['assertionMethod', 'authentication']
},
{
algorithm : 'secp256k1',
id : 'enc',
purposes : ['keyAgreement']
},
{
id : 'did:dht:d71hju6wjeu5j7r5sbujqkubktds1kbtei8imkj859jr4hw77hdy#enc',
type : 'JsonWebKey',
controller : 'did:dht:d71hju6wjeu5j7r5sbujqkubktds1kbtei8imkj859jr4hw77hdy',
publicKeyJwk : {
kty : 'EC',
crv : 'secp256k1',
x : 'oTPWtNfN7e48p3n-VsoSp07kcHfCszSrJ1-qFx3diiI',
y : '5KSDrAkg91yK19zxD6ESRPAI8v91F-QRXPbivZ-v-Ac',
kid : 'K0CBI00sEmYE6Av4PHqiwPNMzrBRA9dyIlzh1a9A2H8',
alg : 'ES256K'
}
]
}
],
authentication: [
'did:dht:d71hju6wjeu5j7r5sbujqkubktds1kbtei8imkj859jr4hw77hdy#0',
'did:dht:d71hju6wjeu5j7r5sbujqkubktds1kbtei8imkj859jr4hw77hdy#sig'
],
assertionMethod: [
'did:dht:d71hju6wjeu5j7r5sbujqkubktds1kbtei8imkj859jr4hw77hdy#0',
'did:dht:d71hju6wjeu5j7r5sbujqkubktds1kbtei8imkj859jr4hw77hdy#sig'
],
capabilityDelegation: [
'did:dht:d71hju6wjeu5j7r5sbujqkubktds1kbtei8imkj859jr4hw77hdy#0'
],
capabilityInvocation: [
'did:dht:d71hju6wjeu5j7r5sbujqkubktds1kbtei8imkj859jr4hw77hdy#0'
],
keyAgreement: [
'did:dht:d71hju6wjeu5j7r5sbujqkubktds1kbtei8imkj859jr4hw77hdy#enc'
],
service: [
{
id : 'did:dht:d71hju6wjeu5j7r5sbujqkubktds1kbtei8imkj859jr4hw77hdy#dwn',
type : 'DecentralizedWebNode',
serviceEndpoint : [
'https://example.com/dwn'
],
enc : '#enc',
sig : '#sig'
}
]
},
metadata: {
published : true,
versionId : '1729109527'
},
privateKeys: [
{
crv : 'Ed25519',
d : '7vRkinnXFRb2GkNVeY5yQ6TCnYwbtq9gJcbdqnzFR2o',
kty : 'OKP',
x : 'H2XEz9RKJ7T0m7BmlyphVEdpKDFFT1WpJ9_STXKd7wY',
kid : '-2bXX6F3hvTHV5EBFX6oyKq11s7gtJdzUjjwdeUyBVA',
alg : 'EdDSA'
},
metadata: { name: 'Alice' },
});
{
crv : 'Ed25519',
d : 'YM-0lQkMc9mNr2NrBVMojpCG2MMAnYk6-4dwxlFeiuw',
kty : 'OKP',
x : 'T2rdfCxGubY_zta8Gy6SVxypcchfmZKJhbXB9Ia9xlg',
kid : 'Ogpmsy5VR3SET9WC0WZD9r5p1WAKdCt1fxT0GNSLE5c',
alg : 'EdDSA'
},
{
kty : 'EC',
crv : 'secp256k1',
d : 'f4BngIzc_N-YDf04vXD5Ya-HdiVWB8Egk4QoSHKKJPg',
x : 'oTPWtNfN7e48p3n-VsoSp07kcHfCszSrJ1-qFx3diiI',
y : '5KSDrAkg91yK19zxD6ESRPAI8v91F-QRXPbivZ-v-Ac',
kid : 'K0CBI00sEmYE6Av4PHqiwPNMzrBRA9dyIlzh1a9A2H8',
alg : 'ES256K'
}
]
};

// control: get the service endpoints of the created DID
const initialDwnEndpoints = await testHarness.agent.identity.getDwnEndpoints({ didUri: identity.did.uri });
expect(initialDwnEndpoints).to.deep.equal(initialEndpoints);
beforeEach(async () => {
// import the keys for the test portable DID
await BearerDid.import({ keyManager: testHarness.agent.keyManager, portableDid: testPortableDid });
});

it('should set the DWN endpoints for a DID', async () => {
// stub did.get to return the test DID
sinon.stub(testHarness.agent.did, 'get').resolves(new BearerDid({ ...testPortableDid, keyManager: testHarness.agent.keyManager }));
const updateSpy = sinon.stub(testHarness.agent.did, 'update').resolves();

// set new endpoints
const newEndpoints = ['https://example.com/dwn2'];
await testHarness.agent.identity.setDwnEndpoints({ didUri: identity.did.uri, endpoints: newEndpoints });

// get the service endpoints of the updated DID
const updatedDwnEndpoints = await testHarness.agent.identity.getDwnEndpoints({ didUri: identity.did.uri });
expect(updatedDwnEndpoints).to.deep.equal(newEndpoints);
await testHarness.agent.identity.setDwnEndpoints({ didUri: testPortableDid.uri, endpoints: newEndpoints });

expect(updateSpy.calledOnce).to.be.true;
// expect the updated DID to have the new DWN service
expect(updateSpy.firstCall.args[0].portableDid.document.service).to.deep.equal([{
id : `${testPortableDid.uri}#dwn`,
type : 'DecentralizedWebNode',
serviceEndpoint : newEndpoints,
enc : '#enc',
sig : '#sig'
}]);
});

it('should throw an error if the service endpoints remain unchanged', async () => {
const initialEndpoints = ['https://example.com/dwn'];
// create a new identity
const identity = await testHarness.agent.identity.create({
didMethod : 'dht',
didOptions : {
services: [
{
id : 'dwn',
type : 'DecentralizedWebNode',
serviceEndpoint : initialEndpoints,
enc : '#enc',
sig : '#sig',
}
],
verificationMethods: [
{
algorithm : 'Ed25519',
id : 'sig',
purposes : ['assertionMethod', 'authentication']
},
{
algorithm : 'secp256k1',
id : 'enc',
purposes : ['keyAgreement']
}
]
},
metadata: { name: 'Alice' },
});

// control: get the service endpoints of the created DID
const initialDwnEndpoints = await testHarness.agent.identity.getDwnEndpoints({ didUri: identity.did.uri });
expect(initialDwnEndpoints).to.deep.equal(initialEndpoints);
// stub did.get to return the test DID
sinon.stub(testHarness.agent.did, 'get').resolves(new BearerDid({ ...testPortableDid, keyManager: testHarness.agent.keyManager }));

// set the same endpoints
try {
await testHarness.agent.identity.setDwnEndpoints({ didUri: identity.did.uri, endpoints: initialEndpoints });
await testHarness.agent.identity.setDwnEndpoints({ didUri: testPortableDid.uri, endpoints: ['https://example.com/dwn'] });
expect.fail('Expected an error to be thrown');
} catch (error: any) {
expect(error.message).to.include('AgentDidApi: No changes detected');
Expand All @@ -320,58 +375,68 @@ describe('AgentIdentityApi', () => {
});

it('should add a DWN service if no services exist', async () => {
// create a new identity without any DWN endpoints or services
const identity = await testHarness.agent.identity.create({
didMethod : 'dht',
metadata : { name: 'Alice' },
});
// stub the did.get to return a DID without any services
const testPortableDidWithoutServices = { ...testPortableDid, document: { ...testPortableDid.document, service: undefined } };
sinon.stub(testHarness.agent.did, 'get').resolves(new BearerDid({ ...testPortableDidWithoutServices, keyManager: testHarness.agent.keyManager }));
sinon.stub(UniversalResolver.prototype, 'resolve').withArgs(testPortableDid.uri).resolves({ didDocument: testPortableDidWithoutServices.document, didDocumentMetadata: {}, didResolutionMetadata: {} });
const updateSpy = sinon.stub(testHarness.agent.did, 'update').resolves();

// control: get the service endpoints of the created DID, should fail
try {
await testHarness.agent.identity.getDwnEndpoints({ didUri: identity.did.uri });
await testHarness.agent.identity.getDwnEndpoints({ didUri: testPortableDid.uri });
expect.fail('should have thrown an error');
} catch(error: any) {
expect(error.message).to.include('Failed to dereference');
}

// set new endpoints
const newEndpoints = ['https://example.com/dwn2'];
await testHarness.agent.identity.setDwnEndpoints({ didUri: identity.did.uri, endpoints: newEndpoints });

// get the service endpoints of the updated DID
const updatedDwnEndpoints = await testHarness.agent.identity.getDwnEndpoints({ didUri: identity.did.uri });
expect(updatedDwnEndpoints).to.deep.equal(newEndpoints);
await testHarness.agent.identity.setDwnEndpoints({ didUri: testPortableDid.uri, endpoints: newEndpoints });

expect(updateSpy.calledOnce).to.be.true;

// expect the updated DID to have the new DWN service
expect(updateSpy.firstCall.args[0].portableDid.document.service).to.deep.equal([{
id : 'dwn',
type : 'DecentralizedWebNode',
serviceEndpoint : newEndpoints,
enc : '#enc',
sig : '#sig'
}]);
});

it('should add a DWN service if one does not exist in the services list', async () => {
// create a new identity without a DWN service
const identity = await testHarness.agent.identity.create({
didMethod : 'dht',
didOptions : {
services: [{
id : 'some-service', // non DWN service
type : 'SomeService',
serviceEndpoint : ['https://example.com/some-service'],
}]
},
metadata: { name: 'Alice' },
});
// stub the did.get and resolver to return a DID with a different service
const testPortableDidWithDifferentService = { ...testPortableDid, document: { ...testPortableDid.document, service: [{ id: 'other', type: 'Other', serviceEndpoint: ['https://example.com/other'] }] } };
sinon.stub(testHarness.agent.did, 'get').resolves(new BearerDid({ ...testPortableDidWithDifferentService, keyManager: testHarness.agent.keyManager }));
sinon.stub(UniversalResolver.prototype, 'resolve').withArgs(testPortableDid.uri).resolves({ didDocument: testPortableDidWithDifferentService.document, didDocumentMetadata: {}, didResolutionMetadata: {} });
const updateSpy = sinon.stub(testHarness.agent.did, 'update').resolves();

// control: get the service endpoints of the created DID, should fail
try {
await testHarness.agent.identity.getDwnEndpoints({ didUri: identity.did.uri });
await testHarness.agent.identity.getDwnEndpoints({ didUri: testPortableDidWithDifferentService.uri });
expect.fail('should have thrown an error');
} catch(error: any) {
expect(error.message).to.include('Failed to dereference');
}

// set new endpoints
const newEndpoints = ['https://example.com/dwn2'];
await testHarness.agent.identity.setDwnEndpoints({ didUri: identity.did.uri, endpoints: newEndpoints });

// get the service endpoints of the updated DID
const updatedDwnEndpoints = await testHarness.agent.identity.getDwnEndpoints({ didUri: identity.did.uri });
expect(updatedDwnEndpoints).to.deep.equal(newEndpoints);
await testHarness.agent.identity.setDwnEndpoints({ didUri: testPortableDidWithDifferentService.uri, endpoints: newEndpoints });

// expect the updated DID to have the new DWN service as well as the existing service
expect(updateSpy.calledOnce).to.be.true;
expect(updateSpy.firstCall.args[0].portableDid.document.service).to.deep.equal([{
id : 'other',
type : 'Other',
serviceEndpoint : ['https://example.com/other']
}, {
id : 'dwn',
type : 'DecentralizedWebNode',
serviceEndpoint : newEndpoints,
enc : '#enc',
sig : '#sig'
}]);
});
});

Expand Down

0 comments on commit c3f971f

Please sign in to comment.