Skip to content

Commit

Permalink
Normalize schema and protocol in create method of permission-grant (#532
Browse files Browse the repository at this point in the history
)

* Normalize schema and protocol in create method of permission-grant (#530)

Signed-off-by: Adarsh A <[email protected]>

* refactor to keep tests consistent and give better assurance of normalization (#530)

Signed-off-by: Adarsh A <[email protected]>

* update test coverage in README.md

Signed-off-by: Adarsh A <[email protected]>

---------

Signed-off-by: Adarsh A <[email protected]>
Co-authored-by: Diane Huxley <[email protected]>
  • Loading branch information
adarsh-a-tw and Diane Huxley authored Oct 4, 2023
1 parent 17199ee commit 032011e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Here's to a thrilling Hacktoberfest voyage with us! 🎉
# Decentralized Web Node (DWN) SDK <!-- omit in toc -->

Code Coverage
![Statements](https://img.shields.io/badge/statements-98.07%25-brightgreen.svg?style=flat) ![Branches](https://img.shields.io/badge/branches-95.3%25-brightgreen.svg?style=flat) ![Functions](https://img.shields.io/badge/functions-95.14%25-brightgreen.svg?style=flat) ![Lines](https://img.shields.io/badge/lines-98.07%25-brightgreen.svg?style=flat)
![Statements](https://img.shields.io/badge/statements-98.07%25-brightgreen.svg?style=flat) ![Branches](https://img.shields.io/badge/branches-95.32%25-brightgreen.svg?style=flat) ![Functions](https://img.shields.io/badge/functions-95.14%25-brightgreen.svg?style=flat) ![Lines](https://img.shields.io/badge/lines-98.07%25-brightgreen.svg?style=flat)

- [Introduction](#introduction)
- [Installation](#installation)
Expand Down
7 changes: 6 additions & 1 deletion src/interfaces/permissions-grant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { removeUndefinedProperties } from '../utils/object.js';
import { validateMessageSignatureIntegrity } from '../core/auth.js';
import { DwnError, DwnErrorCode } from '../core/dwn-error.js';
import { DwnInterfaceName, DwnMethodName, Message } from '../core/message.js';
import { normalizeProtocolUrl, normalizeSchemaUrl } from '../utils/url.js';

export type PermissionsGrantOptions = {
messageTimestamp?: string;
Expand Down Expand Up @@ -42,6 +43,10 @@ export class PermissionsGrant extends Message<PermissionsGrantMessage> {
}

static async create(options: PermissionsGrantOptions): Promise<PermissionsGrant> {
const scope = { ...options.scope } as RecordsPermissionScope;
scope.protocol = scope.protocol !== undefined ? normalizeProtocolUrl(scope.protocol) : undefined;
scope.schema = scope.schema !== undefined ? normalizeSchemaUrl(scope.schema) : undefined;

const descriptor: PermissionsGrantDescriptor = {
interface : DwnInterfaceName.Permissions,
method : DwnMethodName.Grant,
Expand All @@ -52,7 +57,7 @@ export class PermissionsGrant extends Message<PermissionsGrantMessage> {
grantedBy : options.grantedBy,
grantedFor : options.grantedFor,
permissionsRequestId : options.permissionsRequestId,
scope : options.scope,
scope : scope,
conditions : options.conditions,
};

Expand Down
55 changes: 53 additions & 2 deletions tests/interfaces/permissions-grant.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { PermissionScope } from '../../src/index.js';
import { getCurrentTimeInHighPrecision } from '../../src/utils/time.js';
import { PermissionsConditionPublication } from '../../src/types/permissions-types.js';
import { PermissionsGrant } from '../../src/interfaces/permissions-grant.js';
import type { RecordsPermissionScope } from '../../src/types/permissions-types.js';
import { Secp256k1 } from '../../src/utils/secp256k1.js';
import { Temporal } from '@js-temporal/polyfill';
import { TestDataGenerator } from '../utils/test-data-generator.js';
Expand Down Expand Up @@ -35,6 +36,56 @@ describe('PermissionsGrant', () => {
expect(message.descriptor.description).to.eql('drugs');
});

describe('scope property normalizations', async () => {
it('ensures that `schema` is normalized', async () => {
const { privateJwk } = await Secp256k1.generateKeyPair();
const authorizationSigner = new PrivateKeySigner({ privateJwk, keyId: 'did:jank:bob' });

const scope: PermissionScope = {
interface : DwnInterfaceName.Records,
method : DwnMethodName.Write,
schema : 'example.com/',
};

const { message } = await PermissionsGrant.create({
dateExpires : getCurrentTimeInHighPrecision(),
description : 'schema normalization test',
grantedBy : 'did:jank:bob',
grantedTo : 'did:jank:alice',
grantedFor : 'did:jank:bob',
scope : scope,
authorizationSigner
});


expect((message.descriptor.scope as RecordsPermissionScope).schema).to.equal('http://example.com');
});

it('ensures that `protocol` is normalized', async () => {
const { privateJwk } = await Secp256k1.generateKeyPair();
const authorizationSigner = new PrivateKeySigner({ privateJwk, keyId: 'did:jank:bob' });

const scope: PermissionScope = {
interface : DwnInterfaceName.Records,
method : DwnMethodName.Write,
protocol : 'example.com/',
};

const { message } = await PermissionsGrant.create({
dateExpires : getCurrentTimeInHighPrecision(),
description : 'protocol normalization test',
grantedBy : 'did:jank:bob',
grantedTo : 'did:jank:alice',
grantedFor : 'did:jank:bob',
scope : scope,
authorizationSigner
});


expect((message.descriptor.scope as RecordsPermissionScope).protocol).to.equal('http://example.com');
});
});

describe('scope validations', () => {
it('ensures that `schema` and protocol related fields `protocol`, `contextId` or `protocolPath`', async () => {
const { privateJwk } = await Secp256k1.generateKeyPair();
Expand Down Expand Up @@ -129,7 +180,7 @@ describe('PermissionsGrant', () => {
expect(permissionsGrant.message.descriptor.grantedBy).to.eq(permissionsRequest.message.descriptor.grantedBy);
expect(permissionsGrant.message.descriptor.grantedTo).to.eq(permissionsRequest.message.descriptor.grantedTo);
expect(permissionsGrant.message.descriptor.grantedFor).to.eq(permissionsRequest.message.descriptor.grantedFor);
expect(permissionsGrant.message.descriptor.scope).to.eq(permissionsRequest.message.descriptor.scope);
expect(permissionsGrant.message.descriptor.scope).to.eql(permissionsRequest.message.descriptor.scope);
expect(permissionsGrant.message.descriptor.conditions).to.eq(permissionsRequest.message.descriptor.conditions);
expect(permissionsGrant.message.descriptor.permissionsRequestId).to.eq(await Message.getCid(permissionsRequest.message));
});
Expand Down Expand Up @@ -168,7 +219,7 @@ describe('PermissionsGrant', () => {
expect(permissionsGrant.message.descriptor.grantedBy).to.eq(overrides.grantedBy);
expect(permissionsGrant.message.descriptor.grantedTo).to.eq(overrides.grantedTo);
expect(permissionsGrant.message.descriptor.grantedFor).to.eq(overrides.grantedFor);
expect(permissionsGrant.message.descriptor.scope).to.eq(overrides.scope);
expect(permissionsGrant.message.descriptor.scope).to.eql(overrides.scope);
expect(permissionsGrant.message.descriptor.conditions).to.eq(overrides.conditions);
expect(permissionsGrant.message.descriptor.permissionsRequestId).to.eq(await Message.getCid(permissionsRequest.message));
});
Expand Down

0 comments on commit 032011e

Please sign in to comment.