test(api): Remove old tests and introduce new, better ones
Some checks failed
CodeQL Scan / Analyze (javascript-typescript) (push) Failing after 6s
Build Docker Images / lint (push) Successful in 50s
Build Docker Images / check (push) Successful in 1m24s
Build Docker Images / tests (push) Failing after 8s
Build Docker Images / build (server, Dockerfile, ${{ github.repository_owner }}/server) (push) Has been skipped
Build Docker Images / build (worker, Worker.Dockerfile, ${{ github.repository_owner }}/worker) (push) Has been skipped
Deploy Docs to GitHub Pages / build (push) Failing after 15s
Mirror to Codeberg / Mirror (push) Failing after 0s
Deploy Docs to GitHub Pages / Deploy (push) Has been skipped
Nix Build / check (push) Failing after 33m5s

This commit is contained in:
Jesse Wierzbinski 2025-03-23 03:34:17 +01:00
parent f1ef85b314
commit ec506241f0
No known key found for this signature in database
23 changed files with 819 additions and 1001 deletions

View file

@ -0,0 +1,64 @@
import { afterAll, describe, expect, test } from "bun:test";
import { generateClient, getTestUsers } from "~/tests/utils";
const { users, deleteUsers } = await getTestUsers(2);
afterAll(async () => {
await deleteUsers();
});
describe("/api/v1/accounts/:id/note", () => {
test("should return 401 if not authenticated", async () => {
await using client = await generateClient();
const { ok, raw } = await client.updateAccountNote(users[1].id, "test");
expect(ok).toBe(false);
expect(raw.status).toBe(401);
});
test("should return 404 if user not found", async () => {
await using client = await generateClient(users[0]);
const { ok, raw } = await client.updateAccountNote(
"00000000-0000-0000-0000-000000000000",
"test",
);
expect(ok).toBe(false);
expect(raw.status).toBe(404);
});
test("should update note", async () => {
await using client = await generateClient(users[0]);
const { ok, data } = await client.updateAccountNote(
users[1].id,
"test",
);
expect(ok).toBe(true);
expect(data.note).toBe("test");
});
test("should return 200 if note is null", async () => {
await using client = await generateClient(users[0]);
const { ok, data } = await client.updateAccountNote(users[1].id, null);
expect(ok).toBe(true);
expect(data.note).toBe("");
});
test("should return 422 if note is too long", async () => {
await using client = await generateClient(users[0]);
const { ok, raw } = await client.updateAccountNote(
users[1].id,
"a".repeat(10_000),
);
expect(ok).toBe(false);
expect(raw.status).toBe(422);
});
});

View file

@ -0,0 +1,48 @@
import { afterAll, describe, expect, test } from "bun:test";
import { generateClient, getTestUsers } from "~/tests/utils";
const { users, deleteUsers } = await getTestUsers(2);
afterAll(async () => {
await deleteUsers();
});
describe("/api/v1/accounts/:id/pin", () => {
test("should return 401 if not authenticated", async () => {
await using client = await generateClient();
const { ok, raw } = await client.pinAccount(users[1].id);
expect(ok).toBe(false);
expect(raw.status).toBe(401);
});
test("should return 404 if user not found", async () => {
await using client = await generateClient(users[0]);
const { ok, raw } = await client.pinAccount(
"00000000-0000-0000-0000-000000000000",
);
expect(ok).toBe(false);
expect(raw.status).toBe(404);
});
test("should pin account", async () => {
await using client = await generateClient(users[0]);
const { ok, data } = await client.pinAccount(users[1].id);
expect(ok).toBe(true);
expect(data.endorsed).toBe(true);
});
test("should return 200 if account already pinned", async () => {
await using client = await generateClient(users[0]);
const { ok, data } = await client.pinAccount(users[1].id);
expect(ok).toBe(true);
expect(data.endorsed).toBe(true);
});
});

View file

