Skip to content

Commit

Permalink
feat: ws1 1455 e2e test for get events (#95)
Browse files Browse the repository at this point in the history
Co-authored-by: Samika Kashyap <samikas@Samikas-MacBook-Pro.local>
  • Loading branch information
samika98 and Samika Kashyap authored Feb 9, 2024
1 parent 90119c7 commit eb34b30
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
51 changes: 51 additions & 0 deletions suite/src/__tests__/fast/get-events.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { describe, expect, test } from '@jest/globals'
import fetch from 'cross-fetch'
import { generateRandomEventId, generateRandomEvent } from '../../utils/rustCeramicHelpers'

const CeramicUrls = String(process.env.CERAMIC_URLS).split(',')

async function postEvent(url: string, event: any) {
let response = await fetch(url + '/ceramic/events', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(event),
})
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
}

async function getEventData(url: string, eventId: string) {
let response = await fetch(url + `/ceramic/events/${eventId}`)
console.log(response)
return response
}

describe('rust-ceramic e2e test', () => {
const ceramicUrl = CeramicUrls[0]
test('post and get event data, success', async () => {
const eventId = generateRandomEventId()
const event = generateRandomEvent(eventId)
// publishing the event to rust-ceramic
await postEvent(ceramicUrl, event)
// fetching the event from its event-id from rust-ceramic
const getResponse = await getEventData(ceramicUrl, eventId)
expect(getResponse.status).toEqual(200)
expect(await getResponse.json()).toEqual({
id: eventId,
data: event.eventData,
})
})

test('get event data for non-existing event', async () => {
const eventId = generateRandomEventId()
// fetching the event from its event-id from rust-ceramic
const getResponse = await getEventData(ceramicUrl, eventId)
const responseText = await getResponse.text()
expect(getResponse.status).toEqual(404)
expect(responseText).toContain('Event not found')
expect(responseText).toContain(eventId)
})
})
26 changes: 26 additions & 0 deletions suite/src/utils/rustCeramicHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { randomCID, EventID, StreamID } from '@ceramicnetwork/streamid'
import { base16 } from 'multiformats/bases/base16'
import { CARFactory } from 'cartonne'
import { base64 } from 'multiformats/bases/base64'
import * as random from '@stablelib/random'

export function generateRandomEventId(): string {
const modelID = new StreamID('model', randomCID())
const eventId = base16.encode(
EventID.createRandom('dev-unstable', 0, {
separatorKey: 'model',
separatorValue: modelID.toString(),
}).bytes,
)
return eventId
}

export function generateRandomEvent(eventId: string): any {
const carFactory = new CARFactory()
const car = carFactory.build().asV1()
car.put({ data: base64.encode(random.randomBytes(512)) }, { isRoot: true })
return {
eventId: eventId,
eventData: car.toString('base64'),
}
}

0 comments on commit eb34b30

Please sign in to comment.