refactor(api): ♻️ Refactor test code to use new client

This commit is contained in:
Jesse Wierzbinski 2025-03-22 04:04:06 +01:00
parent 232ce83e4d
commit 84b9fc3719
No known key found for this signature in database
23 changed files with 717 additions and 962 deletions

View file

@ -1,8 +1,7 @@
import { afterAll, describe, expect, test } from "bun:test";
import type { Relationship as ApiRelationship } from "@versia/client/types";
import { fakeRequest, getTestUsers } from "~/tests/utils";
import { generateClient, getTestUsers } from "~/tests/utils";
const { users, tokens, deleteUsers } = await getTestUsers(2);
const { users, deleteUsers } = await getTestUsers(2);
afterAll(async () => {
await deleteUsers();
@ -11,57 +10,40 @@ afterAll(async () => {
// /api/v1/accounts/:id/block
describe("/api/v1/accounts/:id/block", () => {
test("should return 401 if not authenticated", async () => {
const response = await fakeRequest(
`/api/v1/accounts/${users[1].id}/block`,
{
method: "POST",
},
);
expect(response.status).toBe(401);
await using client = await generateClient();
const { ok, raw } = await client.blockAccount(users[1].id);
expect(ok).toBe(false);
expect(raw.status).toBe(401);
});
test("should return 404 if user not found", async () => {
const response = await fakeRequest(
"/api/v1/accounts/00000000-0000-0000-0000-000000000000/block",
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].data.accessToken}`,
},
},
await using client = await generateClient(users[0]);
const { ok, raw } = await client.blockAccount(
"00000000-0000-0000-0000-000000000000",
);
expect(response.status).toBe(404);
expect(ok).toBe(false);
expect(raw.status).toBe(404);
});
test("should block user", async () => {
const response = await fakeRequest(
`/api/v1/accounts/${users[1].id}/block`,
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].data.accessToken}`,
},
},
);
expect(response.status).toBe(200);
await using client = await generateClient(users[0]);
const relationship = (await response.json()) as ApiRelationship;
expect(relationship.blocking).toBe(true);
const { ok, data } = await client.blockAccount(users[1].id);
expect(ok).toBe(true);
expect(data.blocking).toBe(true);
});
test("should return 200 if user already blocked", async () => {
const response = await fakeRequest(
`/api/v1/accounts/${users[1].id}/block`,
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].data.accessToken}`,
},
},
);
expect(response.status).toBe(200);
await using client = await generateClient(users[0]);
const relationship = (await response.json()) as ApiRelationship;
expect(relationship.blocking).toBe(true);
const { ok, data } = await client.blockAccount(users[1].id);
expect(ok).toBe(true);
expect(data.blocking).toBe(true);
});
});

View file

@ -1,8 +1,7 @@
import { afterAll, describe, expect, test } from "bun:test";
import type { Relationship as ApiRelationship } from "@versia/client/types";
import { fakeRequest, getTestUsers } from "~/tests/utils";
import { generateClient, getTestUsers } from "~/tests/utils";
const { users, tokens, deleteUsers } = await getTestUsers(2);
const { users, deleteUsers } = await getTestUsers(2);
afterAll(async () => {
await deleteUsers();
@ -11,67 +10,38 @@ afterAll(async () => {
// /api/v1/accounts/:id/follow
describe("/api/v1/accounts/:id/follow", () => {
test("should return 401 if not authenticated", async () => {
const response = await fakeRequest(
`/api/v1/accounts/${users[1].id}/follow`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({}),
},
);
expect(response.status).toBe(401);
await using client = await generateClient();
const { ok, raw } = await client.followAccount(users[1].id);
expect(ok).toBe(false);
expect(raw.status).toBe(401);
});
test("should return 404 if user not found", async () => {
const response = await fakeRequest(
"/api/v1/accounts/00000000-0000-0000-0000-000000000000/follow",
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].data.accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
},
await using client = await generateClient(users[0]);
const { ok, raw } = await client.followAccount(
"00000000-0000-0000-0000-000000000000",
);
expect(response.status).toBe(404);
expect(ok).toBe(false);
expect(raw.status).toBe(404);
});
test("should follow user", async () => {
const response = await fakeRequest(
`/api/v1/accounts/${users[1].id}/follow`,
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].data.accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
},
);
expect(response.status).toBe(200);
await using client = await generateClient(users[0]);
const relationship = (await response.json()) as ApiRelationship;
expect(relationship.following).toBe(true);
const { ok } = await client.followAccount(users[1].id);
expect(ok).toBe(true);
});
test("should return 200 if user already followed", async () => {
const response = await fakeRequest(
`/api/v1/accounts/${users[1].id}/follow`,
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].data.accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
},
);
expect(response.status).toBe(200);
await using client = await generateClient(users[0]);
const relationship = (await response.json()) as ApiRelationship;
expect(relationship.following).toBe(true);
const { ok } = await client.followAccount(users[1].id);
expect(ok).toBe(true);
});
});

View file

@ -1,45 +1,30 @@
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import type { Account as ApiAccount } from "@versia/client/types";
import { fakeRequest, getTestUsers } from "~/tests/utils";
import { generateClient, getTestUsers } from "~/tests/utils";
const { users, tokens, deleteUsers } = await getTestUsers(5);
const { users, deleteUsers } = await getTestUsers(5);
afterAll(async () => {
await deleteUsers();
});
beforeAll(async () => {
// Follow user
const response = await fakeRequest(
`/api/v1/accounts/${users[1].id}/follow`,
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].data.accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
},
);
await using client = await generateClient(users[0]);
expect(response.status).toBe(200);
// Follow user
const { ok, raw } = await client.followAccount(users[1].id);
expect(ok).toBe(true);
expect(raw.status).toBe(200);
});
// /api/v1/accounts/:id/followers
describe("/api/v1/accounts/:id/followers", () => {
test("should return 200 with followers", async () => {
const response = await fakeRequest(
`/api/v1/accounts/${users[1].id}/followers`,
{
headers: {
Authorization: `Bearer ${tokens[0].data.accessToken}`,
},
},
);
await using client = await generateClient(users[0]);
expect(response.status).toBe(200);
const { ok, data } = await client.getAccountFollowers(users[1].id);
const data = (await response.json()) as ApiAccount[];
expect(ok).toBe(true);
expect(data).toBeInstanceOf(Array);
expect(data.length).toBe(1);
@ -47,32 +32,15 @@ describe("/api/v1/accounts/:id/followers", () => {
});
test("should return no followers after unfollowing", async () => {
// Unfollow user
const response = await fakeRequest(
`/api/v1/accounts/${users[1].id}/unfollow`,
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].data.accessToken}`,
},
},
);
await using client = await generateClient(users[0]);
expect(response.status).toBe(200);
const { ok } = await client.unfollowAccount(users[1].id);
const response2 = await fakeRequest(
`/api/v1/accounts/${users[1].id}/followers`,
{
headers: {
Authorization: `Bearer ${tokens[0].data.accessToken}`,
},
},
);
expect(ok).toBe(true);
expect(response2.status).toBe(200);
const data = (await response2.json()) as ApiAccount[];
const { ok: ok2, data } = await client.getAccountFollowers(users[1].id);
expect(ok2).toBe(true);
expect(data).toBeInstanceOf(Array);
expect(data.length).toBe(0);
});

View file

