Add new API endpoint (accounts/verify_credentials)

This commit is contained in:
Jesse Wierzbinski 2023-09-21 16:15:12 -10:00
parent e25b745e25
commit ce2ed0754e
11 changed files with 234 additions and 181 deletions

View file

@ -23,15 +23,12 @@ beforeAll(async () => {
describe("POST /@test/actor", () => {
test("should return a valid ActivityPub Actor when querying an existing user", async () => {
const response = await fetch(
`${config.http.base_url}:${config.http.port}/@test/actor`,
{
method: "GET",
headers: {
Accept: "application/activity+json",
},
}
);
const response = await fetch(`${config.http.base_url}/@test/actor`, {
method: "GET",
headers: {
Accept: "application/activity+json",
},
});
expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toBe(
@ -41,26 +38,16 @@ describe("POST /@test/actor", () => {
const actor: APActor = await response.json();
expect(actor.type).toBe("Person");
expect(actor.id).toBe(
`${config.http.base_url}:${config.http.port}/@test`
);
expect(actor.id).toBe(`${config.http.base_url}/@test`);
expect(actor.preferredUsername).toBe("test");
expect(actor.inbox).toBe(
`${config.http.base_url}:${config.http.port}/@test/inbox`
);
expect(actor.outbox).toBe(
`${config.http.base_url}:${config.http.port}/@test/outbox`
);
expect(actor.followers).toBe(
`${config.http.base_url}:${config.http.port}/@test/followers`
);
expect(actor.following).toBe(
`${config.http.base_url}:${config.http.port}/@test/following`
);
expect(actor.inbox).toBe(`${config.http.base_url}/@test/inbox`);
expect(actor.outbox).toBe(`${config.http.base_url}/@test/outbox`);
expect(actor.followers).toBe(`${config.http.base_url}/@test/followers`);
expect(actor.following).toBe(`${config.http.base_url}/@test/following`);
expect((actor as any).publicKey).toBeDefined();
expect((actor as any).publicKey.id).toBeDefined();
expect((actor as any).publicKey.owner).toBe(
`${config.http.base_url}:${config.http.port}/@test`
`${config.http.base_url}/@test`
);
expect((actor as any).publicKey.publicKeyPem).toBeDefined();
expect((actor as any).publicKey.publicKeyPem).toMatch(
@ -77,7 +64,7 @@ afterAll(async () => {
const activities = await RawActivity.createQueryBuilder("activity")
.where("activity.data->>'actor' = :actor", {
actor: `${config.http.base_url}:${config.http.port}/@test`,
actor: `${config.http.base_url}/@test`,
})
.leftJoinAndSelect("activity.objects", "objects")
.getMany();

View file

@ -54,7 +54,7 @@ beforeAll(async () => {
describe("POST /api/v1/accounts/:id", () => {
test("should return a 404 error when trying to fetch a non-existent user", async () => {
const response = await fetch(
`${config.http.base_url}:${config.http.port}/api/v1/accounts/999999`,
`${config.http.base_url}/api/v1/accounts/999999`,
{
method: "GET",
headers: {
@ -72,7 +72,7 @@ describe("POST /api/v1/accounts/:id", () => {
describe("POST /api/v1/statuses", () => {
test("should create a new status and return an APIStatus object", async () => {
const response = await fetch(
`${config.http.base_url}:${config.http.port}/api/v1/statuses`,
`${config.http.base_url}/api/v1/statuses`,
{
method: "POST",
headers: {
@ -117,7 +117,7 @@ describe("POST /api/v1/statuses", () => {
describe("PATCH /api/v1/accounts/update_credentials", () => {
test("should update the authenticated user's display name", async () => {
const response = await fetch(
`${config.http.base_url}:${config.http.port}/api/v1/accounts/update_credentials`,
`${config.http.base_url}/api/v1/accounts/update_credentials`,
{
method: "PATCH",
headers: {
@ -139,6 +139,47 @@ describe("PATCH /api/v1/accounts/update_credentials", () => {
});
});
describe("GET /api/v1/accounts/verify_credentials", () => {
test("should return the authenticated user's account information", async () => {
const response = await fetch(
`${config.http.base_url}/api/v1/accounts/verify_credentials`,
{
method: "GET",
headers: {
Authorization: `Bearer ${token.access_token}`,
"Content-Type": "application/json",
},
}
);
expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toBe("application/json");
const account: APIAccount = await response.json();
expect(account.username).toBe(user.username);
expect(account.bot).toBe(false);
expect(account.locked).toBe(false);
expect(account.created_at).toBeDefined();
expect(account.followers_count).toBe(0);
expect(account.following_count).toBe(0);
expect(account.statuses_count).toBe(0);
expect(account.note).toBe("");
expect(account.url).toBe(`${config.http.base_url}/@${user.username}`);
expect(account.avatar).toBeDefined();
expect(account.avatar_static).toBeDefined();
expect(account.header).toBeDefined();
expect(account.header_static).toBeDefined();
expect(account.emojis).toEqual([]);
expect(account.fields).toEqual([]);
expect(account.source?.fields).toEqual([]);
expect(account.source?.privacy).toBe("public");
expect(account.source?.language).toBeNull();
expect(account.source?.note).toBe("");
expect(account.source?.sensitive).toBe(false);
});
});
afterAll(async () => {
const user = await User.findOneBy({
username: "test",
@ -146,7 +187,7 @@ afterAll(async () => {
const activities = await RawActivity.createQueryBuilder("activity")
.where("activity.data->>'actor' = :actor", {
actor: `${config.http.base_url}:${config.http.port}/@test`,
actor: `${config.http.base_url}/@test`,
})
.leftJoinAndSelect("activity.objects", "objects")
.getMany();

View file

@ -23,37 +23,34 @@ describe("POST /@test/inbox", () => {
test("should store a new Note object", async () => {
const activityId = `https://example.com/objects/${crypto.randomUUID()}`;
const response = await fetch(
`${config.http.base_url}:${config.http.port}/@test/inbox/`,
{
method: "POST",
headers: {
"Content-Type": "application/activity+json",
const response = await fetch(`${config.http.base_url}/@test/inbox/`, {
method: "POST",
headers: {
"Content-Type": "application/activity+json",
},
body: JSON.stringify({
"@context": "https://www.w3.org/ns/activitystreams",
type: "Create",
id: activityId,
actor: {
id: `${config.http.base_url}/@test`,
type: "Person",
preferredUsername: "test",
},
body: JSON.stringify({
to: ["https://www.w3.org/ns/activitystreams#Public"],
cc: [],
published: "2021-01-01T00:00:00.000Z",
object: {
"@context": "https://www.w3.org/ns/activitystreams",
type: "Create",
id: activityId,
actor: {
id: `${config.http.base_url}:${config.http.port}/@test`,
type: "Person",
preferredUsername: "test",
},
to: ["https://www.w3.org/ns/activitystreams#Public"],
cc: [],
id: "https://example.com/notes/1",
type: "Note",
content: "Hello, world!",
summary: null,
inReplyTo: null,
published: "2021-01-01T00:00:00.000Z",
object: {
"@context": "https://www.w3.org/ns/activitystreams",
id: "https://example.com/notes/1",
type: "Note",
content: "Hello, world!",
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");
@ -85,37 +82,34 @@ describe("POST /@test/inbox", () => {
test("should try to update that Note object", async () => {
const activityId = `https://example.com/objects/${crypto.randomUUID()}`;
const response = await fetch(
`${config.http.base_url}:${config.http.port}/@test/inbox/`,
{
method: "POST",
headers: {
"Content-Type": "application/activity+json",
const response = await fetch(`${config.http.base_url}/@test/inbox/`, {
method: "POST",
headers: {
"Content-Type": "application/activity+json",
},
body: JSON.stringify({
"@context": "https://www.w3.org/ns/activitystreams",
type: "Update",
id: activityId,
actor: {
id: `${config.http.base_url}/@test`,
type: "Person",
preferredUsername: "test",
},
body: JSON.stringify({
to: ["https://www.w3.org/ns/activitystreams#Public"],
cc: [],
published: "2021-01-02T00:00:00.000Z",
object: {
"@context": "https://www.w3.org/ns/activitystreams",
type: "Update",
id: activityId,
actor: {
id: `${config.http.base_url}:${config.http.port}/@test`,
type: "Person",
preferredUsername: "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",
},
}),
}
);
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");
@ -146,37 +140,34 @@ describe("POST /@test/inbox", () => {
test("should delete the Note object", async () => {
const activityId = `https://example.com/objects/${crypto.randomUUID()}`;
const response = await fetch(
`${config.http.base_url}:${config.http.port}/@test/inbox/`,
{
method: "POST",
headers: {
"Content-Type": "application/activity+json",
const response = await fetch(`${config.http.base_url}/@test/inbox/`, {
method: "POST",
headers: {
"Content-Type": "application/activity+json",
},
body: JSON.stringify({
"@context": "https://www.w3.org/ns/activitystreams",
type: "Delete",
id: activityId,
actor: {
id: `${config.http.base_url}/@test`,
type: "Person",
preferredUsername: "test",
},
body: JSON.stringify({
to: ["https://www.w3.org/ns/activitystreams#Public"],
cc: [],
published: "2021-01-03T00:00:00.000Z",
object: {
"@context": "https://www.w3.org/ns/activitystreams",
type: "Delete",
id: activityId,
actor: {
id: `${config.http.base_url}:${config.http.port}/@test`,
type: "Person",
preferredUsername: "test",
},
to: ["https://www.w3.org/ns/activitystreams#Public"],
cc: [],
published: "2021-01-03T00: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",
},
}),
}
);
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");
@ -196,7 +187,7 @@ describe("POST /@test/inbox", () => {
expect(activity?.actors).toHaveLength(1);
expect(activity?.actors[0].data).toEqual({
preferredUsername: "test",
id: `${config.http.base_url}:${config.http.port}/@test`,
id: `${config.http.base_url}/@test`,
type: "Person",
});
@ -211,33 +202,30 @@ describe("POST /@test/inbox", () => {
test("should return a 404 error when trying to delete a non-existent Note object", async () => {
const activityId = `https://example.com/objects/${crypto.randomUUID()}`;
const response = await fetch(
`${config.http.base_url}:${config.http.port}/@test/inbox/`,
{
method: "POST",
headers: {
"Content-Type": "application/activity+json",
const response = await fetch(`${config.http.base_url}/@test/inbox/`, {
method: "POST",
headers: {
"Content-Type": "application/activity+json",
},
body: JSON.stringify({
"@context": "https://www.w3.org/ns/activitystreams",
type: "Delete",
id: activityId,
actor: {
id: `${config.http.base_url}/@test`,
type: "Person",
preferredUsername: "test",
},
body: JSON.stringify({
to: ["https://www.w3.org/ns/activitystreams#Public"],
cc: [],
published: "2021-01-03T00:00:00.000Z",
object: {
"@context": "https://www.w3.org/ns/activitystreams",
type: "Delete",
id: activityId,
actor: {
id: `${config.http.base_url}:${config.http.port}/@test`,
type: "Person",
preferredUsername: "test",
},
to: ["https://www.w3.org/ns/activitystreams#Public"],
cc: [],
published: "2021-01-03T00:00:00.000Z",
object: {
"@context": "https://www.w3.org/ns/activitystreams",
id: "https://example.com/notes/2345678909876543",
type: "Note",
},
}),
}
);
id: "https://example.com/notes/2345678909876543",
type: "Note",
},
}),
});
expect(response.status).toBe(404);
expect(response.headers.get("content-type")).toBe("application/json");
@ -262,10 +250,10 @@ afterAll(async () => {
.leftJoinAndSelect("activity.objects", "objects")
.leftJoinAndSelect("activity.actors", "actors")
// activity.actors is a many-to-many relationship with Actor objects (it is an array of Actor objects)
// Get the actors of the activity that have data.id as `${config.http.base_url}:${config.http.port}/@test`
// Get the actors of the activity that have data.id as `${config.http.base_url}/@test`
.where("actors.data @> :data", {
data: JSON.stringify({
id: `${config.http.base_url}:${config.http.port}/@test`,
id: `${config.http.base_url}/@test`,
}),
})
.getMany();

View file

@ -32,13 +32,10 @@ describe("POST /api/v1/apps/", () => {
formData.append("website", "https://example.com");
formData.append("redirect_uris", "https://example.com");
formData.append("scopes", "read write");
const response = await fetch(
`${config.http.base_url}:${config.http.port}/api/v1/apps/`,
{
method: "POST",
body: formData,
}
);
const response = await fetch(`${config.http.base_url}/api/v1/apps/`, {
method: "POST",
body: formData,
});
expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toBe("application/json");
@ -70,7 +67,7 @@ describe("POST /auth/login/", () => {
formData.append("username", "test");
formData.append("password", "test");
const response = await fetch(
`${config.http.base_url}:${config.http.port}/auth/login/?client_id=${client_id}&redirect_uri=https://example.com&response_type=code&scopes=read+write`,
`${config.http.base_url}/auth/login/?client_id=${client_id}&redirect_uri=https://example.com&response_type=code&scopes=read+write`,
{
method: "POST",
body: formData,
@ -98,13 +95,10 @@ describe("POST /oauth/token/", () => {
formData.append("client_secret", client_secret);
formData.append("scope", "read write");
const response = await fetch(
`${config.http.base_url}:${config.http.port}/oauth/token/`,
{
method: "POST",
body: formData,
}
);
const response = await fetch(`${config.http.base_url}/oauth/token/`, {
method: "POST",
body: formData,
});
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const json = await response.json();
@ -126,7 +120,7 @@ describe("POST /oauth/token/", () => {
describe("GET /api/v1/apps/verify_credentials", () => {
test("should return the authenticated application's credentials", async () => {
const response = await fetch(
`${config.http.base_url}:${config.http.port}/api/v1/apps/verify_credentials`,
`${config.http.base_url}/api/v1/apps/verify_credentials`,
{
method: "GET",
headers: {