From 442b930d89a165ea231a24d096d38868ed71e4be Mon Sep 17 00:00:00 2001 From: gnuxie Date: Sat, 15 Mar 2025 14:39:48 +0000 Subject: [PATCH] Unit test and fix sqlite room audit log. --- .../RoomTakedown/SqliteRoomAuditLog.ts | 6 +-- test/unit/stores/roomAuditLogTest.ts | 45 +++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 test/unit/stores/roomAuditLogTest.ts diff --git a/src/protections/RoomTakedown/SqliteRoomAuditLog.ts b/src/protections/RoomTakedown/SqliteRoomAuditLog.ts index de0d3ecc..46e955ca 100644 --- a/src/protections/RoomTakedown/SqliteRoomAuditLog.ts +++ b/src/protections/RoomTakedown/SqliteRoomAuditLog.ts @@ -31,13 +31,13 @@ const schema = [ room_id TEXT NOT NULL, state_key TEXT NOT NULL, type TEXT NOT NULL, - recommendation TEXT NOT NULL, + recommendation TEXT NOT NULL ) STRICT;`, `CREATE TABLE room_takedown ( id INTEGER PRIMARY KEY AUTOINCREMENT, policy_id TEXT NOT NULL, target_room_id TEXT NOT NULL, - created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL, + created_at INTEGER DEFAULT (unixepoch()) NOT NULL, FOREIGN KEY (policy_id) REFERENCES policy_info(policy_id) ) STRICT;`, ]; @@ -119,7 +119,7 @@ export class SqliteRoomAuditLog private loadTakendownRooms(): StringRoomID[] { return this.db - .prepare(`SELECT room_id FROM room_takedown`) + .prepare(`SELECT target_room_id FROM room_takedown`) .all() as StringRoomID[]; } isRoomTakendown(roomID: StringRoomID): boolean { diff --git a/test/unit/stores/roomAuditLogTest.ts b/test/unit/stores/roomAuditLogTest.ts new file mode 100644 index 00000000..a3692971 --- /dev/null +++ b/test/unit/stores/roomAuditLogTest.ts @@ -0,0 +1,45 @@ +// SPDX-FileCopyrightText: 2025 Gnuxie +// +// SPDX-License-Identifier: AFL-3.0 + +import Database from "better-sqlite3"; +import { BetterSqliteOptions } from "../../../src/backingstore/better-sqlite3/BetterSqliteStore"; +import { SqliteRoomAuditLog } from "../../../src/protections/RoomTakedown/SqliteRoomAuditLog"; +import { + describePolicyRule, + LiteralPolicyRule, + parsePolicyRule, + PolicyRuleType, + randomRoomID, + Recommendation, +} from "matrix-protection-suite"; +import expect from "expect"; + +describe("RoomAuditLog test", function () { + const options = { path: ":memory:" } satisfies BetterSqliteOptions; + const db = new Database(options.path); + db.pragma("FOREIGN_KEYS = ON"); + const store = new SqliteRoomAuditLog(options, db); + it("Test that it doesn't return null or something", async function () { + expect(store.isRoomTakendown(randomRoomID([]).toRoomIDOrAlias())).toBe( + false + ); + }); + it("Can logged takendown rooms", async function () { + const bannedRoom = randomRoomID([]); + const policyRoom = randomRoomID([]); + const ban = parsePolicyRule( + describePolicyRule({ + room_id: policyRoom.toRoomIDOrAlias(), + entity: bannedRoom.toRoomIDOrAlias(), + reason: "spam", + recommendation: Recommendation.Takedown, + type: PolicyRuleType.Room, + }) as never + ).expect("Should bea ble to parse the policy rule."); + (await store.takedownRoom(ban as LiteralPolicyRule)).expect( + "Should be able to takedown a room" + ); + expect(store.isRoomTakendown(bannedRoom.toRoomIDOrAlias())).toBe(true); + }); +});