2016-03-28 21:59:34 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var peg = require('../src/MatrixClientPeg.js');
|
|
|
|
var jssdk = require('matrix-js-sdk');
|
2016-03-30 23:48:46 +00:00
|
|
|
var MatrixEvent = jssdk.MatrixEvent;
|
2016-03-28 21:59:34 +00:00
|
|
|
var sinon = require('sinon');
|
|
|
|
|
2016-04-02 17:09:44 +00:00
|
|
|
/**
|
|
|
|
* Perform common actions before each test case, e.g. printing the test case
|
|
|
|
* name to stdout.
|
|
|
|
* @param {Mocha.Context} context The test context
|
|
|
|
*/
|
|
|
|
module.exports.beforeEach = function(context) {
|
|
|
|
var desc = context.currentTest.fullTitle();
|
|
|
|
console.log();
|
|
|
|
console.log(desc);
|
|
|
|
console.log(new Array(1 + desc.length).join("="));
|
|
|
|
};
|
|
|
|
|
2016-03-30 23:48:46 +00:00
|
|
|
|
2016-03-28 21:59:34 +00:00
|
|
|
/**
|
|
|
|
* Stub out the MatrixClient, and configure the MatrixClientPeg object to
|
|
|
|
* return it when get() is called.
|
2016-04-07 15:47:17 +00:00
|
|
|
*
|
|
|
|
* @returns {sinon.Sandbox}; remember to call sandbox.restore afterwards.
|
2016-03-28 21:59:34 +00:00
|
|
|
*/
|
|
|
|
module.exports.stubClient = function() {
|
2016-04-07 15:47:17 +00:00
|
|
|
var sandbox = sinon.sandbox.create();
|
|
|
|
|
|
|
|
// 'sandbox.restore()' doesn't work correctly on inherited methods,
|
|
|
|
// so we do this for each method
|
|
|
|
var methods = ['get', 'unset', 'replaceUsingUrls',
|
|
|
|
'replaceUsingAccessToken'];
|
|
|
|
for (var i = 0; i < methods.length; i++) {
|
|
|
|
sandbox.stub(peg, methods[i]);
|
|
|
|
}
|
2016-03-28 21:59:34 +00:00
|
|
|
|
|
|
|
var matrixClientStub = sinon.createStubInstance(jssdk.MatrixClient);
|
2016-04-07 15:47:17 +00:00
|
|
|
peg.get.returns(matrixClientStub);
|
|
|
|
return sandbox;
|
2016-03-28 21:59:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-30 23:48:46 +00:00
|
|
|
/**
|
|
|
|
* Create an Event.
|
|
|
|
* @param {Object} opts Values for the event.
|
|
|
|
* @param {string} opts.type The event.type
|
|
|
|
* @param {string} opts.room The event.room_id
|
|
|
|
* @param {string} opts.user The event.user_id
|
|
|
|
* @param {string} opts.skey Optional. The state key (auto inserts empty string)
|
|
|
|
* @param {Number} opts.ts Optional. Timestamp for the event
|
|
|
|
* @param {Object} opts.content The event.content
|
|
|
|
* @param {boolean} opts.event True to make a MatrixEvent.
|
|
|
|
* @return {Object} a JSON object representing this event.
|
|
|
|
*/
|
|
|
|
module.exports.mkEvent = function(opts) {
|
|
|
|
if (!opts.type || !opts.content) {
|
|
|
|
throw new Error("Missing .type or .content =>" + JSON.stringify(opts));
|
|
|
|
}
|
|
|
|
var event = {
|
|
|
|
type: opts.type,
|
|
|
|
room_id: opts.room,
|
|
|
|
sender: opts.user,
|
|
|
|
content: opts.content,
|
|
|
|
event_id: "$" + Math.random() + "-" + Math.random(),
|
|
|
|
origin_server_ts: opts.ts,
|
|
|
|
};
|
|
|
|
if (opts.skey) {
|
|
|
|
event.state_key = opts.skey;
|
|
|
|
}
|
|
|
|
else if (["m.room.name", "m.room.topic", "m.room.create", "m.room.join_rules",
|
|
|
|
"m.room.power_levels", "m.room.topic",
|
|
|
|
"com.example.state"].indexOf(opts.type) !== -1) {
|
|
|
|
event.state_key = "";
|
|
|
|
}
|
|
|
|
return opts.event ? new MatrixEvent(event) : event;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an m.presence event.
|
|
|
|
* @param {Object} opts Values for the presence.
|
|
|
|
* @return {Object|MatrixEvent} The event
|
|
|
|
*/
|
|
|
|
module.exports.mkPresence = function(opts) {
|
|
|
|
if (!opts.user) {
|
|
|
|
throw new Error("Missing user");
|
|
|
|
}
|
|
|
|
var event = {
|
|
|
|
event_id: "$" + Math.random() + "-" + Math.random(),
|
|
|
|
type: "m.presence",
|
|
|
|
sender: opts.user,
|
|
|
|
content: {
|
|
|
|
avatar_url: opts.url,
|
|
|
|
displayname: opts.name,
|
|
|
|
last_active_ago: opts.ago,
|
|
|
|
presence: opts.presence || "offline"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return opts.event ? new MatrixEvent(event) : event;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an m.room.member event.
|
|
|
|
* @param {Object} opts Values for the membership.
|
|
|
|
* @param {string} opts.room The room ID for the event.
|
|
|
|
* @param {string} opts.mship The content.membership for the event.
|
|
|
|
* @param {string} opts.user The user ID for the event.
|
|
|
|
* @param {string} opts.skey The other user ID for the event if applicable
|
|
|
|
* e.g. for invites/bans.
|
|
|
|
* @param {string} opts.name The content.displayname for the event.
|
|
|
|
* @param {string} opts.url The content.avatar_url for the event.
|
|
|
|
* @param {boolean} opts.event True to make a MatrixEvent.
|
|
|
|
* @return {Object|MatrixEvent} The event
|
|
|
|
*/
|
|
|
|
module.exports.mkMembership = function(opts) {
|
|
|
|
opts.type = "m.room.member";
|
|
|
|
if (!opts.skey) {
|
|
|
|
opts.skey = opts.user;
|
|
|
|
}
|
|
|
|
if (!opts.mship) {
|
|
|
|
throw new Error("Missing .mship => " + JSON.stringify(opts));
|
|
|
|
}
|
|
|
|
opts.content = {
|
|
|
|
membership: opts.mship
|
|
|
|
};
|
|
|
|
if (opts.name) { opts.content.displayname = opts.name; }
|
|
|
|
if (opts.url) { opts.content.avatar_url = opts.url; }
|
|
|
|
return module.exports.mkEvent(opts);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an m.room.message event.
|
|
|
|
* @param {Object} opts Values for the message
|
|
|
|
* @param {string} opts.room The room ID for the event.
|
|
|
|
* @param {string} opts.user The user ID for the event.
|
|
|
|
* @param {string} opts.msg Optional. The content.body for the event.
|
|
|
|
* @param {boolean} opts.event True to make a MatrixEvent.
|
|
|
|
* @return {Object|MatrixEvent} The event
|
|
|
|
*/
|
|
|
|
module.exports.mkMessage = function(opts) {
|
|
|
|
opts.type = "m.room.message";
|
|
|
|
if (!opts.msg) {
|
|
|
|
opts.msg = "Random->" + Math.random();
|
|
|
|
}
|
|
|
|
if (!opts.room || !opts.user) {
|
|
|
|
throw new Error("Missing .room or .user from", opts);
|
|
|
|
}
|
|
|
|
opts.content = {
|
|
|
|
msgtype: "m.text",
|
|
|
|
body: opts.msg
|
|
|
|
};
|
|
|
|
return module.exports.mkEvent(opts);
|
|
|
|
};
|