Merge branch 'hotfix/21.56.9' into master
[gitter.git] / test / request-web-tests / matrix-bridging-tests.js
blob648fc220bd72e075ff29c95cb2bb26593e47c043
1 'use strict';
3 process.env.DISABLE_MATRIX_BRIDGE = '1';
4 process.env.DISABLE_API_LISTEN = '1';
5 process.env.DISABLE_API_WEB_LISTEN = '1';
7 const env = require('gitter-web-env');
8 const config = env.config;
10 const assert = require('assert');
11 const request = require('supertest');
12 const fixtureUtils = require('gitter-web-test-utils/lib/fixture-utils');
13 const fixtureLoader = require('gitter-web-test-utils/lib/test-fixtures');
14 const ensureMatrixFixtures = require('./utils/ensure-matrix-fixtures');
15 const registerTestSynapseUser = require('./utils/register-test-synapse-user');
16 const util = require('util');
17 const requestLib = util.promisify(require('request'));
18 const urlJoin = require('url-join');
20 const installBridge = require('gitter-web-matrix-bridge');
21 const matrixStore = require('gitter-web-matrix-bridge/lib/store');
23 const app = require('../../server/web');
25 const homeserverUrl = config.get('matrix:bridge:homeserverUrl');
26 const bridgePortFromConfig = config.get('matrix:bridge:applicationServicePort');
28 describe('Gitter <-> Matrix bridging e2e', () => {
29 const fixture = fixtureLoader.setupEach({
30 user1: {
31 accessToken: 'web-internal'
33 user2: {
34 accessToken: 'web-internal'
36 group1: {},
37 troupe1: {
38 group: 'group1'
40 troupePrivate1: {
41 group: 'group1',
42 users: ['user1'],
43 securityDescriptor: {
44 members: 'INVITE',
45 admins: 'MANUAL',
46 public: false
49 troupeOneToOne: {
50 oneToOne: true,
51 users: ['user1', 'user2']
53 });
55 //let someMatrixUserId;
56 let someMatrixUserAccessToken;
57 let stopBridge;
58 before(async () => {
59 await ensureMatrixFixtures();
61 stopBridge = await installBridge(bridgePortFromConfig + 1);
63 const localPart = fixtureUtils.generateUsername().slice(1);
64 //someMatrixUserId = `@${localPart}:${serverName}`;
65 const res = await registerTestSynapseUser(localPart);
66 someMatrixUserAccessToken = res.access_token;
67 assert(someMatrixUserAccessToken);
68 });
70 after(async () => {
71 if (stopBridge) {
72 await stopBridge();
74 });
76 it('bridges message to Matrix in public Gitter room', async () => {
77 const messageText = 'foo 123 baz';
78 // Send a message in a public room which should trigger the bridged Matrix
79 // room creation and send the message in the room.
80 const messageSendRes = await request(app)
81 .post(`/api/v1/rooms/${fixture.troupe1.id}/chatMessages`)
82 .send({ text: messageText })
83 .set('Authorization', `Bearer ${fixture.user1.accessToken}`)
84 .expect(200);
86 // Since we're using the async out-of-loop Gitter event-listeners to listen
87 // for the new chat message to come through and bridge we just have to wait
88 // until we see that the Matrix room is created and stored
89 let matrixRoomId;
90 do {
91 matrixRoomId = await matrixStore.getMatrixRoomIdByGitterRoomId(fixture.troupe1.id);
92 } while (!matrixRoomId);
93 // And wait for the initial message to be bridged which triggered this whole process
94 assert(messageSendRes.body.id);
95 let matrixEventId;
96 do {
97 matrixEventId = await matrixStore.getBridgedMessageEntryByGitterMessageId(
98 messageSendRes.body.id
100 } while (!matrixEventId);
102 // Try to join the room from some Matrix user's perspective. We should be able to get in!
103 const joinRes = await requestLib({
104 method: 'POST',
105 uri: urlJoin(homeserverUrl, `/_matrix/client/r0/rooms/${matrixRoomId}/join`),
106 json: true,
107 headers: {
108 Authorization: `Bearer ${someMatrixUserAccessToken}`,
109 'Content-Type': 'application/json'
111 body: {}
113 assert.strictEqual(
114 joinRes.statusCode,
115 200,
116 `Expected to be able to join Matrix room (which should be public) for bridged public Gitter room, joinRes.body=${JSON.stringify(
117 joinRes.body
121 // Make sure we can see the Gitter message in the matrix room as well
122 const messageRes = await requestLib({
123 method: 'GET',
124 uri: urlJoin(homeserverUrl, `/_matrix/client/r0/rooms/${matrixRoomId}/messages?dir=b`),
125 json: true,
126 headers: {
127 Authorization: `Bearer ${someMatrixUserAccessToken}`,
128 'Content-Type': 'application/json'
131 assert.strictEqual(
132 messageRes.statusCode,
133 200,
134 `Expected to be able to read messages in public Matrix room, messageRes.body=${JSON.stringify(
135 messageRes.body
138 assert.strictEqual(
139 messageRes.body.chunk.filter(event => event.type === 'm.room.message')[0].content.body,
140 messageText,
141 `Expected the latest message in the room to match our Gitter message we sent initially, messageRes.body=${JSON.stringify(
142 messageRes.body
147 it('bridges message to Matrix in private Gitter room', async () => {
148 const messageText = 'foo 123 baz';
149 // Send a message in a public room which should trigger the bridged Matrix
150 // room creation and send the message in the room.
151 const messageSendRes = await request(app)
152 .post(`/api/v1/rooms/${fixture.troupePrivate1.id}/chatMessages`)
153 .send({ text: messageText })
154 .set('Authorization', `Bearer ${fixture.user1.accessToken}`)
155 .expect(200);
157 // Since we're using the async out-of-loop Gitter event-listeners to listen
158 // for the new chat message to come through and bridge we just have to wait
159 // until we see that the Matrix room is created and stored
160 let matrixRoomId;
161 do {
162 matrixRoomId = await matrixStore.getMatrixRoomIdByGitterRoomId(fixture.troupePrivate1.id);
163 } while (!matrixRoomId);
164 // And wait for the initial message to be bridged which triggered this whole process
165 assert(messageSendRes.body.id);
166 let matrixEventId;
167 do {
168 matrixEventId = await matrixStore.getBridgedMessageEntryByGitterMessageId(
169 messageSendRes.body.id
171 } while (!matrixEventId);
173 // Try to join the room from some Matrix user's perspective. We shouldn't be able to get in!
174 const joinRes = await requestLib({
175 method: 'POST',
176 uri: urlJoin(homeserverUrl, `/_matrix/client/r0/rooms/${matrixRoomId}/join`),
177 json: true,
178 headers: {
179 Authorization: `Bearer ${someMatrixUserAccessToken}`,
180 'Content-Type': 'application/json'
182 body: {}
184 assert.strictEqual(
185 joinRes.statusCode,
186 403,
187 `Expected not to be able to join Matrix room (which should be private) for bridged private Gitter room, joinRes.body=${JSON.stringify(
188 joinRes.body
193 it('bridges message to Matrix in ONE_TO_ONE Gitter room', async () => {
194 const messageText = 'foo 123 baz';
195 // Send a message in a public room which should trigger the bridged Matrix
196 // room creation and send the message in the room.
197 const messageSendRes = await request(app)
198 .post(`/api/v1/rooms/${fixture.troupeOneToOne.id}/chatMessages`)
199 .send({ text: messageText })
200 .set('Authorization', `Bearer ${fixture.user1.accessToken}`)
201 .expect(200);
203 // Since we're using the async out-of-loop Gitter event-listeners to listen
204 // for the new chat message to come through and bridge we just have to wait
205 // until we see that the Matrix room is created and stored
206 let matrixRoomId;
207 do {
208 matrixRoomId = await matrixStore.getMatrixRoomIdByGitterRoomId(fixture.troupeOneToOne.id);
209 } while (!matrixRoomId);
210 // And wait for the initial message to be bridged which triggered this whole process
211 assert(messageSendRes.body.id);
212 let matrixEventId;
213 do {
214 matrixEventId = await matrixStore.getBridgedMessageEntryByGitterMessageId(
215 messageSendRes.body.id
217 } while (!matrixEventId);
219 // Try to join the room from some Matrix user's perspective. We shouldn't be able to get in!
220 const joinRes = await requestLib({
221 method: 'POST',
222 uri: urlJoin(homeserverUrl, `/_matrix/client/r0/rooms/${matrixRoomId}/join`),
223 json: true,
224 headers: {
225 Authorization: `Bearer ${someMatrixUserAccessToken}`,
226 'Content-Type': 'application/json'
228 body: {}
230 assert.strictEqual(
231 joinRes.statusCode,
232 403,
233 `Expected not to be able to join Matrix room (which should be private) for bridged ONE_TO_ONE Gitter room, joinRes.body=${JSON.stringify(
234 joinRes.body