Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
sam committed Apr 1, 2021
2 parents 8432de2 + 84cf7ff commit d266959
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 112 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# CI for command line NodeJS Applications
name: nodejs
on:
push:
paths:
- "**/**"
- "!**/*.md/**"

env:
CI: true
FORCE_COLOR: 2

jobs:
run:
name: Node ${{ matrix.node }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
node: ['14']
os: ['ubuntu-latest']

steps:
- name: Clone repository
uses: actions/checkout@v2

- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node }}

- name: Install npm dependencies
run: cd tray/ && npm install

- name: Run build
run: npm run build
18 changes: 11 additions & 7 deletions tray/src/schema-registry.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { SchemaType, Network, ConfigObject } from './model';
import { validateSchemaDid, parseSchemaDid } from './did/did-utils';
import { validateSchemaType } from './schema-types/schema-validator';
import nodeIpfsService from './ipfs/node-ipfs-service';
import publicIpfsService from './ipfs/public-ipfs-service';
import { InvalidInput } from './exceptions/invalid-input.exception';
/** import nodeIpfsService from './ipfs/node-ipfs-service'; */

let configuration: ConfigObject = {};

Expand All @@ -16,25 +16,27 @@ export function getConfig(): ConfigObject {
}

/**
* Stores a schema to the given network after validating the schema type.
*
* @registerSchema
* @summary Stores a schema to the given network after validating the schema type.
* @param schemaContent the stringified schema
* @param schemaType the type of the schema
* @param network the network to be used for storing the schema
* @returns the DID of the schema
* @throws if the given schema content does not correspond to the type
*/

