3 process.env.DISABLE_MATRIX_BRIDGE = '1';
4 process.env.DISABLE_API_LISTEN = '1';
5 process.env.DISABLE_API_WEB_LISTEN = '1';
7 const fixtureLoader = require('gitter-web-test-utils/lib/test-fixtures');
8 const fixtureUtils = require('gitter-web-test-utils/lib/fixture-utils');
9 const assert = require('assert');
10 const request = require('supertest');
11 const env = require('gitter-web-env');
12 const config = env.config;
13 const ensureMatrixFixtures = require('./utils/ensure-matrix-fixtures');
14 const registerTestSynapseUser = require('./utils/register-test-synapse-user');
16 const installBridge = require('gitter-web-matrix-bridge');
17 const matrixBridge = require('gitter-web-matrix-bridge/lib/matrix-bridge');
18 const GitterUtils = require('gitter-web-matrix-bridge/lib/gitter-utils');
19 const getMxidForGitterUser = require('gitter-web-matrix-bridge/lib/get-mxid-for-gitter-user');
21 const app = require('../../server/web');
23 const serverName = config.get('matrix:bridge:serverName');
24 const bridgePortFromConfig = config.get('matrix:bridge:applicationServicePort');
26 // Finds the regex in the text and creates an excerpt so the test failure message can more easily be understood
27 function findInText(text, regex, excerptBufferLength = 16) {
28 const result = text.match(regex);
32 excerpt: text.substring(
33 Math.max(0, result.index - excerptBufferLength),
34 Math.min(result.index + result[0].length + excerptBufferLength, text.length - 1)
40 describe('Rooms', function() {
41 const fixture = fixtureLoader.setup({
43 accessToken: 'web-internal'
46 accessToken: 'web-internal'
51 it(`Ensure there aren't unserialized documents handed off to the frontend`, async () => {
52 const result = await request(app)
53 .get(`/${fixture.troupeUnjoined1.uri}`)
54 .set('Authorization', `Bearer ${fixture.user1.accessToken}`)
57 const idFindResults = findInText(result.text, /\b_id\b/m);
60 `response should not include unserialized \`_id\` property (expecting \`id\`): ${idFindResults &&
61 idFindResults.excerpt}`
64 const versionFindResults = findInText(result.text, /\b__v\b/m);
67 `response should not include unserialized \`__v\` property (expecting \`v\` or nothing): ${versionFindResults &&
68 versionFindResults.excerpt}`
72 describe('Matrix DMs', () => {
76 await ensureMatrixFixtures();
78 stopBridge = await installBridge(bridgePortFromConfig + 1);
80 gitterUtils = new GitterUtils(matrixBridge);
87 it(`Creates Matrix DM when visiting URL`, async () => {
88 const localPart = fixtureUtils.generateUsername().slice(1);
89 const mxid = `@${localPart}:${serverName}`;
90 await registerTestSynapseUser(localPart);
93 .get(`/matrix/${fixture.user1.id}/${mxid}`)
94 .set('Authorization', `Bearer ${fixture.user1.accessToken}`)
98 it(`able to look at DM room for youself even when you're not joined`, async () => {
99 const mxid = `@${fixtureUtils.generateUsername().slice(1)}:${serverName}`;
101 const gitterRoom = await gitterUtils.createGitterDmRoomByGitterUserIdAndOtherPersonMxid(
107 .get(`/${gitterRoom.uri}`)
108 .set('Authorization', `Bearer ${fixture.user1.accessToken}`)
112 it(`DM room is private`, async () => {
113 const mxid = `@${fixtureUtils.generateUsername().slice(1)}:${serverName}`;
115 const gitterRoom = await gitterUtils.createGitterDmRoomByGitterUserIdAndOtherPersonMxid(
121 .get(`/${gitterRoom.uri}`)
122 // User2 trying to view user1 room is not allowed
123 .set('Authorization', `Bearer ${fixture.user2.accessToken}`)
127 it(`another user can't start a DM for another user by visiting their URL`, async () => {
128 const mxid = `@${fixtureUtils.generateUsername().slice(1)}:${serverName}`;
131 // DM between user1 and mxid
132 .get(`/matrix/${fixture.user1.id}/${mxid}`)
133 // Accessing from user2
134 .set('Authorization', `Bearer ${fixture.user2.accessToken}`)
138 it(`can not start DM with the MXID for a Gitter user`, async () => {
139 const otherGitterUserMxid = getMxidForGitterUser(fixture.user2);
142 .get(`/matrix/${fixture.user1.id}/${otherGitterUserMxid}`)
143 .set('Authorization', `Bearer ${fixture.user1.accessToken}`)
147 it(`Non-existant MXID shows a 404`, async () => {
148 const mxid = `@${fixtureUtils.generateUsername().slice(1)}:does-not-exist`;
151 .get(`/matrix/${fixture.user1.id}/${mxid}`)
152 .set('Authorization', `Bearer ${fixture.user1.accessToken}`)