Test for Update support

This commit is contained in:
Jesse Wierzbinski 2023-09-13 20:12:54 -10:00
parent d92764d81a
commit aad3ee78d1
No known key found for this signature in database
GPG key ID: F9A1E418934E40B0
2 changed files with 79 additions and 5 deletions

View file

@ -78,17 +78,26 @@ export default async (
// Replace the object in database with the new provided object
// TODO: Add authentication
const object = await RawObject.findOneBy({
data: {
id: (body.object as RawObject).id,
},
});
const object = await RawObject.createQueryBuilder("object")
.where("object.data->>'id' = :id", {
id: (body.object as APObject).id,
})
.getOne();
if (!object) return errorResponse("Object not found", 404);
object.data = body.object as APObject;
// Store the Update event in database
const activity = new RawActivity();
activity.data = {
...body,
object: undefined,
};
activity.objects = [object];
await object.save();
await activity.save();
break;
}
case "Delete" as APDelete: {

View file

@ -85,6 +85,71 @@ describe("POST /@test/inbox", () => {
published: "2021-01-01T00:00:00.000Z",
});
});
test("should try to update that Note object", async () => {
const response = await fetch(
`${config.http.base_url}:${config.http.port}/@test/inbox/`,
{
method: "POST",
headers: {
"Content-Type": "application/activity+json",
},
body: JSON.stringify({
"@context": "https://www.w3.org/ns/activitystreams",
type: "Update",
id: "https://example.com/notes/1/activity",
actor: `${config.http.base_url}:${config.http.port}/@test`,
to: ["https://www.w3.org/ns/activitystreams#Public"],
cc: [],
published: "2021-01-02T00:00:00.000Z",
object: {
"@context": "https://www.w3.org/ns/activitystreams",
id: "https://example.com/notes/1",
type: "Note",
content: "This note has been edited!",
summary: null,
inReplyTo: null,
published: "2021-01-01T00:00:00.000Z",
},
}),
}
);
expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toBe("application/json");
const activity = await RawActivity.createQueryBuilder("activity")
// Where id is part of the jsonb column 'data'
.where("activity.data->>'id' = :id", {
id: "https://example.com/notes/1/activity",
})
.leftJoinAndSelect("activity.objects", "objects")
// Sort by most recent
.orderBy("activity.data->>'published'", "DESC")
.getOne();
expect(activity).not.toBeUndefined();
expect(activity?.data).toEqual({
"@context": "https://www.w3.org/ns/activitystreams",
type: "Update",
id: "https://example.com/notes/1/activity",
actor: `${config.http.base_url}:${config.http.port}/@test`,
to: ["https://www.w3.org/ns/activitystreams#Public"],
cc: [],
published: "2021-01-02T00:00:00.000Z",
});
expect(activity?.objects).toHaveLength(1);
expect(activity?.objects[0].data).toEqual({
"@context": "https://www.w3.org/ns/activitystreams",
id: "https://example.com/notes/1",
type: "Note",
content: "This note has been edited!",
summary: null,
inReplyTo: null,
published: "2021-01-01T00:00:00.000Z",
});
});
});
afterAll(async () => {