Skip to content

Commit

Permalink
feat: Add test case for writing event with invalid metadata (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
stbrody authored Jun 7, 2024
1 parent 2d57659 commit 99c85e6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
24 changes: 22 additions & 2 deletions suite/src/__tests__/fast/get-events.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { describe, expect, test } from '@jest/globals'
import fetch from 'cross-fetch'
import { ReconEventInput, generateRandomEvent } from '../../utils/rustCeramicHelpers'
import {
ReconEventInput,
generateRandomEvent,
generateRandomRawEvent,
encodeRawEvent,
} from '../../utils/rustCeramicHelpers'
import { StreamID, randomCID } from '@ceramicnetwork/streamid'

const CeramicUrls = String(process.env.CERAMIC_URLS).split(',')
Expand All @@ -22,7 +27,10 @@ async function postEvent(url: string, event: ReconEventInput) {
body: JSON.stringify(event),
})
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status} ${response.statusText}`)
const message = await response.text()
throw new Error(
`Error posting event data to C1: ${response.status} ${response.statusText}: ${message}`,
)
}
}

Expand All @@ -39,6 +47,18 @@ describe('rust-ceramic e2e test', () => {
expect(await getResponse.json()).toEqual(event)
})

test('Cannot store event with unexpected metadata', async () => {
const modelId = new StreamID('model', randomCID())
const rawEvent = generateRandomRawEvent(modelId, 'did:key:faketestcontroller')
rawEvent.header.unexpected = "this field isn't a valid metadata field!"

const event = encodeRawEvent(rawEvent)
// publishing the event to rust-ceramic
await expect(postEvent(ceramicUrl, { data: event.data })).rejects.toThrow(
/Event bytes do not round-trip. This most likely means the event contains unexpected fields./,
)
})

test('get event data for non-existing event', async () => {
const eventId = randomCID().toString()
// fetching the event from its event-id from rust-ceramic
Expand Down
22 changes: 15 additions & 7 deletions suite/src/utils/rustCeramicHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CARFactory } from 'cartonne'
import * as dagJson from '@ipld/dag-json'
import * as dagCbor from '@ipld/dag-cbor'
import { sha256 } from 'multihashes-sync/sha2'
import { GenesisHeader, GenesisCommit } from '@ceramicnetwork/common'
import { GenesisCommit, GenesisHeader } from '@ceramicnetwork/common'
import { randomBytes } from 'crypto'

export interface ReconEventInput {
Expand All @@ -16,22 +16,25 @@ export interface ReconEvent {
data: string // car file
}

export function generateRandomEvent(modelId: StreamID, controller: string): ReconEvent {
const carFactory = new CARFactory()
carFactory.codecs.add(dagJson)
carFactory.hashers.add(sha256)
const car = carFactory.build().asV1()
export function generateRandomRawEvent(modelId: StreamID, controller: string): GenesisCommit {
const header: GenesisHeader = {
controllers: [controller],
model: modelId.bytes,
sep: 'model', // See CIP-120 for more details on this field
// make the events different so they don't get deduped. is this spec compliant?
unique: randomBytes(12),
}
const commit: GenesisCommit = {
return {
header,
data: null, // deterministic commit has no data and requires no signature
}
}

export function encodeRawEvent(commit: GenesisCommit): ReconEvent {
const carFactory = new CARFactory()
carFactory.codecs.add(dagJson)
carFactory.hashers.add(sha256)
const car = carFactory.build().asV1()
dagCbor.encode(commit)
car.put(commit, { isRoot: true })
const cid = car.roots[0]
Expand All @@ -41,6 +44,11 @@ export function generateRandomEvent(modelId: StreamID, controller: string): Reco
}
}

export function generateRandomEvent(modelId: StreamID, controller: string): ReconEvent {
const commit = generateRandomRawEvent(modelId, controller)
return encodeRawEvent(commit)
}

export function randomEvents(modelID: StreamID, count: number): ReconEvent[] {
let modelEvents = []

Expand Down

0 comments on commit 99c85e6

Please sign in to comment.