@ -1,45 +1,29 @@
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import type { Account as ApiAccount } from "@versia/client/types";
import { fakeRequest, getTestUsers } from "~/tests/utils";
import { generateClient, getTestUsers } from "~/tests/utils";
const { users, tokens, deleteUsers } = await getTestUsers(5);
const { users, deleteUsers } = await getTestUsers(5);
afterAll(async () => {
await deleteUsers();
});
beforeAll(async () => {
// Follow user
const response = await fakeRequest(
`/api/v1/accounts/${users[1].id}/follow`,
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].data.accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
},
);
await using client = await generateClient(users[0]);
expect(response.status).toBe(200);
// Follow user
const { ok } = await client.followAccount(users[1].id);
expect(ok).toBe(true);
});
// /api/v1/accounts/:id/following
describe("/api/v1/accounts/:id/following", () => {
test("should return 200 with following", async () => {
const response = await fakeRequest(
`/api/v1/accounts/${users[0].id}/following`,
{
headers: {
Authorization: `Bearer ${tokens[1].data.accessToken}`,
},
},
);
await using client = await generateClient(users[1]);
expect(response.status).toBe(200);
const { ok, data } = await client.getAccountFollowing(users[0].id);
const data = (await response.json()) as ApiAccount[];
expect(ok).toBe(true);
expect(data).toBeInstanceOf(Array);
expect(data.length).toBe(1);
@ -47,32 +31,15 @@ describe("/api/v1/accounts/:id/following", () => {
});
test("should return no following after unfollowing", async () => {
// Unfollow user
const response = await fakeRequest(
`/api/v1/accounts/${users[1].id}/unfollow`,
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].data.accessToken}`,
},
},
);
await using client = await generateClient(users[0]);
expect(response.status).toBe(200);
const { ok } = await client.unfollowAccount(users[1].id);
const response2 = await fakeRequest(
`/api/v1/accounts/${users[0].id}/following`,
{
headers: {
Authorization: `Bearer ${tokens[1].data.accessToken}`,
},
},
);
expect(ok).toBe(true);
expect(response2.status).toBe(200);
const data = (await response2.json()) as ApiAccount[];
const { ok: ok2, data } = await client.getAccountFollowing(users[1].id);
expect(ok2).toBe(true);
expect(data).toBeInstanceOf(Array);
expect(data.length).toBe(0);
});

View file

@ -1,40 +1,39 @@
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import type { Account as ApiAccount } from "@versia/client/types";
import { fakeRequest, getTestStatuses, getTestUsers } from "~/tests/utils";
import { generateClient, getTestStatuses, getTestUsers } from "~/tests/utils";
const { users, tokens, deleteUsers } = await getTestUsers(5);
const timeline = (await getTestStatuses(40, users[0])).toReversed();
const { users, deleteUsers } = await getTestUsers(5);
const timeline = (await getTestStatuses(5, users[0])).toReversed();
afterAll(async () => {
await deleteUsers();
});
beforeAll(async () => {
await using client = await generateClient(users[0]);
for (const status of timeline) {
await fakeRequest(`/api/v1/statuses/${status.id}/favourite`, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].data.accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
});
await client.favouriteStatus(status.id);
}
});
// /api/v1/accounts/:id
describe("/api/v1/accounts/:id", () => {
test("should return 404 if ID is invalid", async () => {
const response = await fakeRequest("/api/v1/accounts/invalid-id");
expect(response.status).toBe(422);
await using client = await generateClient(users[0]);
const { ok, raw } = await client.getAccount("invalid-id");
expect(ok).toBe(false);
expect(raw.status).toBe(422);
});
test("should return user", async () => {
const response = await fakeRequest(`/api/v1/accounts/${users[0].id}`);
await using client = await generateClient(users[0]);
expect(response.status).toBe(200);
const { ok, data } = await client.getAccount(users[0].id);
const data = (await response.json()) as ApiAccount;
expect(ok).toBe(true);
expect(data).toMatchObject({
id: users[0].id,
username: users[0].data.username,
@ -45,7 +44,7 @@ describe("/api/v1/accounts/:id", () => {
created_at: new Date(users[0].data.createdAt).toISOString(),
followers_count: 0,
following_count: 0,
statuses_count: 40,
statuses_count: 5,
note: users[0].data.note,
acct: users[0].data.username,
uri: expect.any(String),

View file

@ -1,8 +1,7 @@
import { afterAll, describe, expect, test } from "bun:test";
import type { Relationship as ApiRelationship } from "@versia/client/types";
import { fakeRequest, getTestUsers } from "~/tests/utils";
import { generateClient, getTestUsers } from "~/tests/utils";
const { users, tokens, deleteUsers } = await getTestUsers(2);
const { users, deleteUsers } = await getTestUsers(2);
afterAll(async () => {
await deleteUsers();
@ -11,67 +10,44 @@ afterAll(async () => {
// /api/v1/accounts/:id/mute
describe("/api/v1/accounts/:id/mute", () => {
test("should return 401 if not authenticated", async () => {
const response = await fakeRequest(
`/api/v1/accounts/${users[1].id}/mute`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({}),
},
);
expect(response.status).toBe(401);
await using client = await generateClient();
const { ok, raw } = await client.muteAccount(users[1].id);
expect(ok).toBe(false);
expect(raw.status).toBe(401);
});
test("should return 404 if user not found", async () => {
const response = await fakeRequest(
"/api/v1/accounts/00000000-0000-0000-0000-000000000000/mute",
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].data.accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
},
await using client = await generateClient(users[0]);
const { ok, raw } = await client.muteAccount(
"00000000-0000-0000-0000-000000000000",
);
expect(response.status).toBe(404);
expect(ok).toBe(false);
expect(raw.status).toBe(404);
});
test("should mute user", async () => {
const response = await fakeRequest(
`/api/v1/accounts/${users[1].id}/mute`,
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].data.accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
},
);
expect(response.status).toBe(200);
await using client = await generateClient(users[0]);
const relationship = (await response.json()) as ApiRelationship;
expect(relationship.muting).toBe(true);
const { data, ok, raw } = await client.muteAccount(users[1].id);
expect(ok).toBe(true);
expect(raw.status).toBe(200);
expect(data.muting).toBe(true);
});
test("should return 200 if user already muted", async () => {
const response = await fakeRequest(
`/api/v1/accounts/${users[1].id}/mute`,
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].data.accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
},
);
expect(response.status).toBe(200);
await using client = await generateClient(users[0]);
const relationship = (await response.json()) as ApiRelationship;
expect(relationship.muting).toBe(true);
const { data, ok, raw } = await client.muteAccount(users[1].id);
expect(ok).toBe(true);
expect(raw.status).toBe(200);
expect(data.muting).toBe(true);
});
});

View file

@ -1,146 +1,109 @@
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import type { Status as ApiStatus } from "@versia/client/types";
import { fakeRequest, getTestStatuses, getTestUsers } from "~/tests/utils.ts";
import {
generateClient,
getTestStatuses,
getTestUsers,
} from "~/tests/utils.ts";
const { users, tokens, deleteUsers } = await getTestUsers(5);
const timeline = (await getTestStatuses(40, users[1])).toReversed();
const timeline2 = (await getTestStatuses(40, users[2])).toReversed();
const { users, deleteUsers } = await getTestUsers(5);
const timeline = (await getTestStatuses(5, users[1])).toReversed();
const timeline2 = (await getTestStatuses(5, users[2])).toReversed();
afterAll(async () => {
await deleteUsers();
});
beforeAll(async () => {
const response = await fakeRequest(
`/api/v1/statuses/${timeline2[0].id}/reblog`,
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].data.accessToken}`,
},
},
);
await using client = await generateClient(users[1]);
expect(response.status).toBe(200);
const { ok } = await client.reblogStatus(timeline2[0].id);
expect(ok).toBe(true);
});
// /api/v1/accounts/:id/statuses
describe("/api/v1/accounts/:id/statuses", () => {
test("should return 200 with statuses", async () => {
const response = await fakeRequest(
`/api/v1/accounts/${users[1].id}/statuses`,
{
headers: {
Authorization: `Bearer ${tokens[0].data.accessToken}`,
},
},
);
await using client = await generateClient(users[1]);
expect(response.status).toBe(200);
const { data, ok } = await client.getAccountStatuses(users[1].id, {
limit: 5,
});
const data = (await response.json()) as ApiStatus[];
expect(ok).toBe(true);
expect(data.length).toBe(20);
expect(data).toBeArrayOfSize(5);
// Should have reblogs
expect(data[0].reblog).toBeDefined();
});
test("should exclude reblogs", async () => {
const response = await fakeRequest(
`/api/v1/accounts/${users[1].id}/statuses?exclude_reblogs=true`,
{
headers: {
Authorization: `Bearer ${tokens[0].data.accessToken}`,
},
},
);
await using client = await generateClient(users[1]);
expect(response.status).toBe(200);
const { data, ok } = await client.getAccountStatuses(users[1].id, {
exclude_reblogs: true,
limit: 5,
});
const data = (await response.json()) as ApiStatus[];
expect(ok).toBe(true);
expect(data.length).toBe(20);
expect(data).toBeArrayOfSize(5);
// Should not have reblogs
expect(data[0].reblog).toBeNull();
});
test("should exclude replies", async () => {
// Create reply
const replyResponse = await fakeRequest("/api/v1/statuses", {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].data.accessToken}`,
},
body: new URLSearchParams({
status: "Reply",
in_reply_to_id: timeline[0].id,
local_only: "true",
}),
await using client0 = await generateClient(users[0]);
await using client1 = await generateClient(users[1]);
const { ok } = await client1.postStatus("Reply", {
in_reply_to_id: timeline[0].id,
local_only: true,
});
expect(replyResponse.status).toBe(200);
expect(ok).toBe(true);
const response = await fakeRequest(
`/api/v1/accounts/${users[1].id}/statuses?exclude_replies=true`,
const { data, ok: ok2 } = await client0.getAccountStatuses(
users[1].id,
{
headers: {
Authorization: `Bearer ${tokens[0].data.accessToken}`,
},
exclude_replies: true,
limit: 5,
},
);
expect(response.status).toBe(200);
expect(ok2).toBe(true);
const data = (await response.json()) as ApiStatus[];
expect(data.length).toBe(20);
expect(data).toBeArrayOfSize(5);
// Should not have replies
expect(data[0].in_reply_to_id).toBeNull();
});
test("should only include pins", async () => {
const response = await fakeRequest(
`/api/v1/accounts/${users[1].id}/statuses?pinned=true`,
{
headers: {
Authorization: `Bearer ${tokens[0].data.accessToken}`,
},
},
);
await using client0 = await generateClient(users[0]);
await using client1 = await generateClient(users[1]);
expect(response.status).toBe(200);
const { ok, data } = await client0.getAccountStatuses(users[1].id, {
pinned: true,
});
const data = (await response.json()) as ApiStatus[];
expect(data.length).toBe(0);
expect(ok).toBe(true);
expect(data).toBeArrayOfSize(0);
// Create pin
const pinResponse = await fakeRequest(
`/api/v1/statuses/${timeline[3].id}/pin`,
const { ok: ok2 } = await client1.pinStatus(timeline[3].id);
expect(ok2).toBe(true);
const { data: data2, ok: ok3 } = await client0.getAccountStatuses(
users[1].id,
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].data.accessToken}`,
},
pinned: true,
},
);
expect(pinResponse.status).toBe(200);
const response2 = await fakeRequest(
`/api/v1/accounts/${users[1].id}/statuses?pinned=true`,
{
headers: {
Authorization: `Bearer ${tokens[0].data.accessToken}`,
},
},
);
expect(response2.status).toBe(200);
const data2 = (await response2.json()) as ApiStatus[];
expect(data2.length).toBe(1);
expect(ok3).toBe(true);
expect(data2).toBeArrayOfSize(1);
expect(data2[0].id).toBe(timeline[3].id);
});
});

