|
| 1 | +import { MessageEventContent } from "matrix-bot-sdk"; |
| 2 | +import { E2ESetupTestTimeout, E2ETestEnv } from "./util/e2e-test"; |
| 3 | +import { describe, it, beforeEach, afterEach } from "@jest/globals"; |
| 4 | + |
| 5 | +const CryptoRoomState = [{ |
| 6 | + content: { |
| 7 | + "algorithm": "m.megolm.v1.aes-sha2" |
| 8 | + }, |
| 9 | + state_key: "", |
| 10 | + type: "m.room.encryption" |
| 11 | +}]; |
| 12 | + |
| 13 | +describe('End-2-End Encryption support', () => { |
| 14 | + let testEnv: E2ETestEnv; |
| 15 | + |
| 16 | + beforeEach(async () => { |
| 17 | + testEnv = await E2ETestEnv.createTestEnv({ matrixLocalparts: ['user'], enableE2EE: true }); |
| 18 | + await testEnv.setUp(); |
| 19 | + }, E2ESetupTestTimeout); |
| 20 | + |
| 21 | + afterEach(() => { |
| 22 | + return testEnv?.tearDown(); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should be able to send the help command', async () => { |
| 26 | + const user = testEnv.getUser('user'); |
| 27 | + const testRoomId = await user.createRoom({ name: 'Test room', invite:[testEnv.botMxid], initial_state: CryptoRoomState}); |
| 28 | + await user.setUserPowerLevel(testEnv.botMxid, testRoomId, 50); |
| 29 | + await user.waitForRoomJoin({sender: testEnv.botMxid, roomId: testRoomId }); |
| 30 | + await user.sendText(testRoomId, "!hookshot help"); |
| 31 | + await user.waitForRoomEvent<MessageEventContent>({ |
| 32 | + eventType: 'm.room.message', sender: testEnv.botMxid, roomId: testRoomId, |
| 33 | + }); |
| 34 | + }); |
| 35 | + it('should send notices in an encrypted format', async () => { |
| 36 | + const user = testEnv.getUser('user'); |
| 37 | + const testRoomId = await user.createRoom({ name: 'Test room', invite:[testEnv.botMxid], initial_state: CryptoRoomState}); |
| 38 | + await user.setUserPowerLevel(testEnv.botMxid, testRoomId, 50); |
| 39 | + await user.waitForRoomJoin({sender: testEnv.botMxid, roomId: testRoomId }); |
| 40 | + await user.sendText(testRoomId, "!hookshot webhook test-webhook"); |
| 41 | + const inviteResponse = await user.waitForRoomInvite({sender: testEnv.botMxid}); |
| 42 | + await user.waitForEncryptedEvent<MessageEventContent>({ |
| 43 | + eventType: 'm.room.message', sender: testEnv.botMxid, roomId: testRoomId, |
| 44 | + body: 'Room configured to bridge webhooks. See admin room for secret url.' |
| 45 | + }); |
| 46 | + const webhookUrlMessage = user.waitForEncryptedEvent<MessageEventContent>({ |
| 47 | + eventType: 'm.room.message', sender: testEnv.botMxid, roomId: inviteResponse.roomId |
| 48 | + }); |
| 49 | + await user.joinRoom(inviteResponse.roomId); |
| 50 | + const msgData = (await webhookUrlMessage).data.content.body; |
| 51 | + const webhookUrl = msgData.split('\n')[2]; |
| 52 | + const webhookNotice = user.waitForEncryptedEvent<MessageEventContent>({ |
| 53 | + eventType: 'm.room.message', sender: testEnv.botMxid, roomId: testRoomId, body: 'Hello world!' |
| 54 | + }); |
| 55 | + |
| 56 | + // Send a webhook |
| 57 | + await fetch(webhookUrl, { |
| 58 | + method: 'POST', |
| 59 | + headers: { 'Content-Type': 'application/json' }, |
| 60 | + body: JSON.stringify({text: 'Hello world!'}) |
| 61 | + }); |
| 62 | + |
| 63 | + // And await the notice. |
| 64 | + await webhookNotice; |
| 65 | + }); |
| 66 | +}); |
0 commit comments