Skip to content

Commit

Permalink
Removed the need for createProfile()
Browse files Browse the repository at this point in the history
  • Loading branch information
thehenrytsai committed Jan 3, 2024
1 parent 7b9bf23 commit 8849a5c
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 53 deletions.
5 changes: 2 additions & 3 deletions examples/node/stream-data.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { RecordsRead } from '@tbd54566975/dwn-sdk-js';
import { RecordsRead, DidKeyResolver } from '@tbd54566975/dwn-sdk-js';

import fetch from 'node-fetch';
import { v4 as uuidv4 } from 'uuid';

import { createJsonRpcRequest } from '../../dist/src/lib/json-rpc.js';
import {
createProfile,
createRecordsWriteMessage,
getFileAsReadStream,
} from '../../dist/tests/utils.js';

const alice = await createProfile();
const alice = await DidKeyResolver();
const { stream, size, cid } = await getFileAsReadStream('fixtures/test.jpeg');
const { recordsWrite } = await createRecordsWriteMessage(alice, {
dataCid: cid,
Expand Down
88 changes: 84 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"url": "https://github.com/TBD54566975/dwn-server/issues"
},
"dependencies": {
"@tbd54566975/dwn-sdk-js": "0.2.9",
"@tbd54566975/dwn-sdk-js": "0.2.10",
"@tbd54566975/dwn-sql-store": "0.2.5",
"better-sqlite3": "^8.5.0",
"bytes": "3.1.2",
Expand Down
6 changes: 4 additions & 2 deletions tests/dwn-process-message.spec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { DidKeyResolver } from '@tbd54566975/dwn-sdk-js';

import { expect } from 'chai';
import { v4 as uuidv4 } from 'uuid';

import { handleDwnProcessMessage } from '../src/json-rpc-handlers/dwn/process-message.js';
import type { RequestContext } from '../src/lib/json-rpc-router.js';
import { createJsonRpcRequest } from '../src/lib/json-rpc.js';
import { clear as clearDwn, dwn } from './test-dwn.js';
import { createProfile, createRecordsWriteMessage } from './utils.js';
import { createRecordsWriteMessage } from './utils.js';

describe('handleDwnProcessMessage', function () {
afterEach(async function () {
await clearDwn();
});

it('returns a JSON RPC Success Response when DWN returns a 2XX status code', async function () {
const alice = await createProfile();
const alice = await DidKeyResolver.generate();

// Construct a well-formed DWN Request that will be successfully processed.
const { recordsWrite, dataStream } = await createRecordsWriteMessage(alice);
Expand Down
24 changes: 12 additions & 12 deletions tests/http-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import {
Cid,
DataStream,
DidKeyResolver,
RecordsQuery,
RecordsRead,
} from '@tbd54566975/dwn-sdk-js';
Expand All @@ -24,7 +25,6 @@ import {
} from '../src/lib/json-rpc.js';
import { clear as clearDwn, dwn } from './test-dwn.js';
import {
createProfile,
createRecordsWriteMessage,
getFileAsReadStream,
streamHttpRequest,
Expand Down Expand Up @@ -77,7 +77,7 @@ describe('http api', function () {
});

it('responds with a 2XX HTTP status if JSON RPC handler returns 4XX/5XX DWN status code', async function () {
const alice = await createProfile();
const alice = await DidKeyResolver.generate();
const { recordsWrite, dataStream } = await createRecordsWriteMessage(alice);

// Intentionally delete a required property to produce an invalid RecordsWrite message.
Expand Down Expand Up @@ -135,7 +135,7 @@ describe('http api', function () {
});

it('works fine when no request body is provided', async function () {
const alice = await createProfile();
const alice = await DidKeyResolver.generate();
const recordsQuery = await RecordsQuery.create({
filter: {
schema: 'woosa',
Expand Down Expand Up @@ -165,7 +165,7 @@ describe('http api', function () {
const filePath = './fixtures/test.jpeg';
const { cid, size, stream } = await getFileAsReadStream(filePath);

const alice = await createProfile();
const alice = await DidKeyResolver.generate();
const { recordsWrite } = await createRecordsWriteMessage(alice, {
dataCid: cid,
dataSize: size,
Expand Down Expand Up @@ -200,7 +200,7 @@ describe('http api', function () {
});

it('handles RecordsWrite overwrite that does not mutate data', async function () {
const alice = await createProfile();
const alice = await DidKeyResolver.generate();

// First RecordsWrite that creates the record.
const { recordsWrite: initialWrite, dataStream } =
Expand Down Expand Up @@ -258,7 +258,7 @@ describe('http api', function () {
});

it('handles a RecordsWrite tombstone', async function () {
const alice = await createProfile();
const alice = await DidKeyResolver.generate();
const { recordsWrite: tombstone } =
await createRecordsWriteMessage(alice);

Expand Down Expand Up @@ -306,7 +306,7 @@ describe('http api', function () {
stream,
} = await getFileAsReadStream(filePath);

const alice = await createProfile();
const alice = await DidKeyResolver.generate();
const { recordsWrite } = await createRecordsWriteMessage(alice, {
dataCid: expectedCid,
dataSize: size,
Expand Down Expand Up @@ -390,7 +390,7 @@ describe('http api', function () {
stream,
} = await getFileAsReadStream(filePath);

const alice = await createProfile();
const alice = await DidKeyResolver.generate();
const { recordsWrite } = await createRecordsWriteMessage(alice, {
dataCid: expectedCid,
dataSize: size,
Expand Down Expand Up @@ -436,7 +436,7 @@ describe('http api', function () {
stream,
} = await getFileAsReadStream(filePath);

const alice = await createProfile();
const alice = await DidKeyResolver.generate();
const { recordsWrite } = await createRecordsWriteMessage(alice, {
dataCid: expectedCid,
dataSize: size,
Expand Down Expand Up @@ -473,7 +473,7 @@ describe('http api', function () {
});

it('returns a 404 if record doesnt exist', async function () {
const alice = await createProfile();
const alice = await DidKeyResolver.generate();
const { recordsWrite } = await createRecordsWriteMessage(alice);

const response = await fetch(
Expand All @@ -483,7 +483,7 @@ describe('http api', function () {
});

it('returns a 404 for invalid did', async function () {
const alice = await createProfile();
const alice = await DidKeyResolver.generate();
const { recordsWrite } = await createRecordsWriteMessage(alice);

const response = await fetch(
Expand All @@ -493,7 +493,7 @@ describe('http api', function () {
});

it('returns a 404 for invalid record id', async function () {
const alice = await createProfile();
const alice = await DidKeyResolver.generate();
const response = await fetch(
`http://localhost:3000/${alice.did}/records/kaka`,
);
Expand Down
25 changes: 1 addition & 24 deletions tests/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import type { PrivateJwk, PublicJwk, Signer } from '@tbd54566975/dwn-sdk-js';
import {
Cid,
DataStream,
DidKeyResolver,
PrivateKeySigner,
RecordsWrite,
} from '@tbd54566975/dwn-sdk-js';
import { Cid, DataStream, RecordsWrite } from '@tbd54566975/dwn-sdk-js';

import type { ReadStream } from 'node:fs';
import fs from 'node:fs';
Expand All @@ -28,23 +22,6 @@ export type Profile = {
signer: Signer;
};

export async function createProfile(): Promise<Profile> {
const { did, keyPair, keyId } = await DidKeyResolver.generate();

// signer is required by all dwn message classes. it's used to sign messages
const signer = new PrivateKeySigner({
privateJwk: keyPair.privateJwk,
algorithm: keyPair.privateJwk.alg,
keyId: `${did}#${keyId}`,
});

return {
did,
keyPair,
signer,
};
}

export type CreateRecordsWriteOverrides =
| ({
dataCid?: string;
Expand Down
10 changes: 3 additions & 7 deletions tests/ws-api.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DataStream } from '@tbd54566975/dwn-sdk-js';
import { DataStream, DidKeyResolver } from '@tbd54566975/dwn-sdk-js';

import { expect } from 'chai';
import { base64url } from 'multiformats/bases/base64';
Expand All @@ -12,11 +12,7 @@ import {
} from '../src/lib/json-rpc.js';
import { WsApi } from '../src/ws-api.js';
import { clear as clearDwn, dwn } from './test-dwn.js';
import {
createProfile,
createRecordsWriteMessage,
sendWsMessage,
} from './utils.js';
import { createRecordsWriteMessage, sendWsMessage } from './utils.js';

let server: http.Server;
let wsServer: WebSocketServer;
Expand Down Expand Up @@ -60,7 +56,7 @@ describe('websocket api', function () {
});

it('handles RecordsWrite messages', async function () {
const alice = await createProfile();
const alice = await DidKeyResolver.generate();
const { recordsWrite, dataStream } = await createRecordsWriteMessage(alice);
const dataBytes = await DataStream.toBytes(dataStream);
const encodedData = base64url.baseEncode(dataBytes);
Expand Down

0 comments on commit 8849a5c

Please sign in to comment.