Skip to content

Commit

Permalink
Test dwn send PermissionsGrant
Browse files Browse the repository at this point in the history
  • Loading branch information
diehuxx committed Nov 2, 2023
1 parent 3229195 commit a7e015b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
25 changes: 24 additions & 1 deletion packages/api/src/dwn-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ export type PermissionsGrantRequest = {
message: Omit<PermissionsGrantOptions, 'authorizationSigner'>;
}

export type PermissionsGrantResponse = {
permissionsGrant: PermissionsGrant | undefined;
permissionsGrantId: string | undefined;
status: UnionMessageReply['status']
};

export type ProtocolsConfigureRequest = {
message: Omit<ProtocolsConfigureOptions, 'authorizationSigner'>;
}
Expand Down Expand Up @@ -101,6 +107,7 @@ export type RecordsWriteRequest = {
}

export type PermissionGrantRequest = {
target?: string;
message?: Omit<Partial<PermissionsGrantOptions>, 'authorizationSigner'>;
}

Expand Down Expand Up @@ -393,7 +400,14 @@ export class DwnApi {

get permissions() {
return {
grant: async (request: PermissionsGrantRequest): Promise<{ permissionsGrant: PermissionsGrant, status: UnionMessageReply['status'] }> => {
/**
* Create and store a PermissionsGrant DWN message
* @param request.target The DID whose DWN the PermissionsGrant message will be sent to. If undefined,
* the message will be stored in the local DWN of the connectedDid.
* @param request.message The message options used to create the PermissionsGrant messsage.
* @returns {PermissionsGrantResponse}
*/
grant: async (request: PermissionsGrantRequest): Promise<PermissionsGrantResponse> => {
const agentRequest = {
author : this.connectedDid,
messageOptions : request.message,
Expand All @@ -412,16 +426,25 @@ export class DwnApi {
const { message, reply: { status } } = agentResponse;

let permissionsGrant: PermissionsGrant | undefined;
let permissionsGrantId: string | undefined;
if (200 <= status.code && status.code <= 299) {
permissionsGrant = await PermissionsGrant.parse(message as PermissionsGrantMessage);
permissionsGrantId = await Message.getCid(permissionsGrant.message);
}

return {
permissionsGrant,
permissionsGrantId,
status,
};
},

/**
* Send an existing PermissionsGrant message to a remote DWN.
* @param target DID whose remote DWN the Permissions message will be sent to.
* @param message The PermissionsGrant message that will be sent.
* @returns {UnionMessageReply['status']}
*/
send: async (target: string, message: PermissionsGrant): Promise<{ status: UnionMessageReply['status'] }> => {
const { reply: { status } } = await this.agent.sendDwnRequest({
messageType : message.message.descriptor.interface + message.message.descriptor.method,
Expand Down
26 changes: 23 additions & 3 deletions packages/api/tests/dwn-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -577,21 +577,22 @@ describe('DwnApi', () => {
it('returns a PermissionsGrant that can be invoked', async () => {
const { did: bobDid } = await testAgent.createIdentity({ testDwnUrls });

const { status, permissionsGrant } = await dwn.permissions.grant({
const { status, permissionsGrant, permissionsGrantId } = await dwn.permissions.grant({
message: {
dateExpires : Temporal.Now.instant().toString({ smallestUnit: 'microseconds' }),
grantedBy : aliceDid.did,
grantedFor : aliceDid.did,
grantedTo : bobDid.did,
scope : {
interface : DwnInterfaceName.Records,
method : DwnMethodName.Read,
method : DwnMethodName.Write,
}
},
});

expect(status.code).to.eq(202);
expect(permissionsGrant).not.to.be.undefined;
expect(permissionsGrantId).not.to.be.undefined;

// send to Alice's remote DWN
const { status: statusSend } = await dwn.permissions.send(aliceDid.did, permissionsGrant);
Expand All @@ -600,8 +601,27 @@ describe('DwnApi', () => {
});
});

describe('target: did', () => {
describe('target: did', async () => {
it('returns a PermissionsGrant that can be invoked', async () => {
const { did: bobDid } = await testAgent.createIdentity({ testDwnUrls });

const { status, permissionsGrant } = await dwn.permissions.grant({
target : bobDid.did,
message : {
dateExpires : Temporal.Now.instant().toString({ smallestUnit: 'microseconds' }),
grantedBy : aliceDid.did,
grantedFor : aliceDid.did,
grantedTo : bobDid.did,
scope : {
interface : DwnInterfaceName.Records,
method : DwnMethodName.Write,
}
},
});

expect(status.code).to.eq(202);
expect(permissionsGrant).not.to.be.undefined;
});
});
});
});

0 comments on commit a7e015b

Please sign in to comment.