Skip to content

Commit

Permalink
store deleted record
Browse files Browse the repository at this point in the history
  • Loading branch information
LiranCohen committed May 15, 2024
1 parent ef1c6bc commit 692cb54
Showing 1 changed file with 97 additions and 1 deletion.
98 changes: 97 additions & 1 deletion packages/api/tests/record.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2486,7 +2486,7 @@ describe('Record', () => {
expect(readResult.status.code).to.equal(404);
});

it('deletes a record that was only written to a remote DWN', async () => {
it('deletes record remotely that was not on the local DWN', async () => {
const { status: writeStatus, record } = await dwnAlice.records.write({
store : false,
data : 'Hello, world!',
Expand Down Expand Up @@ -2516,6 +2516,10 @@ describe('Record', () => {
expect(readResult.record).to.exist;
expect(readResult.record.id).to.equal(record.id);

// confirm you the record is not found locally when deleting
const { status: deleteLocalStatus } = await record.delete();
expect(deleteLocalStatus.code).to.equal(404);

const { status: deleteStatus } = await record.delete({ store: false});
expect(deleteStatus.code).to.equal(202);

Expand Down Expand Up @@ -2669,6 +2673,98 @@ describe('Record', () => {
expect(storedRecord.id).to.equal(record!.id);
expect(await storedRecord.data.text()).to.equal(updatedText);
});

it('stores a deleted record to the local DWN along with the initial write', async () => {
// Scenario: Alice creates a record.
// Bob reads the record from Alice's DWN and stores it.
// Bob deletes the record and stores the deletion.

// Alice creates a public record then sends it to her remote DWN.
const { status, record } = await dwnAlice.records.write({
data : 'Hello, world!',
message : {
published : true,
schema : 'foo/bar',
dataFormat : 'text/plain'
}
});
expect(status.code).to.equal(202, status.detail);
const sendResponse = await record.send();
expect(sendResponse.status.code).to.equal(202, sendResponse.status.detail);

// Bob reads for the record from his own node, should not return any results
let readResult = await dwnBob.records.read({
message: {
filter: {
recordId: record.id
}
}
});
expect(readResult.status.code).to.equal(404);

// Bob reads the record from Alice's remote DWN.
const readResultFromAlice = await dwnBob.records.read({
from : aliceDid.uri,
message : {
filter: {
recordId: record.id
}
}
});
expect(readResultFromAlice.status.code).to.equal(200);
expect(readResultFromAlice.record).to.exist;
const readRecord = readResultFromAlice.record;

// Attempts to store the record without signing it, which should fail.
let { status: storeRecordStatus } = await readRecord.store();
expect(storeRecordStatus.code).to.equal(401, storeRecordStatus.detail);

// Stores the record in Bob's DWN, the importRecord parameter is set to true so that Bob
// signs the record before storing it.
({ status: storeRecordStatus } = await readRecord.store(true));
expect(storeRecordStatus.code).to.equal(202, storeRecordStatus.detail);

// The record should now exist on Bob's DWN.
readResult = await dwnBob.records.read({
message: {
filter: {
recordId: record.id
}
}
});
expect(readResult.status.code).to.equal(200);
expect(readResult.record).to.exist;
const storedRecord = readResult.record;
expect(storedRecord.id).to.equal(record!.id);

// Bob delete the record
const { status: deleteStatus } = await storedRecord.delete();
expect(deleteStatus.code).to.equal(202);

// Bob sends the delete request to alice's DWN, it should reject
const { status: sendDeleteStatus } = await storedRecord.send(aliceDid.uri);
expect(sendDeleteStatus.code).to.equal(401);

// confirm the record has been deleted from Bob's DWN
readResult = await dwnBob.records.read({
message: {
filter: {
recordId: record.id
}
}
});
expect(readResult.status.code).to.equal(404);

// confirm it is not deleted from Alice's DWN
readResult = await dwnAlice.records.read({
message: {
filter: {
recordId: record.id
}
}
});
expect(readResult.status.code).to.equal(200);
});
});

describe('import()', () => {
Expand Down

0 comments on commit 692cb54

Please sign in to comment.