@ -0,0 +1,59 @@
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import { generateClient, getTestUsers } from "~/tests/utils";
const { users, deleteUsers } = await getTestUsers(2);
afterAll(async () => {
await deleteUsers();
});
beforeAll(async () => {
// Make users[1] follow users[0]
await using client = await generateClient(users[1]);
const { ok } = await client.followAccount(users[0].id);
expect(ok).toBe(true);
});
describe("/api/v1/accounts/:id/remove_from_followers", () => {
test("should return 401 when not authenticated", async () => {
await using client = await generateClient();
const { ok, raw } = await client.removeFromFollowers(users[1].id);
expect(ok).toBe(false);
expect(raw.status).toBe(401);
});
test("should return 404 when target account doesn't exist", async () => {
await using client = await generateClient(users[0]);
const { ok, raw } = await client.removeFromFollowers("non-existent-id");
expect(ok).toBe(false);
expect(raw.status).toBe(422);
});
test("should remove follower and return relationship", async () => {
await using client = await generateClient(users[0]);
const { ok, data } = await client.removeFromFollowers(users[1].id);
expect(ok).toBe(true);
expect(data.id).toBe(users[1].id);
expect(data.following).toBe(false);
expect(data.followed_by).toBe(false);
});
test("should handle case when user is not following", async () => {
await using client = await generateClient(users[0]);
const { ok, data } = await client.removeFromFollowers(users[1].id);
expect(ok).toBe(true);
expect(data.id).toBe(users[1].id);
expect(data.following).toBe(false);
expect(data.followed_by).toBe(false);
});
});

View file

@ -0,0 +1,55 @@
import { afterAll, describe, expect, test } from "bun:test";
import { generateClient, getTestUsers } from "~/tests/utils";
const { users, deleteUsers } = await getTestUsers(2);
afterAll(async () => {
await deleteUsers();
});
describe("/api/v1/accounts/:id/unblock", () => {
test("should return 401 if not authenticated", async () => {
await using client = await generateClient();
const { ok, raw } = await client.unblockAccount(users[1].id);
expect(ok).toBe(false);
expect(raw.status).toBe(401);
});
test("should return 404 if user not found", async () => {
await using client = await generateClient(users[0]);
const { ok, raw } = await client.unblockAccount(
"00000000-0000-0000-0000-000000000000",
);
expect(ok).toBe(false);
expect(raw.status).toBe(404);
});
test("should unblock user", async () => {
await using client = await generateClient(users[0]);
const { ok, data } = await client.blockAccount(users[1].id);
expect(ok).toBe(true);
expect(data.blocking).toBe(true);
const { ok: ok2, data: data2 } = await client.unblockAccount(
users[1].id,
);
expect(ok2).toBe(true);
expect(data2.blocking).toBe(false);
});
test("should return 200 if user already unblocked", async () => {
await using client = await generateClient(users[0]);
const { ok, data } = await client.unblockAccount(users[1].id);
expect(ok).toBe(true);
expect(data.blocking).toBe(false);
});
});

View file

@ -0,0 +1,53 @@
import { afterAll, describe, expect, test } from "bun:test";
import { generateClient, getTestUsers } from "~/tests/utils";
const { users, deleteUsers } = await getTestUsers(2);
afterAll(async () => {
await deleteUsers();
});
describe("/api/v1/accounts/:id/unpin", () => {
test("should return 401 if not authenticated", async () => {
await using client = await generateClient();
const { ok, raw } = await client.unpinAccount(users[1].id);
expect(ok).toBe(false);
expect(raw.status).toBe(401);
});
test("should return 404 if user not found", async () => {
await using client = await generateClient(users[0]);
const { ok, raw } = await client.unpinAccount(
"00000000-0000-0000-0000-000000000000",
);
expect(ok).toBe(false);
expect(raw.status).toBe(404);
});
test("should unpin account", async () => {
await using client = await generateClient(users[0]);
const { ok, data } = await client.pinAccount(users[1].id);
expect(ok).toBe(true);
expect(data.endorsed).toBe(true);
const { ok: ok2, data: data2 } = await client.unpinAccount(users[1].id);
expect(ok2).toBe(true);
expect(data2.endorsed).toBe(false);
});
test("should return 200 if account already unpinned", async () => {
await using client = await generateClient(users[0]);
const { ok, data } = await client.unpinAccount(users[1].id);
expect(ok).toBe(true);
expect(data.endorsed).toBe(false);
});
});

View file

