From b612ec67859153e323772d2efce460cf3067c469 Mon Sep 17 00:00:00 2001 From: kizitorashiro Date: Mon, 6 Jan 2025 09:13:40 +0900 Subject: [PATCH] middleware: Fix Incomming/OutgoingContext domain --------- Co-authored-by: Shizuku Takeichi Co-authored-by: Sonny Piers --- packages/middleware/lib/IncomingContext.js | 5 ++-- packages/middleware/lib/OutgoingContext.js | 5 ++-- packages/middleware/test/IncomingContext.js | 26 +++++++++++------- packages/middleware/test/OutgoingContext.js | 30 ++++++++++++++------- 4 files changed, 43 insertions(+), 23 deletions(-) diff --git a/packages/middleware/lib/IncomingContext.js b/packages/middleware/lib/IncomingContext.js index 33c08001..99f4fa30 100644 --- a/packages/middleware/lib/IncomingContext.js +++ b/packages/middleware/lib/IncomingContext.js @@ -5,9 +5,10 @@ export default class IncomingContext extends Context { constructor(entity, stanza) { super(entity, stanza); - const { jid, domain } = entity; + const { jid } = entity; + const { domain } = entity.options ?? {}; - const to = stanza.attrs.to || (jid && jid.toString()); + const to = stanza.attrs.to || jid?.toString(); const from = stanza.attrs.from || domain; if (to) this.to = new JID(to); diff --git a/packages/middleware/lib/OutgoingContext.js b/packages/middleware/lib/OutgoingContext.js index 88f281ac..3bbb201a 100644 --- a/packages/middleware/lib/OutgoingContext.js +++ b/packages/middleware/lib/OutgoingContext.js @@ -5,9 +5,10 @@ export default class OutgoingContext extends Context { constructor(entity, stanza) { super(entity, stanza); - const { jid, domain } = entity; + const { jid } = entity; + const { domain } = entity.options ?? {}; - const from = stanza.attrs.from || (jid && jid.toString()); + const from = stanza.attrs.from || jid?.toString(); const to = stanza.attrs.to || domain; if (from) this.from = new JID(from); diff --git a/packages/middleware/test/IncomingContext.js b/packages/middleware/test/IncomingContext.js index 96c607b7..9f8a44d1 100644 --- a/packages/middleware/test/IncomingContext.js +++ b/packages/middleware/test/IncomingContext.js @@ -3,37 +3,43 @@ import { JID } from "@xmpp/test"; import _Context from "../lib/Context.js"; test("is instance of Context", () => { - const entity = { jid: new JID("foo@bar"), domain: "bar" }; + const entity = { jid: new JID("foo@bar"), options: { domain: "bar" } }; const ctx = new Context(entity, { attrs: {} }); expect(ctx instanceof _Context).toBe(true); }); test("sets the from property", () => { - const entity = { jid: new JID("foo@bar"), domain: "bar" }; + const entity = { jid: new JID("foo@bar"), options: { domain: "bar" } }; const ctx = new Context(entity, { attrs: { from: "foo@bar" } }); expect(ctx.from).toEqual(new JID("foo@bar")); }); -test("from property default to entity jid domain", () => { - const entity = { jid: new JID("foo@bar"), domain: "bar" }; +test("from property defaults to incoming stanza from attribute", () => { + const entity = { jid: new JID("foo@bar"), options: { domain: "bar" } }; + const ctx = new Context(entity, { attrs: { from: "foo" } }); + expect(ctx.from).toEqual(new JID("foo")); +}); + +test("from property falls back to entity jid domain", () => { + const entity = { jid: new JID("foo@bar"), options: { domain: "bar" } }; const ctx = new Context(entity, { attrs: {} }); expect(ctx.from).toEqual(new JID("bar")); }); test("sets the to property", () => { - const entity = { jid: new JID("foo@bar"), domain: "bar" }; + const entity = { jid: new JID("foo@bar"), options: { domain: "bar" } }; const ctx = new Context(entity, { attrs: { to: "foo@bar" } }); expect(ctx.to).toEqual(new JID("foo@bar")); }); -test("to property default to entity jid", () => { - const entity = { jid: new JID("foo@bar"), domain: "bar" }; - const ctx = new Context(entity, { attrs: {} }); - expect(ctx.to).toEqual(new JID("foo@bar")); +test("to property defaults to incoming stanza to attribute", () => { + const entity = { jid: new JID("foo@bar"), options: { domain: "bar" } }; + const ctx = new Context(entity, { attrs: { to: "hello" } }); + expect(ctx.to).toEqual(new JID("hello")); }); test("sets the local property to from.local", () => { - const entity = { jid: new JID("foo@bar"), domain: "bar" }; + const entity = { jid: new JID("foo@bar"), options: { domain: "bar" } }; const ctx = new Context(entity, { attrs: { from: "foo@bar" } }); expect(ctx.local).toEqual("foo"); }); diff --git a/packages/middleware/test/OutgoingContext.js b/packages/middleware/test/OutgoingContext.js index 6191ed9e..fdddf4a9 100644 --- a/packages/middleware/test/OutgoingContext.js +++ b/packages/middleware/test/OutgoingContext.js @@ -3,37 +3,49 @@ import { JID } from "@xmpp/test"; import _Context from "../lib/Context.js"; test("is instance of Context", () => { - const entity = { jid: new JID("foo@bar"), domain: "bar" }; + const entity = { jid: new JID("foo@bar"), options: { domain: "bar" } }; const ctx = new Context(entity, { attrs: {} }); expect(ctx instanceof _Context).toBe(true); }); test("sets the from property", () => { - const entity = { jid: new JID("foo@bar"), domain: "bar" }; + const entity = { jid: new JID("foo@bar"), options: { domain: "bar" } }; const ctx = new Context(entity, { attrs: { from: "foo@bar" } }); expect(ctx.from).toEqual(new JID("foo@bar")); }); -test("from property default to entity jid", () => { - const entity = { jid: new JID("foo@bar"), domain: "bar" }; +test("from property default to stanza from attribute", () => { + const entity = { jid: new JID("foo@bar"), options: { domain: "bar" } }; + const ctx = new Context(entity, { attrs: { from: "foo" } }); + expect(ctx.from).toEqual(new JID("foo")); +}); + +test("from property falls back to entity jid", () => { + const entity = { jid: new JID("foo@bar"), options: { domain: "bar" } }; const ctx = new Context(entity, { attrs: {} }); expect(ctx.from).toEqual(new JID("foo@bar")); }); test("sets the to property", () => { - const entity = { jid: new JID("foo@bar"), domain: "bar" }; + const entity = { jid: new JID("foo@bar"), options: { domain: "bar" } }; const ctx = new Context(entity, { attrs: { to: "foo@bar" } }); expect(ctx.to).toEqual(new JID("foo@bar")); }); -test("to property default to entity jid domain", () => { - const entity = { jid: new JID("foo@bar"), domain: "bar" }; +test("to property default to stanza to attribute", () => { + const entity = { jid: new JID("foo@bar"), options: { domain: "bar" } }; + const ctx = new Context(entity, { attrs: { to: "baz" } }); + expect(ctx.to).toEqual(new JID("baz")); +}); + +test("to property falls back to entity domain", () => { + const entity = { jid: new JID("foo@bar"), options: { domain: "baz" } }; const ctx = new Context(entity, { attrs: {} }); - expect(ctx.to).toEqual(new JID("bar")); + expect(ctx.to).toEqual(new JID("baz")); }); test("sets the local property to to.local", () => { - const entity = { jid: new JID("foo@bar"), domain: "bar" }; + const entity = { jid: new JID("foo@bar"), options: { domain: "bar" } }; const ctx = new Context(entity, { attrs: { to: "foo@bar" } }); expect(ctx.local).toEqual("foo"); });