View file

@ -1,76 +1,57 @@
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import type { Relationship as ApiRelationship } from "@versia/client/types";
import { fakeRequest, getTestUsers } from "~/tests/utils";
import { generateClient, getTestUsers } from "~/tests/utils";
const { users, tokens, deleteUsers } = await getTestUsers(2);
const { users, deleteUsers } = await getTestUsers(2);
afterAll(async () => {
await deleteUsers();
});
beforeAll(async () => {
await fakeRequest(`/api/v1/accounts/${users[0].id}/mute`, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].data.accessToken}`,
},
});
await using client = await generateClient(users[0]);
const { ok } = await client.muteAccount(users[1].id);
expect(ok).toBe(true);
});
// /api/v1/accounts/:id/unmute
describe("/api/v1/accounts/:id/unmute", () => {
test("should return 401 if not authenticated", async () => {
const response = await fakeRequest(
`/api/v1/accounts/${users[1].id}/unmute`,
{
method: "POST",
},
);
expect(response.status).toBe(401);
await using client = await generateClient();
const { ok, raw } = await client.unmuteAccount(users[0].id);
expect(ok).toBe(false);
expect(raw.status).toBe(401);
});
test("should return 404 if user not found", async () => {
const response = await fakeRequest(
"/api/v1/accounts/00000000-0000-0000-0000-000000000000/unmute",
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].data.accessToken}`,
},
},
await using client = await generateClient(users[0]);
const { ok, raw } = await client.unmuteAccount(
"00000000-0000-0000-0000-000000000000",
);
expect(response.status).toBe(404);
expect(ok).toBe(false);
expect(raw.status).toBe(404);
});
test("should unmute user", async () => {
const response = await fakeRequest(
`/api/v1/accounts/${users[1].id}/unmute`,
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].data.accessToken}`,
},
},
);
expect(response.status).toBe(200);
await using client = await generateClient(users[0]);
const relationship = (await response.json()) as ApiRelationship;
expect(relationship.muting).toBe(false);
const { data, ok } = await client.unmuteAccount(users[1].id);
expect(ok).toBe(true);
expect(data.muting).toBe(false);
});
test("should return 200 if user already unmuted", async () => {
const response = await fakeRequest(
`/api/v1/accounts/${users[1].id}/unmute`,
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].data.accessToken}`,
},
},
);
expect(response.status).toBe(200);
await using client = await generateClient(users[0]);
const relationship = (await response.json()) as ApiRelationship;
expect(relationship.muting).toBe(false);
const { data, ok } = await client.unmuteAccount(users[1].id);
expect(ok).toBe(true);
expect(data.muting).toBe(false);
});
});

View file

