Skip to content

Commit

Permalink
test: refactor sendTransaction tests to use context builder
Browse files Browse the repository at this point in the history
  • Loading branch information
ggazzo committed Dec 18, 2024
1 parent e188b76 commit d162d49
Showing 1 changed file with 82 additions and 169 deletions.
251 changes: 82 additions & 169 deletions packages/homeserver/src/routes/federation/sendTransaction.spec.ts
Original file line number Diff line number Diff line change
@@ -1,121 +1,82 @@
import { beforeAll, describe, expect, it } from "bun:test";
import Elysia from "elysia";
import type Elysia from "elysia";
import { type SigningKey, generateKeyPairsFromString } from "../../keys";
import { toUnpaddedBase64 } from "../../binaryData";
import { sendTransactionRoute } from "./sendTransaction";
import { signJson } from "../../signJson";
import { signEvent } from "../../signEvent";
import { authorizationHeaders, generateId } from "../../authentication";
import { type ContextBuilder, hs1, rc1 } from "../../fixtures/ContextBuilder";
import { pruneEventDict } from "../../pruneEventDict";

describe("/send/:txnId", () => {
describe("PDU validation", () => {
let app: Elysia<any, any, any, any, any, any>;
let signature: SigningKey;

let hs1Content: Awaited<ReturnType<ContextBuilder["build"]>>;
let rc1Content: Awaited<ReturnType<ContextBuilder["build"]>>;

beforeAll(async () => {
signature = await generateKeyPairsFromString(
"ed25519 a_yNbw tBD7FfjyBHgT4TwhwzvyS9Dq2Z9ck38RRQKaZ6Sz2z8",
hs1Content = await hs1.build();
rc1.withLocalSigningKey("hs1", hs1Content.signature);
rc1Content = await rc1.build();
hs1.withLocalSigningKey("rc1", rc1Content.signature);
hs1Content = await hs1.build();

signature = rc1Content.signature;
app = rc1Content.app.group("/_matrix/federation/v1", (app) =>
app.use(sendTransactionRoute),
);

app = new Elysia()
.decorate("config", {
path: "./config.json",
signingKeyPath: "./keys/ed25519.signing.key",
port: 8080,
signingKey: [
await generateKeyPairsFromString(
"ed25519 a_XRhW YjbSyfqQeGto+OFswt+XwtJUUooHXH5w+czSgawN63U",
),
],
name: "synapse2",
version: "org.matrix.msc3757.10",
})
.decorate("mongo", {
getValidPublicKeyFromLocal: async () => {
return toUnpaddedBase64(signature.publicKey);
},
storePublicKey: async () => {
return;
},
eventsCollection: {
findOne: async () => {
return;
},
findOneAndUpdate: async () => {
return;
},
insertMany: async () => {
return;
},
},
createStagingEvent: async () => {
return;
},
getEventsByIds: async () => {
return [];
},
createEvent: async () => {
return;
},
getOldestStagedEvent: async () => {
return;
},
serversCollection: {
findOne: async () => {
return;
},
} as any,
})
.use(sendTransactionRoute);
});

it("Should reject if there is more than 100 edus", async () => {
const transactionid = "asd";

const resp = await app.handle(
new Request("https://localhost/send/txnId", {
headers: {
authorization: "Bearer invalid",
"content-type": "application/json",
},
method: "PUT",
body: JSON.stringify({
edus: Array.from({ length: 101 }, (_, i) => ({
content: {
membership: "join",
avatar_url: null,
displayname: "rodrigo2",
},
origin: "synapse2",
origin_server_ts: 1664987618773,
sender: "@rodrigo2:synapse2",
unsigned: {
age: 2,
},
})),
}),
await hs1Content.makeRequest<
"PUT",
`/_matrix/federation/v1/send/${string}`
>("PUT", `/_matrix/federation/v1/send/${transactionid}`, {
edus: Array.from({ length: 101 }, (_, i) => ({
content: {
membership: "join",
avatar_url: null,
displayname: "rodrigo2",
},
origin: "hs1",
origin_server_ts: 1664987618773,
sender: "@rodrigo2:hs1",
unsigned: {
age: 2,
},
})),
}),
);

const data = await resp.json();
expect(resp.status).toBe(400);
});

it("Should pass if there a proper pdu is provided", async () => {
const signature = await generateKeyPairsFromString(
"ed25519 a_yNbw tBD7FfjyBHgT4TwhwzvyS9Dq2Z9ck38RRQKaZ6Sz2z8",
rc1Content = await rc1.build();
app = rc1Content.app.group("/_matrix/federation/v1", (app) =>
app.use(sendTransactionRoute),
);

const transactionid = "asd";

const pdu = {
event_id: "1664987618773:synapse2",
room_id: "!room:synapse2",
event_id: "1664987618773:hs1",
room_id: "!room:hs1",
type: "m.room.member",
content: {
membership: "join",
avatar_url: null,
displayname: "rodrigo2",
},
origin: "synapse2",
origin: "hs1",
origin_server_ts: 1664987618773,
sender: "@rodrigo2:synapse2",
sender: "@rodrigo2:hs1",
unsigned: {
age: 2,
},
Expand All @@ -128,18 +89,14 @@ describe("/send/:txnId", () => {
depth: 12,
};

const signedPdu = await signEvent(pdu, signature, "synapse2");
const signedPdu = await signEvent(pdu, hs1Content.signature, "hs1");

const resp = await app.handle(
new Request("https://localhost/send/txnId", {
headers: {
authorization: "Bearer invalid",
"content-type": "application/json",
},
method: "PUT",
body: JSON.stringify({
pdus: [signedPdu],
}),
await hs1Content.makeRequest<
"PUT",
`/_matrix/federation/v1/send/${string}`
>("PUT", `/_matrix/federation/v1/send/${transactionid}`, {
pdus: [signedPdu],
}),
);

Expand All @@ -156,41 +113,45 @@ describe("/send/:txnId", () => {
});

it("Should reject if the pdu is invalid", async () => {
const signature = await generateKeyPairsFromString(
"ed25519 a_yNbw tBD7FfjyBHgT4TwhwzvyS9Dq2Z9ck38RRQKaZ6Sz2z8",
);

const pdu = {
event_id: "1664987618773:synapse2",
room_id: "!room:synapse2",
room_id: "!room:hs1",
type: "m.room.member",
content: {
membership: "join",
avatar_url: null,
displayname: "rodrigo2",
},
origin: "synapse2",
auth_events: [
"$A1NdD_Lf1IvcHeg0-pkApLWpKbputIaZ_Z4yIHK5YDg",
"$dOOm8jYy4ioI77w2AbySU1NavHhU7US4Lukm76aOf5w",
"$BLMgX0J7Gd4JZZzTsprQjJWtEfGlccgPUYC7XQyg2ds",
],
prev_events: ["$js6Vn-9W65pkvfigwsod3xqyvA7pRqDOKOcCJ69AxVs"],
origin: "hs1",
origin_server_ts: 1664987618773,
sender: "@rodrigo2:synapse2",
sender: "@rodrigo2:hs1",
unsigned: {
age: 2,
},
depth: 12,
};
const transactionid = "asd";

const signedPdu = await signJson(pdu, signature, "synapse2");
const signedPdu = await signJson(
pruneEventDict(pdu),
hs1Content.signature,
"hs1",
);

signedPdu.content.membership = "invalid";
signedPdu.content!.membership = "invalid";

const resp = await app.handle(
new Request("https://localhost/send/txnId", {
headers: {
authorization: "Bearer invalid",
"content-type": "application/json",
},
method: "PUT",
body: JSON.stringify({
pdus: [signedPdu],
}),
await hs1Content.makeRequest<
"PUT",
`/_matrix/federation/v1/send/${string}`
>("PUT", `/_matrix/federation/v1/send/${transactionid}`, {
pdus: [signedPdu],
edus: [],
}),
);

Expand All @@ -212,70 +173,22 @@ describe("/send/:txnId", () => {
describe("/send/:txnId using real case", () => {
describe("PDU validation", () => {
let app: Elysia<any, any, any, any, any, any>;

let hs1Content: Awaited<ReturnType<ContextBuilder["build"]>>;
let signature: SigningKey;

beforeAll(async () => {
signature = await generateKeyPairsFromString(
"ed25519 a_HDhg WntaJ4JP5WbZZjDShjeuwqCybQ5huaZAiowji7tnIEw",
);
hs1Content = await hs1.build();
rc1.withLocalSigningKey("hs1", hs1Content.signature);
const rc1Content = await rc1.build();

app = new Elysia()
.decorate("config", {
path: "./config.json",
signingKeyPath: "./keys/ed25519.signing.key",
port: 8080,
signingKey: [
await generateKeyPairsFromString(
"ed25519 a_XRhW YjbSyfqQeGto+OFswt+XwtJUUooHXH5w+czSgawN63U",
),
],
name: "synapse2",
version: "org.matrix.msc3757.10",
})
.decorate("mongo", {
getValidPublicKeyFromLocal: async () => {
return toUnpaddedBase64(signature.publicKey);
},
storePublicKey: async () => {
return;
},
eventsCollection: {
findOne: async () => {
return;
},
findOneAndUpdate: async () => {
return;
},
insertMany: async () => {
return;
},
},
createStagingEvent: async () => {
return;
},
getEventsByIds: async () => {
return [];
},
createEvent: async () => {
return;
},
getOldestStagedEvent: async () => {
return;
},
serversCollection: {
findOne: async () => {
return;
},
} as any,
})
.use(sendTransactionRoute);
signature = rc1Content.signature;
app = rc1Content.app.group("/_matrix/federation/v1", (app) =>
app.use(sendTransactionRoute),
);
});

it("real case", async () => {
const signature = await generateKeyPairsFromString(
"ed25519 a_HDhg tBD7FfjyBHgT4TwhwzvyS9Dq2Z9ck38RRQKaZ6Sz2z8",
);

const request = {
origin: "hs1",
origin_server_ts: 1734360416888,
Expand Down Expand Up @@ -316,7 +229,7 @@ describe("/send/:txnId using real case", () => {
],
};
const resp = await app.handle(
new Request("https://localhost/send/txnId", {
new Request("https://localhost/_matrix/federation/v1/send/txnId", {
headers: {
authorization: "Bearer invalid",
"content-type": "application/json",
Expand Down

0 comments on commit d162d49

Please sign in to comment.