@ -4,7 +4,7 @@ import { sanitizedHtmlStrip } from "@/sanitization";
import { createRoute, z } from "@hono/zod-openapi";
import { Account as AccountSchema, zBoolean } from "@versia/client/schemas";
import { RolePermission } from "@versia/client/schemas";
import { Emoji, User } from "@versia/kit/db";
import { Emoji, Media, User } from "@versia/kit/db";
import { Users } from "@versia/kit/tables";
import { and, eq, isNull } from "drizzle-orm";
import { ApiError } from "~/classes/errors/api-error";
@ -229,17 +229,29 @@ export default apiRoute((app) =>
if (avatar) {
if (avatar instanceof File) {
await user.avatar?.updateFromFile(avatar);
if (user.avatar) {
await user.avatar.updateFromFile(avatar);
} else {
user.avatar = await Media.fromFile(avatar);
}
} else if (user.avatar) {
await user.avatar.updateFromUrl(avatar);
} else {
await user.avatar?.updateFromUrl(avatar);
user.avatar = await Media.fromUrl(avatar);
}
}
if (header) {
if (header instanceof File) {
await user.header?.updateFromFile(header);
if (user.header) {
await user.header.updateFromFile(header);
} else {
user.header = await Media.fromFile(header);
}
} else if (user.header) {
await user.header.updateFromUrl(header);
} else {
await user.header?.updateFromUrl(header);
user.header = await Media.fromUrl(header);
}
}
@ -333,7 +345,9 @@ export default apiRoute((app) =>
username: self.username,
note: self.note,
avatar: self.avatar,
avatarId: user.avatar?.id,
header: self.header,
headerId: user.header?.id,
fields: self.fields,
isLocked: self.isLocked,
isBot: self.isBot,

View file

@ -0,0 +1,60 @@
import { afterAll, describe, expect, test } from "bun:test";
import { config } from "~/config";
import { generateClient, getTestUsers } from "~/tests/utils";
const { users, deleteUsers } = await getTestUsers(1);
afterAll(async () => {
await deleteUsers();
});
describe("/api/v1/accounts/verify_credentials", () => {
describe("Authentication", () => {
test("should return 401 when not authenticated", async () => {
await using client = await generateClient();
const { ok, raw } = await client.verifyAccountCredentials();
expect(ok).toBe(false);
expect(raw.status).toBe(401);
});
test("should return user data when authenticated", async () => {
await using client = await generateClient(users[0]);
const { ok, data } = await client.verifyAccountCredentials();
expect(ok).toBe(true);
expect(data.id).toBe(users[0].id);
expect(data.username).toBe(users[0].data.username);
expect(data.acct).toBe(users[0].data.username);
expect(data.display_name).toBe(users[0].data.displayName);
expect(data.note).toBe(users[0].data.note);
expect(data.url).toBe(
new URL(
`/@${users[0].data.username}`,
config.http.base_url,
).toString(),
);
expect(data.avatar).toBeDefined();
expect(data.avatar_static).toBeDefined();
expect(data.header).toBeDefined();
expect(data.header_static).toBeDefined();
expect(data.locked).toBe(users[0].data.isLocked);
expect(data.bot).toBe(users[0].data.isBot);
expect(data.group).toBe(false);
expect(data.discoverable).toBe(users[0].data.isDiscoverable);
expect(data.noindex).toBe(false);
expect(data.moved).toBeNull();
expect(data.suspended).toBe(false);
expect(data.limited).toBe(false);
expect(data.created_at).toBe(
new Date(users[0].data.createdAt).toISOString(),
);
expect(data.last_status_at).toBeNull();
expect(data.statuses_count).toBe(0);
expect(data.followers_count).toBe(0);
expect(data.following_count).toBe(0);
});
});
});

View file

@ -0,0 +1,52 @@
import { afterAll, describe, expect, test } from "bun:test";
import { generateClient, getTestUsers } from "~/tests/utils";
const { users, deleteUsers } = await getTestUsers(3);
afterAll(async () => {
await deleteUsers();
});
describe("/api/v1/blocks", () => {
test("should return 401 when not authenticated", async () => {
await using client = await generateClient();
const { ok, raw } = await client.getBlocks();
expect(ok).toBe(false);
expect(raw.status).toBe(401);
});
test("should return empty array when no blocks", async () => {
await using client = await generateClient(users[0]);
const { ok, data } = await client.getBlocks();
expect(ok).toBe(true);
expect(data).toBeArrayOfSize(0);
});
test("should return blocked users", async () => {
await using client = await generateClient(users[0]);
// Block users[1] and users[2]
await client.blockAccount(users[1].id);
await client.blockAccount(users[2].id);
const { ok, data } = await client.getBlocks();
expect(ok).toBe(true);
expect(data).toBeArrayOfSize(2);
expect(data.map((u) => u.id)).toContain(users[1].id);
expect(data.map((u) => u.id)).toContain(users[2].id);
});
test("should respect limit parameter", async () => {
await using client = await generateClient(users[0]);
const { ok, data } = await client.getBlocks({ limit: 1 });
expect(ok).toBe(true);
expect(data).toBeArrayOfSize(1);
});
});

View file

@ -0,0 +1,43 @@
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import { generateClient, getTestUsers } from "~/tests/utils";
const { users, deleteUsers } = await getTestUsers(2);
let avatarUrl: string;
beforeAll(async () => {
await using client = await generateClient(users[0]);
const { ok, data } = await client.updateCredentials({
avatar: new URL("https://placehold.co/100x100"),
});
expect(ok).toBe(true);
avatarUrl = data.avatar;
});
afterAll(async () => {
await deleteUsers();
});
describe("POST /api/v1/profile/avatar", () => {
test("should return 401 if not authenticated", async () => {
await using client = await generateClient();
const { ok, raw } = await client.deleteAvatar();
expect(ok).toBe(false);
expect(raw.status).toBe(401);
});
test("should delete avatar", async () => {
await using client = await generateClient(users[0]);
const { ok, data } = await client.deleteAvatar();
expect(ok).toBe(true);
// Avatars are defaulted to a placeholder
expect(data.avatar).not.toBe(avatarUrl);
});
});

View file

@ -38,7 +38,8 @@ export default apiRoute((app) =>
app.openapi(route, async (context) => {
const { user } = context.get("auth");
await user.header?.delete();
await user.avatar?.delete();
await user.reload();
return context.json(user.toApi(true), 200);
}),

View file

@ -0,0 +1,41 @@
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import { generateClient, getTestUsers } from "~/tests/utils";
const { users, deleteUsers } = await getTestUsers(3);
let headerUrl: string;
beforeAll(async () => {
await using client = await generateClient(users[0]);
const { ok, data } = await client.updateCredentials({
header: new URL("https://placehold.co/100x100"),
});
expect(ok).toBe(true);
headerUrl = data.header;
});
afterAll(async () => {
await deleteUsers();
});
describe("POST /api/v1/profile/header", () => {
test("should return 401 if not authenticated", async () => {
await using client = await generateClient();
const { ok, raw } = await client.deleteHeader();
expect(ok).toBe(false);
expect(raw.status).toBe(401);
});
test("should delete header", async () => {
await using client = await generateClient(users[0]);
const { ok, data } = await client.deleteHeader();
expect(ok).toBe(true);
expect(data.header).not.toBe(headerUrl);
});
});

View file

@ -38,7 +38,7 @@ export default apiRoute((app) =>
const { user } = context.get("auth");
await user.header?.delete();
await user.reload();
return context.json(user.toApi(true), 200);
}),
);

View file

@ -0,0 +1,83 @@
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import { generateClient, getTestStatuses, getTestUsers } from "~/tests/utils";
const { users, deleteUsers } = await getTestUsers(3);
const statuses = await getTestStatuses(1, users[0]);
let replyId: string;
let replyToReplyId: string;
beforeAll(async () => {
await using client = await generateClient(users[0]);
const { ok, data } = await client.postStatus("Test reply", {
in_reply_to_id: statuses[0].id,
});
expect(ok).toBe(true);
replyId = data.id;
const { ok: ok2, data: data2 } = await client.postStatus(
"Test reply to reply",
{
in_reply_to_id: replyId,
},
);
expect(ok2).toBe(true);
replyToReplyId = data2.id;
});
afterAll(async () => {
await deleteUsers();
});
describe("GET /api/v1/statuses/:id/context", () => {
test("should return 404 if status is not found", async () => {
await using client = await generateClient(users[0]);
const { ok, raw } = await client.getStatusContext(
"00000000-0000-0000-0000-000000000000",
);
expect(ok).toBe(false);
expect(raw.status).toBe(404);
});
test("should return context of status", async () => {
await using client = await generateClient(users[0]);
const { ok, data } = await client.getStatusContext(statuses[0].id);
expect(ok).toBe(true);
expect(data.ancestors).toBeArrayOfSize(0);
expect(data.descendants).toBeArrayOfSize(2);
expect(data.descendants[0].id).toBe(replyId);
expect(data.descendants[1].id).toBe(replyToReplyId);
});
test("should return context of reply", async () => {
await using client = await generateClient(users[0]);
const { ok, data } = await client.getStatusContext(replyId);
expect(ok).toBe(true);
expect(data.ancestors).toBeArrayOfSize(1);
expect(data.ancestors[0].id).toBe(statuses[0].id);
expect(data.descendants).toBeArrayOfSize(1);
expect(data.descendants[0].id).toBe(replyToReplyId);
});
test("should return context of reply to reply", async () => {
await using client = await generateClient(users[0]);
const { ok, data } = await client.getStatusContext(replyToReplyId);
expect(ok).toBe(true);
expect(data.ancestors).toBeArrayOfSize(2);
expect(data.ancestors[0].id).toBe(statuses[0].id);
expect(data.ancestors[1].id).toBe(replyId);
expect(data.descendants).toBeArrayOfSize(0);
});
});

View file

@ -0,0 +1,57 @@
import { afterAll, describe, expect, test } from "bun:test";
import { generateClient, getTestStatuses, getTestUsers } from "~/tests/utils";
const { users, deleteUsers } = await getTestUsers(2);
const statuses = await getTestStatuses(1, users[0]);
afterAll(async () => {
await deleteUsers();
});
describe("GET /api/v1/statuses/:id", () => {
test("should return status", async () => {
await using client = await generateClient(users[0]);
const { ok, data } = await client.getStatus(statuses[0].id);
expect(ok).toBe(true);
expect(data.id).toBe(statuses[0].id);
});
test("should return 404 if status is not found", async () => {
await using client = await generateClient(users[0]);
const { ok, raw } = await client.getStatus(
"00000000-0000-0000-0000-000000000000",
);
expect(ok).toBe(false);
expect(raw.status).toBe(404);
});
test("should return 401 when trying to delete status that is not yours", async () => {
await using client = await generateClient(users[1]);
const { ok, raw } = await client.deleteStatus(statuses[0].id);
expect(ok).toBe(false);
expect(raw.status).toBe(401);
});
test("should delete status", async () => {
await using client = await generateClient(users[0]);
const { ok } = await client.deleteStatus(statuses[0].id);
expect(ok).toBe(true);
});
test("should return 404 if status is deleted", async () => {
await using client = await generateClient(users[0]);
const { ok, raw } = await client.getStatus(statuses[0].id);
expect(ok).toBe(false);
expect(raw.status).toBe(404);
});
});

View file

@ -0,0 +1,49 @@
import { afterAll, describe, expect, test } from "bun:test";
import { generateClient, getTestStatuses, getTestUsers } from "~/tests/utils";
const { users, deleteUsers } = await getTestUsers(2);
const statuses = await getTestStatuses(1, users[0]);
afterAll(async () => {
await deleteUsers();
});
describe("POST /api/v1/statuses/:id/reblog", () => {
test("should return 401 if not authenticated", async () => {
await using client = await generateClient();
const { ok, raw } = await client.reblogStatus(statuses[0].id);
expect(ok).toBe(false);
expect(raw.status).toBe(401);
});
test("should return 404 if status is not found", async () => {
await using client = await generateClient(users[0]);
const { ok, raw } = await client.reblogStatus(
"00000000-0000-0000-0000-000000000000",
);
expect(ok).toBe(false);
expect(raw.status).toBe(404);
});
test("should reblog status", async () => {
await using client = await generateClient(users[0]);
const { ok, data } = await client.reblogStatus(statuses[0].id);
expect(ok).toBe(true);
expect(data.reblog?.id).toBe(statuses[0].id);
});
test("should not error when status is already reblogged", async () => {
await using client = await generateClient(users[0]);
const { ok, data } = await client.reblogStatus(statuses[0].id);
expect(ok).toBe(true);
expect(data.reblog?.id).toBe(statuses[0].id);
});
});

View file

@ -0,0 +1,51 @@
import { afterAll, describe, expect, test } from "bun:test";
import { generateClient, getTestStatuses, getTestUsers } from "~/tests/utils";
const { users, deleteUsers } = await getTestUsers(2);
const statuses = await getTestStatuses(1, users[0]);
afterAll(async () => {
await deleteUsers();
});
describe("POST /api/v1/statuses/:id/unreblog", () => {
test("should return 401 if not authenticated", async () => {
await using client = await generateClient();
const { ok, raw } = await client.unreblogStatus(statuses[0].id);
expect(ok).toBe(false);
expect(raw.status).toBe(401);
});
test("should return 404 if status is not found", async () => {
await using client = await generateClient(users[0]);
const { ok, raw } = await client.unreblogStatus(
"00000000-0000-0000-0000-000000000000",
);
expect(ok).toBe(false);
expect(raw.status).toBe(404);
});
test("should unreblog status", async () => {
await using client = await generateClient(users[0]);
const { ok, data } = await client.unreblogStatus(statuses[0].id);
expect(ok).toBe(true);
expect(data.id).toBe(statuses[0].id);
expect(data.reblogged).toBe(false);
expect(data.reblog).toBeNull();
});
test("should not error when status is not reblogged", async () => {
await using client = await generateClient(users[0]);
const { ok, data } = await client.unreblogStatus(statuses[0].id);
expect(ok).toBe(true);
expect(data.reblog).toBeNull();
});
});