@ -1,81 +1,39 @@
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import { fakeRequest, getTestUsers } from "~/tests/utils.ts";
import { generateClient, getTestUsers } from "~/tests/utils.ts";
const { users, tokens, deleteUsers } = await getTestUsers(5);
const { users, deleteUsers } = await getTestUsers(5);
beforeAll(async () => {
// Create followers relationships
const result1 = await fakeRequest(
`/api/v1/accounts/${users[1].id}/follow`,
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].data.accessToken}`,
},
},
);
await using client = await generateClient(users[0]);
expect(result1.status).toBe(200);
const { ok } = await client.followAccount(users[1].id);
const result2 = await fakeRequest(
`/api/v1/accounts/${users[2].id}/follow`,
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].data.accessToken}`,
},
},
);
expect(ok).toBe(true);
expect(result2.status).toBe(200);
const { ok: ok2 } = await client.followAccount(users[2].id);
const result3 = await fakeRequest(
`/api/v1/accounts/${users[3].id}/follow`,
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].data.accessToken}`,
},
},
);
expect(ok2).toBe(true);
expect(result3.status).toBe(200);
const { ok: ok3 } = await client.followAccount(users[3].id);
const result4 = await fakeRequest(
`/api/v1/accounts/${users[2].id}/follow`,
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].data.accessToken}`,
},
},
);
expect(ok3).toBe(true);
expect(result4.status).toBe(200);
await using client1 = await generateClient(users[1]);
const result5 = await fakeRequest(
`/api/v1/accounts/${users[3].id}/follow`,
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].data.accessToken}`,
},
},
);
const { ok: ok4 } = await client1.followAccount(users[2].id);
expect(result5.status).toBe(200);
expect(ok4).toBe(true);
const result6 = await fakeRequest(
`/api/v1/accounts/${users[3].id}/follow`,
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[2].data.accessToken}`,
},
},
);
const { ok: ok5 } = await client1.followAccount(users[3].id);
expect(result6.status).toBe(200);
expect(ok5).toBe(true);
await using client2 = await generateClient(users[2]);
const { ok: ok6 } = await client2.followAccount(users[3].id);
expect(ok6).toBe(true);
});
afterAll(async () => {
@ -84,55 +42,33 @@ afterAll(async () => {
describe("/api/v1/accounts/familiar_followers", () => {
test("should return 0 familiar followers", async () => {
const response = await fakeRequest(
`/api/v1/accounts/familiar_followers?id=${users[4].id}`,
{
headers: {
Authorization: `Bearer ${tokens[0].data.accessToken}`,
},
},
);
await using client = await generateClient(users[0]);
expect(response.status).toBe(200);
const { data, ok } = await client.getFamiliarFollowers([users[4].id]);
const data = await response.json();
expect(data.length).toBe(1);
expect(data[0].id).toBe(users[4].id);
expect(ok).toBe(true);
expect(data[0].accounts).toBeArrayOfSize(0);
});
test("should return 1 familiar follower", async () => {
const response = await fakeRequest(
`/api/v1/accounts/familiar_followers?id=${users[2].id}`,
{
headers: {
Authorization: `Bearer ${tokens[0].data.accessToken}`,
},
},
);
await using client = await generateClient(users[0]);
expect(response.status).toBe(200);
const { data, ok } = await client.getFamiliarFollowers([users[2].id]);
const data = await response.json();
expect(data.length).toBe(1);
expect(ok).toBe(true);
expect(data).toBeArrayOfSize(1);
expect(data[0].id).toBe(users[2].id);
expect(data[0].accounts).toBeArrayOfSize(1);
expect(data[0].accounts[0].id).toBe(users[1].id);
});
test("should return 2 familiar followers", async () => {
const response = await fakeRequest(
`/api/v1/accounts/familiar_followers?id=${users[3].id}`,
{
headers: {
Authorization: `Bearer ${tokens[0].data.accessToken}`,
},
},
);
await using client = await generateClient(users[0]);
expect(response.status).toBe(200);
const { data, ok } = await client.getFamiliarFollowers([users[3].id]);
const data = await response.json();
expect(data.length).toBe(1);
expect(ok).toBe(true);
expect(data).toBeArrayOfSize(1);
expect(data[0].id).toBe(users[3].id);
expect(data[0].accounts).toBeArrayOfSize(2);
expect(data[0].accounts[0].id).toBe(users[2].id);
@ -140,22 +76,21 @@ describe("/api/v1/accounts/familiar_followers", () => {
});
test("should work with multiple ids", async () => {
const response = await fakeRequest(
`/api/v1/accounts/familiar_followers?id[]=${users[2].id}&id[]=${users[3].id}&id[]=${users[4].id}`,
{
headers: {
Authorization: `Bearer ${tokens[0].data.accessToken}`,
},
},
);
await using client = await generateClient(users[0]);
expect(response.status).toBe(200);
const { data, ok } = await client.getFamiliarFollowers([
users[2].id,
users[3].id,
users[4].id,
]);
const data = await response.json();
expect(data.length).toBe(3);
expect(ok).toBe(true);
expect(data).toBeArrayOfSize(3);
expect(data[0].id).toBe(users[2].id);
expect(data[0].accounts).toBeArrayOfSize(1);
expect(data[0].accounts[0].id).toBe(users[1].id);
expect(data[1].id).toBe(users[3].id);
expect(data[1].accounts).toBeArrayOfSize(2);
expect(data[1].accounts[0].id).toBe(users[2].id);
expect(data[1].accounts[1].id).toBe(users[1].id);
expect(data[2].id).toBe(users[4].id);

View file

@ -3,7 +3,7 @@ import { randomString } from "@/math";
import { db } from "@versia/kit/db";
import { Users } from "@versia/kit/tables";
import { eq } from "drizzle-orm";
import { fakeRequest, getSolvedChallenge } from "~/tests/utils";
import { generateClient, getSolvedChallenge } from "~/tests/utils";
const username = randomString(10, "hex");
const username2 = randomString(10, "hex");
@ -16,208 +16,211 @@ afterEach(async () => {
// /api/v1/statuses
describe("/api/v1/accounts", () => {
test("should create a new account", async () => {
const response = await fakeRequest("/api/v1/accounts", {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Challenge-Solution": await getSolvedChallenge(),
},
body: JSON.stringify({
username,
email: "bob@gamer.com",
password: "password",
agreement: "true",
locale: "en",
reason: "testing",
}),
});
await using client = await generateClient();
expect(response.ok).toBe(true);
expect(response.headers.get("Content-Type")).not.toContain("json");
const { ok, raw } = await client.registerAccount(
username,
"bob@gamer.com",
"password",
true,
"en",
"testing",
{
headers: {
"X-Challenge-Solution": await getSolvedChallenge(),
},
},
);
expect(ok).toBe(true);
expect(raw.headers.get("Content-Type")).not.toContain("json");
});
test("should refuse invalid emails", async () => {
const response = await fakeRequest("/api/v1/accounts", {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Challenge-Solution": await getSolvedChallenge(),
},
body: JSON.stringify({
username,
email: "bob",
password: "password",
agreement: "true",
locale: "en",
reason: "testing",
}),
});
await using client = await generateClient();
expect(response.status).toBe(422);
const { ok, raw } = await client.registerAccount(
username,
"bob",
"password",
true,
"en",
"testing",
{
headers: {
"X-Challenge-Solution": await getSolvedChallenge(),
},
},
);
expect(ok).toBe(false);
expect(raw.status).toBe(422);
});
test("should require a password", async () => {
const response = await fakeRequest("/api/v1/accounts", {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Challenge-Solution": await getSolvedChallenge(),
},
body: JSON.stringify({
username,
email: "contatc@bob.com",
agreement: "true",
locale: "en",
reason: "testing",
}),
});
await using client = await generateClient();
expect(response.status).toBe(422);
const { ok, raw } = await client.registerAccount(
username,
"bob@gamer.com",
"",
true,
"en",
"testing",
{
headers: {
"X-Challenge-Solution": await getSolvedChallenge(),
},
},
);
expect(ok).toBe(false);
expect(raw.status).toBe(422);
});
test("should not allow a previously registered email", async () => {
await fakeRequest("/api/v1/accounts", {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Challenge-Solution": await getSolvedChallenge(),
},
body: JSON.stringify({
username,
email: "contact@george.com",
password: "password",
agreement: "true",
locale: "en",
reason: "testing",
}),
});
await using client = await generateClient();
const response = await fakeRequest("/api/v1/accounts", {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Challenge-Solution": await getSolvedChallenge(),
const { ok } = await client.registerAccount(
username,
"contact@george.com",
"password",
true,
"en",
"testing",
{
headers: {
"X-Challenge-Solution": await getSolvedChallenge(),
},
},
body: JSON.stringify({
username: username2,
email: "contact@george.com",
password: "password",
agreement: "true",
locale: "en",
reason: "testing",
}),
});
);
expect(response.status).toBe(422);
expect(ok).toBe(true);
const { ok: ok2, raw } = await client.registerAccount(
username2,
"contact@george.com",
"password",
true,
"en",
"testing",
{
headers: {
"X-Challenge-Solution": await getSolvedChallenge(),
},
},
);
expect(ok2).toBe(false);
expect(raw.status).toBe(422);
});
test("should not allow a previously registered email (case insensitive)", async () => {
await fakeRequest("/api/v1/accounts", {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Challenge-Solution": await getSolvedChallenge(),
},
body: JSON.stringify({
username,
email: "contact@george.com",
password: "password",
agreement: "true",
locale: "en",
reason: "testing",
}),
});
await using client = await generateClient();
const response = await fakeRequest("/api/v1/accounts", {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Challenge-Solution": await getSolvedChallenge(),
const { ok } = await client.registerAccount(
username,
"contact@george.com",
"password",
true,
"en",
"testing",
{
headers: {
"X-Challenge-Solution": await getSolvedChallenge(),
},
},
body: JSON.stringify({
username: username2,
email: "CONTACT@george.CoM",
password: "password",
agreement: "true",
locale: "en",
reason: "testing",
}),
});
);
expect(response.status).toBe(422);
expect(ok).toBe(true);
const { ok: ok2, raw } = await client.registerAccount(
username2,
"CONTACT@george.CoM",
"password",
true,
"en",
"testing",
{
headers: {
"X-Challenge-Solution": await getSolvedChallenge(),
},
},
);
expect(ok2).toBe(false);
expect(raw.status).toBe(422);
});
test("should not allow invalid usernames (not a-z_0-9)", async () => {
const response1 = await fakeRequest("/api/v1/accounts", {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Challenge-Solution": await getSolvedChallenge(),
await using client = await generateClient();
const { ok: ok1, raw: raw1 } = await client.registerAccount(
"bob$",
"contact@george.com",
"password",
true,
"en",
"testing",
{
headers: {
"X-Challenge-Solution": await getSolvedChallenge(),
},
},
body: JSON.stringify({
username: "bob$",
email: "contact@bob.com",
password: "password",
agreement: "true",
locale: "en",
reason: "testing",
}),
});
);
expect(response1.status).toBe(422);
expect(ok1).toBe(false);
expect(raw1.status).toBe(422);
const response2 = await fakeRequest("/api/v1/accounts", {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Challenge-Solution": await getSolvedChallenge(),
const { ok: ok2, raw: raw2 } = await client.registerAccount(
"bob-markey",
"contact@bob.com",
"password",
true,
"en",
"testing",
{
headers: {
"X-Challenge-Solution": await getSolvedChallenge(),
},
},
body: JSON.stringify({
username: "bob-markey",
email: "contact@bob.com",
password: "password",
agreement: "true",
locale: "en",
reason: "testing",
}),
});
);
expect(response2.status).toBe(422);
expect(ok2).toBe(false);
expect(raw2.status).toBe(422);
const response3 = await fakeRequest("/api/v1/accounts", {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Challenge-Solution": await getSolvedChallenge(),
const { ok: ok3, raw: raw3 } = await client.registerAccount(
"bob markey",
"contact@bob.com",
"password",
true,
"en",
"testing",
{
headers: {
"X-Challenge-Solution": await getSolvedChallenge(),
},
},
body: JSON.stringify({
username: "bob markey",
email: "contact@bob.com",
password: "password",
agreement: "true",
locale: "en",
reason: "testing",
}),
});
);
expect(response3.status).toBe(422);
expect(ok3).toBe(false);
expect(raw3.status).toBe(422);
const response4 = await fakeRequest("/api/v1/accounts", {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Challenge-Solution": await getSolvedChallenge(),
const { ok: ok4, raw: raw4 } = await client.registerAccount(
"BOB",
"contact@bob.com",
"password",
true,
"en",
"testing",
{
headers: {
"X-Challenge-Solution": await getSolvedChallenge(),
},
},
body: JSON.stringify({
username: "BOB",
email: "contact@bob.com",
password: "password",
agreement: "true",
locale: "en",
reason: "testing",
}),
});
);
expect(response4.status).toBe(422);
expect(ok4).toBe(false);
expect(raw4.status).toBe(422);
});
});

View file

@ -1,8 +1,7 @@
import { afterAll, describe, expect, test } from "bun:test";
import type { Account as ApiAccount } from "@versia/client/types";
import { fakeRequest, getTestUsers } from "~/tests/utils";
import { generateClient, getTestUsers } from "~/tests/utils";
const { users, tokens, deleteUsers } = await getTestUsers(5);
const { users, deleteUsers } = await getTestUsers(5);
afterAll(async () => {
await deleteUsers();
@ -11,18 +10,11 @@ afterAll(async () => {
// /api/v1/accounts/lookup
describe("/api/v1/accounts/lookup", () => {
test("should return 200 with users", async () => {
const response = await fakeRequest(
`/api/v1/accounts/lookup?acct=${users[0].data.username}`,
{
headers: {
Authorization: `Bearer ${tokens[0].data.accessToken}`,
},
},
);
await using client = await generateClient(users[0]);
expect(response.status).toBe(200);
const { data, ok } = await client.lookupAccount(users[0].data.username);
const data = (await response.json()) as ApiAccount[];
expect(ok).toBe(true);
expect(data).toEqual(
expect.objectContaining({
id: users[0].id,
@ -35,15 +27,13 @@ describe("/api/v1/accounts/lookup", () => {
});
test("should require exact case", async () => {
const response = await fakeRequest(
`/api/v1/accounts/lookup?acct=${users[0].data.username.toUpperCase()}`,
{
headers: {
Authorization: `Bearer ${tokens[0].data.accessToken}`,
},
},
await using client = await generateClient(users[0]);
const { ok, raw } = await client.lookupAccount(
users[0].data.username.toUpperCase(),
);
expect(response.status).toBe(404);
expect(ok).toBe(false);
expect(raw.status).toBe(404);
});
});

View file

@ -2,9 +2,9 @@ import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import { db } from "@versia/kit/db";
import { Users } from "@versia/kit/tables";
import { eq } from "drizzle-orm";
import { fakeRequest, getTestUsers } from "~/tests/utils";
import { generateClient, getTestUsers } from "~/tests/utils";
const { users, tokens, deleteUsers } = await getTestUsers(5);
const { users, deleteUsers } = await getTestUsers(5);
beforeAll(async () => {
// user0 should be `locked`
@ -15,27 +15,17 @@ beforeAll(async () => {
.set({ isLocked: true })
.where(eq(Users.id, users[0].id));
const res1 = await fakeRequest(`/api/v1/accounts/${users[0].id}/follow`, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].data.accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
});
await using client1 = await generateClient(users[1]);
expect(res1.ok).toBe(true);
const { ok } = await client1.followAccount(users[0].id);
const res2 = await fakeRequest(`/api/v1/accounts/${users[2].id}/follow`, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].data.accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
});
expect(ok).toBe(true);
expect(res2.ok).toBe(true);
await using client0 = await generateClient(users[0]);
const { ok: ok2 } = await client0.followAccount(users[2].id);
expect(ok2).toBe(true);
});
afterAll(async () => {
@ -45,27 +35,21 @@ afterAll(async () => {
// /api/v1/accounts/relationships
describe("/api/v1/accounts/relationships", () => {
test("should return 401 if not authenticated", async () => {
const response = await fakeRequest(
`/api/v1/accounts/relationships?id[]=${users[2].id}`,
);
await using client = await generateClient();
expect(response.status).toBe(401);
const { ok, raw } = await client.getRelationships([users[2].id]);
expect(ok).toBe(false);
expect(raw.status).toBe(401);
});
test("should return relationships", async () => {
const response = await fakeRequest(
`/api/v1/accounts/relationships?id[]=${users[2].id}`,
{
headers: {
Authorization: `Bearer ${tokens[0].data.accessToken}`,
},
},
);
await using client = await generateClient(users[0]);
expect(response.status).toBe(200);
const { data, ok } = await client.getRelationships([users[2].id]);
const body = await response.json();
expect(body).toEqual(
expect(ok).toBe(true);
expect(data).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: users[2].id,
@ -83,19 +67,12 @@ describe("/api/v1/accounts/relationships", () => {
});
test("should be requested_by user1", async () => {
const response = await fakeRequest(
`/api/v1/accounts/relationships?id[]=${users[1].id}`,
{
headers: {
Authorization: `Bearer ${tokens[0].data.accessToken}`,
},
},
);
await using client = await generateClient(users[0]);
expect(response.status).toBe(200);
const { data, ok } = await client.getRelationships([users[1].id]);
const body = await response.json();
expect(body).toEqual(
expect(ok).toBe(true);
expect(data).toEqual(
expect.arrayContaining([
expect.objectContaining({
following: false,

View file

@ -1,8 +1,7 @@
import { afterAll, describe, expect, test } from "bun:test";
import type { Account as ApiAccount } from "@versia/client/types";
import { fakeRequest, getTestUsers } from "~/tests/utils";
import { generateClient, getTestUsers } from "~/tests/utils";
const { users, tokens, deleteUsers } = await getTestUsers(5);
const { users, deleteUsers } = await getTestUsers(5);
afterAll(async () => {
await deleteUsers();
@ -11,18 +10,11 @@ afterAll(async () => {
// /api/v1/accounts/search
describe("/api/v1/accounts/search", () => {
test("should return 200 with users", async () => {
const response = await fakeRequest(
`/api/v1/accounts/search?q=${users[0].data.username}`,
{
headers: {
Authorization: `Bearer ${tokens[0].data.accessToken}`,
},
},
);
await using client = await generateClient(users[0]);
expect(response.status).toBe(200);
const { data, ok } = await client.searchAccount(users[0].data.username);
const data = (await response.json()) as ApiAccount[];
expect(ok).toBe(true);
expect(data).toEqual(
expect.arrayContaining([
expect.objectContaining({

View file

@ -1,9 +1,8 @@
import { afterAll, describe, expect, test } from "bun:test";
import type { Account as APIAccount } from "@versia/client/types";
import { config } from "~/config.ts";
import { fakeRequest, getTestUsers } from "~/tests/utils";
import { generateClient, getTestUsers } from "~/tests/utils";
const { tokens, deleteUsers } = await getTestUsers(1);
const { users, deleteUsers } = await getTestUsers(1);
afterAll(async () => {
await deleteUsers();
@ -13,53 +12,28 @@ afterAll(async () => {
describe("/api/v1/accounts/update_credentials", () => {
describe("HTML injection testing", () => {
test("should not allow HTML injection", async () => {
const response = await fakeRequest(
"/api/v1/accounts/update_credentials",
{
method: "PATCH",
headers: {
Authorization: `Bearer ${tokens[0].data.accessToken}`,
},
body: new URLSearchParams({
note: "Hi! <script>alert('Hello, world!');</script>",
}),
},
);
await using client = await generateClient(users[0]);
expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toContain(
"application/json",
);
const { ok, data } = await client.updateCredentials({
note: "Hi! <script>alert('Hello, world!');</script>",
});
const object = (await response.json()) as APIAccount;
expect(object.note).toBe(
expect(ok).toBe(true);
expect(data.note).toBe(
"<p>Hi! &lt;script&gt;alert('Hello, world!');&lt;/script&gt;</p>\n",
);
});
test("should rewrite all image and video src to go through proxy", async () => {
const response = await fakeRequest(
"/api/v1/accounts/update_credentials",
{
method: "PATCH",
headers: {
Authorization: `Bearer ${tokens[0].data.accessToken}`,
},
body: new URLSearchParams({
note: "<img src='https://example.com/image.jpg'> <video src='https://example.com/video.mp4'> Test!",
}),
},
);
await using client = await generateClient(users[0]);
expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toContain(
"application/json",
);
const { ok, data } = await client.updateCredentials({
note: "<img src='https://example.com/image.jpg'> <video src='https://example.com/video.mp4'> Test!",
});
const object = (await response.json()) as APIAccount;
// Proxy url is base_url/media/proxy/<base64url encoded url>
expect(object.note).toBe(
expect(ok).toBe(true);
expect(data.note).toBe(
// Proxy url is base_url/media/proxy/<base64url encoded url>
`<p><img src="${config.http.base_url}media/proxy/${Buffer.from(
"https://example.com/image.jpg",
).toString("base64url")}"> <video src="${

View file

@ -1,18 +1,15 @@
import { describe, expect, test } from "bun:test";
import { fakeRequest } from "~/tests/utils";
import { generateClient } from "~/tests/utils";
// /api/v1/challenges
describe("/api/v1/challenges", () => {
test("should get a challenge", async () => {
const response = await fakeRequest("/api/v1/challenges", {
method: "POST",
});
await using client = await generateClient();
expect(response.status).toBe(200);
const { data, ok } = await client.getChallenge();
const body = await response.json();
expect(body).toMatchObject({
expect(ok).toBe(true);
expect(data).toMatchObject({
id: expect.any(String),
algorithm: expect.any(String),
challenge: expect.any(String),

View file

@ -1,6 +1,7 @@
import { apiRoute, auth } from "@/api";
import { generateChallenge } from "@/challenges";
import { createRoute, z } from "@hono/zod-openapi";
import { createRoute } from "@hono/zod-openapi";
import { Challenge } from "@versia/client-ng/schemas";
import { ApiError } from "~/classes/errors/api-error";
import { config } from "~/config.ts";
import { ErrorSchema } from "~/types/api";
@ -21,14 +22,7 @@ const route = createRoute({
description: "Challenge",
content: {
"application/json": {
schema: z.object({
id: z.string(),
algorithm: z.enum(["SHA-1", "SHA-256", "SHA-512"]),
challenge: z.string(),
maxnumber: z.number().optional(),
salt: z.string(),
signature: z.string(),
}),
schema: Challenge,
},
},
},

View file

@ -2,53 +2,42 @@ import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import { db } from "@versia/kit/db";
import { Emojis } from "@versia/kit/tables";
import { inArray } from "drizzle-orm";
import { fakeRequest, getTestUsers } from "~/tests/utils";
import { generateClient, getTestUsers } from "~/tests/utils";
const { users, tokens, deleteUsers } = await getTestUsers(2);
const { users, deleteUsers } = await getTestUsers(2);
// Make user 2 an admin
beforeAll(async () => {
await users[1].update({ isAdmin: true });
// Upload one emoji as admin, then one as each user
const response = await fakeRequest("/api/v1/emojis", {
headers: {
Authorization: `Bearer ${tokens[1].data.accessToken}`,
"Content-Type": "application/json",
},
method: "POST",
body: JSON.stringify({
shortcode: "test1",
element: "https://cdn.versia.social/logo.webp",
await using client1 = await generateClient(users[1]);
const { ok } = await client1.uploadEmoji(
"test1",
new URL("https://cdn.versia.social/logo.webp"),
{
global: true,
}),
});
expect(response.status).toBe(201);
await fakeRequest("/api/v1/emojis", {
headers: {
Authorization: `Bearer ${tokens[0].data.accessToken}`,
"Content-Type": "application/json",
},
method: "POST",
body: JSON.stringify({
shortcode: "test2",
element: "https://cdn.versia.social/logo.webp",
}),
});
);
await fakeRequest("/api/v1/emojis", {
headers: {
Authorization: `Bearer ${tokens[1].data.accessToken}`,
"Content-Type": "application/json",
},
method: "POST",
body: JSON.stringify({
shortcode: "test3",
element: "https://cdn.versia.social/logo.webp",
}),
});
expect(ok).toBe(true);
await using client0 = await generateClient(users[0]);
const { ok: ok2 } = await client0.uploadEmoji(
"test2",
new URL("https://cdn.versia.social/logo.webp"),
);
expect(ok2).toBe(true);
const { ok: ok3 } = await client1.uploadEmoji(
"test3",
new URL("https://cdn.versia.social/logo.webp"),
);
expect(ok3).toBe(true);
});
afterAll(async () => {
@ -61,31 +50,23 @@ afterAll(async () => {
describe("/api/v1/custom_emojis", () => {
test("should return all global emojis", async () => {
const response = await fakeRequest("/api/v1/custom_emojis", {
headers: {
Authorization: `Bearer ${tokens[1].data.accessToken}`,
},
});
await using client = await generateClient(users[1]);
expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toContain(
"application/json",
);
const emojis = await response.json();
const { data, ok } = await client.getInstanceCustomEmojis();
expect(ok).toBe(true);
// Should contain test1 and test2, but not test2
expect(emojis).toContainEqual(
expect(data).toContainEqual(
expect.objectContaining({
shortcode: "test1",
}),
);
expect(emojis).not.toContainEqual(
expect(data).not.toContainEqual(
expect.objectContaining({
shortcode: "test2",
}),
);
expect(emojis).toContainEqual(
expect(data).toContainEqual(
expect.objectContaining({
shortcode: "test3",
}),
@ -93,31 +74,23 @@ describe("/api/v1/custom_emojis", () => {
});
test("should return all user emojis", async () => {
const response = await fakeRequest("/api/v1/custom_emojis", {
headers: {
Authorization: `Bearer ${tokens[0].data.accessToken}`,
},
});
await using client = await generateClient(users[0]);
expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toContain(
"application/json",
);
const emojis = await response.json();
const { data, ok } = await client.getInstanceCustomEmojis();
expect(ok).toBe(true);
// Should contain test1 and test2, but not test3
expect(emojis).toContainEqual(
expect(data).toContainEqual(
expect.objectContaining({
shortcode: "test1",
}),
);
expect(emojis).toContainEqual(
expect(data).toContainEqual(
expect.objectContaining({
shortcode: "test2",
}),
);
expect(emojis).not.toContainEqual(
expect(data).not.toContainEqual(
expect.objectContaining({
shortcode: "test3",
}),
@ -125,27 +98,24 @@ describe("/api/v1/custom_emojis", () => {
});
test("should return all global emojis when signed out", async () => {
const response = await fakeRequest("/api/v1/custom_emojis");
await using client = await generateClient();
expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toContain(
"application/json",
);
const { data, ok } = await client.getInstanceCustomEmojis();
const emojis = await response.json();
expect(ok).toBe(true);
// Should contain test1, but not test2 or test3
expect(emojis).toContainEqual(
expect(data).toContainEqual(
expect.objectContaining({
shortcode: "test1",
}),
);
expect(emojis).not.toContainEqual(
expect(data).not.toContainEqual(
expect.objectContaining({
shortcode: "test2",
}),
);
expect(emojis).not.toContainEqual(
expect(data).not.toContainEqual(
expect.objectContaining({
shortcode: "test3",
}),

View file

@ -34,6 +34,6 @@ export { Status, Mention, StatusSource } from "./schemas/status.ts";
export { Tag } from "./schemas/tag.ts";
export { Token } from "./schemas/token.ts";
export { TermsOfService } from "./schemas/tos.ts";
export { Role, NoteReaction, SSOConfig } from "./schemas/versia.ts";
export { Role, NoteReaction, SSOConfig, Challenge } from "./schemas/versia.ts";
export { Id, iso631, zBoolean } from "./schemas/common.ts";

View file

@ -105,3 +105,35 @@ export const SSOConfig = z.object({
"An array of external OpenID Connect providers that users can link their accounts to.",
}),
});
/* Versia Server API extension */
export const Challenge = z
.object({
id: Id.openapi({}).openapi({
description: "Challenge ID in the database.",
example: "b4a7e0f0-8f6a-479b-910b-9265c070d5bd",
}),
algorithm: z.enum(["SHA-1", "SHA-256", "SHA-512"]).openapi({
description: "Algorithm used to generate the challenge.",
example: "SHA-1",
}),
challenge: z.string().openapi({
description: "Challenge to solve.",
example: "1234567890",
}),
maxnumber: z.number().int().nonnegative().optional().openapi({
description: "Maximum number to solve the challenge.",
example: 100,
}),
salt: z.string().openapi({
description: "Salt used to generate the challenge.",
example: "1234567890",
}),
signature: z.string().openapi({
description: "Signature of the challenge.",
example: "1234567890",
}),
})
.openapi({
description: "A cryptographic challenge to solve. Used for Captchas.",
});

View file

@ -91,9 +91,10 @@ export class BaseClient {
public constructor(
protected baseUrl: URL,
private accessToken?: string,
public globalCatch: (error: ResponseError) => void = () => {
// Do nothing by default
},
private options: {
globalCatch?: (error: ResponseError) => void;
throwOnError?: boolean;
} = {},
) {}
public get url(): URL {
@ -104,16 +105,21 @@ export class BaseClient {
return this.accessToken;
}
/** Overridable by testing */
private fetch = fetch;
private async request<ReturnType>(
request: Request,
): Promise<Output<ReturnType>> {
const result = await fetch(request);
const result = await this.fetch(request);
const isJson = result.headers
.get("Content-Type")
?.includes("application/json");
if (!result.ok) {
const error = isJson ? await result.json() : await result.text();
if (!result.ok && this.options.throwOnError) {
const error = isJson
? await result.clone().json()
: await result.clone().text();
throw new ResponseError(
{
data: error,
@ -127,8 +133,10 @@ export class BaseClient {
}
return {
data: isJson ? await result.json() : (await result.text()) || null,
ok: true,
data: isJson
? await result.clone().json()
: (await result.clone().text()) || null,
ok: result.ok,
raw: result,
};
}
@ -168,19 +176,19 @@ export class BaseClient {
});
}
public get<ReturnType>(
public get<ReturnType = void>(
path: string,
extra?: RequestInit,
): Promise<Output<ReturnType>> {
return this.request<ReturnType>(
this.constructRequest(path, "GET", undefined, extra),
).catch((e) => {
this.globalCatch(e);
this.options.globalCatch?.(e);
throw e;
});
}
public post<ReturnType>(
public post<ReturnType = void>(
path: string,
body?: object,
extra?: RequestInit,
@ -188,12 +196,12 @@ export class BaseClient {
return this.request<ReturnType>(
this.constructRequest(path, "POST", body, extra),
).catch((e) => {
this.globalCatch(e);
this.options.globalCatch?.(e);
throw e;
});
}
public postForm<ReturnType>(
public postForm<ReturnType = void>(
path: string,
body: FormData | ConvertibleObject,
extra?: RequestInit,
@ -206,12 +214,12 @@ export class BaseClient {
extra,
),
).catch((e) => {
this.globalCatch(e);
this.options.globalCatch?.(e);
throw e;
});
}
public put<ReturnType>(
public put<ReturnType = void>(
path: string,
body?: object,
extra?: RequestInit,
@ -219,12 +227,12 @@ export class BaseClient {
return this.request<ReturnType>(
this.constructRequest(path, "PUT", body, extra),
).catch((e) => {
this.globalCatch(e);
this.options.globalCatch?.(e);
throw e;
});
}
public putForm<ReturnType>(
public putForm<ReturnType = void>(
path: string,
body: FormData | ConvertibleObject,
extra?: RequestInit,
@ -237,12 +245,12 @@ export class BaseClient {
extra,
),
).catch((e) => {
this.globalCatch(e);
this.options.globalCatch?.(e);
throw e;
});
}
public patch<ReturnType>(
public patch<ReturnType = void>(
path: string,
body?: object,
extra?: RequestInit,
@ -250,12 +258,12 @@ export class BaseClient {
return this.request<ReturnType>(
this.constructRequest(path, "PATCH", body, extra),
).catch((e) => {
this.globalCatch(e);
this.options.globalCatch?.(e);
throw e;
});
}
public patchForm<ReturnType>(
public patchForm<ReturnType = void>(
path: string,
body: FormData | ConvertibleObject,
extra?: RequestInit,
@ -268,12 +276,12 @@ export class BaseClient {
extra,
),
).catch((e) => {
this.globalCatch(e);
this.options.globalCatch?.(e);
throw e;
});
}
public delete<ReturnType>(
public delete<ReturnType = void>(
path: string,
body?: object,
extra?: RequestInit,
@ -281,12 +289,12 @@ export class BaseClient {
return this.request<ReturnType>(
this.constructRequest(path, "DELETE", body, extra),
).catch((e) => {
this.globalCatch(e);
this.options.globalCatch?.(e);
throw e;
});
}
public deleteForm<ReturnType>(
public deleteForm<ReturnType = void>(
path: string,
body: FormData | ConvertibleObject,
extra?: RequestInit,
@ -299,7 +307,7 @@ export class BaseClient {
extra,
),
).catch((e) => {
this.globalCatch(e);
this.options.globalCatch?.(e);
throw e;
});
}

View file

@ -7,6 +7,7 @@ import type { Attachment } from "../schemas/attachment.ts";
import type { Context } from "../schemas/context.ts";
import type { CustomEmoji } from "../schemas/emoji.ts";
import type { ExtendedDescription } from "../schemas/extended-description.ts";
import type { FamiliarFollowers } from "../schemas/familiar-followers.ts";
import type { Instance } from "../schemas/instance.ts";
import type { Marker } from "../schemas/marker.ts";
import type { Notification } from "../schemas/notification.ts";
@ -21,7 +22,7 @@ import type { Status, StatusSource } from "../schemas/status.ts";
import type { Tag } from "../schemas/tag.ts";
import type { Token } from "../schemas/token.ts";
import type { TermsOfService } from "../schemas/tos.ts";
import type { Role } from "../schemas/versia.ts";
import type { Challenge, Role } from "../schemas/versia.ts";
import { BaseClient, type Output } from "./base.ts";
import { DEFAULT_SCOPE, NO_REDIRECT } from "./constants.ts";
@ -71,7 +72,7 @@ export class Client extends BaseClient {
account_ids: string[],
extra?: RequestInit,
): Promise<Output<void>> {
return this.post<void>(
return this.post(
`/api/v1/lists/${id}/accounts`,
{ account_ids },
extra,
@ -89,7 +90,7 @@ export class Client extends BaseClient {
name: string,
extra?: RequestInit,
): Promise<Output<void>> {
return this.put<void>(
return this.put(
`/api/v1/announcements/${id}/reactions/${name}`,
undefined,
extra,
@ -108,7 +109,7 @@ export class Client extends BaseClient {
role_id: string,
extra?: RequestInit,
): Promise<Output<void>> {
return this.post<void>(
return this.post(
`/api/v1/accounts/${account_id}/roles/${role_id}`,
undefined,
extra,
@ -141,7 +142,7 @@ export class Client extends BaseClient {
domain: string,
extra?: RequestInit,
): Promise<Output<void>> {
return this.post<void>("/api/v1/domain_blocks", { domain }, extra);
return this.post("/api/v1/domain_blocks", { domain }, extra);
}
public bookmarkStatus(
@ -164,7 +165,7 @@ export class Client extends BaseClient {
id: string,
extra?: RequestInit,
): Promise<Output<void>> {
return this.delete<void>(
return this.delete(
`/api/v1/scheduled_statuses/${id}/cancel`,
undefined,
extra,
@ -287,7 +288,7 @@ export class Client extends BaseClient {
account_ids: string[],
extra?: RequestInit,
): Promise<Output<void>> {
return this.delete<void>(
return this.delete(
`/api/v1/lists/${id}/accounts`,
{ account_ids },
extra,
@ -303,11 +304,7 @@ export class Client extends BaseClient {
id: string,
extra?: RequestInit,
): Promise<Output<void>> {
return this.delete<void>(
`/api/v1/conversations/${id}`,
undefined,
extra,
);
return this.delete(`/api/v1/conversations/${id}`, undefined, extra);
}
/**
@ -318,7 +315,7 @@ export class Client extends BaseClient {
* @return Empty.
*/
public deleteEmoji(id: string, extra?: RequestInit): Promise<Output<void>> {
return this.delete<void>(`/api/v1/emojis/${id}`, undefined, extra);
return this.delete(`/api/v1/emojis/${id}`, undefined, extra);
}
/**
@ -350,11 +347,7 @@ export class Client extends BaseClient {
id: string,
extra?: RequestInit,
): Promise<Output<void>> {
return this.delete<void>(
`/api/v1/featured_tags/${id}`,
undefined,
extra,
);
return this.delete(`/api/v1/featured_tags/${id}`, undefined, extra);
}
/**
@ -363,14 +356,14 @@ export class Client extends BaseClient {
* @param id Target list ID.
*/
public deleteList(id: string, extra?: RequestInit): Promise<Output<void>> {
return this.delete<void>(`/api/v1/lists/${id}`, undefined, extra);
return this.delete(`/api/v1/lists/${id}`, undefined, extra);
}
/**
* DELETE /api/v1/push/subscription
*/
public deletePushSubscription(extra?: RequestInit): Promise<Output<void>> {
return this.delete<void>("/api/v1/push/subscription", undefined, extra);
return this.delete("/api/v1/push/subscription", undefined, extra);
}
/**
@ -381,7 +374,7 @@ export class Client extends BaseClient {
* @return Empty.
*/
public deleteRole(id: string, extra?: RequestInit): Promise<Output<void>> {
return this.delete<void>(`/api/v1/roles/${id}`, undefined, extra);
return this.delete(`/api/v1/roles/${id}`, undefined, extra);
}
/**
@ -412,7 +405,7 @@ export class Client extends BaseClient {
id: string,
extra?: RequestInit,
): Promise<Output<void>> {
return this.post<void>(
return this.post(
`/api/v1/instance/announcements/${id}/dismiss`,
undefined,
extra,
@ -428,7 +421,7 @@ export class Client extends BaseClient {
id: string,
extra?: RequestInit,
): Promise<Output<void>> {
return this.post<void>(
return this.post(
`/api/v1/notifications/${id}/dismiss`,
undefined,
extra,
@ -439,7 +432,7 @@ export class Client extends BaseClient {
* POST /api/v1/notifications/clear
*/
public dismissNotifications(extra?: RequestInit): Promise<Output<void>> {
return this.post<void>("/api/v1/notifications/clear", undefined, extra);
return this.post("/api/v1/notifications/clear", undefined, extra);
}
/**
@ -600,6 +593,22 @@ export class Client extends BaseClient {
});
}
/**
* POST /api/v1/challenges
*
* Generates a new Captcha to solve.
* @returns A challenge.
*/
public getChallenge(
extra?: RequestInit,
): Promise<Output<z.infer<typeof Challenge>>> {
return this.post<z.infer<typeof Challenge>>(
"/api/v1/challenges",
undefined,
extra,
);
}
/**
* GET /api/v1/accounts/:id
*
@ -1047,6 +1056,28 @@ export class Client extends BaseClient {
);
}
/**
* GET /api/v1/accounts/familiar_followers
*
* @param ids Array of account IDs.
* @return Array of familiar followers.
*/
public getFamiliarFollowers(
ids: string[],
): Promise<Output<z.infer<typeof FamiliarFollowers>[]>> {
const params = new URLSearchParams();
if (ids) {
for (const id of ids) {
params.append("id[]", id);
}
}
return this.get<z.infer<typeof FamiliarFollowers>[]>(
`/api/v1/accounts/familiar_followers?${params}`,
);
}
/**
* GET /api/v1/favourites
*
@ -2354,7 +2385,7 @@ export class Client extends BaseClient {
role_id: string,
extra?: RequestInit,
): Promise<Output<void>> {
return this.delete<void>(`/api/v1/roles/${role_id}`, undefined, extra);
return this.delete(`/api/v1/roles/${role_id}`, undefined, extra);
}
/**
@ -2400,7 +2431,7 @@ export class Client extends BaseClient {
token: string,
extra?: RequestInit,
): Promise<Output<void>> {
return this.post<void>(
return this.post(
"/oauth/revoke",
{ client_id, client_secret, token },
extra,
@ -2534,7 +2565,7 @@ export class Client extends BaseClient {
*/
public searchAccount(
q: string,
options: Partial<{
options?: Partial<{
following: boolean;
limit: number;
max_id: string;
@ -2547,22 +2578,20 @@ export class Client extends BaseClient {
params.set("q", q);
if (options) {
if (options.following) {
params.set("following", "true");
}
if (options.limit) {
params.set("limit", options.limit.toString());
}
if (options.max_id) {
params.set("max_id", options.max_id);
}
if (options.resolve) {
params.set("resolve", "true");
}
if (options.since_id) {
params.set("since_id", options.since_id);
}
if (options?.following) {
params.set("following", "true");
}
if (options?.limit) {
params.set("limit", options.limit.toString());
}
if (options?.max_id) {
params.set("max_id", options.max_id);
}
if (options?.resolve) {
params.set("resolve", "true");
}
if (options?.since_id) {
params.set("since_id", options.since_id);
}
return this.get<z.infer<typeof Account>[]>(
@ -2639,7 +2668,7 @@ export class Client extends BaseClient {
role_id: string,
extra?: RequestInit,
): Promise<Output<void>> {
return this.delete<void>(
return this.delete(
`/api/v1/accounts/${account_id}/roles/${role_id}`,
undefined,
extra,
@ -2672,7 +2701,7 @@ export class Client extends BaseClient {
domain: string,
extra?: RequestInit,
): Promise<Output<void>> {
return this.delete<void>("/api/v1/domain_blocks", { domain }, extra);
return this.delete("/api/v1/domain_blocks", { domain }, extra);
}
/**

View file

@ -1,5 +1,6 @@
import { generateChallenge } from "@/challenges";
import { randomString } from "@/math";
import { Client as VersiaClient } from "@versia/client-ng";
import { Note, Token, User, db } from "@versia/kit/db";
import { Notes, Users } from "@versia/kit/tables";
import { solveChallenge } from "altcha-lib";
@ -26,6 +27,52 @@ export function fakeRequest(
);
}
/**
* Generate a client instance monkeypatched to use the test server
* instead of going through HTTP, and also doesn't throw on errors
* @param user User to automatically generate a token for
* @returns Client instance
*/
export const generateClient = async (
user?: User,
): Promise<
VersiaClient & {
[Symbol.asyncDispose](): Promise<void>;
}
> => {
const token = user
? await Token.insert({
accessToken: randomString(32, "hex"),
tokenType: "bearer",
userId: user.id,
applicationId: null,
code: randomString(32, "hex"),
scope: "read write follow push",
})
: null;
const client = new VersiaClient(
new URL(config.http.base_url),
token?.data.accessToken,
);
// biome-ignore lint/complexity/useLiteralKeys: Overriding private properties
client["fetch"] = (
input: RequestInfo | string | URL | Request,
init?: RequestInit,
): Promise<Response> => {
return Promise.resolve(app.fetch(new Request(input, init)));
};
// @ts-expect-error This is REAL monkeypatching done by REAL programmers, BITCH!
client[Symbol.asyncDispose] = async (): Promise<void> => {
await token?.delete();
};
return client as VersiaClient & {
[Symbol.asyncDispose](): Promise<void>;
};
};
export const deleteOldTestUsers = async (): Promise<void> => {
// Deletes all users that match the test username (test-<32 random characters>)
await db.delete(Users).where(like(Users.username, "test-%"));

View file

@ -40,5 +40,6 @@ export const applyToHono = (app: OpenAPIHono<HonoEnv>): void => {
});
serverAdapter.setBasePath("/admin/queues");
// @ts-ignore Causes infinite instantiation for some reason
app.route("/admin/queues", serverAdapter.registerPlugin());
};