Skip to content

Commit

Permalink
complete coverage for update
Browse files Browse the repository at this point in the history
  • Loading branch information
LiranCohen committed Sep 26, 2024
1 parent 86ed654 commit bae84ff
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/agent/src/did-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ export class AgentDidApi<TKeyManager extends AgentKeyManager = AgentKeyManager>
}): Promise<BearerDid> {

// Check if the DID exists in the store.
const existingDid = await this.get({ didUri: portableDid.uri, tenant });
const existingDid = await this.get({ didUri: portableDid.uri, tenant: tenant ?? portableDid.uri });
if (!existingDid) {
throw new Error(`AgentDidApi: Could not update, DID not found: ${portableDid.uri}`);
}
Expand Down
65 changes: 65 additions & 0 deletions packages/agent/tests/did-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,71 @@ describe('AgentDidApi', () => {
// Verify publish was called
expect(publishSpy.called).to.be.false;
});

it('updates a DID under the tenant of the updated DID if tenant is not provided ', async () => {
// Generate a new DID.
const did = await testHarness.agent.did.create({ method: 'dht'});
const portableDid = await did.export();

const updateDid = {
...portableDid,
document: {
...portableDid.document,
service: [{ id: 'service1', type: 'example', serviceEndpoint: 'https://example.com' }]
}
};

// Update the DID.
await testHarness.agent.did.update({ portableDid: updateDid });

// get the updated DID
const updatedDid = await testHarness.agent.did.get({ didUri: did.uri, tenant: did.uri });

// Verify the result.
expect(updatedDid).to.have.property('uri', did.uri);
expect(updatedDid).to.have.property('document');
expect(updatedDid).to.have.property('metadata');

// Verify the updated document.
expect(updatedDid!.document).to.deep.equal(updateDid.document);
});

it('throws if DID does not exist in the store', async () => {
// Generate a new DID.
const did = await testHarness.agent.did.create({ method: 'dht'});
const portableDid = await did.export();

const updateDid = {
...portableDid,
uri : 'did:example:123', // change the uri to a different DID
document : {
...portableDid.document,
service: [{ id: 'service1', type: 'example', serviceEndpoint: 'https://example.com' }]
}
};

try {
// Update the DID.
await testHarness.agent.did.update({ portableDid: updateDid, tenant: testHarness.agent.agentDid.uri });
expect.fail('Expected an error to be thrown');
} catch(error: any) {
expect(error.message).to.include('AgentDidApi: Could not update, DID not found');
}
});

it('throws if the DID document is not updated', async () => {
// Generate a new DID.
const did = await testHarness.agent.did.create({ method: 'jwk', tenant: testHarness.agent.agentDid.uri });
const portableDid = await did.export();

try {
// Update the DID.
await testHarness.agent.did.update({ portableDid, tenant: testHarness.agent.agentDid.uri });
expect.fail('Expected an error to be thrown');
} catch(error: any) {
expect(error.message).to.include('AgentDidApi: No changes detected, update aborted');
}
});
});
});
});
Expand Down

0 comments on commit bae84ff

Please sign in to comment.