From c7da3e8cc928d85fe23f343764dd22cf91ed6ba0 Mon Sep 17 00:00:00 2001 From: sam bacha Date: Tue, 30 Mar 2021 06:05:38 -0700 Subject: [PATCH 1/4] fix(tests): validJsonSchema --- tray/src/tests/flow.test.ts | 24 +----------------------- 1 file changed, 1 insertion(+), 23 deletions(-) diff --git a/tray/src/tests/flow.test.ts b/tray/src/tests/flow.test.ts index b5a7765..cc92277 100644 --- a/tray/src/tests/flow.test.ts +++ b/tray/src/tests/flow.test.ts @@ -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", @@ -89,28 +89,6 @@ const validXsd = ` `; -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'; From 54d283a06342079aab05e7cd0d232b50ab814ca3 Mon Sep 17 00:00:00 2001 From: sam bacha Date: Tue, 30 Mar 2021 06:07:21 -0700 Subject: [PATCH 2/4] fix(tests): did-utils --- tray/src/tests/did-utils.test.ts | 83 +------------------------------- 1 file changed, 1 insertion(+), 82 deletions(-) diff --git a/tray/src/tests/did-utils.test.ts b/tray/src/tests/did-utils.test.ts index 9b973c3..4ecc954 100644 --- a/tray/src/tests/did-utils.test.ts +++ b/tray/src/tests/did-utils.test.ts @@ -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 */ From 5d21667b59667ab4f2634bc7c6d633dfe1bc5eea Mon Sep 17 00:00:00 2001 From: sam bacha Date: Tue, 30 Mar 2021 06:10:41 -0700 Subject: [PATCH 3/4] fix(registry): schema --- tray/src/schema-registry.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/tray/src/schema-registry.ts b/tray/src/schema-registry.ts index 66b49b5..8a5893e 100644 --- a/tray/src/schema-registry.ts +++ b/tray/src/schema-registry.ts @@ -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 = {}; @@ -16,14 +16,15 @@ 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 { if (!validateSchemaType(schemaContent, schemaType)) { throw new InvalidInput('Schema Type'); @@ -31,10 +32,11 @@ export async function registerSchema(schemaContent: string, schemaType: SchemaTy 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; @@ -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 @@ -57,9 +60,10 @@ export async function getSchema(did: string): Promise { 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; From 84cf7fff005cc07cd9104c9f68a813f47ad15fd6 Mon Sep 17 00:00:00 2001 From: sam bacha Date: Tue, 30 Mar 2021 06:12:51 -0700 Subject: [PATCH 4/4] Create nodejs.yml --- .github/workflows/nodejs.yml | 37 ++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 .github/workflows/nodejs.yml diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml new file mode 100644 index 0000000..24b4c8b --- /dev/null +++ b/.github/workflows/nodejs.yml @@ -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