refactor: ♻️ Refactor tests to use a simpler syntax

This commit is contained in:
Jesse Wierzbinski 2024-08-27 21:25:26 +02:00
parent 6ed1bd747f
commit 47c666894c
No known key found for this signature in database
49 changed files with 1870 additions and 2973 deletions

View file

@ -4,7 +4,7 @@ import { eq } from "drizzle-orm";
import { db } from "~/drizzle/db";
import { Applications } from "~/drizzle/schema";
import { config } from "~/packages/config-manager";
import { getTestUsers, sendTestRequest } from "~/tests/utils";
import { fakeRequest, getTestUsers } from "~/tests/utils";
import { meta } from "./index";
const { users, deleteUsers, passwords } = await getTestUsers(1);
@ -36,17 +36,12 @@ describe(meta.route, () => {
formData.append("identifier", users[0]?.data.email ?? "");
formData.append("password", passwords[0]);
const response = await sendTestRequest(
new Request(
new URL(
`/api/auth/login?client_id=${application.clientId}&redirect_uri=https://example.com&response_type=code&scope=read+write`,
config.http.base_url,
),
{
method: "POST",
body: formData,
},
),
const response = await fakeRequest(
`/api/auth/login?client_id=${application.clientId}&redirect_uri=https://example.com&response_type=code&scope=read+write`,
{
method: "POST",
body: formData,
},
);
expect(response.status).toBe(302);
@ -75,17 +70,12 @@ describe(meta.route, () => {
formData.append("identifier", users[0]?.data.username ?? "");
formData.append("password", passwords[0]);
const response = await sendTestRequest(
new Request(
new URL(
`/api/auth/login?client_id=${application.clientId}&redirect_uri=https://example.com&response_type=code&scope=read+write`,
config.http.base_url,
),
{
method: "POST",
body: formData,
},
),
const response = await fakeRequest(
`/api/auth/login?client_id=${application.clientId}&redirect_uri=https://example.com&response_type=code&scope=read+write`,
{
method: "POST",
body: formData,
},
);
expect(response.status).toBe(302);
@ -114,17 +104,12 @@ describe(meta.route, () => {
formData.append("identifier", users[0]?.data.email ?? "");
formData.append("password", passwords[0]);
const response = await sendTestRequest(
new Request(
new URL(
`/api/auth/login?client_id=${application.clientId}&redirect_uri=https://example.com&response_type=code&scope=read+write&state=abc`,
config.http.base_url,
),
{
method: "POST",
body: formData,
},
),
const response = await fakeRequest(
`/api/auth/login?client_id=${application.clientId}&redirect_uri=https://example.com&response_type=code&scope=read+write&state=abc`,
{
method: "POST",
body: formData,
},
);
expect(response.status).toBe(302);
@ -156,17 +141,13 @@ describe(meta.route, () => {
formData.append("identifier", "ababa@gmail.com");
formData.append("password", "password");
const response = await sendTestRequest(
new Request(
new URL(
`/api/auth/login?client_id=${application.clientId}&redirect_uri=https://example.com&response_type=code&scope=read+write`,
config.http.base_url,
),
{
method: "POST",
body: formData,
},
),
const response = await fakeRequest(
`/api/auth/login?client_id=${application.clientId}&redirect_uri=https://example.com&response_type=code&scope=read+write`,
{
method: "POST",
body: formData,
},
);
expect(response.status).toBe(302);
@ -193,17 +174,12 @@ describe(meta.route, () => {
formData.append("identifier", "ababa");
formData.append("password", "password");
const response = await sendTestRequest(
new Request(
new URL(
`/api/auth/login?client_id=${application.clientId}&redirect_uri=https://example.com&response_type=code&scope=read+write`,
config.http.base_url,
),
{
method: "POST",
body: formData,
},
),
const response = await fakeRequest(
`/api/auth/login?client_id=${application.clientId}&redirect_uri=https://example.com&response_type=code&scope=read+write`,
{
method: "POST",
body: formData,
},
);
expect(response.status).toBe(302);
@ -230,17 +206,12 @@ describe(meta.route, () => {
formData.append("identifier", users[0]?.data.email ?? "");
formData.append("password", "password");
const response = await sendTestRequest(
new Request(
new URL(
`/api/auth/login?client_id=${application.clientId}&redirect_uri=https://example.com&response_type=code&scope=read+write`,
config.http.base_url,
),
{
method: "POST",
body: formData,
},
),
const response = await fakeRequest(
`/api/auth/login?client_id=${application.clientId}&redirect_uri=https://example.com&response_type=code&scope=read+write`,
{
method: "POST",
body: formData,
},
);
expect(response.status).toBe(302);

View file

@ -4,7 +4,7 @@ import { eq } from "drizzle-orm";
import { db } from "~/drizzle/db";
import { Applications } from "~/drizzle/schema";
import { config } from "~/packages/config-manager";
import { getTestUsers, sendTestRequest } from "~/tests/utils";
import { fakeRequest, getTestUsers } from "~/tests/utils";
import { meta } from "./index";
const { users, deleteUsers, passwords } = await getTestUsers(1);
@ -38,17 +38,13 @@ describe(meta.route, () => {
formData.append("identifier", users[0]?.data.username ?? "");
formData.append("password", passwords[0]);
const response = await sendTestRequest(
new Request(
new URL(
`/api/auth/login?client_id=${application.clientId}&redirect_uri=https://example.com&response_type=code&scope=read+write`,
config.http.base_url,
),
{
method: "POST",
body: formData,
},
),
const response = await fakeRequest(
`/api/auth/login?client_id=${application.clientId}&redirect_uri=https://example.com&response_type=code&scope=read+write`,
{
method: "POST",
body: formData,
},
);
expect(response.status).toBe(302);
@ -65,17 +61,12 @@ describe(meta.route, () => {
formData.append("identifier", users[0]?.data.username ?? "");
formData.append("password", passwords[0]);
const response = await sendTestRequest(
new Request(
new URL(
`/api/auth/login?client_id=${application.clientId}&redirect_uri=https://example.com&response_type=code&scope=read+write`,
config.http.base_url,
),
{
method: "POST",
body: formData,
},
),
const response = await fakeRequest(
`/api/auth/login?client_id=${application.clientId}&redirect_uri=https://example.com&response_type=code&scope=read+write`,
{
method: "POST",
body: formData,
},
);
expect(response.status).toBe(302);
@ -96,12 +87,10 @@ describe(meta.route, () => {
formData.append("password", newPassword);
formData.append("password2", newPassword);
const response = await sendTestRequest(
new Request(new URL("/api/auth/reset", config.http.base_url), {
method: "POST",
body: formData,
}),
);
const response = await fakeRequest("/api/auth/reset", {
method: "POST",
body: formData,
});
expect(response.status).toBe(302);
expect(response.headers.get("location")).toBeDefined();
@ -111,17 +100,12 @@ describe(meta.route, () => {
loginFormData.append("identifier", users[0]?.data.username ?? "");
loginFormData.append("password", newPassword);
const loginResponse = await sendTestRequest(
new Request(
new URL(
`/api/auth/login?client_id=${application.clientId}&redirect_uri=https://example.com&response_type=code&scope=read+write`,
config.http.base_url,
),
{
method: "POST",
body: loginFormData,
},
),
const loginResponse = await fakeRequest(
`/api/auth/login?client_id=${application.clientId}&redirect_uri=https://example.com&response_type=code&scope=read+write`,
{
method: "POST",
body: loginFormData,
},
);
expect(loginResponse.status).toBe(302);

View file

@ -1,7 +1,6 @@
import { afterAll, describe, expect, test } from "bun:test";
import type { Relationship as ApiRelationship } from "@versia/client/types";
import { config } from "~/packages/config-manager/index";
import { getTestUsers, sendTestRequest } from "~/tests/utils";
import { fakeRequest, getTestUsers } from "~/tests/utils";
import { meta } from "./block";
const { users, tokens, deleteUsers } = await getTestUsers(2);
@ -13,55 +12,37 @@ afterAll(async () => {
// /api/v1/accounts/:id/block
describe(meta.route, () => {
test("should return 401 if not authenticated", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", users[1].id),
config.http.base_url,
),
{
method: "POST",
},
),
const response = await fakeRequest(
meta.route.replace(":id", users[1].id),
{
method: "POST",
},
);
expect(response.status).toBe(401);
});
test("should return 404 if user not found", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(
":id",
"00000000-0000-0000-0000-000000000000",
),
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response = await fakeRequest(
meta.route.replace(":id", "00000000-0000-0000-0000-000000000000"),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response.status).toBe(404);
});
test("should block user", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", users[1].id),
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response = await fakeRequest(
meta.route.replace(":id", users[1].id),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response.status).toBe(200);
@ -70,19 +51,14 @@ describe(meta.route, () => {
});
test("should return 200 if user already blocked", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", users[1].id),
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response = await fakeRequest(
meta.route.replace(":id", users[1].id),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response.status).toBe(200);

View file

@ -1,7 +1,6 @@
import { afterAll, describe, expect, test } from "bun:test";
import type { Relationship as ApiRelationship } from "@versia/client/types";
import { config } from "~/packages/config-manager/index";
import { getTestUsers, sendTestRequest } from "~/tests/utils";
import { fakeRequest, getTestUsers } from "~/tests/utils";
import { meta } from "./follow";
const { users, tokens, deleteUsers } = await getTestUsers(2);
@ -13,63 +12,45 @@ afterAll(async () => {
// /api/v1/accounts/:id/follow
describe(meta.route, () => {
test("should return 401 if not authenticated", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", users[1].id),
config.http.base_url,
),
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({}),
const response = await fakeRequest(
meta.route.replace(":id", users[1].id),
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
),
body: JSON.stringify({}),
},
);
expect(response.status).toBe(401);
});
test("should return 404 if user not found", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(
":id",
"00000000-0000-0000-0000-000000000000",
),
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
const response = await fakeRequest(
meta.route.replace(":id", "00000000-0000-0000-0000-000000000000"),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
"Content-Type": "application/json",
},
),
body: JSON.stringify({}),
},
);
expect(response.status).toBe(404);
});
test("should follow user", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", users[1].id),
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
const response = await fakeRequest(
meta.route.replace(":id", users[1].id),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
"Content-Type": "application/json",
},
),
body: JSON.stringify({}),
},
);
expect(response.status).toBe(200);
@ -78,21 +59,16 @@ describe(meta.route, () => {
});
test("should return 200 if user already followed", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", users[1].id),
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
const response = await fakeRequest(
meta.route.replace(":id", users[1].id),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
"Content-Type": "application/json",
},
),
body: JSON.stringify({}),
},
);
expect(response.status).toBe(200);

View file

@ -1,7 +1,6 @@
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import type { Account as ApiAccount } from "@versia/client/types";
import { config } from "~/packages/config-manager/index";
import { getTestUsers, sendTestRequest } from "~/tests/utils";
import { fakeRequest, getTestUsers } from "~/tests/utils";
import { meta } from "./followers";
const { users, tokens, deleteUsers } = await getTestUsers(5);
@ -12,21 +11,16 @@ afterAll(async () => {
beforeAll(async () => {
// Follow user
const response = await sendTestRequest(
new Request(
new URL(
`/api/v1/accounts/${users[1].id}/follow`,
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
const response = await fakeRequest(
`/api/v1/accounts/${users[1].id}/follow`,
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
"Content-Type": "application/json",
},
),
body: JSON.stringify({}),
},
);
expect(response.status).toBe(200);
@ -35,18 +29,13 @@ beforeAll(async () => {
// /api/v1/accounts/:id/followers
describe(meta.route, () => {
test("should return 200 with followers", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", users[1].id),
config.http.base_url,
),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response = await fakeRequest(
meta.route.replace(":id", users[1].id),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response.status).toBe(200);
@ -60,35 +49,25 @@ describe(meta.route, () => {
test("should return no followers after unfollowing", async () => {
// Unfollow user
const response = await sendTestRequest(
new Request(
new URL(
`/api/v1/accounts/${users[1].id}/unfollow`,
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response = await fakeRequest(
`/api/v1/accounts/${users[1].id}/unfollow`,
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response.status).toBe(200);
const response2 = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", users[1].id),
config.http.base_url,
),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response2 = await fakeRequest(
meta.route.replace(":id", users[1].id),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response2.status).toBe(200);

View file

@ -1,7 +1,6 @@
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import type { Account as ApiAccount } from "@versia/client/types";
import { config } from "~/packages/config-manager/index";
import { getTestUsers, sendTestRequest } from "~/tests/utils";
import { fakeRequest, getTestUsers } from "~/tests/utils";
import { meta } from "./following";
const { users, tokens, deleteUsers } = await getTestUsers(5);
@ -12,21 +11,16 @@ afterAll(async () => {
beforeAll(async () => {
// Follow user
const response = await sendTestRequest(
new Request(
new URL(
`/api/v1/accounts/${users[1].id}/follow`,
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
const response = await fakeRequest(
`/api/v1/accounts/${users[1].id}/follow`,
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
"Content-Type": "application/json",
},
),
body: JSON.stringify({}),
},
);
expect(response.status).toBe(200);
@ -35,18 +29,13 @@ beforeAll(async () => {
// /api/v1/accounts/:id/following
describe(meta.route, () => {
test("should return 200 with following", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", users[0].id),
config.http.base_url,
),
{
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
const response = await fakeRequest(
meta.route.replace(":id", users[0].id),
{
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
),
},
);
expect(response.status).toBe(200);
@ -60,35 +49,25 @@ describe(meta.route, () => {
test("should return no following after unfollowing", async () => {
// Unfollow user
const response = await sendTestRequest(
new Request(
new URL(
`/api/v1/accounts/${users[1].id}/unfollow`,
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response = await fakeRequest(
`/api/v1/accounts/${users[1].id}/unfollow`,
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response.status).toBe(200);
const response2 = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", users[0].id),
config.http.base_url,
),
{
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
const response2 = await fakeRequest(
meta.route.replace(":id", users[0].id),
{
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
),
},
);
expect(response2.status).toBe(200);

View file

@ -1,7 +1,6 @@
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import type { Account as ApiAccount } from "@versia/client/types";
import { config } from "~/packages/config-manager/index";
import { getTestStatuses, getTestUsers, sendTestRequest } from "~/tests/utils";
import { fakeRequest, getTestStatuses, getTestUsers } from "~/tests/utils";
import { meta } from "./index";
const { users, tokens, deleteUsers } = await getTestUsers(5);
@ -13,47 +12,29 @@ afterAll(async () => {
beforeAll(async () => {
for (const status of timeline) {
await sendTestRequest(
new Request(
new URL(
`/api/v1/statuses/${status.id}/favourite`,
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
},
),
);
await fakeRequest(`/api/v1/statuses/${status.id}/favourite`, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
});
}
});
// /api/v1/accounts/:id
describe(meta.route, () => {
test("should return 404 if ID is invalid", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", "invalid"),
config.http.base_url,
),
),
const response = await fakeRequest(
meta.route.replace(":id", "invalid"),
);
expect(response.status).toBe(422);
});
test("should return user", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", users[0].id),
config.http.base_url,
),
),
const response = await fakeRequest(
meta.route.replace(":id", users[0].id),
);
expect(response.status).toBe(200);

View file

@ -1,7 +1,6 @@
import { afterAll, describe, expect, test } from "bun:test";
import type { Relationship as ApiRelationship } from "@versia/client/types";
import { config } from "~/packages/config-manager/index";
import { getTestUsers, sendTestRequest } from "~/tests/utils";
import { fakeRequest, getTestUsers } from "~/tests/utils";
import { meta } from "./mute";
const { users, tokens, deleteUsers } = await getTestUsers(2);
@ -13,63 +12,45 @@ afterAll(async () => {
// /api/v1/accounts/:id/mute
describe(meta.route, () => {
test("should return 401 if not authenticated", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", users[1].id),
config.http.base_url,
),
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({}),
const response = await fakeRequest(
meta.route.replace(":id", users[1].id),
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
),
body: JSON.stringify({}),
},
);
expect(response.status).toBe(401);
});
test("should return 404 if user not found", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(
":id",
"00000000-0000-0000-0000-000000000000",
),
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
const response = await fakeRequest(
meta.route.replace(":id", "00000000-0000-0000-0000-000000000000"),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
"Content-Type": "application/json",
},
),
body: JSON.stringify({}),
},
);
expect(response.status).toBe(404);
});
test("should mute user", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", users[1].id),
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
const response = await fakeRequest(
meta.route.replace(":id", users[1].id),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
"Content-Type": "application/json",
},
),
body: JSON.stringify({}),
},
);
expect(response.status).toBe(200);
@ -78,21 +59,16 @@ describe(meta.route, () => {
});
test("should return 200 if user already muted", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", users[1].id),
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
const response = await fakeRequest(
meta.route.replace(":id", users[1].id),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
"Content-Type": "application/json",
},
),
body: JSON.stringify({}),
},
);
expect(response.status).toBe(200);

View file

@ -1,7 +1,6 @@
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import type { Status as ApiStatus } from "@versia/client/types";
import { config } from "~/packages/config-manager/index";
import { getTestStatuses, getTestUsers, sendTestRequest } from "~/tests/utils";
import { fakeRequest, getTestStatuses, getTestUsers } from "~/tests/utils";
import { meta } from "./statuses";
const { users, tokens, deleteUsers } = await getTestUsers(5);
@ -13,19 +12,14 @@ afterAll(async () => {
});
beforeAll(async () => {
const response = await sendTestRequest(
new Request(
new URL(
`/api/v1/statuses/${timeline2[0].id}/reblog`,
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
const response = await fakeRequest(
`/api/v1/statuses/${timeline2[0].id}/reblog`,
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
),
},
);
expect(response.status).toBe(200);
@ -34,18 +28,14 @@ beforeAll(async () => {
// /api/v1/accounts/:id/statuses
describe(meta.route, () => {
test("should return 200 with statuses", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", users[1].id),
config.http.base_url,
),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response = await fakeRequest(
meta.route.replace(":id", users[1].id),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response.status).toBe(200);
@ -58,21 +48,14 @@ describe(meta.route, () => {
});
test("should exclude reblogs", async () => {
const response = await sendTestRequest(
new Request(
new URL(
`${meta.route.replace(
":id",
users[1].id,
)}?exclude_reblogs=true`,
config.http.base_url,
),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response = await fakeRequest(
`${meta.route.replace(":id", users[1].id)}?exclude_reblogs=true`,
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response.status).toBe(200);
@ -86,37 +69,28 @@ describe(meta.route, () => {
test("should exclude replies", async () => {
// Create reply
const replyResponse = await sendTestRequest(
new Request(new URL("/api/v1/statuses", config.http.base_url), {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
body: new URLSearchParams({
status: "Reply",
in_reply_to_id: timeline[0].id,
local_only: "true",
}),
const replyResponse = await fakeRequest("/api/v1/statuses", {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
body: new URLSearchParams({
status: "Reply",
in_reply_to_id: timeline[0].id,
local_only: "true",
}),
);
});
expect(replyResponse.status).toBe(200);
const response = await sendTestRequest(
new Request(
new URL(
`${meta.route.replace(
":id",
users[1].id,
)}?exclude_replies=true`,
config.http.base_url,
),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response = await fakeRequest(
`${meta.route.replace(":id", users[1].id)}?exclude_replies=true`,
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response.status).toBe(200);
@ -129,18 +103,14 @@ describe(meta.route, () => {
});
test("should only include pins", async () => {
const response = await sendTestRequest(
new Request(
new URL(
`${meta.route.replace(":id", users[1].id)}?pinned=true`,
config.http.base_url,
),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response = await fakeRequest(
`${meta.route.replace(":id", users[1].id)}?pinned=true`,
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response.status).toBe(200);
@ -150,35 +120,27 @@ describe(meta.route, () => {
expect(data.length).toBe(0);
// Create pin
const pinResponse = await sendTestRequest(
new Request(
new URL(
`/api/v1/statuses/${timeline[3].id}/pin`,
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
const pinResponse = await fakeRequest(
`/api/v1/statuses/${timeline[3].id}/pin`,
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
),
},
);
expect(pinResponse.status).toBe(200);
const response2 = await sendTestRequest(
new Request(
new URL(
`${meta.route.replace(":id", users[1].id)}?pinned=true`,
config.http.base_url,
),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response2 = await fakeRequest(
`${meta.route.replace(":id", users[1].id)}?pinned=true`,
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response2.status).toBe(200);

View file

@ -1,7 +1,6 @@
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import type { Relationship as ApiRelationship } from "@versia/client/types";
import { config } from "~/packages/config-manager/index";
import { getTestUsers, sendTestRequest } from "~/tests/utils";
import { fakeRequest, getTestUsers } from "~/tests/utils";
import { meta } from "./unmute";
const { users, tokens, deleteUsers } = await getTestUsers(2);
@ -11,74 +10,48 @@ afterAll(async () => {
});
beforeAll(async () => {
await sendTestRequest(
new Request(
new URL(
`/api/v1/accounts/${users[0].id}/mute`,
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
},
),
);
await fakeRequest(`/api/v1/accounts/${users[0].id}/mute`, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
});
});
// /api/v1/accounts/:id/unmute
describe(meta.route, () => {
test("should return 401 if not authenticated", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", users[1].id),
config.http.base_url,
),
{
method: "POST",
},
),
const response = await fakeRequest(
meta.route.replace(":id", users[1].id),
{
method: "POST",
},
);
expect(response.status).toBe(401);
});
test("should return 404 if user not found", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(
":id",
"00000000-0000-0000-0000-000000000000",
),
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response = await fakeRequest(
meta.route.replace(":id", "00000000-0000-0000-0000-000000000000"),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response.status).toBe(404);
});
test("should unmute user", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", users[1].id),
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response = await fakeRequest(
meta.route.replace(":id", users[1].id),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response.status).toBe(200);
@ -87,19 +60,14 @@ describe(meta.route, () => {
});
test("should return 200 if user already unmuted", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", users[1].id),
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response = await fakeRequest(
meta.route.replace(":id", users[1].id),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response.status).toBe(200);

View file

@ -1,7 +1,6 @@
import { afterAll, describe, expect, test } from "bun:test";
import type { Account as ApiAccount } from "@versia/client/types";
import { config } from "~/packages/config-manager/index";
import { getTestUsers, sendTestRequest } from "~/tests/utils";
import { fakeRequest, getTestUsers } from "~/tests/utils";
import { meta } from "./index";
const { users, deleteUsers } = await getTestUsers(5);
@ -13,13 +12,8 @@ afterAll(async () => {
// /api/v1/accounts/id
describe(meta.route, () => {
test("should correctly get user from username", async () => {
const response = await sendTestRequest(
new Request(
new URL(
`${meta.route}?username=${users[0].data.username}`,
config.http.base_url,
),
),
const response = await fakeRequest(
`${meta.route}?username=${users[0].data.username}`,
);
expect(response.status).toBe(200);
@ -30,13 +24,8 @@ describe(meta.route, () => {
});
test("should return 404 for non-existent user", async () => {
const response = await sendTestRequest(
new Request(
new URL(
`${meta.route}?username=${users[0].data.username}-nonexistent`,
config.http.base_url,
),
),
const response = await fakeRequest(
`${meta.route}?username=${users[0].data.username}-nonexistent`,
);
expect(response.status).toBe(404);

View file

@ -3,8 +3,7 @@ import { randomString } from "@/math";
import { eq } from "drizzle-orm";
import { db } from "~/drizzle/db";
import { Users } from "~/drizzle/schema";
import { config } from "~/packages/config-manager/index";
import { getSolvedChallenge, sendTestRequest } from "~/tests/utils";
import { fakeRequest, getSolvedChallenge } from "~/tests/utils";
import { meta } from "./index";
const username = randomString(10, "hex");
@ -18,228 +17,206 @@ afterEach(async () => {
// /api/v1/statuses
describe(meta.route, () => {
test("should create a new account", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Challenge-Solution": await getSolvedChallenge(),
},
body: JSON.stringify({
username: username,
email: "bob@gamer.com",
password: "password",
agreement: "true",
locale: "en",
reason: "testing",
}),
const response = await fakeRequest(meta.route, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Challenge-Solution": await getSolvedChallenge(),
},
body: JSON.stringify({
username: username,
email: "bob@gamer.com",
password: "password",
agreement: "true",
locale: "en",
reason: "testing",
}),
);
});
expect(response.ok).toBe(true);
});
test("should refuse invalid emails", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Challenge-Solution": await getSolvedChallenge(),
},
body: JSON.stringify({
username: username,
email: "bob",
password: "password",
agreement: "true",
locale: "en",
reason: "testing",
}),
const response = await fakeRequest(meta.route, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Challenge-Solution": await getSolvedChallenge(),
},
body: JSON.stringify({
username: username,
email: "bob",
password: "password",
agreement: "true",
locale: "en",
reason: "testing",
}),
);
});
expect(response.status).toBe(422);
});
test("should require a password", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Challenge-Solution": await getSolvedChallenge(),
},
body: JSON.stringify({
username: username,
email: "contatc@bob.com",
agreement: "true",
locale: "en",
reason: "testing",
}),
const response = await fakeRequest(meta.route, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Challenge-Solution": await getSolvedChallenge(),
},
body: JSON.stringify({
username: username,
email: "contatc@bob.com",
agreement: "true",
locale: "en",
reason: "testing",
}),
);
});
expect(response.status).toBe(422);
});
test("should not allow a previously registered email", async () => {
await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Challenge-Solution": await getSolvedChallenge(),
},
body: JSON.stringify({
username: username,
email: "contact@george.com",
password: "password",
agreement: "true",
locale: "en",
reason: "testing",
}),
await fakeRequest(meta.route, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Challenge-Solution": await getSolvedChallenge(),
},
body: JSON.stringify({
username: username,
email: "contact@george.com",
password: "password",
agreement: "true",
locale: "en",
reason: "testing",
}),
);
});
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Challenge-Solution": await getSolvedChallenge(),
},
body: JSON.stringify({
username: username2,
email: "contact@george.com",
password: "password",
agreement: "true",
locale: "en",
reason: "testing",
}),
const response = await fakeRequest(meta.route, {
method: "POST",
headers: {
"Content-Type": "application/json",
"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);
});
test("should not allow a previously registered email (case insensitive)", async () => {
await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Challenge-Solution": await getSolvedChallenge(),
},
body: JSON.stringify({
username: username,
email: "contact@george.com",
password: "password",
agreement: "true",
locale: "en",
reason: "testing",
}),
await fakeRequest(meta.route, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Challenge-Solution": await getSolvedChallenge(),
},
body: JSON.stringify({
username: username,
email: "contact@george.com",
password: "password",
agreement: "true",
locale: "en",
reason: "testing",
}),
);
});
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Challenge-Solution": await getSolvedChallenge(),
},
body: JSON.stringify({
username: username2,
email: "CONTACT@george.CoM",
password: "password",
agreement: "true",
locale: "en",
reason: "testing",
}),
const response = await fakeRequest(meta.route, {
method: "POST",
headers: {
"Content-Type": "application/json",
"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);
});
test("should not allow invalid usernames (not a-z_0-9)", async () => {
const response1 = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Challenge-Solution": await getSolvedChallenge(),
},
body: JSON.stringify({
username: "bob$",
email: "contact@bob.com",
password: "password",
agreement: "true",
locale: "en",
reason: "testing",
}),
const response1 = await fakeRequest(meta.route, {
method: "POST",
headers: {
"Content-Type": "application/json",
"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);
const response2 = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Challenge-Solution": await getSolvedChallenge(),
},
body: JSON.stringify({
username: "bob-markey",
email: "contact@bob.com",
password: "password",
agreement: "true",
locale: "en",
reason: "testing",
}),
const response2 = await fakeRequest(meta.route, {
method: "POST",
headers: {
"Content-Type": "application/json",
"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);
const response3 = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Challenge-Solution": await getSolvedChallenge(),
},
body: JSON.stringify({
username: "bob markey",
email: "contact@bob.com",
password: "password",
agreement: "true",
locale: "en",
reason: "testing",
}),
const response3 = await fakeRequest(meta.route, {
method: "POST",
headers: {
"Content-Type": "application/json",
"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);
const response4 = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Challenge-Solution": await getSolvedChallenge(),
},
body: JSON.stringify({
username: "BOB",
email: "contact@bob.com",
password: "password",
agreement: "true",
locale: "en",
reason: "testing",
}),
const response4 = await fakeRequest(meta.route, {
method: "POST",
headers: {
"Content-Type": "application/json",
"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);
});

View file

@ -1,7 +1,6 @@
import { afterAll, describe, expect, test } from "bun:test";
import type { Account as ApiAccount } from "@versia/client/types";
import { config } from "~/packages/config-manager/index";
import { getTestUsers, sendTestRequest } from "~/tests/utils";
import { fakeRequest, getTestUsers } from "~/tests/utils";
import { meta } from "./index";
const { users, tokens, deleteUsers } = await getTestUsers(5);
@ -13,18 +12,13 @@ afterAll(async () => {
// /api/v1/accounts/lookup
describe(meta.route, () => {
test("should return 200 with users", async () => {
const response = await sendTestRequest(
new Request(
new URL(
`${meta.route}?acct=${users[0].data.username}`,
config.http.base_url,
),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response = await fakeRequest(
`${meta.route}?acct=${users[0].data.username}`,
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response.status).toBe(200);

View file

@ -2,8 +2,7 @@ import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import { eq } from "drizzle-orm";
import { db } from "~/drizzle/db";
import { Users } from "~/drizzle/schema";
import { config } from "~/packages/config-manager/index";
import { getTestUsers, sendTestRequest } from "~/tests/utils";
import { fakeRequest, getTestUsers } from "~/tests/utils";
import { meta } from "./index";
const { users, tokens, deleteUsers } = await getTestUsers(5);
@ -17,41 +16,25 @@ beforeAll(async () => {
.set({ isLocked: true })
.where(eq(Users.id, users[0].id));
const res1 = await sendTestRequest(
new Request(
new URL(
`/api/v1/accounts/${users[0].id}/follow`,
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
},
),
);
const res1 = await fakeRequest(`/api/v1/accounts/${users[0].id}/follow`, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
});
expect(res1.ok).toBe(true);
const res2 = await sendTestRequest(
new Request(
new URL(
`/api/v1/accounts/${users[2].id}/follow`,
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
},
),
);
const res2 = await fakeRequest(`/api/v1/accounts/${users[2].id}/follow`, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
});
expect(res2.ok).toBe(true);
});
@ -63,31 +46,19 @@ afterAll(async () => {
// /api/v1/accounts/relationships
describe(meta.route, () => {
test("should return 401 if not authenticated", async () => {
const response = await sendTestRequest(
new Request(
new URL(
`${meta.route}?id[]=${users[2].id}`,
config.http.base_url,
),
),
);
const response = await fakeRequest(`${meta.route}?id[]=${users[2].id}`);
expect(response.status).toBe(401);
});
test("should return relationships", async () => {
const response = await sendTestRequest(
new Request(
new URL(
`${meta.route}?id[]=${users[2].id}`,
config.http.base_url,
),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response = await fakeRequest(
`${meta.route}?id[]=${users[2].id}`,
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response.status).toBe(200);
@ -111,18 +82,13 @@ describe(meta.route, () => {
});
test("should be requested_by user1", async () => {
const response = await sendTestRequest(
new Request(
new URL(
`${meta.route}?id[]=${users[1].id}`,
config.http.base_url,
),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response = await fakeRequest(
`${meta.route}?id[]=${users[1].id}`,
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response.status).toBe(200);

View file

@ -1,7 +1,6 @@
import { afterAll, describe, expect, test } from "bun:test";
import type { Account as ApiAccount } from "@versia/client/types";
import { config } from "~/packages/config-manager/index";
import { getTestUsers, sendTestRequest } from "~/tests/utils";
import { fakeRequest, getTestUsers } from "~/tests/utils";
import { meta } from "./index";
const { users, tokens, deleteUsers } = await getTestUsers(5);
@ -13,18 +12,13 @@ afterAll(async () => {
// /api/v1/accounts/search
describe(meta.route, () => {
test("should return 200 with users", async () => {
const response = await sendTestRequest(
new Request(
new URL(
`${meta.route}?q=${users[0].data.username}`,
config.http.base_url,
),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response = await fakeRequest(
`${meta.route}?q=${users[0].data.username}`,
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response.status).toBe(200);

View file

@ -1,16 +1,13 @@
import { describe, expect, test } from "bun:test";
import { config } from "~/packages/config-manager/index";
import { sendTestRequest } from "~/tests/utils";
import { fakeRequest } from "~/tests/utils";
import { meta } from "./index";
// /api/v1/challenges
describe(meta.route, () => {
test("should get a challenge", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
}),
);
const response = await fakeRequest(meta.route, {
method: "POST",
});
expect(response.status).toBe(200);

View file

@ -2,8 +2,7 @@ import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import { inArray } from "drizzle-orm";
import { db } from "~/drizzle/db";
import { Emojis } from "~/drizzle/schema";
import { config } from "~/packages/config-manager/index";
import { getTestUsers, sendTestRequest } from "~/tests/utils";
import { fakeRequest, getTestUsers } from "~/tests/utils";
import { meta } from "./index";
const { users, tokens, deleteUsers } = await getTestUsers(2);
@ -13,50 +12,44 @@ beforeAll(async () => {
await users[1].update({ isAdmin: true });
// Upload one emoji as admin, then one as each user
const response = await sendTestRequest(
new Request(new URL("/api/v1/emojis", config.http.base_url), {
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
"Content-Type": "application/json",
},
method: "POST",
body: JSON.stringify({
shortcode: "test1",
element: "https://cdn.versia.social/logo.webp",
global: true,
}),
const response = await fakeRequest("/api/v1/emojis", {
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
"Content-Type": "application/json",
},
method: "POST",
body: JSON.stringify({
shortcode: "test1",
element: "https://cdn.versia.social/logo.webp",
global: true,
}),
);
});
expect(response.status).toBe(200);
await sendTestRequest(
new Request(new URL("/api/v1/emojis", config.http.base_url), {
headers: {
Authorization: `Bearer ${tokens[0].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[0].accessToken}`,
"Content-Type": "application/json",
},
method: "POST",
body: JSON.stringify({
shortcode: "test2",
element: "https://cdn.versia.social/logo.webp",
}),
);
});
await sendTestRequest(
new Request(new URL("/api/v1/emojis", config.http.base_url), {
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
"Content-Type": "application/json",
},
method: "POST",
body: JSON.stringify({
shortcode: "test3",
element: "https://cdn.versia.social/logo.webp",
}),
await fakeRequest("/api/v1/emojis", {
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
"Content-Type": "application/json",
},
method: "POST",
body: JSON.stringify({
shortcode: "test3",
element: "https://cdn.versia.social/logo.webp",
}),
);
});
});
afterAll(async () => {
@ -69,13 +62,11 @@ afterAll(async () => {
describe(meta.route, () => {
test("should return all global emojis", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
}),
);
const response = await fakeRequest(meta.route, {
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
});
expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toContain(
@ -103,13 +94,11 @@ describe(meta.route, () => {
});
test("should return all user emojis", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
}),
);
const response = await fakeRequest(meta.route, {
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
});
expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toContain(
@ -137,9 +126,7 @@ describe(meta.route, () => {
});
test("should return all global emojis when signed out", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url)),
);
const response = await fakeRequest(meta.route);
expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toContain(

View file

@ -2,8 +2,7 @@ import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import { inArray } from "drizzle-orm";
import { db } from "~/drizzle/db";
import { Emojis } from "~/drizzle/schema";
import { config } from "~/packages/config-manager/index";
import { getTestUsers, sendTestRequest } from "~/tests/utils";
import { fakeRequest, getTestUsers } from "~/tests/utils";
import { meta } from "./index";
const { users, tokens, deleteUsers } = await getTestUsers(2);
@ -14,20 +13,18 @@ beforeAll(async () => {
await users[1].update({ isAdmin: true });
// Create an emoji
const response = await sendTestRequest(
new Request(new URL("/api/v1/emojis", config.http.base_url), {
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
"Content-Type": "application/json",
},
method: "POST",
body: JSON.stringify({
shortcode: "test",
element: "https://cdn.versia.social/logo.webp",
global: true,
}),
const response = await fakeRequest("/api/v1/emojis", {
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
"Content-Type": "application/json",
},
method: "POST",
body: JSON.stringify({
shortcode: "test",
element: "https://cdn.versia.social/logo.webp",
global: true,
}),
);
});
expect(response.ok).toBe(true);
const emoji = await response.json();
@ -45,72 +42,50 @@ afterAll(async () => {
// /api/v1/emojis/:id (PATCH, DELETE, GET)
describe(meta.route, () => {
test("should return 401 if not authenticated", async () => {
const response = await sendTestRequest(
new Request(
new URL(meta.route.replace(":id", id), config.http.base_url),
{
method: "GET",
},
),
);
const response = await fakeRequest(meta.route.replace(":id", id), {
method: "GET",
});
expect(response.status).toBe(401);
});
test("should return 404 if emoji does not exist", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(
":id",
"00000000-0000-0000-0000-000000000000",
),
config.http.base_url,
),
{
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
method: "GET",
const response = await fakeRequest(
meta.route.replace(":id", "00000000-0000-0000-0000-000000000000"),
{
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
),
method: "GET",
},
);
expect(response.status).toBe(404);
});
test("should not work if the user is trying to update an emoji they don't own", async () => {
const response = await sendTestRequest(
new Request(
new URL(meta.route.replace(":id", id), config.http.base_url),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
"Content-Type": "application/json",
},
method: "PATCH",
body: JSON.stringify({
shortcode: "test2",
}),
},
),
);
const response = await fakeRequest(meta.route.replace(":id", id), {
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
"Content-Type": "application/json",
},
method: "PATCH",
body: JSON.stringify({
shortcode: "test2",
}),
});
expect(response.status).toBe(403);
});
test("should return the emoji", async () => {
const response = await sendTestRequest(
new Request(
new URL(meta.route.replace(":id", id), config.http.base_url),
{
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
method: "GET",
},
),
);
const response = await fakeRequest(meta.route.replace(":id", id), {
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
method: "GET",
});
expect(response.ok).toBe(true);
const emoji = await response.json();
@ -118,21 +93,16 @@ describe(meta.route, () => {
});
test("should update the emoji", async () => {
const response = await sendTestRequest(
new Request(
new URL(meta.route.replace(":id", id), config.http.base_url),
{
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
"Content-Type": "application/json",
},
method: "PATCH",
body: JSON.stringify({
shortcode: "test2",
}),
},
),
);
const response = await fakeRequest(meta.route.replace(":id", id), {
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
"Content-Type": "application/json",
},
method: "PATCH",
body: JSON.stringify({
shortcode: "test2",
}),
});
expect(response.ok).toBe(true);
const emoji = await response.json();
@ -140,22 +110,16 @@ describe(meta.route, () => {
});
test("should update the emoji with another url, but keep the shortcode", async () => {
const response = await sendTestRequest(
new Request(
new URL(meta.route.replace(":id", id), config.http.base_url),
{
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
"Content-Type": "application/json",
},
method: "PATCH",
body: JSON.stringify({
element:
"https://avatars.githubusercontent.com/u/30842467?v=4",
}),
},
),
);
const response = await fakeRequest(meta.route.replace(":id", id), {
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
"Content-Type": "application/json",
},
method: "PATCH",
body: JSON.stringify({
element: "https://avatars.githubusercontent.com/u/30842467?v=4",
}),
});
expect(response.ok).toBe(true);
const emoji = await response.json();
@ -163,36 +127,26 @@ describe(meta.route, () => {
});
test("should update the emoji to be non-global", async () => {
const response = await sendTestRequest(
new Request(
new URL(meta.route.replace(":id", id), config.http.base_url),
{
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
"Content-Type": "application/json",
},
method: "PATCH",
body: JSON.stringify({
global: false,
}),
},
),
);
const response = await fakeRequest(meta.route.replace(":id", id), {
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
"Content-Type": "application/json",
},
method: "PATCH",
body: JSON.stringify({
global: false,
}),
});
expect(response.ok).toBe(true);
// Check if the other user can see it
const response2 = await sendTestRequest(
new Request(
new URL("/api/v1/custom_emojis", config.http.base_url),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
method: "GET",
},
),
);
const response2 = await fakeRequest("/api/v1/custom_emojis", {
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
method: "GET",
});
expect(response2.ok).toBe(true);
const emojis = await response2.json();
@ -200,17 +154,12 @@ describe(meta.route, () => {
});
test("should delete the emoji", async () => {
const response = await sendTestRequest(
new Request(
new URL(meta.route.replace(":id", id), config.http.base_url),
{
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
method: "DELETE",
},
),
);
const response = await fakeRequest(meta.route.replace(":id", id), {
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
method: "DELETE",
});
expect(response.status).toBe(204);
});

View file

@ -3,8 +3,7 @@ import { inArray } from "drizzle-orm";
import sharp from "sharp";
import { db } from "~/drizzle/db";
import { Emojis } from "~/drizzle/schema";
import { config } from "~/packages/config-manager/index";
import { getTestUsers, sendTestRequest } from "~/tests/utils";
import { fakeRequest, getTestUsers } from "~/tests/utils";
import { meta } from "./index";
const { users, tokens, deleteUsers } = await getTestUsers(3);
@ -41,18 +40,16 @@ const createImage = async (name: string): Promise<File> => {
describe(meta.route, () => {
test("should return 401 if not authenticated", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
shortcode: "test",
element: "https://cdn.versia.social/logo.webp",
}),
const response = await fakeRequest(meta.route, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
shortcode: "test",
element: "https://cdn.versia.social/logo.webp",
}),
);
});
expect(response.status).toBe(401);
});
@ -64,15 +61,13 @@ describe(meta.route, () => {
formData.append("element", await createImage("test.png"));
formData.append("global", "true");
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
body: formData,
}),
);
const response = await fakeRequest(meta.route, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
body: formData,
});
expect(response.ok).toBe(true);
const emoji = await response.json();
@ -85,33 +80,29 @@ describe(meta.route, () => {
formData.append("shortcode", "test2");
formData.append("element", new File(["test"], "test.txt"));
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
body: formData,
}),
);
const response = await fakeRequest(meta.route, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
body: formData,
});
expect(response.status).toBe(422);
});
test("should upload an emoji by url", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
shortcode: "test3",
element: "https://cdn.versia.social/logo.webp",
}),
const response = await fakeRequest(meta.route, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
shortcode: "test3",
element: "https://cdn.versia.social/logo.webp",
}),
);
});
expect(response.ok).toBe(true);
const emoji = await response.json();
@ -124,15 +115,13 @@ describe(meta.route, () => {
formData.append("shortcode", "test1");
formData.append("element", await createImage("test-image.png"));
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
body: formData,
}),
);
const response = await fakeRequest(meta.route, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
body: formData,
});
expect(response.status).toBe(422);
});
@ -144,15 +133,13 @@ describe(meta.route, () => {
formData.append("shortcode", "test4");
formData.append("element", await createImage("test-image.png"));
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: formData,
}),
);
const response = await fakeRequest(meta.route, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: formData,
});
expect(response.ok).toBe(true);
const emoji = await response.json();
@ -165,15 +152,13 @@ describe(meta.route, () => {
formData.append("shortcode", "test1");
formData.append("element", await createImage("test-image.png"));
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: formData,
}),
);
const response = await fakeRequest(meta.route, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: formData,
});
expect(response.status).toBe(422);
});
@ -183,15 +168,13 @@ describe(meta.route, () => {
formData.append("shortcode", "test4");
formData.append("element", await createImage("test-image.png"));
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[2].accessToken}`,
},
body: formData,
}),
);
const response = await fakeRequest(meta.route, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[2].accessToken}`,
},
body: formData,
});
expect(response.ok).toBe(true);
const emoji = await response.json();

View file

@ -1,14 +1,11 @@
import { describe, expect, test } from "bun:test";
import { config } from "~/packages/config-manager/index";
import { sendTestRequest } from "~/tests/utils";
import { fakeRequest } from "~/tests/utils";
import { meta } from "./extended_description";
// /api/v1/instance/extended_description
describe(meta.route, () => {
test("should return extended description", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url)),
);
const response = await fakeRequest(meta.route);
expect(response.status).toBe(200);

View file

@ -1,14 +1,11 @@
import { describe, expect, test } from "bun:test";
import { config } from "~/packages/config-manager/index";
import { sendTestRequest } from "~/tests/utils";
import { fakeRequest } from "~/tests/utils";
import { meta } from "./privacy_policy";
// /api/v1/instance/privacy_policy
describe(meta.route, () => {
test("should return privacy policy", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url)),
);
const response = await fakeRequest(meta.route);
expect(response.status).toBe(200);

View file

@ -1,14 +1,12 @@
import { describe, expect, test } from "bun:test";
import { config } from "~/packages/config-manager/index";
import { sendTestRequest } from "~/tests/utils";
import { fakeRequest } from "~/tests/utils";
import { meta } from "./rules";
// /api/v1/instance/rules
describe(meta.route, () => {
test("should return rules", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url)),
);
const response = await fakeRequest(meta.route);
expect(response.status).toBe(200);

View file

@ -1,14 +1,11 @@
import { describe, expect, test } from "bun:test";
import { config } from "~/packages/config-manager/index";
import { sendTestRequest } from "~/tests/utils";
import { fakeRequest } from "~/tests/utils";
import { meta } from "./tos";
// /api/v1/instance/tos
describe(meta.route, () => {
test("should return terms of service", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url)),
);
const response = await fakeRequest(meta.route);
expect(response.status).toBe(200);

View file

@ -1,6 +1,5 @@
import { afterAll, describe, expect, test } from "bun:test";
import { config } from "~/packages/config-manager/index";
import { getTestStatuses, getTestUsers, sendTestRequest } from "~/tests/utils";
import { fakeRequest, getTestStatuses, getTestUsers } from "~/tests/utils";
import { meta } from "./index";
const { users, tokens, deleteUsers } = await getTestUsers(1);
@ -13,31 +12,24 @@ afterAll(async () => {
// /api/v1/markers
describe(meta.route, () => {
test("should return 401 if not authenticated", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "GET",
}),
);
const response = await fakeRequest(meta.route, {
method: "GET",
});
expect(response.status).toBe(401);
});
test("should return empty markers", async () => {
const response = await sendTestRequest(
new Request(
`${new URL(
meta.route,
config.http.base_url,
)}?${new URLSearchParams([
["timeline[]", "home"],
["timeline[]", "notifications"],
])}`,
{
method: "GET",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response = await fakeRequest(
`${meta.route}?${new URLSearchParams([
["timeline[]", "home"],
["timeline[]", "notifications"],
])}`,
{
method: "GET",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response.status).toBe(200);
@ -45,21 +37,17 @@ describe(meta.route, () => {
});
test("should create markers", async () => {
const response = await sendTestRequest(
new Request(
new URL(
`${meta.route}?${new URLSearchParams({
"home[last_read_id]": timeline[0].id,
})}`,
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response = await fakeRequest(
`${meta.route}?${new URLSearchParams({
"home[last_read_id]": timeline[0].id,
})}`,
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response.status).toBe(200);
@ -73,22 +61,17 @@ describe(meta.route, () => {
});
test("should return markers", async () => {
const response = await sendTestRequest(
new Request(
`${new URL(
meta.route,
config.http.base_url,
)}?${new URLSearchParams([
["timeline[]", "home"],
["timeline[]", "notifications"],
])}`,
{
method: "GET",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response = await fakeRequest(
`${meta.route}?${new URLSearchParams([
["timeline[]", "home"],
["timeline[]", "notifications"],
])}`,
{
method: "GET",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response.status).toBe(200);

View file

@ -1,6 +1,5 @@
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import { config } from "~/packages/config-manager/index";
import { getTestUsers, sendTestRequest } from "~/tests/utils";
import { fakeRequest, getTestUsers } from "~/tests/utils";
import { meta } from "./index";
const { users, tokens, deleteUsers } = await getTestUsers(3);
@ -10,21 +9,17 @@ afterAll(async () => {
});
beforeAll(async () => {
const response = await sendTestRequest(
new Request(
new URL(
`/api/v1/accounts/${users[1].id}/mute`,
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
const response = await fakeRequest(
`/api/v1/accounts/${users[1].id}/mute`,
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
"Content-Type": "application/json",
},
),
body: JSON.stringify({}),
},
);
expect(response.status).toBe(200);
});
@ -32,29 +27,23 @@ beforeAll(async () => {
// /api/v1/mutes
describe(meta.route, () => {
test("should return 401 if not authenticated", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", users[1].id),
config.http.base_url,
),
{
method: "GET",
},
),
const response = await fakeRequest(
meta.route.replace(":id", users[1].id),
{
method: "GET",
},
);
expect(response.status).toBe(401);
});
test("should return mutes", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "GET",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
}),
);
const response = await fakeRequest(meta.route, {
method: "GET",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
});
expect(response.status).toBe(200);
const body = await response.json();
expect(body).toEqual(
@ -67,32 +56,26 @@ describe(meta.route, () => {
});
test("should return mutes after unmute", async () => {
const response = await sendTestRequest(
new Request(
new URL(
`/api/v1/accounts/${users[1].id}/unmute`,
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
const response = await fakeRequest(
`/api/v1/accounts/${users[1].id}/unmute`,
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
"Content-Type": "application/json",
},
),
body: JSON.stringify({}),
},
);
expect(response.status).toBe(200);
const response2 = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "GET",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
}),
);
const response2 = await fakeRequest(meta.route, {
method: "GET",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
});
expect(response2.status).toBe(200);
const body = await response2.json();
expect(body).toEqual([]);

View file

@ -1,7 +1,6 @@
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import type { Notification as ApiNotification } from "@versia/client/types";
import { config } from "~/packages/config-manager/index";
import { getTestUsers, sendTestRequest } from "~/tests/utils";
import { fakeRequest, getTestUsers } from "~/tests/utils";
import { meta } from "./dismiss";
const { users, tokens, deleteUsers } = await getTestUsers(2);
@ -9,30 +8,20 @@ let notifications: ApiNotification[] = [];
// Create some test notifications: follow, favourite, reblog, mention
beforeAll(async () => {
await sendTestRequest(
new Request(
new URL(
`/api/v1/accounts/${users[0].id}/follow`,
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
},
),
);
await fakeRequest(`/api/v1/accounts/${users[0].id}/follow`, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
});
notifications = await sendTestRequest(
new Request(new URL("/api/v1/notifications", config.http.base_url), {
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
}),
).then((r) => r.json());
notifications = await fakeRequest("/api/v1/notifications", {
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
}).then((r) => r.json());
expect(notifications.length).toBe(1);
});
@ -44,51 +33,36 @@ afterAll(async () => {
// /api/v1/notifications/:id/dismiss
describe(meta.route, () => {
test("should return 401 if not authenticated", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", notifications[0].id),
config.http.base_url,
),
{
method: "POST",
},
),
const response = await fakeRequest(
meta.route.replace(":id", notifications[0].id),
{
method: "POST",
},
);
expect(response.status).toBe(401);
});
test("should dismiss notification", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", notifications[0].id),
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response = await fakeRequest(
meta.route.replace(":id", notifications[0].id),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response.status).toBe(200);
});
test("should not display dismissed notification", async () => {
const response = await sendTestRequest(
new Request(
new URL("/api/v1/notifications", config.http.base_url),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
},
),
);
const response = await fakeRequest("/api/v1/notifications", {
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
});
expect(response.status).toBe(200);

View file

@ -1,7 +1,6 @@
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import type { Notification as ApiNotification } from "@versia/client/types";
import { config } from "~/packages/config-manager/index";
import { getTestUsers, sendTestRequest } from "~/tests/utils";
import { fakeRequest, getTestUsers } from "~/tests/utils";
import { meta } from "./index";
const { users, tokens, deleteUsers } = await getTestUsers(2);
@ -9,30 +8,20 @@ let notifications: ApiNotification[] = [];
// Create some test notifications: follow, favourite, reblog, mention
beforeAll(async () => {
await sendTestRequest(
new Request(
new URL(
`/api/v1/accounts/${users[0].id}/follow`,
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
},
),
);
await fakeRequest(`/api/v1/accounts/${users[0].id}/follow`, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
});
notifications = await sendTestRequest(
new Request(new URL("/api/v1/notifications", config.http.base_url), {
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
}),
).then((r) => r.json());
notifications = await fakeRequest("/api/v1/notifications", {
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
}).then((r) => r.json());
expect(notifications.length).toBe(1);
});
@ -44,72 +33,46 @@ afterAll(async () => {
// /api/v1/notifications/:id
describe(meta.route, () => {
test("should return 401 if not authenticated", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(
":id",
"00000000-0000-0000-0000-000000000000",
),
config.http.base_url,
),
),
const response = await fakeRequest(
meta.route.replace(":id", "00000000-0000-0000-0000-000000000000"),
);
expect(response.status).toBe(401);
});
test("should return 422 if ID is invalid", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", "invalid"),
config.http.base_url,
),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response = await fakeRequest(
meta.route.replace(":id", "invalid"),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response.status).toBe(422);
});
test("should return 404 if notification not found", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(
":id",
"00000000-0000-0000-0000-000000000000",
),
config.http.base_url,
),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response = await fakeRequest(
meta.route.replace(":id", "00000000-0000-0000-0000-000000000000"),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response.status).toBe(404);
});
test("should return notification", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", notifications[0].id),
config.http.base_url,
),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response = await fakeRequest(
meta.route.replace(":id", notifications[0].id),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response.status).toBe(200);

View file

@ -1,7 +1,6 @@
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import type { Notification as ApiNotification } from "@versia/client/types";
import { config } from "~/packages/config-manager/index";
import { getTestUsers, sendTestRequest } from "~/tests/utils";
import { fakeRequest, getTestUsers } from "~/tests/utils";
import { meta } from "./index";
const { users, tokens, deleteUsers } = await getTestUsers(2);
@ -9,30 +8,20 @@ let notifications: ApiNotification[] = [];
// Create some test notifications: follow, favourite, reblog, mention
beforeAll(async () => {
await sendTestRequest(
new Request(
new URL(
`/api/v1/accounts/${users[0].id}/follow`,
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
},
),
);
await fakeRequest(`/api/v1/accounts/${users[0].id}/follow`, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
});
notifications = await sendTestRequest(
new Request(new URL("/api/v1/notifications", config.http.base_url), {
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
}),
).then((r) => r.json());
notifications = await fakeRequest("/api/v1/notifications", {
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
}).then((r) => r.json());
expect(notifications.length).toBe(1);
});
@ -44,37 +33,28 @@ afterAll(async () => {
// /api/v1/notifications/clear
describe(meta.route, () => {
test("should return 401 if not authenticated", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
}),
);
const response = await fakeRequest(meta.route, {
method: "POST",
});
expect(response.status).toBe(401);
});
test("should clear notifications", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
}),
);
const response = await fakeRequest(meta.route, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
});
expect(response.status).toBe(200);
const newNotifications = await sendTestRequest(
new Request(
new URL("/api/v1/notifications", config.http.base_url),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
},
),
).then((r) => r.json());
const newNotifications = await fakeRequest("/api/v1/notifications", {
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
}).then((r) => r.json());
expect(newNotifications.length).toBe(0);
});

View file

@ -1,7 +1,6 @@
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import type { Notification as ApiNotification } from "@versia/client/types";
import { config } from "~/packages/config-manager/index";
import { getTestStatuses, getTestUsers, sendTestRequest } from "~/tests/utils";
import { fakeRequest, getTestStatuses, getTestUsers } from "~/tests/utils";
import { meta } from "./index";
const { users, tokens, deleteUsers } = await getTestUsers(2);
@ -10,49 +9,31 @@ let notifications: ApiNotification[] = [];
// Create some test notifications
beforeAll(async () => {
await sendTestRequest(
new Request(
new URL(
`/api/v1/accounts/${users[0].id}/follow`,
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
},
),
);
await fakeRequest(`/api/v1/accounts/${users[0].id}/follow`, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
});
for (const i of [0, 1, 2, 3]) {
await sendTestRequest(
new Request(
new URL(
`/api/v1/statuses/${statuses[i].id}/favourite`,
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
},
),
);
await fakeRequest(`/api/v1/statuses/${statuses[i].id}/favourite`, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
});
}
notifications = await sendTestRequest(
new Request(new URL("/api/v1/notifications", config.http.base_url), {
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
}),
).then((r) => r.json());
notifications = await fakeRequest("/api/v1/notifications", {
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
}).then((r) => r.json());
expect(notifications.length).toBe(5);
});
@ -64,55 +45,40 @@ afterAll(async () => {
// /api/v1/notifications/destroy_multiple
describe(meta.route, () => {
test("should return 401 if not authenticated", async () => {
const response = await sendTestRequest(
new Request(
new URL(
`${meta.route}?${new URLSearchParams(
notifications.slice(1).map((n) => ["ids[]", n.id]),
).toString()}`,
config.http.base_url,
),
{
method: "DELETE",
},
),
const response = await fakeRequest(
`${meta.route}?${new URLSearchParams(
notifications.slice(1).map((n) => ["ids[]", n.id]),
).toString()}`,
{
method: "DELETE",
},
);
expect(response.status).toBe(401);
});
test("should dismiss notifications", async () => {
const response = await sendTestRequest(
new Request(
new URL(
`${meta.route}?${new URLSearchParams(
notifications.slice(1).map((n) => ["ids[]", n.id]),
).toString()}`,
config.http.base_url,
),
{
method: "DELETE",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response = await fakeRequest(
`${meta.route}?${new URLSearchParams(
notifications.slice(1).map((n) => ["ids[]", n.id]),
).toString()}`,
{
method: "DELETE",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response.status).toBe(200);
});
test("should not display dismissed notification", async () => {
const response = await sendTestRequest(
new Request(
new URL("/api/v1/notifications", config.http.base_url),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
},
),
);
const response = await fakeRequest("/api/v1/notifications", {
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
});
expect(response.status).toBe(200);

View file

@ -1,7 +1,6 @@
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import type { Notification as ApiNotification } from "@versia/client/types";
import { config } from "~/packages/config-manager/index";
import { getTestStatuses, getTestUsers, sendTestRequest } from "~/tests/utils";
import { fakeRequest, getTestStatuses, getTestUsers } from "~/tests/utils";
import { meta } from "./index";
const getFormData = (object: Record<string, string | number | boolean>) =>
@ -14,76 +13,56 @@ const { users, tokens, deleteUsers } = await getTestUsers(2);
const timeline = (await getTestStatuses(40, users[0])).toReversed();
// Create some test notifications: follow, favourite, reblog, mention
beforeAll(async () => {
const res1 = await sendTestRequest(
new Request(
new URL(
`/api/v1/accounts/${users[0].id}/follow`,
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
},
),
);
const res1 = await fakeRequest(`/api/v1/accounts/${users[0].id}/follow`, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
});
expect(res1.status).toBe(200);
const res2 = await sendTestRequest(
new Request(
new URL(
`/api/v1/statuses/${timeline[0].id}/favourite`,
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
const res2 = await fakeRequest(
`/api/v1/statuses/${timeline[0].id}/favourite`,
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
"Content-Type": "application/json",
},
),
body: JSON.stringify({}),
},
);
expect(res2.status).toBe(200);
const res3 = await sendTestRequest(
new Request(
new URL(
`/api/v1/statuses/${timeline[0].id}/reblog`,
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
body: getFormData({}),
},
),
);
expect(res3.status).toBe(200);
const res4 = await sendTestRequest(
new Request(new URL("/api/v1/statuses", config.http.base_url), {
const res3 = await fakeRequest(
`/api/v1/statuses/${timeline[0].id}/reblog`,
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
body: new URLSearchParams({
status: `@${users[0].data.username} test mention`,
visibility: "direct",
local_only: "true",
}),
}),
body: getFormData({}),
},
);
expect(res3.status).toBe(200);
const res4 = await fakeRequest("/api/v1/statuses", {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
body: new URLSearchParams({
status: `@${users[0].data.username} test mention`,
visibility: "direct",
local_only: "true",
}),
});
expect(res4.status).toBe(200);
});
@ -94,21 +73,17 @@ afterAll(async () => {
// /api/v1/notifications
describe(meta.route, () => {
test("should return 401 if not authenticated", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url)),
);
const response = await fakeRequest(meta.route);
expect(response.status).toBe(401);
});
test("should return 200 with notifications", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
}),
);
const response = await fakeRequest(meta.route, {
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
});
expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toContain(
@ -133,36 +108,31 @@ describe(meta.route, () => {
});
test("should not return notifications with filtered keywords", async () => {
const filterResponse = await sendTestRequest(
new Request(new URL("/api/v2/filters", config.http.base_url), {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams({
title: "Test Filter",
"context[]": "notifications",
filter_action: "hide",
"keywords_attributes[0][keyword]":
timeline[0].content.slice(4, 20),
"keywords_attributes[0][whole_word]": "false",
}),
const filterResponse = await fakeRequest("/api/v2/filters", {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams({
title: "Test Filter",
"context[]": "notifications",
filter_action: "hide",
"keywords_attributes[0][keyword]": timeline[0].content.slice(
4,
20,
),
"keywords_attributes[0][whole_word]": "false",
}),
);
});
expect(filterResponse.status).toBe(200);
const response = await sendTestRequest(
new Request(
new URL(`${meta.route}?limit=20`, config.http.base_url),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
},
),
);
const response = await fakeRequest(`${meta.route}?limit=20`, {
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
});
expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toContain(
@ -180,19 +150,14 @@ describe(meta.route, () => {
);
// Delete filter
const filterDeleteResponse = await sendTestRequest(
new Request(
new URL(
`/api/v2/filters/${(await filterResponse.json()).id}`,
config.http.base_url,
),
{
method: "DELETE",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const filterDeleteResponse = await fakeRequest(
`/api/v2/filters/${(await filterResponse.json()).id}`,
{
method: "DELETE",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(filterDeleteResponse.status).toBe(200);

View file

@ -1,8 +1,7 @@
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import { ADMIN_ROLES, DEFAULT_ROLES, RolePermissions } from "~/drizzle/schema";
import { config } from "~/packages/config-manager/index";
import { Role } from "~/packages/database-interface/role";
import { getTestUsers, sendTestRequest } from "~/tests/utils";
import { fakeRequest, getTestUsers } from "~/tests/utils";
import { meta } from "./index";
const { users, tokens, deleteUsers } = await getTestUsers(1);
@ -61,58 +60,34 @@ afterAll(async () => {
// /api/v1/roles/:id
describe(meta.route, () => {
test("should return 401 if not authenticated", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", role.id),
config.http.base_url,
),
{
method: "GET",
},
),
);
const response = await fakeRequest(meta.route.replace(":id", role.id), {
method: "GET",
});
expect(response.status).toBe(401);
});
test("should return 404 if role does not exist", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(
":id",
"00000000-0000-0000-0000-000000000000",
),
config.http.base_url,
),
{
method: "GET",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response = await fakeRequest(
meta.route.replace(":id", "00000000-0000-0000-0000-000000000000"),
{
method: "GET",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response.status).toBe(404);
});
test("should return the role", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", role.id),
config.http.base_url,
),
{
method: "GET",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
},
),
);
const response = await fakeRequest(meta.route.replace(":id", role.id), {
method: "GET",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
});
expect(response.ok).toBe(true);
const output = await response.json();
@ -127,19 +102,14 @@ describe(meta.route, () => {
});
test("should return 403 if user does not have MANAGE_ROLES permission", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", roleNotLinked.id),
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response = await fakeRequest(
meta.route.replace(":id", roleNotLinked.id),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response.status).toBe(403);
@ -154,32 +124,25 @@ describe(meta.route, () => {
permissions: [RolePermissions.ManageRoles],
});
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", roleNotLinked.id),
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response = await fakeRequest(
meta.route.replace(":id", roleNotLinked.id),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response.status).toBe(204);
// Check if role was assigned
const response2 = await sendTestRequest(
new Request(new URL("/api/v1/roles", config.http.base_url), {
method: "GET",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
}),
);
const response2 = await fakeRequest("/api/v1/roles", {
method: "GET",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
});
expect(response2.ok).toBe(true);
const roles = await response2.json();
@ -201,32 +164,22 @@ describe(meta.route, () => {
});
test("should unassign role", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", role.id),
config.http.base_url,
),
{
method: "DELETE",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
},
),
);
const response = await fakeRequest(meta.route.replace(":id", role.id), {
method: "DELETE",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
});
expect(response.status).toBe(204);
// Check if role was unassigned
const response2 = await sendTestRequest(
new Request(new URL("/api/v1/roles", config.http.base_url), {
method: "GET",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
}),
);
const response2 = await fakeRequest("/api/v1/roles", {
method: "GET",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
});
expect(response2.ok).toBe(true);
const roles = await response2.json();
@ -248,19 +201,14 @@ describe(meta.route, () => {
permissions: [RolePermissions.ManageRoles],
});
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", higherPriorityRole.id),
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response = await fakeRequest(
meta.route.replace(":id", higherPriorityRole.id),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response.status).toBe(403);

View file

@ -2,7 +2,7 @@ import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import { ADMIN_ROLES } from "~/drizzle/schema";
import { config } from "~/packages/config-manager/index";
import { Role } from "~/packages/database-interface/role";
import { getTestUsers, sendTestRequest } from "~/tests/utils";
import { fakeRequest, getTestUsers } from "~/tests/utils";
import { meta } from "./index";
const { users, deleteUsers, tokens } = await getTestUsers(1);
@ -32,24 +32,20 @@ afterAll(async () => {
// /api/v1/roles
describe(meta.route, () => {
test("should return 401 if not authenticated", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "GET",
}),
);
const response = await fakeRequest(meta.route, {
method: "GET",
});
expect(response.status).toBe(401);
});
test("should return a list of roles", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "GET",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
}),
);
const response = await fakeRequest(meta.route, {
method: "GET",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
});
expect(response.ok).toBe(true);
const roles = await response.json();

View file

@ -1,6 +1,5 @@
import { afterAll, describe, expect, test } from "bun:test";
import { config } from "~/packages/config-manager";
import { getTestUsers, sendTestRequest } from "~/tests/utils";
import { fakeRequest, getTestUsers } from "~/tests/utils";
import { meta } from "./index";
const { deleteUsers, tokens } = await getTestUsers(1);
@ -12,19 +11,14 @@ afterAll(async () => {
// /api/v1/sso/:id
describe(meta.route, () => {
test("should not find unknown issuer", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", "unknown"),
config.http.base_url,
),
{
method: "GET",
headers: {
Authorization: `Bearer ${tokens[0]?.accessToken}`,
},
const response = await fakeRequest(
meta.route.replace(":id", "unknown"),
{
method: "GET",
headers: {
Authorization: `Bearer ${tokens[0]?.accessToken}`,
},
),
},
);
expect(response.status).toBe(404);
@ -32,20 +26,15 @@ describe(meta.route, () => {
error: "Issuer not found",
});
const response2 = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", "unknown"),
config.http.base_url,
),
{
method: "DELETE",
headers: {
Authorization: `Bearer ${tokens[0]?.accessToken}`,
"Content-Type": "application/json",
},
const response2 = await fakeRequest(
meta.route.replace(":id", "unknown"),
{
method: "DELETE",
headers: {
Authorization: `Bearer ${tokens[0]?.accessToken}`,
"Content-Type": "application/json",
},
),
},
);
expect(response2.status).toBe(404);

View file

@ -1,6 +1,5 @@
import { afterAll, describe, expect, test } from "bun:test";
import { config } from "~/packages/config-manager";
import { getTestUsers, sendTestRequest } from "~/tests/utils";
import { fakeRequest, getTestUsers } from "~/tests/utils";
import { meta } from "./index";
const { deleteUsers, tokens } = await getTestUsers(1);
@ -12,32 +11,28 @@ afterAll(async () => {
// /api/v1/sso
describe(meta.route, () => {
test("should return empty list", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "GET",
headers: {
Authorization: `Bearer ${tokens[0]?.accessToken}`,
},
}),
);
const response = await fakeRequest(meta.route, {
method: "GET",
headers: {
Authorization: `Bearer ${tokens[0]?.accessToken}`,
},
});
expect(response.status).toBe(200);
expect(await response.json()).toMatchObject([]);
});
test("should return an error if provider doesn't exist", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0]?.accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
issuer: "unknown",
}),
const response = await fakeRequest(meta.route, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0]?.accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
issuer: "unknown",
}),
);
});
expect(response.status).toBe(404);
expect(await response.json()).toMatchObject({

View file

@ -1,7 +1,6 @@
import { afterAll, describe, expect, test } from "bun:test";
import type { Status as ApiStatus } from "@versia/client/types";
import { config } from "~/packages/config-manager/index";
import { getTestStatuses, getTestUsers, sendTestRequest } from "~/tests/utils";
import { fakeRequest, getTestStatuses, getTestUsers } from "~/tests/utils";
import { meta } from "./favourite";
const { users, tokens, deleteUsers } = await getTestUsers(5);
@ -14,35 +13,25 @@ afterAll(async () => {
// /api/v1/statuses/:id/favourite
describe(meta.route, () => {
test("should return 401 if not authenticated", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", timeline[0].id),
config.http.base_url,
),
{
method: "POST",
},
),
const response = await fakeRequest(
meta.route.replace(":id", timeline[0].id),
{
method: "POST",
},
);
expect(response.status).toBe(401);
});
test("should favourite post", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", timeline[0].id),
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
const response = await fakeRequest(
meta.route.replace(":id", timeline[0].id),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
),
},
);
expect(response.status).toBe(200);
@ -54,18 +43,13 @@ describe(meta.route, () => {
});
test("post should be favourited when fetched", async () => {
const response = await sendTestRequest(
new Request(
new URL(
`/api/v1/statuses/${timeline[0].id}`,
config.http.base_url,
),
{
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
const response = await fakeRequest(
`/api/v1/statuses/${timeline[0].id}`,
{
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
),
},
);
expect(response.status).toBe(200);

View file

@ -1,7 +1,6 @@
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import type { Account as ApiAccount } from "@versia/client/types";
import { config } from "~/packages/config-manager/index";
import { getTestStatuses, getTestUsers, sendTestRequest } from "~/tests/utils";
import { fakeRequest, getTestStatuses, getTestUsers } from "~/tests/utils";
import { meta } from "./favourited_by";
const { users, tokens, deleteUsers } = await getTestUsers(5);
@ -13,51 +12,33 @@ afterAll(async () => {
beforeAll(async () => {
for (const status of timeline) {
await sendTestRequest(
new Request(
new URL(
`/api/v1/statuses/${status.id}/favourite`,
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
},
),
);
await fakeRequest(`/api/v1/statuses/${status.id}/favourite`, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
});
}
});
// /api/v1/statuses/:id/favourited_by
describe(meta.route, () => {
test("should return 401 if not authenticated", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", timeline[0].id),
config.http.base_url,
),
),
const response = await fakeRequest(
meta.route.replace(":id", timeline[0].id),
);
expect(response.status).toBe(401);
});
test("should return 200 with users", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", timeline[0].id),
config.http.base_url,
),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response = await fakeRequest(
meta.route.replace(":id", timeline[0].id),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response.status).toBe(200);

View file

@ -1,7 +1,6 @@
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import type { Account as ApiAccount } from "@versia/client/types";
import { config } from "~/packages/config-manager/index";
import { getTestStatuses, getTestUsers, sendTestRequest } from "~/tests/utils";
import { fakeRequest, getTestStatuses, getTestUsers } from "~/tests/utils";
import { meta } from "./reblogged_by";
const { users, tokens, deleteUsers } = await getTestUsers(5);
@ -13,51 +12,33 @@ afterAll(async () => {
beforeAll(async () => {
for (const status of timeline) {
await sendTestRequest(
new Request(
new URL(
`/api/v1/statuses/${status.id}/reblog`,
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
},
),
);
await fakeRequest(`/api/v1/statuses/${status.id}/reblog`, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
});
}
});
// /api/v1/statuses/:id/reblogged_by
describe(meta.route, () => {
test("should return 401 if not authenticated", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", timeline[0].id),
config.http.base_url,
),
),
const response = await fakeRequest(
meta.route.replace(":id", timeline[0].id),
);
expect(response.status).toBe(401);
});
test("should return 200 with users", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", timeline[0].id),
config.http.base_url,
),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response = await fakeRequest(
meta.route.replace(":id", timeline[0].id),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response.status).toBe(200);

View file

@ -1,7 +1,6 @@
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import type { Status as ApiStatus } from "@versia/client/types";
import { config } from "~/packages/config-manager/index";
import { getTestStatuses, getTestUsers, sendTestRequest } from "~/tests/utils";
import { fakeRequest, getTestStatuses, getTestUsers } from "~/tests/utils";
import { meta } from "./unfavourite";
const { users, tokens, deleteUsers } = await getTestUsers(5);
@ -14,35 +13,25 @@ afterAll(async () => {
// /api/v1/statuses/:id/unfavourite
describe(meta.route, () => {
test("should return 401 if not authenticated", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", timeline[0].id),
config.http.base_url,
),
{
method: "POST",
},
),
const response = await fakeRequest(
meta.route.replace(":id", timeline[0].id),
{
method: "POST",
},
);
expect(response.status).toBe(401);
});
test("should be able to unfavourite post that is not favourited", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", timeline[0].id),
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
const response = await fakeRequest(
meta.route.replace(":id", timeline[0].id),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
),
},
);
expect(response.status).toBe(200);
@ -50,35 +39,22 @@ describe(meta.route, () => {
test("should unfavourite post", async () => {
beforeAll(async () => {
await sendTestRequest(
new Request(
new URL(
`/api/v1/statuses/${timeline[1].id}/favourite`,
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
},
),
);
await fakeRequest(`/api/v1/statuses/${timeline[1].id}/favourite`, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
});
});
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", timeline[1].id),
config.http.base_url,
),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
const response = await fakeRequest(
meta.route.replace(":id", timeline[1].id),
{
method: "POST",
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
),
},
);
expect(response.status).toBe(200);
@ -90,18 +66,13 @@ describe(meta.route, () => {
});
test("post should not be favourited when fetched", async () => {
const response = await sendTestRequest(
new Request(
new URL(
`/api/v1/statuses/${timeline[1].id}`,
config.http.base_url,
),
{
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
const response = await fakeRequest(
`/api/v1/statuses/${timeline[1].id}`,
{
headers: {
Authorization: `Bearer ${tokens[1].accessToken}`,
},
),
},
);
expect(response.status).toBe(200);

View file

@ -4,7 +4,7 @@ import { eq } from "drizzle-orm";
import { db } from "~/drizzle/db";
import { Emojis } from "~/drizzle/schema";
import { config } from "~/packages/config-manager/index";
import { getTestUsers, sendTestRequest } from "~/tests/utils";
import { fakeRequest, getTestUsers } from "~/tests/utils";
import { meta } from "./index";
const { users, tokens, deleteUsers } = await getTestUsers(5);
@ -25,152 +25,134 @@ beforeAll(async () => {
describe(meta.route, () => {
test("should return 401 if not authenticated", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
body: new URLSearchParams({
status: "Hello, world!",
}),
const response = await fakeRequest(meta.route, {
method: "POST",
body: new URLSearchParams({
status: "Hello, world!",
}),
);
});
expect(response.status).toBe(401);
});
test("should return 422 is status is empty", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: new URLSearchParams(),
}),
);
const response = await fakeRequest(meta.route, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: new URLSearchParams(),
});
expect(response.status).toBe(422);
});
test("should return 422 is status is too long", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: new URLSearchParams({
status: "a".repeat(config.validation.max_note_size + 1),
local_only: "true",
}),
const response = await fakeRequest(meta.route, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: new URLSearchParams({
status: "a".repeat(config.validation.max_note_size + 1),
local_only: "true",
}),
);
});
expect(response.status).toBe(422);
});
test("should return 422 is visibility is invalid", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: new URLSearchParams({
status: "Hello, world!",
visibility: "invalid",
local_only: "true",
}),
const response = await fakeRequest(meta.route, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: new URLSearchParams({
status: "Hello, world!",
visibility: "invalid",
local_only: "true",
}),
);
});
expect(response.status).toBe(422);
});
test("should return 422 if scheduled_at is invalid", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: new URLSearchParams({
status: "Hello, world!",
scheduled_at: "invalid",
local_only: "true",
}),
const response = await fakeRequest(meta.route, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: new URLSearchParams({
status: "Hello, world!",
scheduled_at: "invalid",
local_only: "true",
}),
);
});
expect(response.status).toBe(422);
});
test("should return 422 is in_reply_to_id is invalid", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: new URLSearchParams({
status: "Hello, world!",
in_reply_to_id: "invalid",
local_only: "true",
}),
const response = await fakeRequest(meta.route, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: new URLSearchParams({
status: "Hello, world!",
in_reply_to_id: "invalid",
local_only: "true",
}),
);
});
expect(response.status).toBe(422);
});
test("should return 422 is quote_id is invalid", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: new URLSearchParams({
status: "Hello, world!",
quote_id: "invalid",
local_only: "true",
}),
const response = await fakeRequest(meta.route, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: new URLSearchParams({
status: "Hello, world!",
quote_id: "invalid",
local_only: "true",
}),
);
});
expect(response.status).toBe(422);
});
test("should return 422 is media_ids is invalid", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: new URLSearchParams({
status: "Hello, world!",
"media_ids[]": "invalid",
local_only: "true",
}),
const response = await fakeRequest(meta.route, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: new URLSearchParams({
status: "Hello, world!",
"media_ids[]": "invalid",
local_only: "true",
}),
);
});
expect(response.status).toBe(422);
});
test("should create a post", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: new URLSearchParams({
status: "Hello, world!",
local_only: "true",
}),
const response = await fakeRequest(meta.route, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: new URLSearchParams({
status: "Hello, world!",
local_only: "true",
}),
);
});
expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toContain(
@ -184,20 +166,18 @@ describe(meta.route, () => {
test("should create a post with visibility", async () => {
// This one uses JSON to test the interop
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
status: "Hello, world!",
visibility: "unlisted",
local_only: "true",
}),
const response = await fakeRequest(meta.route, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
status: "Hello, world!",
visibility: "unlisted",
local_only: "true",
}),
);
});
expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toContain(
@ -211,34 +191,30 @@ describe(meta.route, () => {
});
test("should create a post with a reply", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: new URLSearchParams({
status: "Hello, world!",
local_only: "true",
}),
const response = await fakeRequest(meta.route, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: new URLSearchParams({
status: "Hello, world!",
local_only: "true",
}),
);
});
const object = (await response.json()) as ApiStatus;
const response2 = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: new URLSearchParams({
status: "Hello, world again!",
in_reply_to_id: object.id,
local_only: "true",
}),
const response2 = await fakeRequest(meta.route, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: new URLSearchParams({
status: "Hello, world again!",
in_reply_to_id: object.id,
local_only: "true",
}),
);
});
expect(response2.status).toBe(200);
expect(response2.headers.get("content-type")).toContain(
@ -252,34 +228,30 @@ describe(meta.route, () => {
});
test("should create a post with a quote", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: new URLSearchParams({
status: "Hello, world!",
local_only: "true",
}),
const response = await fakeRequest(meta.route, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: new URLSearchParams({
status: "Hello, world!",
local_only: "true",
}),
);
});
const object = (await response.json()) as ApiStatus;
const response2 = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: new URLSearchParams({
status: "Hello, world again!",
quote_id: object.id,
local_only: "true",
}),
const response2 = await fakeRequest(meta.route, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: new URLSearchParams({
status: "Hello, world again!",
quote_id: object.id,
local_only: "true",
}),
);
});
expect(response2.status).toBe(200);
expect(response2.headers.get("content-type")).toContain(
@ -293,18 +265,16 @@ describe(meta.route, () => {
});
test("should correctly parse emojis", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: new URLSearchParams({
status: "Hello, :test:!",
local_only: "true",
}),
const response = await fakeRequest(meta.route, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: new URLSearchParams({
status: "Hello, :test:!",
local_only: "true",
}),
);
});
expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toContain(
@ -322,18 +292,16 @@ describe(meta.route, () => {
describe("mentions testing", () => {
test("should correctly parse @mentions", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: new URLSearchParams({
status: `Hello, @${users[1].data.username}!`,
local_only: "true",
}),
const response = await fakeRequest(meta.route, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: new URLSearchParams({
status: `Hello, @${users[1].data.username}!`,
local_only: "true",
}),
);
});
expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toContain(
@ -351,20 +319,18 @@ describe(meta.route, () => {
});
test("should correctly parse @mentions@domain", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: new URLSearchParams({
status: `Hello, @${users[1].data.username}@${
new URL(config.http.base_url).host
}!`,
local_only: "true",
}),
const response = await fakeRequest(meta.route, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: new URLSearchParams({
status: `Hello, @${users[1].data.username}@${
new URL(config.http.base_url).host
}!`,
local_only: "true",
}),
);
});
expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toContain(
@ -384,18 +350,16 @@ describe(meta.route, () => {
describe("HTML injection testing", () => {
test("should not allow HTML injection", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: new URLSearchParams({
status: "Hi! <script>alert('Hello, world!');</script>",
local_only: "true",
}),
const response = await fakeRequest(meta.route, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: new URLSearchParams({
status: "Hi! <script>alert('Hello, world!');</script>",
local_only: "true",
}),
);
});
expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toContain(
@ -410,20 +374,18 @@ describe(meta.route, () => {
});
test("should not allow HTML injection in spoiler_text", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: new URLSearchParams({
status: "Hello, world!",
spoiler_text:
"uwu <script>alert('Hello, world!');</script>",
local_only: "true",
}),
const response = await fakeRequest(meta.route, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: new URLSearchParams({
status: "Hello, world!",
spoiler_text:
"uwu <script>alert('Hello, world!');</script>",
local_only: "true",
}),
);
});
expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toContain(
@ -438,18 +400,16 @@ describe(meta.route, () => {
});
test("should rewrite all image and video src to go through proxy", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: new URLSearchParams({
status: "<img src='https://example.com/image.jpg'> <video src='https://example.com/video.mp4'> Test!",
local_only: "true",
}),
const response = await fakeRequest(meta.route, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: new URLSearchParams({
status: "<img src='https://example.com/image.jpg'> <video src='https://example.com/video.mp4'> Test!",
local_only: "true",
}),
);
});
expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toContain(

View file

@ -1,7 +1,7 @@
import { afterAll, describe, expect, test } from "bun:test";
import type { Status as ApiStatus } from "@versia/client/types";
import { config } from "~/packages/config-manager/index";
import { getTestStatuses, getTestUsers, sendTestRequest } from "~/tests/utils";
import { fakeRequest, getTestStatuses, getTestUsers } from "~/tests/utils";
import { meta } from "./home";
const { users, tokens, deleteUsers } = await getTestUsers(5);
@ -13,24 +13,17 @@ afterAll(async () => {
describe(meta.route, () => {
test("should return 401 if not authenticated", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url)),
);
const response = await fakeRequest(meta.route);
expect(response.status).toBe(401);
});
test("should correctly parse limit", async () => {
const response = await sendTestRequest(
new Request(
new URL(`${meta.route}?limit=5`, config.http.base_url),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
},
),
);
const response = await fakeRequest(`${meta.route}?limit=5`, {
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
});
expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toContain(
@ -43,13 +36,11 @@ describe(meta.route, () => {
});
test("should return 200 with statuses", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
}),
);
const response = await fakeRequest(meta.route, {
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
});
expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toContain(
@ -70,16 +61,11 @@ describe(meta.route, () => {
describe("should paginate properly", () => {
test("should send correct Link header", async () => {
const response = await sendTestRequest(
new Request(
new URL(`${meta.route}?limit=20`, config.http.base_url),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
},
),
);
const response = await fakeRequest(`${meta.route}?limit=20`, {
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
});
expect(response.headers.get("link")).toBe(
`<${config.http.base_url}/api/v1/timelines/home?limit=20&max_id=${timeline[19].id}>; rel="next"`,
@ -87,18 +73,13 @@ describe(meta.route, () => {
});
test("should correct statuses with max", async () => {
const response = await sendTestRequest(
new Request(
new URL(
`${meta.route}?limit=20&max_id=${timeline[19].id}`,
config.http.base_url,
),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response = await fakeRequest(
`${meta.route}?limit=20&max_id=${timeline[19].id}`,
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response.status).toBe(200);
@ -119,18 +100,13 @@ describe(meta.route, () => {
});
test("should send correct Link prev header", async () => {
const response = await sendTestRequest(
new Request(
new URL(
`${meta.route}?limit=20&max_id=${timeline[19].id}`,
config.http.base_url,
),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response = await fakeRequest(
`${meta.route}?limit=20&max_id=${timeline[19].id}`,
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response.headers.get("link")).toInclude(
@ -139,18 +115,13 @@ describe(meta.route, () => {
});
test("should correct statuses with min_id", async () => {
const response = await sendTestRequest(
new Request(
new URL(
`${meta.route}?limit=20&min_id=${timeline[20].id}`,
config.http.base_url,
),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response = await fakeRequest(
`${meta.route}?limit=20&min_id=${timeline[20].id}`,
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response.status).toBe(200);
@ -171,36 +142,29 @@ describe(meta.route, () => {
});
test("should not return statuses with filtered keywords", async () => {
const filterResponse = await sendTestRequest(
new Request(new URL("/api/v2/filters", config.http.base_url), {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams({
title: "Test Filter",
"context[]": "home",
filter_action: "hide",
"keywords_attributes[0][keyword]":
timeline[0].content.slice(4, 20),
"keywords_attributes[0][whole_word]": "false",
}),
const filterResponse = await fakeRequest("/api/v2/filters", {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams({
title: "Test Filter",
"context[]": "home",
filter_action: "hide",
"keywords_attributes[0][keyword]":
timeline[0].content.slice(4, 20),
"keywords_attributes[0][whole_word]": "false",
}),
);
});
expect(filterResponse.status).toBe(200);
const response = await sendTestRequest(
new Request(
new URL(`${meta.route}?limit=20`, config.http.base_url),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
},
),
);
const response = await fakeRequest(`${meta.route}?limit=20`, {
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
});
expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toContain(
@ -216,19 +180,14 @@ describe(meta.route, () => {
);
// Delete filter
const filterDeleteResponse = await sendTestRequest(
new Request(
new URL(
`/api/v2/filters/${(await filterResponse.json()).id}`,
config.http.base_url,
),
{
method: "DELETE",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const filterDeleteResponse = await fakeRequest(
`/api/v2/filters/${(await filterResponse.json()).id}`,
{
method: "DELETE",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(filterDeleteResponse.status).toBe(200);

View file

@ -1,7 +1,7 @@
import { afterAll, describe, expect, test } from "bun:test";
import type { Status as ApiStatus } from "@versia/client/types";
import { config } from "~/packages/config-manager/index";
import { getTestStatuses, getTestUsers, sendTestRequest } from "~/tests/utils";
import { fakeRequest, getTestStatuses, getTestUsers } from "~/tests/utils";
import { meta } from "./public";
const { users, tokens, deleteUsers } = await getTestUsers(5);
@ -13,16 +13,11 @@ afterAll(async () => {
describe(meta.route, () => {
test("should correctly parse limit", async () => {
const response = await sendTestRequest(
new Request(
new URL(`${meta.route}?limit=5`, config.http.base_url),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
},
),
);
const response = await fakeRequest(`${meta.route}?limit=5`, {
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
});
expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toContain(
@ -35,13 +30,11 @@ describe(meta.route, () => {
});
test("should return 200 with statuses", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
}),
);
const response = await fakeRequest(meta.route, {
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
});
expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toContain(
@ -61,18 +54,13 @@ describe(meta.route, () => {
});
test("should only fetch local statuses", async () => {
const response = await sendTestRequest(
new Request(
new URL(
`${meta.route}?limit=20&local=true`,
config.http.base_url,
),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response = await fakeRequest(
`${meta.route}?limit=20&local=true`,
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response.status).toBe(200);
@ -93,18 +81,13 @@ describe(meta.route, () => {
});
test("should only fetch remote statuses", async () => {
const response = await sendTestRequest(
new Request(
new URL(
`${meta.route}?remote=true&allow_local_only=false&only_media=false`,
config.http.base_url,
),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response = await fakeRequest(
`${meta.route}?remote=true&allow_local_only=false&only_media=false`,
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response.status).toBe(200);
@ -119,16 +102,11 @@ describe(meta.route, () => {
describe("should paginate properly", () => {
test("should send correct Link header", async () => {
const response = await sendTestRequest(
new Request(
new URL(`${meta.route}?limit=20`, config.http.base_url),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
},
),
);
const response = await fakeRequest(`${meta.route}?limit=20`, {
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
});
expect(response.headers.get("link")).toBe(
`<${config.http.base_url}/api/v1/timelines/public?limit=20&max_id=${timeline[19].id}>; rel="next"`,
@ -136,18 +114,13 @@ describe(meta.route, () => {
});
test("should correct statuses with max", async () => {
const response = await sendTestRequest(
new Request(
new URL(
`${meta.route}?limit=20&max_id=${timeline[19].id}`,
config.http.base_url,
),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response = await fakeRequest(
`${meta.route}?limit=20&max_id=${timeline[19].id}`,
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response.status).toBe(200);
@ -168,18 +141,13 @@ describe(meta.route, () => {
});
test("should send correct Link prev header", async () => {
const response = await sendTestRequest(
new Request(
new URL(
`${meta.route}?limit=20&max_id=${timeline[19].id}`,
config.http.base_url,
),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response = await fakeRequest(
`${meta.route}?limit=20&max_id=${timeline[19].id}`,
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response.headers.get("link")).toInclude(
@ -188,18 +156,13 @@ describe(meta.route, () => {
});
test("should correct statuses with min_id", async () => {
const response = await sendTestRequest(
new Request(
new URL(
`${meta.route}?limit=20&min_id=${timeline[20].id}`,
config.http.base_url,
),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response = await fakeRequest(
`${meta.route}?limit=20&min_id=${timeline[20].id}`,
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response.status).toBe(200);
@ -221,36 +184,31 @@ describe(meta.route, () => {
});
test("should not return statuses with filtered keywords", async () => {
const filterResponse = await sendTestRequest(
new Request(new URL("/api/v2/filters", config.http.base_url), {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams({
title: "Test Filter",
"context[]": "public",
filter_action: "hide",
"keywords_attributes[0][keyword]":
timeline[0].content.slice(4, 20),
"keywords_attributes[0][whole_word]": "false",
}),
const filterResponse = await fakeRequest("/api/v2/filters", {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams({
title: "Test Filter",
"context[]": "public",
filter_action: "hide",
"keywords_attributes[0][keyword]": timeline[0].content.slice(
4,
20,
),
"keywords_attributes[0][whole_word]": "false",
}),
);
});
expect(filterResponse.status).toBe(200);
const response = await sendTestRequest(
new Request(
new URL(`${meta.route}?limit=20`, config.http.base_url),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
},
),
);
const response = await fakeRequest(`${meta.route}?limit=20`, {
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
});
expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toContain(
@ -266,19 +224,14 @@ describe(meta.route, () => {
);
// Delete filter
const filterDeleteResponse = await sendTestRequest(
new Request(
new URL(
`/api/v2/filters/${(await filterResponse.json()).id}`,
config.http.base_url,
),
{
method: "DELETE",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const filterDeleteResponse = await fakeRequest(
`/api/v2/filters/${(await filterResponse.json()).id}`,
{
method: "DELETE",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(filterDeleteResponse.status).toBe(200);

View file

@ -1,27 +1,24 @@
import { afterAll, describe, expect, test } from "bun:test";
import { config } from "~/packages/config-manager/index";
import { getTestUsers, sendTestRequest } from "~/tests/utils";
import { fakeRequest, getTestUsers } from "~/tests/utils";
import { meta } from "./index";
const { tokens, deleteUsers } = await getTestUsers(2);
const response = await sendTestRequest(
new Request(new URL("/api/v2/filters", config.http.base_url), {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams({
title: "Test Filter",
"context[]": "home",
filter_action: "warn",
expires_in: "86400",
"keywords_attributes[0][keyword]": "test",
"keywords_attributes[0][whole_word]": "true",
}),
const response = await fakeRequest("/api/v2/filters", {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams({
title: "Test Filter",
"context[]": "home",
filter_action: "warn",
expires_in: "86400",
"keywords_attributes[0][keyword]": "test",
"keywords_attributes[0][whole_word]": "true",
}),
);
});
expect(response.status).toBe(200);
@ -35,36 +32,26 @@ afterAll(async () => {
// /api/v2/filters/:id
describe(meta.route, () => {
test("should return 401 if not authenticated", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", filter.id),
config.http.base_url,
),
{
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
const response = await fakeRequest(
meta.route.replace(":id", filter.id),
{
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
),
},
);
expect(response.status).toBe(401);
});
test("should get that filter", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", filter.id),
config.http.base_url,
),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const response = await fakeRequest(
meta.route.replace(":id", filter.id),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(response.status).toBe(200);
@ -83,29 +70,24 @@ describe(meta.route, () => {
});
test("should edit that filter", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", filter.id),
config.http.base_url,
),
{
method: "PUT",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams({
title: "New Filter",
"context[]": "notifications",
filter_action: "hide",
expires_in: "86400",
"keywords_attributes[0][keyword]": "new",
"keywords_attributes[0][id]": filter.keywords[0].id,
"keywords_attributes[0][whole_word]": "false",
}),
const response = await fakeRequest(
meta.route.replace(":id", filter.id),
{
method: "PUT",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
"Content-Type": "application/x-www-form-urlencoded",
},
),
body: new URLSearchParams({
title: "New Filter",
"context[]": "notifications",
filter_action: "hide",
expires_in: "86400",
"keywords_attributes[0][keyword]": "new",
"keywords_attributes[0][id]": filter.keywords[0].id,
"keywords_attributes[0][whole_word]": "false",
}),
},
);
expect(response.status).toBe(200);
@ -124,23 +106,18 @@ describe(meta.route, () => {
});
test("should delete keyword", async () => {
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", filter.id),
config.http.base_url,
),
{
method: "PUT",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: new URLSearchParams({
"keywords_attributes[0][id]": filter.keywords[0].id,
"keywords_attributes[0][_destroy]": "true",
}),
const response = await fakeRequest(
meta.route.replace(":id", filter.id),
{
method: "PUT",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
body: new URLSearchParams({
"keywords_attributes[0][id]": filter.keywords[0].id,
"keywords_attributes[0][_destroy]": "true",
}),
},
);
expect(response.status).toBe(200);
@ -150,18 +127,13 @@ describe(meta.route, () => {
expect(json.keywords).toBeEmpty();
// Get the filter again and check
const getResponse = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", filter.id),
config.http.base_url,
),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const getResponse = await fakeRequest(
meta.route.replace(":id", filter.id),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(getResponse.status).toBe(200);
@ -171,37 +143,27 @@ describe(meta.route, () => {
test("should delete filter", async () => {
const formData = new FormData();
const response = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", filter.id),
config.http.base_url,
),
{
method: "DELETE",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
body: formData,
const response = await fakeRequest(
meta.route.replace(":id", filter.id),
{
method: "DELETE",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
body: formData,
},
);
expect(response.status).toBe(200);
// Try to GET the filter again
const getResponse = await sendTestRequest(
new Request(
new URL(
meta.route.replace(":id", filter.id),
config.http.base_url,
),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
const getResponse = await fakeRequest(
meta.route.replace(":id", filter.id),
{
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
),
},
);
expect(getResponse.status).toBe(404);

View file

@ -1,6 +1,5 @@
import { afterAll, describe, expect, test } from "bun:test";
import { config } from "~/packages/config-manager/index";
import { getTestUsers, sendTestRequest } from "~/tests/utils";
import { fakeRequest, getTestUsers } from "~/tests/utils";
import { meta } from "./index";
const { tokens, deleteUsers } = await getTestUsers(2);
@ -12,21 +11,17 @@ afterAll(async () => {
// /api/v2/filters
describe(meta.route, () => {
test("should return 401 if not authenticated", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url)),
);
const response = await fakeRequest(meta.route);
expect(response.status).toBe(401);
});
test("should return user filters (none)", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
}),
);
const response = await fakeRequest(meta.route, {
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
},
});
expect(response.status).toBe(200);
@ -45,23 +40,21 @@ describe(meta.route, () => {
formData.append("keywords_attributes[0][keyword]", "test");
formData.append("keywords_attributes[0][whole_word]", "true");
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url), {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams({
title: "Test Filter",
"context[]": "home",
filter_action: "warn",
expires_in: "86400",
"keywords_attributes[0][keyword]": "test",
"keywords_attributes[0][whole_word]": "true",
}),
const response = await fakeRequest(meta.route, {
method: "POST",
headers: {
Authorization: `Bearer ${tokens[0].accessToken}`,
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams({
title: "Test Filter",
"context[]": "home",
filter_action: "warn",
expires_in: "86400",
"keywords_attributes[0][keyword]": "test",
"keywords_attributes[0][whole_word]": "true",
}),
);
});
expect(response.status).toBe(200);

View file

@ -1,14 +1,11 @@
import { describe, expect, test } from "bun:test";
import { config } from "~/packages/config-manager/index";
import { sendTestRequest } from "~/tests/utils";
import { fakeRequest } from "~/tests/utils";
import { meta } from "./index";
// /api/v2/instance
describe(meta.route, () => {
test("should return instance information", async () => {
const response = await sendTestRequest(
new Request(new URL(meta.route, config.http.base_url)),
);
const response = await fakeRequest(meta.route);
expect(response.status).toBe(200);