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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -1,8 +1,5 @@
import { afterAll, describe, expect, test } from "bun:test"; import { afterAll, describe, expect, test } from "bun:test";
import { config } from "~/packages/config-manager/index"; import { fakeRequest, getTestUsers } from "./utils";
import { getTestUsers, sendTestRequest, wrapRelativeUrl } from "./utils";
const baseUrl = config.http.base_url;
const { tokens, deleteUsers } = await getTestUsers(1); const { tokens, deleteUsers } = await getTestUsers(1);
@ -15,19 +12,14 @@ describe("API Tests", () => {
const formData = new FormData(); const formData = new FormData();
formData.append("test", "test"); formData.append("test", "test");
const response = await sendTestRequest( const response = await fakeRequest("/api/v1/statuses", {
new Request( method: "POST",
wrapRelativeUrl(`${baseUrl}/api/v1/statuses`, baseUrl), headers: {
{ Authorization: `Bearer ${tokens[0].accessToken}`,
method: "POST", "Content-Type": "multipart/form-data",
headers: { },
Authorization: `Bearer ${tokens[0].accessToken}`, body: formData,
"Content-Type": "multipart/form-data", });
},
body: formData,
},
),
);
expect(response.status).toBe(400); expect(response.status).toBe(400);
const data = await response.json(); const data = await response.json();
@ -42,9 +34,8 @@ describe("API Tests", () => {
return; return;
} }
const response = await sendTestRequest( const response = await fakeRequest(
new Request(
new URL(
"/api/v1/instance", "/api/v1/instance",
base_url.replace("https://", "http://"), base_url.replace("https://", "http://"),
), ),

View file

@ -7,9 +7,7 @@ import type {
Relationship as ApiRelationship, Relationship as ApiRelationship,
} from "@versia/client/types"; } from "@versia/client/types";
import { config } from "~/packages/config-manager/index"; import { config } from "~/packages/config-manager/index";
import { getTestUsers, sendTestRequest, wrapRelativeUrl } from "~/tests/utils"; import { fakeRequest, getTestUsers } from "~/tests/utils";
const baseUrl = config.http.base_url;
const { users, tokens, deleteUsers } = await getTestUsers(2); const { users, tokens, deleteUsers } = await getTestUsers(2);
const user = users[0]; const user = users[0];
@ -29,22 +27,17 @@ const getFormData = (object: Record<string, string | number | boolean>) =>
describe("API Tests", () => { describe("API Tests", () => {
describe("PATCH /api/v1/accounts/update_credentials", () => { describe("PATCH /api/v1/accounts/update_credentials", () => {
test("should update the authenticated user's display name", async () => { test("should update the authenticated user's display name", async () => {
const response = await sendTestRequest( const response = await fakeRequest(
new Request( "/api/v1/accounts/update_credentials",
wrapRelativeUrl( {
"/api/v1/accounts/update_credentials", method: "PATCH",
baseUrl, headers: {
), Authorization: `Bearer ${token.accessToken}`,
{
method: "PATCH",
headers: {
Authorization: `Bearer ${token.accessToken}`,
},
body: getFormData({
display_name: "New Display Name",
}),
}, },
), body: getFormData({
display_name: "New Display Name",
}),
},
); );
expect(response.status).toBe(200); expect(response.status).toBe(200);
@ -60,18 +53,13 @@ describe("API Tests", () => {
describe("GET /api/v1/accounts/verify_credentials", () => { describe("GET /api/v1/accounts/verify_credentials", () => {
test("should return the authenticated user's account information", async () => { test("should return the authenticated user's account information", async () => {
const response = await sendTestRequest( const response = await fakeRequest(
new Request( "/api/v1/accounts/verify_credentials",
wrapRelativeUrl( {
"/api/v1/accounts/verify_credentials", headers: {
baseUrl, Authorization: `Bearer ${token.accessToken}`,
),
{
headers: {
Authorization: `Bearer ${token.accessToken}`,
},
}, },
), },
); );
expect(response.status).toBe(200); expect(response.status).toBe(200);
@ -111,21 +99,16 @@ describe("API Tests", () => {
describe("POST /api/v1/accounts/:id/remove_from_followers", () => { describe("POST /api/v1/accounts/:id/remove_from_followers", () => {
test("should remove the specified user from the authenticated user's followers and return an APIRelationship object", async () => { test("should remove the specified user from the authenticated user's followers and return an APIRelationship object", async () => {
const response = await sendTestRequest( const response = await fakeRequest(
new Request( `/api/v1/accounts/${user2.id}/remove_from_followers`,
wrapRelativeUrl( {
`/api/v1/accounts/${user2.id}/remove_from_followers`, method: "POST",
baseUrl, headers: {
), Authorization: `Bearer ${token.accessToken}`,
{ "Content-Type": "application/json",
method: "POST",
headers: {
Authorization: `Bearer ${token.accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
}, },
), body: JSON.stringify({}),
},
); );
expect(response.status).toBe(200); expect(response.status).toBe(200);
@ -142,21 +125,16 @@ describe("API Tests", () => {
describe("POST /api/v1/accounts/:id/block", () => { describe("POST /api/v1/accounts/:id/block", () => {
test("should block the specified user and return an APIRelationship object", async () => { test("should block the specified user and return an APIRelationship object", async () => {
const response = await sendTestRequest( const response = await fakeRequest(
new Request( `/api/v1/accounts/${user2.id}/block`,
wrapRelativeUrl( {
`/api/v1/accounts/${user2.id}/block`, method: "POST",
baseUrl, headers: {
), Authorization: `Bearer ${token.accessToken}`,
{ "Content-Type": "application/json",
method: "POST",
headers: {
Authorization: `Bearer ${token.accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
}, },
), body: JSON.stringify({}),
},
); );
expect(response.status).toBe(200); expect(response.status).toBe(200);
@ -173,14 +151,12 @@ describe("API Tests", () => {
describe("GET /api/v1/blocks", () => { describe("GET /api/v1/blocks", () => {
test("should return an array of APIAccount objects for the user's blocked accounts", async () => { test("should return an array of APIAccount objects for the user's blocked accounts", async () => {
const response = await sendTestRequest( const response = await fakeRequest("/api/v1/blocks", {
new Request(wrapRelativeUrl("/api/v1/blocks", baseUrl), { method: "GET",
method: "GET", headers: {
headers: { Authorization: `Bearer ${token.accessToken}`,
Authorization: `Bearer ${token.accessToken}`, },
}, });
}),
);
expect(response.status).toBe(200); expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toContain( expect(response.headers.get("content-type")).toContain(
@ -196,21 +172,16 @@ describe("API Tests", () => {
describe("POST /api/v1/accounts/:id/unblock", () => { describe("POST /api/v1/accounts/:id/unblock", () => {
test("should unblock the specified user and return an APIRelationship object", async () => { test("should unblock the specified user and return an APIRelationship object", async () => {
const response = await sendTestRequest( const response = await fakeRequest(
new Request( `/api/v1/accounts/${user2.id}/unblock`,
wrapRelativeUrl( {
`/api/v1/accounts/${user2.id}/unblock`, method: "POST",
baseUrl, headers: {
), Authorization: `Bearer ${token.accessToken}`,
{ "Content-Type": "application/json",
method: "POST",
headers: {
Authorization: `Bearer ${token.accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
}, },
), body: JSON.stringify({}),
},
); );
expect(response.status).toBe(200); expect(response.status).toBe(200);
@ -227,21 +198,16 @@ describe("API Tests", () => {
describe("POST /api/v1/accounts/:id/pin", () => { describe("POST /api/v1/accounts/:id/pin", () => {
test("should pin the specified user and return an APIRelationship object", async () => { test("should pin the specified user and return an APIRelationship object", async () => {
const response = await sendTestRequest( const response = await fakeRequest(
new Request( `/api/v1/accounts/${user2.id}/pin`,
wrapRelativeUrl( {
`/api/v1/accounts/${user2.id}/pin`, method: "POST",
baseUrl, headers: {
), Authorization: `Bearer ${token.accessToken}`,
{ "Content-Type": "application/json",
method: "POST",
headers: {
Authorization: `Bearer ${token.accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
}, },
), body: JSON.stringify({}),
},
); );
expect(response.status).toBe(200); expect(response.status).toBe(200);
@ -258,21 +224,16 @@ describe("API Tests", () => {
describe("POST /api/v1/accounts/:id/unpin", () => { describe("POST /api/v1/accounts/:id/unpin", () => {
test("should unpin the specified user and return an APIRelationship object", async () => { test("should unpin the specified user and return an APIRelationship object", async () => {
const response = await sendTestRequest( const response = await fakeRequest(
new Request( `/api/v1/accounts/${user2.id}/unpin`,
wrapRelativeUrl( {
`/api/v1/accounts/${user2.id}/unpin`, method: "POST",
baseUrl, headers: {
), Authorization: `Bearer ${token.accessToken}`,
{ "Content-Type": "application/json",
method: "POST",
headers: {
Authorization: `Bearer ${token.accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
}, },
), body: JSON.stringify({}),
},
); );
expect(response.status).toBe(200); expect(response.status).toBe(200);
@ -289,21 +250,16 @@ describe("API Tests", () => {
describe("POST /api/v1/accounts/:id/note", () => { describe("POST /api/v1/accounts/:id/note", () => {
test("should update the specified account's note and return the updated account object", async () => { test("should update the specified account's note and return the updated account object", async () => {
const response = await sendTestRequest( const response = await fakeRequest(
new Request( `/api/v1/accounts/${user2.id}/note`,
wrapRelativeUrl( {
`/api/v1/accounts/${user2.id}/note`, method: "POST",
baseUrl, headers: {
), Authorization: `Bearer ${token.accessToken}`,
{ "Content-Type": "application/json",
method: "POST",
headers: {
Authorization: `Bearer ${token.accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ comment: "This is a new note" }),
}, },
), body: JSON.stringify({ comment: "This is a new note" }),
},
); );
expect(response.status).toBe(200); expect(response.status).toBe(200);
@ -320,19 +276,14 @@ describe("API Tests", () => {
describe("GET /api/v1/accounts/relationships", () => { describe("GET /api/v1/accounts/relationships", () => {
test("should return an array of APIRelationship objects for the authenticated user's relationships", async () => { test("should return an array of APIRelationship objects for the authenticated user's relationships", async () => {
const response = await sendTestRequest( const response = await fakeRequest(
new Request( `/api/v1/accounts/relationships?id[]=${user2.id}`,
wrapRelativeUrl( {
`/api/v1/accounts/relationships?id[]=${user2.id}`, method: "GET",
baseUrl, headers: {
), Authorization: `Bearer ${token.accessToken}`,
{
method: "GET",
headers: {
Authorization: `Bearer ${token.accessToken}`,
},
}, },
), },
); );
expect(response.status).toBe(200); expect(response.status).toBe(200);
@ -358,17 +309,12 @@ describe("API Tests", () => {
describe("DELETE /api/v1/profile/avatar", () => { describe("DELETE /api/v1/profile/avatar", () => {
test("should delete the avatar of the authenticated user and return the updated account object", async () => { test("should delete the avatar of the authenticated user and return the updated account object", async () => {
const response = await sendTestRequest( const response = await fakeRequest("/api/v1/profile/avatar", {
new Request( method: "DELETE",
wrapRelativeUrl("/api/v1/profile/avatar", baseUrl), headers: {
{ Authorization: `Bearer ${token.accessToken}`,
method: "DELETE", },
headers: { });
Authorization: `Bearer ${token.accessToken}`,
},
},
),
);
expect(response.status).toBe(200); expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toContain( expect(response.headers.get("content-type")).toContain(
@ -384,17 +330,12 @@ describe("API Tests", () => {
describe("DELETE /api/v1/profile/header", () => { describe("DELETE /api/v1/profile/header", () => {
test("should delete the header of the authenticated user and return the updated account object", async () => { test("should delete the header of the authenticated user and return the updated account object", async () => {
const response = await sendTestRequest( const response = await fakeRequest("/api/v1/profile/header", {
new Request( method: "DELETE",
wrapRelativeUrl("/api/v1/profile/header", baseUrl), headers: {
{ Authorization: `Bearer ${token.accessToken}`,
method: "DELETE", },
headers: { });
Authorization: `Bearer ${token.accessToken}`,
},
},
),
);
expect(response.status).toBe(200); expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toContain( expect(response.headers.get("content-type")).toContain(
@ -410,21 +351,16 @@ describe("API Tests", () => {
describe("GET /api/v1/accounts/familiar_followers", () => { describe("GET /api/v1/accounts/familiar_followers", () => {
test("should follow the user", async () => { test("should follow the user", async () => {
const response = await sendTestRequest( const response = await fakeRequest(
new Request( `/api/v1/accounts/${user2.id}/follow`,
wrapRelativeUrl( {
`/api/v1/accounts/${user2.id}/follow`, method: "POST",
baseUrl, headers: {
), Authorization: `Bearer ${token.accessToken}`,
{ "Content-Type": "application/json",
method: "POST",
headers: {
Authorization: `Bearer ${token.accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({}),
}, },
), body: JSON.stringify({}),
},
); );
expect(response.status).toBe(200); expect(response.status).toBe(200);
@ -434,19 +370,14 @@ describe("API Tests", () => {
}); });
test("should return an array of objects with id and accounts properties, where id is a string and accounts is an array of APIAccount objects", async () => { test("should return an array of objects with id and accounts properties, where id is a string and accounts is an array of APIAccount objects", async () => {
const response = await sendTestRequest( const response = await fakeRequest(
new Request( `/api/v1/accounts/familiar_followers?id[]=${user2.id}`,
wrapRelativeUrl( {
`/api/v1/accounts/familiar_followers?id[]=${user2.id}`, method: "GET",
baseUrl, headers: {
), Authorization: `Bearer ${token.accessToken}`,
{
method: "GET",
headers: {
Authorization: `Bearer ${token.accessToken}`,
},
}, },
), },
); );
expect(response.status).toBe(200); expect(response.status).toBe(200);

View file

@ -7,10 +7,7 @@ import type {
Context as ApiContext, Context as ApiContext,
Status as ApiStatus, Status as ApiStatus,
} from "@versia/client/types"; } from "@versia/client/types";
import { config } from "~/packages/config-manager/index"; import { fakeRequest, getTestUsers } from "~/tests/utils";
import { getTestUsers, sendTestRequest, wrapRelativeUrl } from "~/tests/utils";
const baseUrl = config.http.base_url;
const { users, tokens, deleteUsers } = await getTestUsers(1); const { users, tokens, deleteUsers } = await getTestUsers(1);
const user = users[0]; const user = users[0];
@ -29,18 +26,13 @@ describe("API Tests", () => {
const formData = new FormData(); const formData = new FormData();
formData.append("file", new Blob(["test"], { type: "text/plain" })); formData.append("file", new Blob(["test"], { type: "text/plain" }));
const response = await sendTestRequest( const response = await fakeRequest("/api/v2/media", {
new Request( method: "POST",
wrapRelativeUrl(`${baseUrl}/api/v2/media`, baseUrl), headers: {
{ Authorization: `Bearer ${token.accessToken}`,
method: "POST", },
headers: { body: formData,
Authorization: `Bearer ${token.accessToken}`, });
},
body: formData,
},
),
);
expect(response.status).toBe(202); expect(response.status).toBe(202);
expect(response.headers.get("content-type")).toContain( expect(response.headers.get("content-type")).toContain(
@ -57,23 +49,18 @@ describe("API Tests", () => {
describe("POST /api/v1/statuses", () => { describe("POST /api/v1/statuses", () => {
test("should create a new status and return an APIStatus object", async () => { test("should create a new status and return an APIStatus object", async () => {
const response = await sendTestRequest( const response = await fakeRequest("/api/v1/statuses", {
new Request( method: "POST",
wrapRelativeUrl(`${baseUrl}/api/v1/statuses`, baseUrl), headers: {
{ Authorization: `Bearer ${token.accessToken}`,
method: "POST", },
headers: { body: new URLSearchParams({
Authorization: `Bearer ${token.accessToken}`, status: "Hello, world!",
}, visibility: "public",
body: new URLSearchParams({ "media_ids[]": media1?.id ?? "",
status: "Hello, world!", local_only: "true",
visibility: "public", }),
"media_ids[]": media1?.id ?? "", });
local_only: "true",
}),
},
),
);
expect(response.status).toBe(200); expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toContain( expect(response.headers.get("content-type")).toContain(
@ -104,23 +91,18 @@ describe("API Tests", () => {
}); });
test("should create a new status in reply to the previous one", async () => { test("should create a new status in reply to the previous one", async () => {
const response = await sendTestRequest( const response = await fakeRequest("/api/v1/statuses", {
new Request( method: "POST",
wrapRelativeUrl(`${baseUrl}/api/v1/statuses`, baseUrl), headers: {
{ Authorization: `Bearer ${token.accessToken}`,
method: "POST", },
headers: { body: new URLSearchParams({
Authorization: `Bearer ${token.accessToken}`, status: "This is a reply!",
}, visibility: "public",
body: new URLSearchParams({ in_reply_to_id: status?.id ?? "",
status: "This is a reply!", local_only: "true",
visibility: "public", }),
in_reply_to_id: status?.id ?? "", });
local_only: "true",
}),
},
),
);
expect(response.status).toBe(200); expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toContain( expect(response.headers.get("content-type")).toContain(
@ -153,18 +135,13 @@ describe("API Tests", () => {
describe("GET /api/v1/statuses/:id", () => { describe("GET /api/v1/statuses/:id", () => {
test("should return the specified status object", async () => { test("should return the specified status object", async () => {
const response = await sendTestRequest( const response = await fakeRequest(
new Request( `/api/v1/statuses/${status?.id}`,
wrapRelativeUrl( {
`${baseUrl}/api/v1/statuses/${status?.id}`, headers: {
baseUrl, Authorization: `Bearer ${token.accessToken}`,
),
{
headers: {
Authorization: `Bearer ${token.accessToken}`,
},
}, },
), },
); );
expect(response.status).toBe(200); expect(response.status).toBe(200);
@ -202,19 +179,14 @@ describe("API Tests", () => {
describe("POST /api/v1/statuses/:id/reblog", () => { describe("POST /api/v1/statuses/:id/reblog", () => {
test("should reblog the specified status and return the reblogged status object", async () => { test("should reblog the specified status and return the reblogged status object", async () => {
const response = await sendTestRequest( const response = await fakeRequest(
new Request( `/api/v1/statuses/${status?.id}/reblog`,
wrapRelativeUrl( {
`${baseUrl}/api/v1/statuses/${status?.id}/reblog`, method: "POST",
baseUrl, headers: {
), Authorization: `Bearer ${token.accessToken}`,
{
method: "POST",
headers: {
Authorization: `Bearer ${token.accessToken}`,
},
}, },
), },
); );
expect(response.status).toBe(200); expect(response.status).toBe(200);
@ -232,19 +204,14 @@ describe("API Tests", () => {
describe("POST /api/v1/statuses/:id/unreblog", () => { describe("POST /api/v1/statuses/:id/unreblog", () => {
test("should unreblog the specified status and return the original status object", async () => { test("should unreblog the specified status and return the original status object", async () => {
const response = await sendTestRequest( const response = await fakeRequest(
new Request( `/api/v1/statuses/${status?.id}/unreblog`,
wrapRelativeUrl( {
`${baseUrl}/api/v1/statuses/${status?.id}/unreblog`, method: "POST",
baseUrl, headers: {
), Authorization: `Bearer ${token.accessToken}`,
{
method: "POST",
headers: {
Authorization: `Bearer ${token.accessToken}`,
},
}, },
), },
); );
expect(response.status).toBe(200); expect(response.status).toBe(200);
@ -261,18 +228,13 @@ describe("API Tests", () => {
describe("GET /api/v1/statuses/:id/context", () => { describe("GET /api/v1/statuses/:id/context", () => {
test("should return the context of the specified status", async () => { test("should return the context of the specified status", async () => {
const response = await sendTestRequest( const response = await fakeRequest(
new Request( `/api/v1/statuses/${status?.id}/context`,
wrapRelativeUrl( {
`${baseUrl}/api/v1/statuses/${status?.id}/context`, headers: {
baseUrl, Authorization: `Bearer ${token.accessToken}`,
),
{
headers: {
Authorization: `Bearer ${token.accessToken}`,
},
}, },
), },
); );
expect(response.status).toBe(200); expect(response.status).toBe(200);
@ -292,19 +254,14 @@ describe("API Tests", () => {
describe("GET /api/v1/accounts/:id/statuses", () => { describe("GET /api/v1/accounts/:id/statuses", () => {
test("should return the statuses of the specified user", async () => { test("should return the statuses of the specified user", async () => {
const response = await sendTestRequest( const response = await fakeRequest(
new Request( `/api/v1/accounts/${user.id}/statuses`,
wrapRelativeUrl( {
`${baseUrl}/api/v1/accounts/${user.id}/statuses`, method: "GET",
baseUrl, headers: {
), Authorization: `Bearer ${token.accessToken}`,
{
method: "GET",
headers: {
Authorization: `Bearer ${token.accessToken}`,
},
}, },
), },
); );
expect(response.status).toBe(200); expect(response.status).toBe(200);
@ -327,19 +284,14 @@ describe("API Tests", () => {
describe("POST /api/v1/statuses/:id/favourite", () => { describe("POST /api/v1/statuses/:id/favourite", () => {
test("should favourite the specified status object", async () => { test("should favourite the specified status object", async () => {
const response = await sendTestRequest( const response = await fakeRequest(
new Request( `/api/v1/statuses/${status?.id}/favourite`,
wrapRelativeUrl( {
`${baseUrl}/api/v1/statuses/${status?.id}/favourite`, method: "POST",
baseUrl, headers: {
), Authorization: `Bearer ${token.accessToken}`,
{
method: "POST",
headers: {
Authorization: `Bearer ${token.accessToken}`,
},
}, },
), },
); );
expect(response.status).toBe(200); expect(response.status).toBe(200);
@ -349,19 +301,14 @@ describe("API Tests", () => {
describe("POST /api/v1/statuses/:id/unfavourite", () => { describe("POST /api/v1/statuses/:id/unfavourite", () => {
test("should unfavourite the specified status object", async () => { test("should unfavourite the specified status object", async () => {
// Unfavourite the status // Unfavourite the status
const response = await sendTestRequest( const response = await fakeRequest(
new Request( `/api/v1/statuses/${status?.id}/unfavourite`,
wrapRelativeUrl( {
`${baseUrl}/api/v1/statuses/${status?.id}/unfavourite`, method: "POST",
baseUrl, headers: {
), Authorization: `Bearer ${token.accessToken}`,
{
method: "POST",
headers: {
Authorization: `Bearer ${token.accessToken}`,
},
}, },
), },
); );
expect(response.status).toBe(200); expect(response.status).toBe(200);
@ -378,19 +325,14 @@ describe("API Tests", () => {
describe("DELETE /api/v1/statuses/:id", () => { describe("DELETE /api/v1/statuses/:id", () => {
test("should delete the specified status object", async () => { test("should delete the specified status object", async () => {
const response = await sendTestRequest( const response = await fakeRequest(
new Request( `/api/v1/statuses/${status?.id}`,
wrapRelativeUrl( {
`${baseUrl}/api/v1/statuses/${status?.id}`, method: "DELETE",
baseUrl, headers: {
), Authorization: `Bearer ${token.accessToken}`,
{
method: "DELETE",
headers: {
Authorization: `Bearer ${token.accessToken}`,
},
}, },
), },
); );
expect(response.status).toBe(200); expect(response.status).toBe(200);

View file

@ -6,10 +6,7 @@ import type {
Application as ApiApplication, Application as ApiApplication,
Token as ApiToken, Token as ApiToken,
} from "@versia/client/types"; } from "@versia/client/types";
import { config } from "~/packages/config-manager"; import { fakeRequest, getTestUsers } from "./utils";
import { getTestUsers, sendTestRequest, wrapRelativeUrl } from "./utils";
const baseUrl = config.http.base_url;
let clientId: string; let clientId: string;
let clientSecret: string; let clientSecret: string;
@ -31,20 +28,18 @@ describe("POST /api/v1/apps/", () => {
formData.append("redirect_uris", "https://example.com"); formData.append("redirect_uris", "https://example.com");
formData.append("scopes", "read write"); formData.append("scopes", "read write");
const response = await sendTestRequest( const response = await fakeRequest("/api/v1/apps", {
new Request(new URL("/api/v1/apps", config.http.base_url), { method: "POST",
method: "POST", headers: {
headers: { "Content-Type": "application/json",
"Content-Type": "application/json", },
}, body: JSON.stringify({
body: JSON.stringify({ client_name: "Test Application",
client_name: "Test Application", website: "https://example.com",
website: "https://example.com", redirect_uris: "https://example.com",
redirect_uris: "https://example.com", scopes: "read write",
scopes: "read write",
}),
}), }),
); });
expect(response.status).toBe(200); expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toContain( expect(response.headers.get("content-type")).toContain(
@ -75,17 +70,12 @@ describe("POST /api/auth/login/", () => {
formData.append("identifier", users[0]?.data.email ?? ""); formData.append("identifier", users[0]?.data.email ?? "");
formData.append("password", passwords[0]); formData.append("password", passwords[0]);
const response = await sendTestRequest( const response = await fakeRequest(
new Request( `/api/auth/login?client_id=${clientId}&redirect_uri=https://example.com&response_type=code&scope=read+write`,
new URL( {
`/api/auth/login?client_id=${clientId}&redirect_uri=https://example.com&response_type=code&scope=read+write`, method: "POST",
baseUrl, body: formData,
), },
{
method: "POST",
body: formData,
},
),
); );
expect(response.status).toBe(302); expect(response.status).toBe(302);
@ -98,22 +88,20 @@ describe("POST /api/auth/login/", () => {
describe("GET /oauth/authorize/", () => { describe("GET /oauth/authorize/", () => {
test("should get a code", async () => { test("should get a code", async () => {
const response = await sendTestRequest( const response = await fakeRequest("/oauth/authorize", {
new Request(new URL("/oauth/authorize", baseUrl), { method: "POST",
method: "POST", headers: {
headers: { Cookie: `jwt=${jwt}`,
Cookie: `jwt=${jwt}`, },
}, body: new URLSearchParams({
body: new URLSearchParams({ client_id: clientId,
client_id: clientId, client_secret: clientSecret,
client_secret: clientSecret, redirect_uri: "https://example.com",
redirect_uri: "https://example.com", response_type: "code",
response_type: "code", scope: "read write",
scope: "read write", max_age: "604800",
max_age: "604800",
}),
}), }),
); });
expect(response.status).toBe(302); expect(response.status).toBe(302);
expect(response.headers.get("location")).toBeDefined(); expect(response.headers.get("location")).toBeDefined();
@ -130,23 +118,21 @@ describe("GET /oauth/authorize/", () => {
describe("POST /oauth/token/", () => { describe("POST /oauth/token/", () => {
test("should get an access token", async () => { test("should get an access token", async () => {
const response = await sendTestRequest( const response = await fakeRequest("/oauth/token", {
new Request(wrapRelativeUrl("/oauth/token", baseUrl), { method: "POST",
method: "POST", headers: {
headers: { Authorization: `Bearer ${jwt}`,
Authorization: `Bearer ${jwt}`, "Content-Type": "application/x-www-form-urlencoded",
"Content-Type": "application/x-www-form-urlencoded", },
}, body: new URLSearchParams({
body: new URLSearchParams({ grant_type: "authorization_code",
grant_type: "authorization_code", code,
code, redirect_uri: "https://example.com",
redirect_uri: "https://example.com", client_id: clientId,
client_id: clientId, client_secret: clientSecret,
client_secret: clientSecret, scope: "read write",
scope: "read write",
}),
}), }),
); });
const json = await response.json(); const json = await response.json();
@ -170,16 +156,11 @@ describe("POST /oauth/token/", () => {
describe("GET /api/v1/apps/verify_credentials", () => { describe("GET /api/v1/apps/verify_credentials", () => {
test("should return the authenticated application's credentials", async () => { test("should return the authenticated application's credentials", async () => {
const response = await sendTestRequest( const response = await fakeRequest("/api/v1/apps/verify_credentials", {
new Request( headers: {
wrapRelativeUrl("/api/v1/apps/verify_credentials", baseUrl), Authorization: `Bearer ${token.access_token}`,
{ },
headers: { });
Authorization: `Bearer ${token.access_token}`,
},
},
),
);
expect(response.status).toBe(200); expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toContain( expect(response.headers.get("content-type")).toContain(

View file

@ -19,18 +19,10 @@ if (config.sonic.enabled) {
const app = await appFactory(); const app = await appFactory();
/** export function fakeRequest(url: URL | string, init?: RequestInit) {
* This allows us to send a test request to the server even when it isnt running return Promise.resolve(
* @param req Request to send app.fetch(new Request(new URL(url, config.http.base_url), init)),
* @returns Response from the server );
*/
export function sendTestRequest(req: Request): Promise<Response> {
// return fetch(req);
return Promise.resolve(app.fetch(req));
}
export function wrapRelativeUrl(url: string, baseUrl: string) {
return new URL(url, baseUrl);
} }
export const deleteOldTestUsers = async () => { export const deleteOldTestUsers = async () => {