-
Notifications
You must be signed in to change notification settings - Fork 127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(document): Enforce schema when loading genesis record #472
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,6 +102,15 @@ const create = async (params: TileParams, ceramic: Ceramic, context: Context, op | |
return await ceramic._createDocFromGenesis(record, opts) | ||
} | ||
|
||
const stringMapSchema = { | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"title": "StringMap", | ||
"type": "object", | ||
"additionalProperties": { | ||
"type": "string" | ||
} | ||
} | ||
|
||
let stateStore: LevelStateStore | ||
let pinStore: PinStore | ||
let pinning: PinningBackend | ||
|
@@ -130,6 +139,7 @@ describe('Document', () => { | |
let findHandler: any; | ||
let anchorService: AnchorService; | ||
let ceramic: Ceramic; | ||
let ceramicWithoutSchemaValidation: Ceramic; | ||
let context: Context; | ||
|
||
beforeEach(() => { | ||
|
@@ -177,6 +187,9 @@ describe('Document', () => { | |
|
||
ceramic = new Ceramic(dispatcher, pinStore, context) | ||
ceramic._doctypeHandlers['tile'] = doctypeHandler | ||
|
||
ceramicWithoutSchemaValidation = new Ceramic(dispatcher, pinStore, context, false) | ||
ceramicWithoutSchemaValidation._doctypeHandlers['tile'] = doctypeHandler | ||
}) | ||
|
||
it('is created correctly', async () => { | ||
|
@@ -326,6 +339,66 @@ describe('Document', () => { | |
await doc1._handleTip(tipInvalidUpdate) | ||
expect(doc1.content).toEqual(newContent) | ||
}) | ||
|
||
it('Enforces schema at document creation', async () => { | ||
const schemaDoc = await create({ content: stringMapSchema, metadata: { controllers } }, ceramic, context) | ||
await anchorUpdate(schemaDoc) | ||
|
||
try { | ||
const docParams = { | ||
content: {stuff: 1}, | ||
metadata: {controllers, schema: schemaDoc.id.toString()} | ||
} | ||
await create(docParams, ceramic, context) | ||
throw new Error('Should not be able to create a document with an invalid schema') | ||
} catch (e) { | ||
expect(e.message).toEqual('Validation Error: data[\'stuff\'] should be string') | ||
} | ||
}) | ||
|
||
it('Enforces schema at document update', async () => { | ||
const schemaDoc = await create({ content: stringMapSchema, metadata: { controllers } }, ceramic, context) | ||
await anchorUpdate(schemaDoc) | ||
|
||
const docParams = { | ||
content: {stuff: 1}, | ||
metadata: {controllers} | ||
} | ||
const doc = await create(docParams, ceramic, context) | ||
await anchorUpdate(doc) | ||
|
||
try { | ||
const updateRec = await TileDoctype._makeRecord(doc.doctype, user, null, doc.controllers, schemaDoc.id.toString()) | ||
await doc.applyRecord(updateRec) | ||
throw new Error('Should not be able to assign a schema to a document that does not conform') | ||
} catch (e) { | ||
expect(e.message).toEqual('Validation Error: data[\'stuff\'] should be string') | ||
} | ||
}) | ||
|
||
it('Enforces schema when loading genesis record', async () => { | ||
const schemaDoc = await create({ content: stringMapSchema, metadata: { controllers } }, ceramic, context) | ||
await anchorUpdate(schemaDoc) | ||
|
||
const docParams = { | ||
content: {stuff: 1}, | ||
metadata: {controllers, schema: schemaDoc.id.toString()} | ||
} | ||
// Create a document that isn't conforming to the schema | ||
const doc = await create(docParams, ceramicWithoutSchemaValidation, context) | ||
await anchorUpdate(doc) | ||
|
||
expect(doc.content).toEqual({stuff:1}) | ||
expect(doc.metadata.schema).toEqual(schemaDoc.id.toString()) | ||
|
||
try { | ||
await Document.load(doc.id, findHandler, dispatcher, pinStore, context, {skipWait:true}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the actual test for the core fix in this PR. Without the change to Document.load, this test fails. |
||
throw new Error('Should not be able to assign a schema to a document that does not conform') | ||
} catch (e) { | ||
expect(e.message).toEqual('Validation Error: data[\'stuff\'] should be string') | ||
} | ||
}) | ||
|
||
}) | ||
|
||
describe('Network update logic', () => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -145,6 +145,13 @@ class Document extends EventEmitter { | |
doc._doctype.state = await doc._doctypeHandler.applyRecord(record, doc._genesisCid, context) | ||
} | ||
|
||
if (validate) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep, that's it 👍 |
||
const schema = await Document.loadSchema(context.api, doc._doctype) | ||
if (schema) { | ||
Utils.validate(doc._doctype.content, schema) | ||
} | ||
} | ||
|
||
await doc._register(opts) | ||
return doc | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The whole point of this test was to test the behavior when loading a document that doesn't conform to its schema, but this check here isn't actually telling us anything useful since it's not going through the "real" document loading process, but instead just fetching it out of the local cache here.
To do this test properly it would need to be re-written as an integration test with two ceramic instances, one that creates the non-conforming document and a second that loads it. I'm happy to do that if you think it's worthwhile to add, but in the interest of time I decided to just focus on unit testing the core Document/applyRecord behavior and trust that it continues to work when being driven by the pubsub network
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
feel free to add a test into
ceramic.test.ts
test suite which is already spinning a couple of instances (ipfs+ceramic). It currently serves as a basic integration test for testing propagation between multiple instances. On the other side, it's not crucial to add it as part of this PR, we can open another story for that 👍