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",
}),