move everything to subfolder to merge into react-sdk
This commit is contained in:
parent
6cb9ef7e65
commit
ca86969f92
47 changed files with 0 additions and 0 deletions
30
test/end-to-end-tests/src/rest/consent.js
Normal file
30
test/end-to-end-tests/src/rest/consent.js
Normal file
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
Copyright 2018 New Vector Ltd
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
const request = require('request-promise-native');
|
||||
const cheerio = require('cheerio');
|
||||
const url = require("url");
|
||||
|
||||
module.exports.approveConsent = async function(consentUrl) {
|
||||
const body = await request.get(consentUrl);
|
||||
const doc = cheerio.load(body);
|
||||
const v = doc("input[name=v]").val();
|
||||
const u = doc("input[name=u]").val();
|
||||
const h = doc("input[name=h]").val();
|
||||
const formAction = doc("form").attr("action");
|
||||
const absAction = url.resolve(consentUrl, formAction);
|
||||
await request.post(absAction).form({v, u, h});
|
||||
};
|
91
test/end-to-end-tests/src/rest/creator.js
Normal file
91
test/end-to-end-tests/src/rest/creator.js
Normal file
|
@ -0,0 +1,91 @@
|
|||
/*
|
||||
Copyright 2018 New Vector Ltd
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
const {exec} = require('child_process');
|
||||
const request = require('request-promise-native');
|
||||
const RestSession = require('./session');
|
||||
const RestMultiSession = require('./multi');
|
||||
|
||||
function execAsync(command, options) {
|
||||
return new Promise((resolve, reject) => {
|
||||
exec(command, options, (error, stdout, stderr) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve({stdout, stderr});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = class RestSessionCreator {
|
||||
constructor(synapseSubdir, hsUrl, cwd) {
|
||||
this.synapseSubdir = synapseSubdir;
|
||||
this.hsUrl = hsUrl;
|
||||
this.cwd = cwd;
|
||||
}
|
||||
|
||||
async createSessionRange(usernames, password, groupName) {
|
||||
const sessionPromises = usernames.map((username) => this.createSession(username, password));
|
||||
const sessions = await Promise.all(sessionPromises);
|
||||
return new RestMultiSession(sessions, groupName);
|
||||
}
|
||||
|
||||
async createSession(username, password) {
|
||||
await this._register(username, password);
|
||||
console.log(` * created REST user ${username} ... done`);
|
||||
const authResult = await this._authenticate(username, password);
|
||||
return new RestSession(authResult);
|
||||
}
|
||||
|
||||
async _register(username, password) {
|
||||
const registerArgs = [
|
||||
'-c homeserver.yaml',
|
||||
`-u ${username}`,
|
||||
`-p ${password}`,
|
||||
'--no-admin',
|
||||
this.hsUrl
|
||||
];
|
||||
const registerCmd = `./register_new_matrix_user ${registerArgs.join(' ')}`;
|
||||
const allCmds = [
|
||||
`cd ${this.synapseSubdir}`,
|
||||
". ./activate",
|
||||
registerCmd
|
||||
].join(' && ');
|
||||
|
||||
await execAsync(allCmds, {cwd: this.cwd, encoding: 'utf-8'});
|
||||
}
|
||||
|
||||
async _authenticate(username, password) {
|
||||
const requestBody = {
|
||||
"type": "m.login.password",
|
||||
"identifier": {
|
||||
"type": "m.id.user",
|
||||
"user": username
|
||||
},
|
||||
"password": password
|
||||
};
|
||||
const url = `${this.hsUrl}/_matrix/client/r0/login`;
|
||||
const responseBody = await request.post({url, json: true, body: requestBody});
|
||||
return {
|
||||
accessToken: responseBody.access_token,
|
||||
homeServer: responseBody.home_server,
|
||||
userId: responseBody.user_id,
|
||||
deviceId: responseBody.device_id,
|
||||
hsUrl: this.hsUrl,
|
||||
};
|
||||
}
|
||||
}
|
95
test/end-to-end-tests/src/rest/multi.js
Normal file
95
test/end-to-end-tests/src/rest/multi.js
Normal file
|
@ -0,0 +1,95 @@
|
|||
/*
|
||||
Copyright 2018 New Vector Ltd
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
const request = require('request-promise-native');
|
||||
const RestRoom = require('./room');
|
||||
const {approveConsent} = require('./consent');
|
||||
const Logger = require('../logger');
|
||||
|
||||
module.exports = class RestMultiSession {
|
||||
constructor(sessions, groupName) {
|
||||
this.log = new Logger(groupName);
|
||||
this.sessions = sessions;
|
||||
}
|
||||
|
||||
slice(groupName, start, end) {
|
||||
return new RestMultiSession(this.sessions.slice(start, end), groupName);
|
||||
}
|
||||
|
||||
pop(userName) {
|
||||
const idx = this.sessions.findIndex((s) => s.userName() === userName);
|
||||
if(idx === -1) {
|
||||
throw new Error(`user ${userName} not found`);
|
||||
}
|
||||
const session = this.sessions.splice(idx, 1)[0];
|
||||
return session;
|
||||
}
|
||||
|
||||
async setDisplayName(fn) {
|
||||
this.log.step("set their display name")
|
||||
await Promise.all(this.sessions.map(async (s) => {
|
||||
s.log.mute();
|
||||
await s.setDisplayName(fn(s));
|
||||
s.log.unmute();
|
||||
}));
|
||||
this.log.done();
|
||||
}
|
||||
|
||||
async join(roomIdOrAlias) {
|
||||
this.log.step(`join ${roomIdOrAlias}`)
|
||||
const rooms = await Promise.all(this.sessions.map(async (s) => {
|
||||
s.log.mute();
|
||||
const room = await s.join(roomIdOrAlias);
|
||||
s.log.unmute();
|
||||
return room;
|
||||
}));
|
||||
this.log.done();
|
||||
return new RestMultiRoom(rooms, roomIdOrAlias, this.log);
|
||||
}
|
||||
|
||||
room(roomIdOrAlias) {
|
||||
const rooms = this.sessions.map(s => s.room(roomIdOrAlias));
|
||||
return new RestMultiRoom(rooms, roomIdOrAlias, this.log);
|
||||
}
|
||||
}
|
||||
|
||||
class RestMultiRoom {
|
||||
constructor(rooms, roomIdOrAlias, log) {
|
||||
this.rooms = rooms;
|
||||
this.roomIdOrAlias = roomIdOrAlias;
|
||||
this.log = log;
|
||||
}
|
||||
|
||||
async talk(message) {
|
||||
this.log.step(`say "${message}" in ${this.roomIdOrAlias}`)
|
||||
await Promise.all(this.rooms.map(async (r) => {
|
||||
r.log.mute();
|
||||
await r.talk(message);
|
||||
r.log.unmute();
|
||||
}));
|
||||
this.log.done();
|
||||
}
|
||||
|
||||
async leave() {
|
||||
this.log.step(`leave ${this.roomIdOrAlias}`)
|
||||
await Promise.all(this.rooms.map(async (r) => {
|
||||
r.log.mute();
|
||||
await r.leave();
|
||||
r.log.unmute();
|
||||
}));
|
||||
this.log.done();
|
||||
}
|
||||
}
|
47
test/end-to-end-tests/src/rest/room.js
Normal file
47
test/end-to-end-tests/src/rest/room.js
Normal file
|
@ -0,0 +1,47 @@
|
|||
/*
|
||||
Copyright 2018 New Vector Ltd
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
const uuidv4 = require('uuid/v4');
|
||||
|
||||
/* no pun intented */
|
||||
module.exports = class RestRoom {
|
||||
constructor(session, roomId, log) {
|
||||
this.session = session;
|
||||
this._roomId = roomId;
|
||||
this.log = log;
|
||||
}
|
||||
|
||||
async talk(message) {
|
||||
this.log.step(`says "${message}" in ${this._roomId}`)
|
||||
const txId = uuidv4();
|
||||
await this.session._put(`/rooms/${this._roomId}/send/m.room.message/${txId}`, {
|
||||
"msgtype": "m.text",
|
||||
"body": message
|
||||
});
|
||||
this.log.done();
|
||||
return txId;
|
||||
}
|
||||
|
||||
async leave() {
|
||||
this.log.step(`leaves ${this._roomId}`)
|
||||
await this.session._post(`/rooms/${this._roomId}/leave`);
|
||||
this.log.done();
|
||||
}
|
||||
|
||||
roomId() {
|
||||
return this._roomId;
|
||||
}
|
||||
}
|
127
test/end-to-end-tests/src/rest/session.js
Normal file
127
test/end-to-end-tests/src/rest/session.js
Normal file
|
@ -0,0 +1,127 @@
|
|||
/*
|
||||
Copyright 2018 New Vector Ltd
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
const request = require('request-promise-native');
|
||||
const Logger = require('../logger');
|
||||
const RestRoom = require('./room');
|
||||
const {approveConsent} = require('./consent');
|
||||
|
||||
module.exports = class RestSession {
|
||||
constructor(credentials) {
|
||||
this.log = new Logger(credentials.userId);
|
||||
this._credentials = credentials;
|
||||
this._displayName = null;
|
||||
this._rooms = {};
|
||||
}
|
||||
|
||||
userId() {
|
||||
return this._credentials.userId;
|
||||
}
|
||||
|
||||
userName() {
|
||||
return this._credentials.userId.split(":")[0].substr(1);
|
||||
}
|
||||
|
||||
displayName() {
|
||||
return this._displayName;
|
||||
}
|
||||
|
||||
async setDisplayName(displayName) {
|
||||
this.log.step(`sets their display name to ${displayName}`);
|
||||
this._displayName = displayName;
|
||||
await this._put(`/profile/${this._credentials.userId}/displayname`, {
|
||||
displayname: displayName
|
||||
});
|
||||
this.log.done();
|
||||
}
|
||||
|
||||
async join(roomIdOrAlias) {
|
||||
this.log.step(`joins ${roomIdOrAlias}`);
|
||||
const {room_id} = await this._post(`/join/${encodeURIComponent(roomIdOrAlias)}`);
|
||||
this.log.done();
|
||||
const room = new RestRoom(this, room_id, this.log);
|
||||
this._rooms[room_id] = room;
|
||||
this._rooms[roomIdOrAlias] = room;
|
||||
return room;
|
||||
}
|
||||
|
||||
room(roomIdOrAlias) {
|
||||
if (this._rooms.hasOwnProperty(roomIdOrAlias)) {
|
||||
return this._rooms[roomIdOrAlias];
|
||||
} else {
|
||||
throw new Error(`${this._credentials.userId} is not in ${roomIdOrAlias}`);
|
||||
}
|
||||
}
|
||||
|
||||
async createRoom(name, options) {
|
||||
this.log.step(`creates room ${name}`);
|
||||
const body = {
|
||||
name,
|
||||
};
|
||||
if (options.invite) {
|
||||
body.invite = options.invite;
|
||||
}
|
||||
if (options.public) {
|
||||
body.visibility = "public";
|
||||
} else {
|
||||
body.visibility = "private";
|
||||
}
|
||||
if (options.dm) {
|
||||
body.is_direct = true;
|
||||
}
|
||||
if (options.topic) {
|
||||
body.topic = options.topic;
|
||||
}
|
||||
|
||||
const {room_id} = await this._post(`/createRoom`, body);
|
||||
this.log.done();
|
||||
return new RestRoom(this, room_id, this.log);
|
||||
}
|
||||
|
||||
_post(csApiPath, body) {
|
||||
return this._request("POST", csApiPath, body);
|
||||
}
|
||||
|
||||
_put(csApiPath, body) {
|
||||
return this._request("PUT", csApiPath, body);
|
||||
}
|
||||
|
||||
async _request(method, csApiPath, body) {
|
||||
try {
|
||||
const responseBody = await request({
|
||||
url: `${this._credentials.hsUrl}/_matrix/client/r0${csApiPath}`,
|
||||
method,
|
||||
headers: {
|
||||
"Authorization": `Bearer ${this._credentials.accessToken}`
|
||||
},
|
||||
json: true,
|
||||
body
|
||||
});
|
||||
return responseBody;
|
||||
|
||||
} catch(err) {
|
||||
const responseBody = err.response.body;
|
||||
if (responseBody.errcode === 'M_CONSENT_NOT_GIVEN') {
|
||||
await approveConsent(responseBody.consent_uri);
|
||||
return this._request(method, csApiPath, body);
|
||||
} else if(responseBody && responseBody.error) {
|
||||
throw new Error(`${method} ${csApiPath}: ${responseBody.error}`);
|
||||
} else {
|
||||
throw new Error(`${method} ${csApiPath}: ${err.response.statusCode}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue