Skip to content

Commit

Permalink
Added unit tests for updateDID and revokeDID
Browse files Browse the repository at this point in the history
  • Loading branch information
macterra committed Jun 7, 2024
1 parent 160a730 commit a287bf9
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 2 deletions.
4 changes: 2 additions & 2 deletions keymaster-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ export async function verifySignature(obj) {
}
}

async function updateDID(did, doc) {
export async function updateDID(did, doc) {
const current = await resolveDID(did);
const prev = cipher.hashJSON(current);

Expand All @@ -385,7 +385,7 @@ async function updateDID(did, doc) {
return gatekeeper.updateDID(signed);
}

async function revokeDID(did) {
export async function revokeDID(did) {
const current = await resolveDID(did);
const prev = cipher.hashJSON(current);

Expand Down
105 changes: 105 additions & 0 deletions keymaster.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,111 @@ describe('createAsset', () => {
});
});

describe('updateDID', () => {

afterEach(() => {
mockFs.restore();
});

it('should update an asset DID', async () => {
mockFs({});

await keymaster.createId('Bob');
const mockAnchor = { name: 'mockAnchor' };
const dataDid = await keymaster.createAsset(mockAnchor);
const doc = await keymaster.resolveDID(dataDid);

const dataUpdated = { name: 'updated' };
doc.didDocumentData = dataUpdated;

const ok = await keymaster.updateDID(dataDid, doc);
const doc2 = await keymaster.resolveDID(dataDid);

expect(ok).toBe(true);
expect(doc2.didDocumentData).toStrictEqual(dataUpdated);
expect(doc2.didDocumentMetadata.version).toBe(2);
});

it('should update an asset DID when current ID is not owner ID', async () => {
mockFs({});

const bob = await keymaster.createId('Bob');
await keymaster.createId('Alice');

keymaster.setCurrentId('Bob');

const mockAnchor = { name: 'mockAnchor' };
const dataDid = await keymaster.createAsset(mockAnchor);
const doc = await keymaster.resolveDID(dataDid);

const dataUpdated = { name: 'updated' };
doc.didDocumentData = dataUpdated;

keymaster.setCurrentId('Alice');

const ok = await keymaster.updateDID(dataDid, doc);
const doc2 = await keymaster.resolveDID(dataDid);

expect(ok).toBe(true);
expect(doc2.didDocument.controller).toBe(bob);
expect(doc2.didDocumentData).toStrictEqual(dataUpdated);
expect(doc2.didDocumentMetadata.version).toBe(2);
});
});

describe('revokeDID', () => {

afterEach(() => {
mockFs.restore();
});

it('should revoke an asset DID', async () => {
mockFs({});

await keymaster.createId('Bob');
const mockAnchor = { name: 'mockAnchor' };
const dataDid = await keymaster.createAsset(mockAnchor);
const doc = await keymaster.resolveDID(dataDid);

const dataUpdated = { name: 'updated' };
doc.didDocumentData = dataUpdated;

const ok = await keymaster.revokeDID(dataDid, doc);
const doc2 = await keymaster.resolveDID(dataDid);

expect(ok).toBe(true);
expect(doc2.didDocument).toStrictEqual({});
expect(doc2.didDocumentData).toStrictEqual({});
expect(doc2.didDocumentMetadata.deactivated).toBe(true);
});

it('should revoke an asset DID when current ID is not owner ID', async () => {
mockFs({});

await keymaster.createId('Bob');
await keymaster.createId('Alice');

keymaster.setCurrentId('Bob');

const mockAnchor = { name: 'mockAnchor' };
const dataDid = await keymaster.createAsset(mockAnchor);
const doc = await keymaster.resolveDID(dataDid);

const dataUpdated = { name: 'updated' };
doc.didDocumentData = dataUpdated;

keymaster.setCurrentId('Alice');

const ok = await keymaster.revokeDID(dataDid, doc);
const doc2 = await keymaster.resolveDID(dataDid);

expect(ok).toBe(true);
expect(doc2.didDocument).toStrictEqual({});
expect(doc2.didDocumentData).toStrictEqual({});
expect(doc2.didDocumentMetadata.deactivated).toBe(true);
});
});

function generateRandomString(length) {
let result = '';
let characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
Expand Down

0 comments on commit a287bf9

Please sign in to comment.