From d1345d0f83e7cec03fa478dbab83b3fdc8f30583 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Mon, 21 Jun 2021 17:52:02 +0200 Subject: [PATCH] write test for redaction in RelationWriter --- .../timeline/persistence/RelationWriter.js | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/matrix/room/timeline/persistence/RelationWriter.js b/src/matrix/room/timeline/persistence/RelationWriter.js index 853dcb0b..09c56f2a 100644 --- a/src/matrix/room/timeline/persistence/RelationWriter.js +++ b/src/matrix/room/timeline/persistence/RelationWriter.js @@ -251,3 +251,43 @@ const _REDACT_KEEP_CONTENT_MAP = { 'm.room.aliases': {'aliases': 1}, }; // end of matrix-js-sdk code + +import {createMockStorage} from "../../../../mocks/Storage.js"; +import {createEvent, withTextBody, withRedacts} from "../../../../mocks/event.js"; +import {FragmentIdComparer} from "../FragmentIdComparer.js"; +import {NullLogItem} from "../../../../logging/NullLogger.js"; + +export function tests() { + const fragmentIdComparer = new FragmentIdComparer([]); + const roomId = "$abc"; + const alice = "@alice:hs.tld"; + const bob = "@bob:hs.tld"; + + return { + "apply redaction": async assert => { + const event = withTextBody("Dogs > Cats", createEvent("m.room.message", "!abc", bob)); + const reason = "nonsense, cats are the best!"; + const redaction = withRedacts(event.event_id, reason, createEvent("m.room.redaction", "!def", alice)); + const redactionEntry = new EventEntry({fragmentId: 1, eventIndex: 3, event: redaction, roomId}, fragmentIdComparer); + const relationWriter = new RelationWriter({roomId, ownUserId: bob, fragmentIdComparer}); + + const storage = await createMockStorage(); + const txn = await storage.readWriteTxn([storage.storeNames.timelineEvents, storage.storeNames.timelineRelations]); + txn.timelineEvents.insert({fragmentId: 1, eventIndex: 2, event, roomId}); + const updatedEntries = await relationWriter.writeRelation(redactionEntry, txn, new NullLogItem()); + await txn.complete(); + + assert.equal(updatedEntries.length, 1); + const redactedMessage = updatedEntries[0]; + assert.equal(redactedMessage.id, "!abc"); + assert.equal(redactedMessage.content.body, undefined); + assert.equal(redactedMessage.redactionReason, reason); + + const readTxn = await storage.readTxn([storage.storeNames.timelineEvents]); + const storedMessage = await readTxn.timelineEvents.getByEventId(roomId, "!abc"); + await readTxn.complete(); + assert.equal(storedMessage.event.content.body, undefined); + assert.equal(storedMessage.event.unsigned.redacted_because.content.reason, reason); + } + } +}