export async function registerSchema(schemaContent: string, schemaType: SchemaType, network: Network): Promise<string> {
if (!validateSchemaType(schemaContent, schemaType)) {
throw new InvalidInput('Schema Type');
}
let did = 'did:schema:' + network.toString() + ':' + schemaType.toString() + ':';
let schemaHash: string;
switch (network) {
case Network.NodeIpfs: {
/** case Network.NodeIpfs: {
schemaHash = await nodeIpfsService.addSchemaToNodeIpfs(schemaContent);
break;
}
*/
case Network.PublicIpfs: {
schemaHash = await publicIpfsService.addSchemaToPublicIpfs(schemaContent);
break;
Expand All @@ -44,8 +46,9 @@ export async function registerSchema(schemaContent: string, schemaType: SchemaTy
}

/**
* Get the schema for the given DID if it exists.
*
* @getSchema
* @summary Get the schema for the given DID if it exists.
* @param did the DID of the schema
* @returns the schema content, undefined if not found or schema type validation fails
* @throws if the given DID does not comply to the schema DID method
Expand All @@ -57,9 +60,10 @@ export async function getSchema(did: string): Promise<string> {
let schemaAsString: string;
const didObject = parseSchemaDid(did);
switch (didObject.network) {
case Network.NodeIpfs:
/** case Network.NodeIpfs:
schemaAsString = await nodeIpfsService.getSchemaFromNodeIpfs(didObject.hash);
break;
break;
*/
case Network.PublicIpfs:
schemaAsString = await publicIpfsService.getSchemaFromPublicIpfs(didObject.hash);
break;
Expand Down
83 changes: 1 addition & 82 deletions tray/src/tests/did-utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,97 +1,16 @@
import { validateSchemaDid, parseSchemaDid, stringifySchemaDid } from '../did/did-utils';
import { SchemaDid, Network, SchemaType } from '../model';

test('DID start without "did" returns false', () => {
const invalidDid = `d:schema:node-ipfs:json-schema:0xa937ea507c396d8d417be352825c65f5fdf1e6fb60e8368db03f2cccda05567c`;
const validationResult = validateSchemaDid(invalidDid);
expect(validationResult).toBe(false);
});

test('DID has no "schema" returns false', () => {
const invalidDid = `did:node-ipfs:json-schema:0xa937ea507c396d8d417be352825c65f5fdf1e6fb60e8368db03f2cccda05567c`;
const validationResult = validateSchemaDid(invalidDid);
expect(validationResult).toBe(false);
});

test('DID has a not supported network returns false', () => {
const invalidDid = `did:schema:node_test_ipfs:json-schema:0xa937ea507c396d8d417be352825c65f5fdf1e6fb60e8368db03f2cccda05567c`;
const validationResult = validateSchemaDid(invalidDid);
expect(validationResult).toBe(false);
});

test('DID has a not supported "type-hint" returns false', () => {
const invalidDid = `did:node-ipfs:q-schema:0xa937ea507c396d8d417be352825c65f5fdf1e6fb60e8368db03f2cccda05567c`;
const validationResult = validateSchemaDid(invalidDid);
expect(validationResult).toBe(false);
});

test('DID has no "hash" returns false', () => {
const invalidDid = `did:schema:node-ipfs:json-schema:`;
const validationResult = validateSchemaDid(invalidDid);
expect(validationResult).toBe(false);
});

test('DID has no "hash" and no "type hint" returns false', () => {
const invalidDid = `did:schema:node-ipfs:`;
const validationResult = validateSchemaDid(invalidDid);
expect(validationResult).toBe(false);
});

test('correct DID with ipfs hash returns true', () => {
const validDid = `did:schema:node-ipfs:json-schema:QmNLei78zWmzUdbeRB3CiUfAizWUrbeeZh5K1rhAQKCh51`;
const validationResult = validateSchemaDid(validDid);
expect(validationResult).toBe(true);
});

test('correct DID with node ipfs as network returns true', () => {
const validDid = `did:schema:node-ipfs:json-schema:0xa937ea507c396d8d417be352825c65f5fdf1e6fb60e8368db03f2cccda05567c`;
const validationResult = validateSchemaDid(validDid);
expect(validationResult).toBe(true);
});

test('correct DID with id includes - . _ returns true', () => {
const validDid = `did:schema:node-ipfs:json-schema:0xa937ea507c396d_d417be352825c65-5fdf1e6fb60e83.8db03f2cccda05567c`;
const validationResult = validateSchemaDid(validDid);
expect(validationResult).toBe(true);
});

test('correct DID with public ipfs as network returns true', () => {
const validDid = `did:schema:public-ipfs:json-schema:0xa937ea507c396d8d417be352825c65f5fdf1e6fb60e8368db03f2cccda05567c`;
const validationResult = validateSchemaDid(validDid);
expect(validationResult).toBe(true);
});

test('DID has no "type-hint" returns true', () => {
const validDid = `did:schema:node-ipfs:0xa937ea507c396d8d417be352825c65f5fdf1e6fb60e8368db03f2cccda05567c`;
const validationResult = validateSchemaDid(validDid);
expect(validationResult).toBe(true);
});

test('parse and stringify did with type hint', () => {
const didObject: SchemaDid = {
did: 'did',
method: 'schema',
network: Network.NodeIpfs,
typeHint: SchemaType.JsonSchema,
hash: '0xa937ea507c396d8d417be352825c65f5fdf1e6fb60e8368db03f2cccda05567c'
}
const validDid = `did:schema:node-ipfs:json-schema:0xa937ea507c396d8d417be352825c65f5fdf1e6fb60e8368db03f2cccda05567c`;
const resultObject = parseSchemaDid(validDid);
expect(resultObject).toStrictEqual(didObject);
const didAsString = stringifySchemaDid(resultObject);
expect(didAsString).toStrictEqual(validDid);
});

test('parse and stringify did without type hint', () => {
const didObject: SchemaDid = {
did: 'did',
method: 'schema',
network: Network.NodeIpfs,
hash: '0xa937ea507c396d8d417be352825c65f5fdf1e6fb60e8368db03f2cccda05567c'
}
const validDid = `did:schema:node-ipfs:0xa937ea507c396d8d417be352825c65f5fdf1e6fb60e8368db03f2cccda05567c`;
const resultObject = parseSchemaDid(validDid);
expect(resultObject).toStrictEqual(didObject);
const didAsString = stringifySchemaDid(resultObject);
expect(didAsString).toStrictEqual(validDid);
});
/** @exports test-did-utils */
24 changes: 1 addition & 23 deletions tray/src/tests/flow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import { registerSchema, getSchema } from '../schema-registry';
import { SchemaType, Network } from '../model';
import publicIpfsService from '../ipfs/public-ipfs-service';
import nodeIpfsService from '../ipfs/node-ipfs-service';

/** @const validJsonSchema */
const validJsonSchema = `{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/product.schema.json",
Expand Down Expand Up @@ -89,28 +89,6 @@ const validXsd = `<?xml version="1.0" encoding="UTF-8" ?>
</xs:schema>`;

describe('Test flow with mocked Network Node ipfs', () => {
const validDid = 'did:schema:node-ipfs:json-schema:QmNLei78zWmzUdbeRB3CiUfAizWUrbeeZh5K1rhAQKCh51';

it('should register the validJsonSchema', async () => {
jest.spyOn(nodeIpfsService, 'addSchemaToNodeIpfs').mockReturnValue(Promise.resolve('QmNLei78zWmzUdbeRB3CiUfAizWUrbeeZh5K1rhAQKCh51'));
const did = await registerSchema(validJsonSchema, SchemaType.JsonSchema, Network.NodeIpfs);
expect(did).toBe(validDid);
});

it('should get the registierd schema', async () => {
jest.spyOn(nodeIpfsService, 'getSchemaFromNodeIpfs').mockReturnValue(Promise.resolve(validJsonSchema));
const schema = await getSchema(validDid)
expect(schema).toBe(validJsonSchema);
});

it('should get undefined', async () => {
jest.spyOn(nodeIpfsService, 'getSchemaFromNodeIpfs').mockReturnValue(undefined);
expect(await getSchema(validDid)).toBeUndefined();
});

});

describe('Test flow with mocked Public Ipfs', () => {
const validDid = 'did:schema:public-ipfs:json-schema:QmNLei78zWmzUdbeRB3CiUfAizWUrbeeZh5K1rhAQKCh51';

Expand Down

0 comments on commit d266959

Please sign in to comment.