-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a test verifying messageIsMediaProtection #545
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4f1854a
add a test verifying messageIsMediaProtection
H-Shay d7c8c61
revoke protection after tests to avoid interfering with other tests
H-Shay b3a7445
stop the client after using it
H-Shay 1c84b09
add copyright header
H-Shay 10f49cd
lint
H-Shay File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,96 @@ | ||||||
import { MatrixClient } from "@vector-im/matrix-bot-sdk"; | ||||||
import { newTestUser } from "./clientHelper"; | ||||||
import { getFirstReaction } from "./commands/commandUtils"; | ||||||
import { readFileSync } from "fs"; | ||||||
import { strict as assert } from "assert"; | ||||||
|
||||||
describe("Test: Message is media", function () { | ||||||
let client: MatrixClient; | ||||||
let testRoom: string; | ||||||
this.beforeEach(async function () { | ||||||
client = await newTestUser(this.config.homeserverUrl, { name: { contains: "message-is-media" } }); | ||||||
await client.start(); | ||||||
const mjolnirId = await this.mjolnir.client.getUserId(); | ||||||
testRoom = await client.createRoom({ invite: [mjolnirId] }); | ||||||
await client.joinRoom(testRoom); | ||||||
await client.joinRoom(this.config.managementRoom); | ||||||
await client.setUserPowerLevel(mjolnirId, testRoom, 100); | ||||||
}); | ||||||
this.afterEach(async function () { | ||||||
await getFirstReaction(client, this.mjolnir.managementRoomId, "✅", async () => { | ||||||
return await client.sendMessage(this.mjolnir.managementRoomId, { | ||||||
msgtype: "m.text", | ||||||
body: `!mjolnir disable MessageIsMediaProtection`, | ||||||
}); | ||||||
}); | ||||||
await client.stop(); | ||||||
}); | ||||||
|
||||||
function delay(ms: number) { | ||||||
return new Promise((resolve) => setTimeout(resolve, ms)); | ||||||
} | ||||||
|
||||||
it("Redacts all messages that are media if protection enabled", async function () { | ||||||
this.timeout(20000); | ||||||
|
||||||
await client.sendMessage(this.mjolnir.managementRoomId, { | ||||||
msgtype: "m.text", | ||||||
body: `!mjolnir rooms add ${testRoom}`, | ||||||
}); | ||||||
await getFirstReaction(client, this.mjolnir.managementRoomId, "✅", async () => { | ||||||
return await client.sendMessage(this.mjolnir.managementRoomId, { | ||||||
msgtype: "m.text", | ||||||
body: `!mjolnir enable MessageIsMediaProtection`, | ||||||
}); | ||||||
}); | ||||||
const data = readFileSync("test_tree.jpg"); | ||||||
const mxc = await client.uploadContent(data, "image/png"); | ||||||
let content = { msgtype: "m.image", body: "test.jpeg", url: mxc }; | ||||||
let imageMessage = await client.sendMessage(testRoom, content); | ||||||
|
||||||
let formatted_body = `<img src=${mxc} />`; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
let htmlContent = { | ||||||
msgtype: "m.image", | ||||||
body: formatted_body, | ||||||
format: "org.matrix.custom.html", | ||||||
formatted_body: formatted_body, | ||||||
}; | ||||||
let htmlMessage = await client.sendMessage(testRoom, htmlContent); | ||||||
|
||||||
// use a bogus mxc for video message as it doesn't matter for this test | ||||||
let videoContent = { msgtype: "m.video", body: "some_file.mp4", url: mxc }; | ||||||
let videoMessage = await client.sendMessage(testRoom, videoContent); | ||||||
|
||||||
await delay(700); | ||||||
let processedImage = await client.getEvent(testRoom, imageMessage); | ||||||
assert.equal(Object.keys(processedImage.content).length, 0, "This event should have been redacted."); | ||||||
|
||||||
let processedHtml = await client.getEvent(testRoom, htmlMessage); | ||||||
assert.equal(Object.keys(processedHtml.content).length, 0, "This html image event should have been redacted"); | ||||||
|
||||||
let processedVideo = await client.getEvent(testRoom, videoMessage); | ||||||
assert.equal(Object.keys(processedVideo.content).length, 0, "This event should have been redacted"); | ||||||
}); | ||||||
|
||||||
it("Doesn't redact massages that are not media.", async function () { | ||||||
this.timeout(20000); | ||||||
|
||||||
await client.sendMessage(this.mjolnir.managementRoomId, { | ||||||
msgtype: "m.text", | ||||||
body: `!mjolnir rooms add ${testRoom}`, | ||||||
}); | ||||||
await getFirstReaction(client, this.mjolnir.managementRoomId, "✅", async () => { | ||||||
return await client.sendMessage(this.mjolnir.managementRoomId, { | ||||||
msgtype: "m.text", | ||||||
body: `!mjolnir enable MessageIsMediaProtection`, | ||||||
}); | ||||||
}); | ||||||
|
||||||
let content = { msgtype: "m.text", body: "don't redact me bro" }; | ||||||
let textMessage = await client.sendMessage(testRoom, content); | ||||||
|
||||||
await delay(500); | ||||||
let processedImage = await client.getEvent(testRoom, textMessage); | ||||||
assert.equal(Object.keys(processedImage.content).length, 2, "This event should not have been redacted."); | ||||||
}); | ||||||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests should also have the copyright header. (and we should probably go back and fix the other tests one day)