Fix failing test

This commit is contained in:
JC Brand 2021-09-09 11:11:43 +02:00
parent b78c19f6f2
commit ef2c206507
5 changed files with 21 additions and 19 deletions

View File

@ -23,7 +23,7 @@ import {
isHeadline,
isServerMessage,
isValidReceiptRequest,
rejectUnencapsulatedForward,
throwErrorIfInvalidForward,
} from '@converse/headless/shared/parsers';
const { Strophe, sizzle } = converse.env;
@ -37,10 +37,7 @@ const { Strophe, sizzle } = converse.env;
* @returns { (MessageAttributes|Error) }
*/
export async function parseMessage (stanza, _converse) {
const err = rejectUnencapsulatedForward(stanza);
if (err) {
return err;
}
throwErrorIfInvalidForward(stanza);
let to_jid = stanza.getAttribute('to');
const to_resource = Strophe.getResourceFromJid(to_jid);

View File

@ -554,13 +554,17 @@ const ChatRoomMixin = {
}
/**
* @typedef { Object } MUCMessageData
* An object containing the original groupchat message stanza,
* as well as the parsed attributes.
* @property { XMLElement } stanza
* An object containing the parsed { @link MUCMessageAttributes } and
* current { @link ChatRoom }.
* @property { MUCMessageAttributes } attrs
* @property { ChatRoom } chatbox
*/
const attrs = await parseMUCMessage(stanza, this, _converse);
let attrs;
try {
attrs = await parseMUCMessage(stanza, this, _converse);
} catch (e) {
return log.error(e.message);
}
const data = { stanza, attrs, 'chatbox': this };
/**
* Triggered when a groupchat message stanza has been received and parsed.

View File

@ -18,7 +18,7 @@ import {
isCarbon,
isHeadline,
isValidReceiptRequest,
rejectUnencapsulatedForward,
throwErrorIfInvalidForward,
} from '@converse/headless/shared/parsers';
import { api, converse } from '@converse/headless/core';
@ -103,10 +103,7 @@ function getModerationAttributes (stanza) {
* @returns { Promise<MUCMessageAttributes|Error> }
*/
export async function parseMUCMessage (stanza, chatbox, _converse) {
const err = rejectUnencapsulatedForward(stanza);
if (err) {
return err;
}
throwErrorIfInvalidForward(stanza);
const selector = `[xmlns="${NS.MAM}"] > forwarded[xmlns="${NS.FORWARD}"] > message`;
const original_stanza = stanza;

View File

@ -307,12 +307,17 @@ export function isValidReceiptRequest (stanza, attrs) {
);
}
export function rejectUnencapsulatedForward (stanza) {
/**
* Check whether the passed-in stanza is a forwarded message that is "bare",
* i.e. it's not forwarded as part of a larger protocol, like MAM.
* @param { XMLElement } stanza
*/
export function throwErrorIfInvalidForward (stanza) {
const bare_forward = sizzle(`message > forwarded[xmlns="${Strophe.NS.FORWARD}"]`, stanza).length;
if (bare_forward) {
rejectMessage(stanza, 'Forwarded messages not part of an encapsulating protocol are not supported');
const from_jid = stanza.getAttribute('from');
return new StanzaParseError(`Ignoring unencapsulated forwarded message from ${from_jid}`, stanza);
throw new StanzaParseError(`Ignoring unencapsulated forwarded message from ${from_jid}`, stanza);
}
}

View File

@ -143,10 +143,9 @@ describe("A Groupchat Message", function () {
</message>
`);
const view = _converse.chatboxviews.get(muc_jid);
spyOn(view.model, 'onMessage').and.callThrough();
spyOn(converse.env.log, 'error');
spyOn(converse.env.log, 'error').and.callThrough();
_converse.connection._dataRecv(mock.createRequest(received_stanza));
await u.waitUntil(() => view.model.onMessage.calls.count() === 1);
await u.waitUntil(() => converse.env.log.error.calls.count() === 1);
expect(converse.env.log.error).toHaveBeenCalledWith(
`Ignoring unencapsulated forwarded message from ${muc_jid}/mallory`
);