|
| 1 | +import { E2ESetupTestTimeout, E2ETestEnv, E2ETestMatrixClient } from "./util/e2e-test"; |
| 2 | +import { describe, it, beforeEach, afterEach } from "@jest/globals"; |
| 3 | +import { OutboundHookConnection } from "../src/Connections"; |
| 4 | +import { TextualMessageEventContent } from "matrix-bot-sdk"; |
| 5 | +import { IncomingHttpHeaders, createServer } from "http"; |
| 6 | +import busboy, { FileInfo } from "busboy"; |
| 7 | +import { TEST_FILE } from "./util/fixtures"; |
| 8 | + |
| 9 | +async function createOutboundConnection(user: E2ETestMatrixClient, botMxid: string, roomId: string) { |
| 10 | + const join = user.waitForRoomJoin({ sender: botMxid, roomId }); |
| 11 | + const connectionEvent = user.waitForRoomEvent({ |
| 12 | + eventType: OutboundHookConnection.CanonicalEventType, |
| 13 | + stateKey: 'test', |
| 14 | + sender: botMxid |
| 15 | + }); |
| 16 | + await user.inviteUser(botMxid, roomId); |
| 17 | + await user.setUserPowerLevel(botMxid, roomId, 50); |
| 18 | + await join; |
| 19 | + |
| 20 | + // Note: Here we create the DM proactively so this works across multiple |
| 21 | + // tests. |
| 22 | + // Get the DM room so we can get the token. |
| 23 | + const dmRoomId = await user.dms.getOrCreateDm(botMxid); |
| 24 | + |
| 25 | + await user.sendText(roomId, '!hookshot outbound-hook test http://localhost:8111/test-path'); |
| 26 | + // Test the contents of this. |
| 27 | + await connectionEvent; |
| 28 | + |
| 29 | + const msgPromise = user.waitForRoomEvent({ sender: botMxid, eventType: "m.room.message", roomId: dmRoomId }); |
| 30 | + const { data: msgData } = await msgPromise; |
| 31 | + |
| 32 | + const [_match, token ] = /<code>(.+)<\/code>/.exec((msgData.content as unknown as TextualMessageEventContent).formatted_body ?? "") ?? []; |
| 33 | + return token; |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * |
| 38 | + * @returns |
| 39 | + */ |
| 40 | +function awaitOutboundWebhook() { |
| 41 | + return new Promise<{headers: IncomingHttpHeaders, files: {name: string, file: Buffer, info: FileInfo}[]}>((resolve, reject) => { |
| 42 | + const server = createServer((req, res) => { |
| 43 | + const bb = busboy({headers: req.headers}); |
| 44 | + const files: {name: string, file: Buffer, info: FileInfo}[] = []; |
| 45 | + bb.on('file', (name, stream, info) => { |
| 46 | + const buffers: Buffer[] = []; |
| 47 | + stream.on('data', d => { |
| 48 | + buffers.push(d) |
| 49 | + }); |
| 50 | + stream.once('close', () => { |
| 51 | + files.push({name, info, file: Buffer.concat(buffers)}) |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + bb.once('close', () => { |
| 56 | + res.writeHead(200, { 'Content-Type': 'text/plain' }); |
| 57 | + res.end('OK'); |
| 58 | + resolve({ |
| 59 | + headers: req.headers, |
| 60 | + files, |
| 61 | + }); |
| 62 | + clearTimeout(timer); |
| 63 | + server.close(); |
| 64 | + }); |
| 65 | + |
| 66 | + req.pipe(bb); |
| 67 | + }); |
| 68 | + server.listen(8111); |
| 69 | + let timer: NodeJS.Timeout; |
| 70 | + timer = setTimeout(() => { |
| 71 | + reject(new Error("Request did not arrive")); |
| 72 | + server.close(); |
| 73 | + }, 10000); |
| 74 | + |
| 75 | + }); |
| 76 | +} |
| 77 | + |
| 78 | +describe('OutboundHooks', () => { |
| 79 | + let testEnv: E2ETestEnv; |
| 80 | + |
| 81 | + beforeAll(async () => { |
| 82 | + const webhooksPort = 9500 + E2ETestEnv.workerId; |
| 83 | + testEnv = await E2ETestEnv.createTestEnv({ |
| 84 | + matrixLocalparts: ['user'], |
| 85 | + config: { |
| 86 | + generic: { |
| 87 | + enabled: true, |
| 88 | + outbound: true, |
| 89 | + urlPrefix: `http://localhost:${webhooksPort}` |
| 90 | + }, |
| 91 | + listeners: [{ |
| 92 | + port: webhooksPort, |
| 93 | + bindAddress: '0.0.0.0', |
| 94 | + // Bind to the SAME listener to ensure we don't have conflicts. |
| 95 | + resources: ['webhooks'], |
| 96 | + }], |
| 97 | + } |
| 98 | + }); |
| 99 | + await testEnv.setUp(); |
| 100 | + }, E2ESetupTestTimeout); |
| 101 | + |
| 102 | + afterAll(() => { |
| 103 | + return testEnv?.tearDown(); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should be able to create a new webhook and push an event.', async () => { |
| 107 | + const user = testEnv.getUser('user'); |
| 108 | + const roomId = await user.createRoom({ name: 'My Test Webhooks room'}); |
| 109 | + const token = await createOutboundConnection(user, testEnv.botMxid, roomId); |
| 110 | + const gotWebhookRequest = awaitOutboundWebhook(); |
| 111 | + |
| 112 | + const eventId = await user.sendText(roomId, 'hello!'); |
| 113 | + const { headers, files } = await gotWebhookRequest; |
| 114 | + expect(headers['x-matrix-hookshot-roomid']).toEqual(roomId); |
| 115 | + expect(headers['x-matrix-hookshot-eventid']).toEqual(eventId); |
| 116 | + expect(headers['x-matrix-hookshot-token']).toEqual(token); |
| 117 | + |
| 118 | + // And check the JSON payload |
| 119 | + const [event, media] = files; |
| 120 | + expect(event.name).toEqual('event'); |
| 121 | + expect(event.info.mimeType).toEqual('application/json'); |
| 122 | + expect(event.info.filename).toEqual('event_data.json'); |
| 123 | + const eventJson = JSON.parse(event.file.toString('utf-8')); |
| 124 | + |
| 125 | + // Check that the content looks sane. |
| 126 | + expect(eventJson.room_id).toEqual(roomId); |
| 127 | + expect(eventJson.event_id).toEqual(eventId); |
| 128 | + expect(eventJson.sender).toEqual(await user.getUserId()); |
| 129 | + expect(eventJson.content.body).toEqual('hello!'); |
| 130 | + |
| 131 | + // No media should be present. |
| 132 | + expect(media).toBeUndefined(); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should be able to create a new webhook and push a media attachment.', async () => { |
| 136 | + const user = testEnv.getUser('user'); |
| 137 | + const roomId = await user.createRoom({ name: 'My Test Webhooks room'}); |
| 138 | + await createOutboundConnection(user, testEnv.botMxid, roomId); |
| 139 | + const gotWebhookRequest = awaitOutboundWebhook(); |
| 140 | + |
| 141 | + const mxcUrl = await user.uploadContent(TEST_FILE, 'image/svg+xml', "matrix.svg"); |
| 142 | + await user.sendMessage(roomId, { |
| 143 | + url: mxcUrl, |
| 144 | + msgtype: "m.file", |
| 145 | + body: "matrix.svg", |
| 146 | + }) |
| 147 | + const { files } = await gotWebhookRequest; |
| 148 | + const [event, media] = files; |
| 149 | + expect(event.info.mimeType).toEqual('application/json'); |
| 150 | + expect(event.info.filename).toEqual('event_data.json'); |
| 151 | + const eventJson = JSON.parse(event.file.toString('utf-8')); |
| 152 | + expect(eventJson.content.body).toEqual('matrix.svg'); |
| 153 | + |
| 154 | + |
| 155 | + expect(media.info.mimeType).toEqual('image/svg+xml'); |
| 156 | + expect(media.info.filename).toEqual('matrix.svg'); |
| 157 | + expect(media.file).toEqual(TEST_FILE); |
| 158 | + }); |
| 159 | + |
| 160 | + // TODO: This requires us to support Redis in test conditions, as encryption is not possible |
| 161 | + // in hookshot without it at the moment. |
| 162 | + |
| 163 | + // it.only('should be able to create a new webhook and push an encrypted media attachment.', async () => { |
| 164 | + // const user = testEnv.getUser('user'); |
| 165 | + // const roomId = await user.createRoom({ name: 'My Test Webhooks room', initial_state: [{ |
| 166 | + // content: { |
| 167 | + // "algorithm": "m.megolm.v1.aes-sha2" |
| 168 | + // }, |
| 169 | + // state_key: "", |
| 170 | + // type: "m.room.encryption" |
| 171 | + // }]}); |
| 172 | + // await createOutboundConnection(user, testEnv.botMxid, roomId); |
| 173 | + // const gotWebhookRequest = awaitOutboundWebhook(); |
| 174 | + |
| 175 | + // const encrypted = await user.crypto.encryptMedia(Buffer.from(TEST_FILE)); |
| 176 | + // const mxc = await user.uploadContent(TEST_FILE); |
| 177 | + // await user.sendMessage(roomId, { |
| 178 | + // msgtype: "m.image", |
| 179 | + // body: "matrix.svg", |
| 180 | + // info: { |
| 181 | + // mimetype: "image/svg+xml", |
| 182 | + // }, |
| 183 | + // file: { |
| 184 | + // url: mxc, |
| 185 | + // ...encrypted.file, |
| 186 | + // }, |
| 187 | + // }); |
| 188 | + |
| 189 | + // const { headers, files } = await gotWebhookRequest; |
| 190 | + // const [event, media] = files; |
| 191 | + // expect(event.info.mimeType).toEqual('application/json'); |
| 192 | + // expect(event.info.filename).toEqual('event_data.json'); |
| 193 | + // const eventJson = JSON.parse(event.file.toString('utf-8')); |
| 194 | + // expect(eventJson.content.body).toEqual('matrix.svg'); |
| 195 | + |
| 196 | + |
| 197 | + // expect(media.info.mimeType).toEqual('image/svg+xml'); |
| 198 | + // expect(media.info.filename).toEqual('matrix.svg'); |
| 199 | + // expect(media.file).toEqual(TEST_FILE); |
| 200 | + // }); |
| 201 | +}); |
0 commit comments