add logging to rest session actions

This commit is contained in:
Bruno Windels 2018-09-14 12:17:22 +02:00
parent af255c6386
commit 6deb595fec
5 changed files with 58 additions and 16 deletions

View file

@ -27,10 +27,10 @@ module.exports = class RestSessionCreator {
this.cwd = cwd;
}
async createSessionRange(usernames, password) {
async createSessionRange(usernames, password, groupName) {
const sessionPromises = usernames.map((username) => this.createSession(username, password));
const sessions = await Promise.all(sessionPromises);
return new RestMultiSession(sessions);
return new RestMultiSession(sessions, groupName);
}
async createSession(username, password) {

View file

@ -17,14 +17,16 @@ 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) {
constructor(sessions, groupName) {
this.log = new Logger(groupName);
this.sessions = sessions;
}
slice(start, end) {
return new RestMultiSession(this.sessions.slice(start, end));
slice(start, end, groupName) {
return new RestMultiSession(this.sessions.slice(start, end), groupName);
}
pop(userName) {
@ -37,25 +39,52 @@ module.exports = class RestMultiSession {
}
async setDisplayName(fn) {
await Promise.all(this.sessions.map((s) => s.setDisplayName(fn(s))));
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(roomId) {
const rooms = await Promise.all(this.sessions.map((s) => s.join(roomId)));
return new RestMultiRoom(rooms);
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);
}
}
class RestMultiRoom {
constructor(rooms) {
constructor(rooms, roomIdOrAlias, log) {
this.rooms = rooms;
this.roomIdOrAlias = roomIdOrAlias;
this.log = log;
}
async talk(message) {
await Promise.all(this.rooms.map((r) => r.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() {
await Promise.all(this.rooms.map((r) => r.leave()));
this.log.step(`leave ${this.roomIdOrAlias}`)
await Promise.all(this.rooms.map(async (r) => {
r.log.mute();
await r.leave(message);
r.log.unmute();
}));
this.log.done();
}
}

View file

@ -18,22 +18,27 @@ const uuidv4 = require('uuid/v4');
/* no pun intented */
module.exports = class RestRoom {
constructor(session, roomId) {
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() {

View file

@ -15,11 +15,13 @@ 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;
}
@ -37,18 +39,23 @@ module.exports = class RestSession {
}
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)}`);
return new RestRoom(this, room_id);
this.log.done();
return new RestRoom(this, room_id, this.log);
}
async createRoom(name, options) {
this.log.step(`creates room ${name}`);
const body = {
name,
};
@ -68,7 +75,8 @@ module.exports = class RestSession {
}
const {room_id} = await this._post(`/createRoom`, body);
return new RestRoom(this, room_id);
this.log.done();
return new RestRoom(this, room_id, this.log);
}
_post(csApiPath, body) {

View file

@ -41,7 +41,7 @@ module.exports = async function scenario(createSession, restCreator) {
async function createRestUsers(restCreator) {
const usernames = range(1, 10).map((i) => `charly-${i}`);
const charlies = await restCreator.createSessionRange(usernames, 'testtest');
const charlies = await restCreator.createSessionRange(usernames, "testtest", "charly-1..10");
await charlies.setDisplayName((s) => `Charly #${s.userName().split('-')[1]}`);
return charlies;
}