Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Dwn.handleRecordsDelete function #549

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/dwn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import type { TenantGate } from './core/tenant-gate.js';
import type { EventsGetMessage, EventsGetReply, PermissionsGrantMessage, PermissionsRequestMessage, PermissionsRevokeMessage, ProtocolsConfigureMessage, ProtocolsQueryMessage, ProtocolsQueryReply } from './index.js';
import type { GenericMessageReply, UnionMessageReply } from './core/message-reply.js';
import type { MessagesGetMessage, MessagesGetReply } from './types/messages-types.js';
import type { RecordsDeleteMessage, RecordsQueryMessage, RecordsQueryReply, RecordsReadMessage, RecordsReadReply, RecordsWriteMessage, RecordsWriteReply } from './types/records-types.js';
import type { RecordsDeleteMessage, RecordsDeleteReply, RecordsQueryMessage, RecordsQueryReply,
RecordsReadMessage, RecordsReadReply, RecordsWriteMessage, RecordsWriteReply } from './types/records-types.js';

import { AllowAllTenantGate } from './core/tenant-gate.js';
import { DidResolver } from './did/did-resolver.js';
Expand Down Expand Up @@ -168,6 +169,21 @@ export class Dwn {
return handler.handle({ tenant, message });
}

/**
* Handles a `RecordsDelete` message.
*/
public async handleRecordsDelete(tenant: string, message: RecordsDeleteMessage): Promise<RecordsDeleteReply> {
const errorMessageReply =
await this.validateTenant(tenant) ??
await this.validateMessageIntegrity(message, DwnInterfaceName.Records, DwnMethodName.Delete);
if (errorMessageReply !== undefined) {
return errorMessageReply;
}

const handler = new RecordsDeleteHandler(this.didResolver, this.messageStore, this.dataStore, this.eventLog);
return handler.handle({ tenant, message });
}

/**
* Handles a `MessagesGet` message.
*/
Expand Down
2 changes: 2 additions & 0 deletions src/types/records-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export type RecordsWriteDescriptor = {

export type RecordsWriteReply = GenericMessageReply;

export type RecordsDeleteReply = GenericMessageReply;
thehenrytsai marked this conversation as resolved.
Show resolved Hide resolved

/**
* Internal RecordsWrite message representation that can be in an incomplete state.
*/
Expand Down
16 changes: 16 additions & 0 deletions tests/dwn.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,5 +365,21 @@ export function testDwnClass(): void {

});

describe('handleRecordsDelete', () => {
it('should return 400 error for bad message (invalid method)', async () => {
const alice = await DidKeyResolver.generate();

const { message } = await TestDataGenerator.generateRecordsDelete();

( message as any).descriptor.method = 'Foo';

const reply = await dwn.handleRecordsDelete(alice.did, message);

expect(reply.status.code).to.equal(400);
expect(reply.status.detail).to.equal('Expected method RecordsDelete, received RecordsFoo');
});

});

});
}
18 changes: 9 additions & 9 deletions tests/handlers/records-delete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export function testRecordsDeleteHandler(): void {
authorizationSigner : Jws.createSigner(alice)
});

const deleteReply = await dwn.processMessage(alice.did, recordsDelete.message);
const deleteReply = await dwn.handleRecordsDelete(alice.did, recordsDelete.message);
expect(deleteReply.status.code).to.equal(202);

// ensure a query will no longer find the deleted record
Expand All @@ -95,7 +95,7 @@ export function testRecordsDeleteHandler(): void {
authorizationSigner : Jws.createSigner(alice)
});

const recordsDelete2Reply = await dwn.processMessage(alice.did, recordsDelete2.message);
const recordsDelete2Reply = await dwn.handleRecordsDelete(alice.did, recordsDelete2.message);
expect(recordsDelete2Reply.status.code).to.equal(404);
});

Expand Down Expand Up @@ -129,7 +129,7 @@ export function testRecordsDeleteHandler(): void {
author : alice,
recordId : aliceWriteData.message.recordId
});
const aliceDeleteWriteReply = await dwn.processMessage(alice.did, aliceDeleteWriteData.message);
const aliceDeleteWriteReply = await dwn.handleRecordsDelete(alice.did, aliceDeleteWriteData.message);
expect(aliceDeleteWriteReply.status.code).to.equal(202);

// verify the other record with the same data is unaffected
Expand All @@ -151,7 +151,7 @@ export function testRecordsDeleteHandler(): void {
author : alice,
recordId : aliceAssociateData.message.recordId
});
const aliceDeleteAssociateReply = await dwn.processMessage(alice.did, aliceDeleteAssociateData.message);
const aliceDeleteAssociateReply = await dwn.handleRecordsDelete(alice.did, aliceDeleteAssociateData.message);
expect(aliceDeleteAssociateReply.status.code).to.equal(202);

// verify that alice can no longer fetch the 2nd record
Expand Down Expand Up @@ -182,7 +182,7 @@ export function testRecordsDeleteHandler(): void {
authorizationSigner : Jws.createSigner(alice)
});

const deleteReply = await dwn.processMessage(alice.did, recordsDelete.message);
const deleteReply = await dwn.handleRecordsDelete(alice.did, recordsDelete.message);
expect(deleteReply.status.code).to.equal(404);
});

Expand Down Expand Up @@ -211,7 +211,7 @@ export function testRecordsDeleteHandler(): void {
expect(subsequentWriteReply.status.code).to.equal(202);

// test that a delete with an earlier `messageTimestamp` results in a 409
const deleteReply = await dwn.processMessage(alice.did, recordsDelete.message);
const deleteReply = await dwn.handleRecordsDelete(alice.did, recordsDelete.message);
expect(deleteReply.status.code).to.equal(409);

// ensure data still exists
Expand Down Expand Up @@ -253,7 +253,7 @@ export function testRecordsDeleteHandler(): void {
author : alice,
recordId : aliceWriteData.message.recordId
});
const aliceDeleteWriteReply = await dwn.processMessage(alice.did, aliceDeleteWriteData.message);
const aliceDeleteWriteReply = await dwn.handleRecordsDelete(alice.did, aliceDeleteWriteData.message);
expect(aliceDeleteWriteReply.status.code).to.equal(202);

const aliceQueryWriteAfterAliceDeleteData = await TestDataGenerator.generateRecordsQuery({
Expand Down Expand Up @@ -295,7 +295,7 @@ export function testRecordsDeleteHandler(): void {
authorizationSigner : Jws.createSigner(alice)
});

const deleteReply = await dwn.processMessage(alice.did, recordsDelete.message);
const deleteReply = await dwn.handleRecordsDelete(alice.did, recordsDelete.message);
expect(deleteReply.status.code).to.equal(202);

const events = await eventLog.getEvents(alice.did);
Expand Down Expand Up @@ -333,7 +333,7 @@ export function testRecordsDeleteHandler(): void {
authorizationSigner : Jws.createSigner(author)
});

const deleteReply = await dwn.processMessage(author.did, recordsDelete.message);
const deleteReply = await dwn.handleRecordsDelete(author.did, recordsDelete.message);
expect(deleteReply.status.code).to.equal(202);

const events = await eventLog.getEvents(author.did);
Expand Down
2 changes: 1 addition & 1 deletion tests/handlers/records-read.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1428,7 +1428,7 @@ export function testRecordsReadHandler(): void {
authorizationSigner : Jws.createSigner(alice)
});

const deleteReply = await dwn.processMessage(alice.did, recordsDelete.message);
const deleteReply = await dwn.handleRecordsDelete(alice.did, recordsDelete.message);
expect(deleteReply.status.code).to.equal(202);

// RecordsRead
Expand Down
8 changes: 4 additions & 4 deletions tests/handlers/records-write.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ export function testRecordsWriteHandler(): void {
recordId : message.recordId,
authorizationSigner : Jws.createSigner(author),
});
const deleteReply = await dwn.processMessage(tenant, recordsDelete.message);
const deleteReply = await dwn.handleRecordsDelete(tenant, recordsDelete.message);
expect(deleteReply.status.code).to.equal(202);

const write = await RecordsWrite.createFrom({
Expand Down Expand Up @@ -676,7 +676,7 @@ export function testRecordsWriteHandler(): void {
recordId : message.recordId,
authorizationSigner : Jws.createSigner(author),
});
const deleteReply = await dwn.processMessage(tenant, recordsDelete.message);
const deleteReply = await dwn.handleRecordsDelete(tenant, recordsDelete.message);
expect(deleteReply.status.code).to.equal(202);

const write = await RecordsWrite.createFrom({
Expand Down Expand Up @@ -1336,7 +1336,7 @@ export function testRecordsWriteHandler(): void {
author : alice,
recordId : friendRoleRecord.message.recordId,
});
const deleteFriendReply = await dwn.processMessage(alice.did, deleteFriend.message);
const deleteFriendReply = await dwn.handleRecordsDelete(alice.did, deleteFriend.message);
expect(deleteFriendReply.status.code).to.equal(202);

// Alice writes a new record adding Bob as a 'friend' again
Expand Down Expand Up @@ -1553,7 +1553,7 @@ export function testRecordsWriteHandler(): void {
author : alice,
recordId : participantRecord1.message.recordId,
});
const participantDeleteReply = await dwn.processMessage(alice.did, partipantDelete.message);
const participantDeleteReply = await dwn.handleRecordsDelete(alice.did, partipantDelete.message);
expect(participantDeleteReply.status.code).to.equal(202);

// Alice creates a new 'thread/participant' record
Expand Down