Update for MPS 2.5.2 (#683)

* 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
  https://github.com/the-draupnir-project/Draupnir/issues/560.

- Fix an issue where the implementation of the RoomUnbanner capability
  was actually calling `/ban`.
This commit is contained in:
Gnuxie authored and GitHub committed 2025-01-18 00:54:25 +00:00
1 parent a16fb7b2c0
commit 8a17cfed5d
4 files changed
+67 -23

No files matched your search

+27 -10
View File
@@ -7,6 +7,7 @@ import { newTestUser } from "./clientHelper";
import { getFirstEventMatching } from "./commands/commandUtils";
import { DraupnirTestContext, draupnirClient } from "./mjolnirSetupUtils";
import {
MembershipChangeType,
NoticeMessageContent,
PolicyRuleType,
PropagationType,
@@ -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
@@ -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;
})
);
@@ -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 {
@@ -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
@@ -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 {
@@ -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
);
});
+30 -3
View File
@@ -5,6 +5,7 @@
import { CommandExecutorHelper } from "@the-draupnir-project/interface-manager";
import {
MatrixRoomID,
MatrixRoomReference,
MatrixUserID,
StringUserID,
} from "@the-draupnir-project/matrix-basic-types";
@@ -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(
@@ -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
);
});
});