From b3aef10082fd8d34604400ff3634a7642b456603 Mon Sep 17 00:00:00 2001 From: Samika Kashyap Date: Tue, 6 Feb 2024 15:35:51 +0530 Subject: [PATCH 1/6] feat: e2e tests for fetching event from rust ceramic --- suite/src/__tests__/fast/get-events.test.ts | 54 +++++++++++++++++++++ suite/src/utils/rustCeramicHelpers.ts | 28 +++++++++++ 2 files changed, 82 insertions(+) create mode 100644 suite/src/__tests__/fast/get-events.test.ts create mode 100644 suite/src/utils/rustCeramicHelpers.ts diff --git a/suite/src/__tests__/fast/get-events.test.ts b/suite/src/__tests__/fast/get-events.test.ts new file mode 100644 index 00000000..9a81f134 --- /dev/null +++ b/suite/src/__tests__/fast/get-events.test.ts @@ -0,0 +1,54 @@ +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] + // const ceramicUrl = "http://127.0.0.1:5001" + 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({ + "eventId": eventId, + "eventData": 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(400); + expect(responseText).toContain('Event not found'); + expect(responseText).toContain(eventId); + expect(getResponse.statusText).toContain('Bad Request'); + }) +}) \ No newline at end of file diff --git a/suite/src/utils/rustCeramicHelpers.ts b/suite/src/utils/rustCeramicHelpers.ts new file mode 100644 index 00000000..c4a80f58 --- /dev/null +++ b/suite/src/utils/rustCeramicHelpers.ts @@ -0,0 +1,28 @@ +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'), + }; + } \ No newline at end of file From 54fce43b35eed88431ff6a8c555a4ddcbc4db57e Mon Sep 17 00:00:00 2001 From: Samika Kashyap Date: Tue, 6 Feb 2024 15:53:49 +0530 Subject: [PATCH 2/6] fix: format --- suite/src/__tests__/fast/get-events.test.ts | 34 +++++++++--------- suite/src/utils/rustCeramicHelpers.ts | 40 ++++++++++----------- 2 files changed, 35 insertions(+), 39 deletions(-) diff --git a/suite/src/__tests__/fast/get-events.test.ts b/suite/src/__tests__/fast/get-events.test.ts index 9a81f134..a0742d2c 100644 --- a/suite/src/__tests__/fast/get-events.test.ts +++ b/suite/src/__tests__/fast/get-events.test.ts @@ -4,17 +4,16 @@ import { generateRandomEventId, generateRandomEvent } from '../../utils/rustCera 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', + 'Content-Type': 'application/json', }, body: JSON.stringify(event), }) if (!response.ok) { - throw new Error(`HTTP error! status: ${response.status}`); + throw new Error(`HTTP error! status: ${response.status}`) } } @@ -25,30 +24,29 @@ async function getEventData(url: string, eventId: string) { } describe('rust-ceramic e2e test', () => { - const ceramicUrl = CeramicUrls[0] - // const ceramicUrl = "http://127.0.0.1:5001" + const ceramicUrl = CeramicUrls[0] test('post and get event data, success', async () => { - const eventId = generateRandomEventId(); - const event = generateRandomEvent(eventId); + 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({ - "eventId": eventId, - "eventData": event.eventData + expect(getResponse.status).toEqual(200) + expect(await getResponse.json()).toEqual({ + eventId: eventId, + eventData: event.eventData, }) }) test('get event data for non-existing event', async () => { - const eventId = generateRandomEventId(); + 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(400); - expect(responseText).toContain('Event not found'); - expect(responseText).toContain(eventId); - expect(getResponse.statusText).toContain('Bad Request'); + const responseText = await getResponse.text() + expect(getResponse.status).toEqual(400) + expect(responseText).toContain('Event not found') + expect(responseText).toContain(eventId) + expect(getResponse.statusText).toContain('Bad Request') }) -}) \ No newline at end of file +}) diff --git a/suite/src/utils/rustCeramicHelpers.ts b/suite/src/utils/rustCeramicHelpers.ts index c4a80f58..05bd86b2 100644 --- a/suite/src/utils/rustCeramicHelpers.ts +++ b/suite/src/utils/rustCeramicHelpers.ts @@ -1,28 +1,26 @@ import { randomCID, EventID, StreamID } from '@ceramicnetwork/streamid' import { base16 } from 'multiformats/bases/base16' -import { CARFactory } from "cartonne" +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 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'), - }; - } \ No newline at end of file +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'), + } +} From b57718f5f565152449a84bad0ef8173eb782db18 Mon Sep 17 00:00:00 2001 From: Samika Kashyap Date: Tue, 6 Feb 2024 23:19:27 +0530 Subject: [PATCH 3/6] fix: update status code on negative case --- suite/src/__tests__/fast/get-events.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/suite/src/__tests__/fast/get-events.test.ts b/suite/src/__tests__/fast/get-events.test.ts index a0742d2c..cc467176 100644 --- a/suite/src/__tests__/fast/get-events.test.ts +++ b/suite/src/__tests__/fast/get-events.test.ts @@ -44,7 +44,7 @@ describe('rust-ceramic e2e test', () => { // 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(400) + expect(getResponse.status).toEqual(404) expect(responseText).toContain('Event not found') expect(responseText).toContain(eventId) expect(getResponse.statusText).toContain('Bad Request') From 8d9a4d0e52d6bca790a0e9eaa206b443d2fb0062 Mon Sep 17 00:00:00 2001 From: Samika Kashyap Date: Tue, 6 Feb 2024 23:27:03 +0530 Subject: [PATCH 4/6] fix: code smell --- suite/src/__tests__/fast/get-events.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/suite/src/__tests__/fast/get-events.test.ts b/suite/src/__tests__/fast/get-events.test.ts index cc467176..0b93f55f 100644 --- a/suite/src/__tests__/fast/get-events.test.ts +++ b/suite/src/__tests__/fast/get-events.test.ts @@ -2,7 +2,7 @@ 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(',') +// const CeramicUrls = String(process.env.CERAMIC_URLS).split(',') async function postEvent(url: string, event: any) { let response = await fetch(url + '/ceramic/events', { @@ -24,7 +24,8 @@ async function getEventData(url: string, eventId: string) { } describe('rust-ceramic e2e test', () => { - const ceramicUrl = CeramicUrls[0] + // const ceramicUrl = CeramicUrls[0] + const ceramicUrl = "http://127.0.0.1:5001" test('post and get event data, success', async () => { const eventId = generateRandomEventId() const event = generateRandomEvent(eventId) @@ -47,6 +48,5 @@ describe('rust-ceramic e2e test', () => { expect(getResponse.status).toEqual(404) expect(responseText).toContain('Event not found') expect(responseText).toContain(eventId) - expect(getResponse.statusText).toContain('Bad Request') }) }) From be87e9bfddab0baa1ada8198ec12ff7bc937c40a Mon Sep 17 00:00:00 2001 From: Samika Kashyap Date: Tue, 6 Feb 2024 23:27:51 +0530 Subject: [PATCH 5/6] fix: code smell --- suite/src/__tests__/fast/get-events.test.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/suite/src/__tests__/fast/get-events.test.ts b/suite/src/__tests__/fast/get-events.test.ts index 0b93f55f..e57b742e 100644 --- a/suite/src/__tests__/fast/get-events.test.ts +++ b/suite/src/__tests__/fast/get-events.test.ts @@ -2,7 +2,7 @@ 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(',') +const CeramicUrls = String(process.env.CERAMIC_URLS).split(',') async function postEvent(url: string, event: any) { let response = await fetch(url + '/ceramic/events', { @@ -24,8 +24,7 @@ async function getEventData(url: string, eventId: string) { } describe('rust-ceramic e2e test', () => { - // const ceramicUrl = CeramicUrls[0] - const ceramicUrl = "http://127.0.0.1:5001" + const ceramicUrl = CeramicUrls[0] test('post and get event data, success', async () => { const eventId = generateRandomEventId() const event = generateRandomEvent(eventId) From bc058ada39d9d2cb17a31471b4d08fdf3c459eaa Mon Sep 17 00:00:00 2001 From: Samika Kashyap Date: Wed, 7 Feb 2024 21:49:03 +0530 Subject: [PATCH 6/6] fix: update response according to the new Event properties --- suite/src/__tests__/fast/get-events.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/suite/src/__tests__/fast/get-events.test.ts b/suite/src/__tests__/fast/get-events.test.ts index e57b742e..49f58d7f 100644 --- a/suite/src/__tests__/fast/get-events.test.ts +++ b/suite/src/__tests__/fast/get-events.test.ts @@ -34,8 +34,8 @@ describe('rust-ceramic e2e test', () => { const getResponse = await getEventData(ceramicUrl, eventId) expect(getResponse.status).toEqual(200) expect(await getResponse.json()).toEqual({ - eventId: eventId, - eventData: event.eventData, + id: eventId, + data: event.eventData, }) })