Skip to content

Commit

Permalink
Update for MPS 2.5.2 (#683)
Browse files Browse the repository at this point in the history
* Test UnbanCommand and UnbanPrompt have unbanned as a side effect.

You'd think it would be obvious, but clearly it hasn't been.  Part of
the reason for it is just how un-ergonomic it used to be to verify
room state.  Fortunately it's quite straight forward to do since MPS
has picked up. So now we can do it easily.

* Update for MPS 2.5.2.

- Fix SchemedMatrixData putting the wrong version number into data.
  Sadly a complication of
  #560.

- Fix an issue where the implementation of the RoomUnbanner capability
  was actually calling `/ban`.
  • Loading branch information
Gnuxie authored Jan 18, 2025
1 parent a16fb7b commit 8a17cfe
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 23 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@
"jsdom": "^24.0.0",
"matrix-appservice-bridge": "^10.3.1",
"matrix-bot-sdk": "npm:@vector-im/matrix-bot-sdk@^0.7.1-element.6",
"matrix-protection-suite": "npm:@gnuxie/matrix-protection-suite@2.5.1",
"matrix-protection-suite-for-matrix-bot-sdk": "npm:@gnuxie/matrix-protection-suite-for-matrix-bot-sdk@2.5.1",
"matrix-protection-suite": "npm:@gnuxie/matrix-protection-suite@2.5.2",
"matrix-protection-suite-for-matrix-bot-sdk": "npm:@gnuxie/matrix-protection-suite-for-matrix-bot-sdk@2.5.2",
"parse-duration": "^1.0.2",
"pg": "^8.8.0",
"shell-quote": "^1.7.3",
Expand Down
37 changes: 27 additions & 10 deletions test/integration/banPropagationTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { newTestUser } from "./clientHelper";
import { getFirstEventMatching } from "./commands/commandUtils";
import { DraupnirTestContext, draupnirClient } from "./mjolnirSetupUtils";
import {
MembershipChangeType,
NoticeMessageContent,
PolicyRuleType,
PropagationType,
Expand All @@ -17,6 +18,7 @@ import {
import {
MatrixRoomReference,
StringRoomID,
StringUserID,
} from "@the-draupnir-project/matrix-basic-types";

// We will need to disable this in tests that are banning people otherwise it will cause
Expand Down Expand Up @@ -55,17 +57,22 @@ describe("Ban propagation test", function () {
const moderator = await newTestUser(this.config.homeserverUrl, {
name: { contains: "moderator" },
});
const spammer = await newTestUser(this.config.homeserverUrl, {
name: { contains: "spam" },
});
const spamUserID = (await spammer.getUserId()) as StringUserID;
await moderator.joinRoom(draupnir.managementRoomID);
const protectedRooms = await Promise.all(
[...Array(5)].map(async (_) => {
const room = await moderator.createRoom({
invite: [draupnir.clientUserID],
invite: [draupnir.clientUserID, spamUserID],
});
await draupnir.client.joinRoom(room);
await moderator.setUserPowerLevel(draupnir.clientUserID, room, 100);
await draupnir.protectedRoomsSet.protectedRoomsManager.addRoom(
MatrixRoomReference.fromRoomID(room as StringRoomID)
);
await spammer.joinRoom(room);
return room;
})
);
Expand All @@ -91,11 +98,7 @@ describe("Ban propagation test", function () {
targetRoom: draupnir.managementRoomID,
lookAfterEvent: async function () {
// ban a user in one of our protected rooms using the moderator
await moderator.banUser(
"@test:example.com",
protectedRooms[0],
"spam"
);
await moderator.banUser(spamUserID, protectedRooms[0], "spam");
return undefined;
},
predicate: function (event: unknown): boolean {
Expand All @@ -119,11 +122,11 @@ describe("Ban propagation test", function () {
draupnir.protectedRoomsSet.issuerManager.policyListRevisionIssuer
.currentRevision;
const rules = policyListRevisionAfterBan.allRulesMatchingEntity(
"@test:example.com",
spamUserID,
PolicyRuleType.User
);
expect(rules.length).toBe(1);
expect(rules[0]?.entity).toBe("@test:example.com");
expect(rules[0]?.entity).toBe(spamUserID);
expect(rules[0]?.reason).toBe("spam");

// now unban them >:3
Expand All @@ -132,7 +135,7 @@ describe("Ban propagation test", function () {
targetRoom: draupnir.managementRoomID,
lookAfterEvent: async function () {
// ban a user in one of our protected rooms using the moderator
await moderator.unbanUser("@test:example.com", protectedRooms[0]);
await moderator.unbanUser(spamUserID, protectedRooms[0]);
return undefined;
},
predicate: function (event: unknown): boolean {
Expand All @@ -156,10 +159,24 @@ describe("Ban propagation test", function () {

const rulesAfterUnban =
policyListRevisionAfterUnBan.allRulesMatchingEntity(
"@test:example.com",
spamUserID,
PolicyRuleType.User
);
expect(rulesAfterUnban.length).toBe(0);
for (const room of protectedRooms) {
const membershipRevision =
draupnir.protectedRoomsSet.setRoomMembership.getRevision(
room as StringRoomID
);
if (membershipRevision === undefined) {
throw new TypeError(
`We should be able to get the membership for the protected room`
);
}
expect(
membershipRevision.membershipForUser(spamUserID)?.membershipChangeType
).toBe(MembershipChangeType.Unbanned);
}
} as unknown as Mocha.AsyncFunc
);
});
33 changes: 30 additions & 3 deletions test/unit/commands/UnbanCommandTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import { CommandExecutorHelper } from "@the-draupnir-project/interface-manager";
import {
MatrixRoomID,
MatrixRoomReference,
MatrixUserID,
StringUserID,
} from "@the-draupnir-project/matrix-basic-types";
Expand Down Expand Up @@ -75,14 +76,29 @@ describe("Test the DraupnirUnbanCommand", function () {
},
});
it("Will add a user to the policy list when they are banned", async function () {
const { protectedRoomsSet, policyRoomManager, roomStateManager } =
await createProtectedRooms();
const roomUnbanner = createMock<RoomUnbanner>({
async unbanUser(_room, userID, _reason) {
async unbanUser(roomOrRoomID, userID, reason) {
const room =
roomOrRoomID instanceof MatrixRoomID
? roomOrRoomID
: MatrixRoomReference.fromRoomID(roomOrRoomID);
expect(userID).toBe(ExistingBanUserID);
roomStateManager.appendState({
room,
membershipDescriptions: [
{
sender: DraupnirUserID,
target: userID,
membership: Membership.Leave,
...(reason === undefined ? {} : { reason }),
},
],
});
return Ok(undefined);
},
});
const { protectedRoomsSet, policyRoomManager } =
await createProtectedRooms();
const policyRoom = protectedRoomsSet.allProtectedRooms[0];
if (policyRoom === undefined) {
throw new TypeError(
Expand Down Expand Up @@ -128,5 +144,16 @@ describe("Test the DraupnirUnbanCommand", function () {
policyRoom
);
expect(banResult.isOkay).toBe(true);
const membership = protectedRoomsSet.setRoomMembership.getRevision(
policyRoom.toRoomIDOrAlias()
);
if (membership === undefined) {
throw new TypeError(
`We should be able to get the membership for the protected policy room`
);
}
expect(membership.membershipForUser(ExistingBanUserID)?.membership).toBe(
Membership.Leave
);
});
});
16 changes: 8 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2677,17 +2677,17 @@ matrix-appservice@^2.0.0:
request-promise "^4.2.6"
sanitize-html "^2.11.0"

"matrix-protection-suite-for-matrix-bot-sdk@npm:@gnuxie/matrix-protection-suite-for-matrix-bot-sdk@2.5.1":
version "2.5.1"
resolved "https://registry.yarnpkg.com/@gnuxie/matrix-protection-suite-for-matrix-bot-sdk/-/matrix-protection-suite-for-matrix-bot-sdk-2.5.1.tgz#eca2429114c23d1c8e17720644c8b89e86f537ba"
integrity sha512-uyY47lcCz1MyxgbZvYPGadhSNAT5c4IbljJmccx0bnCnj2IXK25jn48fPoN/auXWcXHGD0Qtpj/O1/pABeSaaA==
"matrix-protection-suite-for-matrix-bot-sdk@npm:@gnuxie/matrix-protection-suite-for-matrix-bot-sdk@2.5.2":
version "2.5.2"
resolved "https://registry.yarnpkg.com/@gnuxie/matrix-protection-suite-for-matrix-bot-sdk/-/matrix-protection-suite-for-matrix-bot-sdk-2.5.2.tgz#6391c8cb37d4a7cba06c3f5c985f9fbc02e1de4e"
integrity sha512-3wsQTOV5leMVhoM5jNfTcl8el5e6D0VVCDMlwVDzuh+3CwPYco0H0OUv2mC8UusoMdC3hjfOKmv0TZ1ywr8PWw==
dependencies:
"@gnuxie/typescript-result" "^1.0.0"

"matrix-protection-suite@npm:@gnuxie/matrix-protection-suite@2.5.1":
version "2.5.1"
resolved "https://registry.yarnpkg.com/@gnuxie/matrix-protection-suite/-/matrix-protection-suite-2.5.1.tgz#fb45f57beb7b8d37f1101c4fc5da9feb2c74f312"
integrity sha512-Jyq40bkI+kP0GpQ5U3AKJdu1bklXrsBbxyZOnNweCgLcMAv0TuZUQWQaJZRaC/C30KL8mPf4EEelwW2JOluwVA==
"matrix-protection-suite@npm:@gnuxie/matrix-protection-suite@2.5.2":
version "2.5.2"
resolved "https://registry.yarnpkg.com/@gnuxie/matrix-protection-suite/-/matrix-protection-suite-2.5.2.tgz#dc7e46efa5401ee23f61a01a9589337686acf417"
integrity sha512-Sp2siABcyyCXRMprofonW6GuWS1mVDuePKf0cEiA+me4PvHaDiuHKN6xr1QO8FHBwN9Nn2DAQwVGiXWwAxsB3g==
dependencies:
"@gnuxie/typescript-result" "^1.0.0"
await-lock "^2.2.2"
Expand Down

0 comments on commit 8a17cfe

Please sign in to comment.