-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ws1 1455 e2e test for get events (#95)
Co-authored-by: Samika Kashyap <samikas@Samikas-MacBook-Pro.local>
- Loading branch information
Showing
2 changed files
with
77 additions
and
0 deletions.
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
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) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -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'), | ||
} | ||
} |