Skip to content

Commit

Permalink
middleware: Fix Incomming/OutgoingContext domain
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Shizuku Takeichi <shizuku@bleu.local>
Co-authored-by: Sonny Piers <sonny@fastmail.net>
  • Loading branch information
3 people authored Jan 6, 2025
1 parent 259d2cf commit b612ec6
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 23 deletions.
5 changes: 3 additions & 2 deletions packages/middleware/lib/IncomingContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions packages/middleware/lib/OutgoingContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
26 changes: 16 additions & 10 deletions packages/middleware/test/IncomingContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
Expand Down
30 changes: 21 additions & 9 deletions packages/middleware/test/OutgoingContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
Expand Down

0 comments on commit b612ec6

Please sign in to comment.