mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
refactor: ♻️ Refactor tests to use a simpler syntax
This commit is contained in:
parent
6ed1bd747f
commit
47c666894c
49 changed files with 1870 additions and 2973 deletions
|
|
@ -1,8 +1,5 @@
|
|||
import { afterAll, describe, expect, test } from "bun:test";
|
||||
import { config } from "~/packages/config-manager/index";
|
||||
import { getTestUsers, sendTestRequest, wrapRelativeUrl } from "./utils";
|
||||
|
||||
const baseUrl = config.http.base_url;
|
||||
import { fakeRequest, getTestUsers } from "./utils";
|
||||
|
||||
const { tokens, deleteUsers } = await getTestUsers(1);
|
||||
|
||||
|
|
@ -15,19 +12,14 @@ describe("API Tests", () => {
|
|||
const formData = new FormData();
|
||||
formData.append("test", "test");
|
||||
|
||||
const response = await sendTestRequest(
|
||||
new Request(
|
||||
wrapRelativeUrl(`${baseUrl}/api/v1/statuses`, baseUrl),
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${tokens[0].accessToken}`,
|
||||
"Content-Type": "multipart/form-data",
|
||||
},
|
||||
body: formData,
|
||||
},
|
||||
),
|
||||
);
|
||||
const response = await fakeRequest("/api/v1/statuses", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${tokens[0].accessToken}`,
|
||||
"Content-Type": "multipart/form-data",
|
||||
},
|
||||
body: formData,
|
||||
});
|
||||
|
||||
expect(response.status).toBe(400);
|
||||
const data = await response.json();
|
||||
|
|
@ -42,9 +34,8 @@ describe("API Tests", () => {
|
|||
return;
|
||||
}
|
||||
|
||||
const response = await sendTestRequest(
|
||||
new Request(
|
||||
new URL(
|
||||
const response = await fakeRequest(
|
||||
|
||||
"/api/v1/instance",
|
||||
base_url.replace("https://", "http://"),
|
||||
),
|
||||
|
|
|
|||
|
|
@ -7,9 +7,7 @@ import type {
|
|||
Relationship as ApiRelationship,
|
||||
} from "@versia/client/types";
|
||||
import { config } from "~/packages/config-manager/index";
|
||||
import { getTestUsers, sendTestRequest, wrapRelativeUrl } from "~/tests/utils";
|
||||
|
||||
const baseUrl = config.http.base_url;
|
||||
import { fakeRequest, getTestUsers } from "~/tests/utils";
|
||||
|
||||
const { users, tokens, deleteUsers } = await getTestUsers(2);
|
||||
const user = users[0];
|
||||
|
|
@ -29,22 +27,17 @@ const getFormData = (object: Record<string, string | number | boolean>) =>
|
|||
describe("API Tests", () => {
|
||||
describe("PATCH /api/v1/accounts/update_credentials", () => {
|
||||
test("should update the authenticated user's display name", async () => {
|
||||
const response = await sendTestRequest(
|
||||
new Request(
|
||||
wrapRelativeUrl(
|
||||
"/api/v1/accounts/update_credentials",
|
||||
baseUrl,
|
||||
),
|
||||
{
|
||||
method: "PATCH",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
},
|
||||
body: getFormData({
|
||||
display_name: "New Display Name",
|
||||
}),
|
||||
const response = await fakeRequest(
|
||||
"/api/v1/accounts/update_credentials",
|
||||
{
|
||||
method: "PATCH",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
},
|
||||
),
|
||||
body: getFormData({
|
||||
display_name: "New Display Name",
|
||||
}),
|
||||
},
|
||||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
|
|
@ -60,18 +53,13 @@ describe("API Tests", () => {
|
|||
|
||||
describe("GET /api/v1/accounts/verify_credentials", () => {
|
||||
test("should return the authenticated user's account information", async () => {
|
||||
const response = await sendTestRequest(
|
||||
new Request(
|
||||
wrapRelativeUrl(
|
||||
"/api/v1/accounts/verify_credentials",
|
||||
baseUrl,
|
||||
),
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
},
|
||||
const response = await fakeRequest(
|
||||
"/api/v1/accounts/verify_credentials",
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
},
|
||||
),
|
||||
},
|
||||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
|
|
@ -111,21 +99,16 @@ describe("API Tests", () => {
|
|||
|
||||
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 () => {
|
||||
const response = await sendTestRequest(
|
||||
new Request(
|
||||
wrapRelativeUrl(
|
||||
`/api/v1/accounts/${user2.id}/remove_from_followers`,
|
||||
baseUrl,
|
||||
),
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({}),
|
||||
const response = await fakeRequest(
|
||||
`/api/v1/accounts/${user2.id}/remove_from_followers`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
),
|
||||
body: JSON.stringify({}),
|
||||
},
|
||||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
|
|
@ -142,21 +125,16 @@ describe("API Tests", () => {
|
|||
|
||||
describe("POST /api/v1/accounts/:id/block", () => {
|
||||
test("should block the specified user and return an APIRelationship object", async () => {
|
||||
const response = await sendTestRequest(
|
||||
new Request(
|
||||
wrapRelativeUrl(
|
||||
`/api/v1/accounts/${user2.id}/block`,
|
||||
baseUrl,
|
||||
),
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({}),
|
||||
const response = await fakeRequest(
|
||||
`/api/v1/accounts/${user2.id}/block`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
),
|
||||
body: JSON.stringify({}),
|
||||
},
|
||||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
|
|
@ -173,14 +151,12 @@ describe("API Tests", () => {
|
|||
|
||||
describe("GET /api/v1/blocks", () => {
|
||||
test("should return an array of APIAccount objects for the user's blocked accounts", async () => {
|
||||
const response = await sendTestRequest(
|
||||
new Request(wrapRelativeUrl("/api/v1/blocks", baseUrl), {
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
},
|
||||
}),
|
||||
);
|
||||
const response = await fakeRequest("/api/v1/blocks", {
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("content-type")).toContain(
|
||||
|
|
@ -196,21 +172,16 @@ describe("API Tests", () => {
|
|||
|
||||
describe("POST /api/v1/accounts/:id/unblock", () => {
|
||||
test("should unblock the specified user and return an APIRelationship object", async () => {
|
||||
const response = await sendTestRequest(
|
||||
new Request(
|
||||
wrapRelativeUrl(
|
||||
`/api/v1/accounts/${user2.id}/unblock`,
|
||||
baseUrl,
|
||||
),
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({}),
|
||||
const response = await fakeRequest(
|
||||
`/api/v1/accounts/${user2.id}/unblock`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
),
|
||||
body: JSON.stringify({}),
|
||||
},
|
||||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
|
|
@ -227,21 +198,16 @@ describe("API Tests", () => {
|
|||
|
||||
describe("POST /api/v1/accounts/:id/pin", () => {
|
||||
test("should pin the specified user and return an APIRelationship object", async () => {
|
||||
const response = await sendTestRequest(
|
||||
new Request(
|
||||
wrapRelativeUrl(
|
||||
`/api/v1/accounts/${user2.id}/pin`,
|
||||
baseUrl,
|
||||
),
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({}),
|
||||
const response = await fakeRequest(
|
||||
`/api/v1/accounts/${user2.id}/pin`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
),
|
||||
body: JSON.stringify({}),
|
||||
},
|
||||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
|
|
@ -258,21 +224,16 @@ describe("API Tests", () => {
|
|||
|
||||
describe("POST /api/v1/accounts/:id/unpin", () => {
|
||||
test("should unpin the specified user and return an APIRelationship object", async () => {
|
||||
const response = await sendTestRequest(
|
||||
new Request(
|
||||
wrapRelativeUrl(
|
||||
`/api/v1/accounts/${user2.id}/unpin`,
|
||||
baseUrl,
|
||||
),
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({}),
|
||||
const response = await fakeRequest(
|
||||
`/api/v1/accounts/${user2.id}/unpin`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
),
|
||||
body: JSON.stringify({}),
|
||||
},
|
||||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
|
|
@ -289,21 +250,16 @@ describe("API Tests", () => {
|
|||
|
||||
describe("POST /api/v1/accounts/:id/note", () => {
|
||||
test("should update the specified account's note and return the updated account object", async () => {
|
||||
const response = await sendTestRequest(
|
||||
new Request(
|
||||
wrapRelativeUrl(
|
||||
`/api/v1/accounts/${user2.id}/note`,
|
||||
baseUrl,
|
||||
),
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({ comment: "This is a new note" }),
|
||||
const response = await fakeRequest(
|
||||
`/api/v1/accounts/${user2.id}/note`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
),
|
||||
body: JSON.stringify({ comment: "This is a new note" }),
|
||||
},
|
||||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
|
|
@ -320,19 +276,14 @@ describe("API Tests", () => {
|
|||
|
||||
describe("GET /api/v1/accounts/relationships", () => {
|
||||
test("should return an array of APIRelationship objects for the authenticated user's relationships", async () => {
|
||||
const response = await sendTestRequest(
|
||||
new Request(
|
||||
wrapRelativeUrl(
|
||||
`/api/v1/accounts/relationships?id[]=${user2.id}`,
|
||||
baseUrl,
|
||||
),
|
||||
{
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
},
|
||||
const response = await fakeRequest(
|
||||
`/api/v1/accounts/relationships?id[]=${user2.id}`,
|
||||
{
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
},
|
||||
),
|
||||
},
|
||||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
|
|
@ -358,17 +309,12 @@ describe("API Tests", () => {
|
|||
|
||||
describe("DELETE /api/v1/profile/avatar", () => {
|
||||
test("should delete the avatar of the authenticated user and return the updated account object", async () => {
|
||||
const response = await sendTestRequest(
|
||||
new Request(
|
||||
wrapRelativeUrl("/api/v1/profile/avatar", baseUrl),
|
||||
{
|
||||
method: "DELETE",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
},
|
||||
},
|
||||
),
|
||||
);
|
||||
const response = await fakeRequest("/api/v1/profile/avatar", {
|
||||
method: "DELETE",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("content-type")).toContain(
|
||||
|
|
@ -384,17 +330,12 @@ describe("API Tests", () => {
|
|||
|
||||
describe("DELETE /api/v1/profile/header", () => {
|
||||
test("should delete the header of the authenticated user and return the updated account object", async () => {
|
||||
const response = await sendTestRequest(
|
||||
new Request(
|
||||
wrapRelativeUrl("/api/v1/profile/header", baseUrl),
|
||||
{
|
||||
method: "DELETE",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
},
|
||||
},
|
||||
),
|
||||
);
|
||||
const response = await fakeRequest("/api/v1/profile/header", {
|
||||
method: "DELETE",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("content-type")).toContain(
|
||||
|
|
@ -410,21 +351,16 @@ describe("API Tests", () => {
|
|||
|
||||
describe("GET /api/v1/accounts/familiar_followers", () => {
|
||||
test("should follow the user", async () => {
|
||||
const response = await sendTestRequest(
|
||||
new Request(
|
||||
wrapRelativeUrl(
|
||||
`/api/v1/accounts/${user2.id}/follow`,
|
||||
baseUrl,
|
||||
),
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({}),
|
||||
const response = await fakeRequest(
|
||||
`/api/v1/accounts/${user2.id}/follow`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
),
|
||||
body: JSON.stringify({}),
|
||||
},
|
||||
);
|
||||
|
||||
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 () => {
|
||||
const response = await sendTestRequest(
|
||||
new Request(
|
||||
wrapRelativeUrl(
|
||||
`/api/v1/accounts/familiar_followers?id[]=${user2.id}`,
|
||||
baseUrl,
|
||||
),
|
||||
{
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
},
|
||||
const response = await fakeRequest(
|
||||
`/api/v1/accounts/familiar_followers?id[]=${user2.id}`,
|
||||
{
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
},
|
||||
),
|
||||
},
|
||||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
|
|
|
|||
|
|
@ -7,10 +7,7 @@ import type {
|
|||
Context as ApiContext,
|
||||
Status as ApiStatus,
|
||||
} from "@versia/client/types";
|
||||
import { config } from "~/packages/config-manager/index";
|
||||
import { getTestUsers, sendTestRequest, wrapRelativeUrl } from "~/tests/utils";
|
||||
|
||||
const baseUrl = config.http.base_url;
|
||||
import { fakeRequest, getTestUsers } from "~/tests/utils";
|
||||
|
||||
const { users, tokens, deleteUsers } = await getTestUsers(1);
|
||||
const user = users[0];
|
||||
|
|
@ -29,18 +26,13 @@ describe("API Tests", () => {
|
|||
const formData = new FormData();
|
||||
formData.append("file", new Blob(["test"], { type: "text/plain" }));
|
||||
|
||||
const response = await sendTestRequest(
|
||||
new Request(
|
||||
wrapRelativeUrl(`${baseUrl}/api/v2/media`, baseUrl),
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
},
|
||||
body: formData,
|
||||
},
|
||||
),
|
||||
);
|
||||
const response = await fakeRequest("/api/v2/media", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
},
|
||||
body: formData,
|
||||
});
|
||||
|
||||
expect(response.status).toBe(202);
|
||||
expect(response.headers.get("content-type")).toContain(
|
||||
|
|
@ -57,23 +49,18 @@ describe("API Tests", () => {
|
|||
|
||||
describe("POST /api/v1/statuses", () => {
|
||||
test("should create a new status and return an APIStatus object", async () => {
|
||||
const response = await sendTestRequest(
|
||||
new Request(
|
||||
wrapRelativeUrl(`${baseUrl}/api/v1/statuses`, baseUrl),
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
},
|
||||
body: new URLSearchParams({
|
||||
status: "Hello, world!",
|
||||
visibility: "public",
|
||||
"media_ids[]": media1?.id ?? "",
|
||||
local_only: "true",
|
||||
}),
|
||||
},
|
||||
),
|
||||
);
|
||||
const response = await fakeRequest("/api/v1/statuses", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
},
|
||||
body: new URLSearchParams({
|
||||
status: "Hello, world!",
|
||||
visibility: "public",
|
||||
"media_ids[]": media1?.id ?? "",
|
||||
local_only: "true",
|
||||
}),
|
||||
});
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
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 () => {
|
||||
const response = await sendTestRequest(
|
||||
new Request(
|
||||
wrapRelativeUrl(`${baseUrl}/api/v1/statuses`, baseUrl),
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
},
|
||||
body: new URLSearchParams({
|
||||
status: "This is a reply!",
|
||||
visibility: "public",
|
||||
in_reply_to_id: status?.id ?? "",
|
||||
local_only: "true",
|
||||
}),
|
||||
},
|
||||
),
|
||||
);
|
||||
const response = await fakeRequest("/api/v1/statuses", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
},
|
||||
body: new URLSearchParams({
|
||||
status: "This is a reply!",
|
||||
visibility: "public",
|
||||
in_reply_to_id: status?.id ?? "",
|
||||
local_only: "true",
|
||||
}),
|
||||
});
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("content-type")).toContain(
|
||||
|
|
@ -153,18 +135,13 @@ describe("API Tests", () => {
|
|||
|
||||
describe("GET /api/v1/statuses/:id", () => {
|
||||
test("should return the specified status object", async () => {
|
||||
const response = await sendTestRequest(
|
||||
new Request(
|
||||
wrapRelativeUrl(
|
||||
`${baseUrl}/api/v1/statuses/${status?.id}`,
|
||||
baseUrl,
|
||||
),
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
},
|
||||
const response = await fakeRequest(
|
||||
`/api/v1/statuses/${status?.id}`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
},
|
||||
),
|
||||
},
|
||||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
|
|
@ -202,19 +179,14 @@ describe("API Tests", () => {
|
|||
|
||||
describe("POST /api/v1/statuses/:id/reblog", () => {
|
||||
test("should reblog the specified status and return the reblogged status object", async () => {
|
||||
const response = await sendTestRequest(
|
||||
new Request(
|
||||
wrapRelativeUrl(
|
||||
`${baseUrl}/api/v1/statuses/${status?.id}/reblog`,
|
||||
baseUrl,
|
||||
),
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
},
|
||||
const response = await fakeRequest(
|
||||
`/api/v1/statuses/${status?.id}/reblog`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
},
|
||||
),
|
||||
},
|
||||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
|
|
@ -232,19 +204,14 @@ describe("API Tests", () => {
|
|||
|
||||
describe("POST /api/v1/statuses/:id/unreblog", () => {
|
||||
test("should unreblog the specified status and return the original status object", async () => {
|
||||
const response = await sendTestRequest(
|
||||
new Request(
|
||||
wrapRelativeUrl(
|
||||
`${baseUrl}/api/v1/statuses/${status?.id}/unreblog`,
|
||||
baseUrl,
|
||||
),
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
},
|
||||
const response = await fakeRequest(
|
||||
`/api/v1/statuses/${status?.id}/unreblog`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
},
|
||||
),
|
||||
},
|
||||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
|
|
@ -261,18 +228,13 @@ describe("API Tests", () => {
|
|||
|
||||
describe("GET /api/v1/statuses/:id/context", () => {
|
||||
test("should return the context of the specified status", async () => {
|
||||
const response = await sendTestRequest(
|
||||
new Request(
|
||||
wrapRelativeUrl(
|
||||
`${baseUrl}/api/v1/statuses/${status?.id}/context`,
|
||||
baseUrl,
|
||||
),
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
},
|
||||
const response = await fakeRequest(
|
||||
`/api/v1/statuses/${status?.id}/context`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
},
|
||||
),
|
||||
},
|
||||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
|
|
@ -292,19 +254,14 @@ describe("API Tests", () => {
|
|||
|
||||
describe("GET /api/v1/accounts/:id/statuses", () => {
|
||||
test("should return the statuses of the specified user", async () => {
|
||||
const response = await sendTestRequest(
|
||||
new Request(
|
||||
wrapRelativeUrl(
|
||||
`${baseUrl}/api/v1/accounts/${user.id}/statuses`,
|
||||
baseUrl,
|
||||
),
|
||||
{
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
},
|
||||
const response = await fakeRequest(
|
||||
`/api/v1/accounts/${user.id}/statuses`,
|
||||
{
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
},
|
||||
),
|
||||
},
|
||||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
|
|
@ -327,19 +284,14 @@ describe("API Tests", () => {
|
|||
|
||||
describe("POST /api/v1/statuses/:id/favourite", () => {
|
||||
test("should favourite the specified status object", async () => {
|
||||
const response = await sendTestRequest(
|
||||
new Request(
|
||||
wrapRelativeUrl(
|
||||
`${baseUrl}/api/v1/statuses/${status?.id}/favourite`,
|
||||
baseUrl,
|
||||
),
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
},
|
||||
const response = await fakeRequest(
|
||||
`/api/v1/statuses/${status?.id}/favourite`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
},
|
||||
),
|
||||
},
|
||||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
|
|
@ -349,19 +301,14 @@ describe("API Tests", () => {
|
|||
describe("POST /api/v1/statuses/:id/unfavourite", () => {
|
||||
test("should unfavourite the specified status object", async () => {
|
||||
// Unfavourite the status
|
||||
const response = await sendTestRequest(
|
||||
new Request(
|
||||
wrapRelativeUrl(
|
||||
`${baseUrl}/api/v1/statuses/${status?.id}/unfavourite`,
|
||||
baseUrl,
|
||||
),
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
},
|
||||
const response = await fakeRequest(
|
||||
`/api/v1/statuses/${status?.id}/unfavourite`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
},
|
||||
),
|
||||
},
|
||||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
|
|
@ -378,19 +325,14 @@ describe("API Tests", () => {
|
|||
|
||||
describe("DELETE /api/v1/statuses/:id", () => {
|
||||
test("should delete the specified status object", async () => {
|
||||
const response = await sendTestRequest(
|
||||
new Request(
|
||||
wrapRelativeUrl(
|
||||
`${baseUrl}/api/v1/statuses/${status?.id}`,
|
||||
baseUrl,
|
||||
),
|
||||
{
|
||||
method: "DELETE",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
},
|
||||
const response = await fakeRequest(
|
||||
`/api/v1/statuses/${status?.id}`,
|
||||
{
|
||||
method: "DELETE",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.accessToken}`,
|
||||
},
|
||||
),
|
||||
},
|
||||
);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
|
|
|
|||
|
|
@ -6,10 +6,7 @@ import type {
|
|||
Application as ApiApplication,
|
||||
Token as ApiToken,
|
||||
} from "@versia/client/types";
|
||||
import { config } from "~/packages/config-manager";
|
||||
import { getTestUsers, sendTestRequest, wrapRelativeUrl } from "./utils";
|
||||
|
||||
const baseUrl = config.http.base_url;
|
||||
import { fakeRequest, getTestUsers } from "./utils";
|
||||
|
||||
let clientId: string;
|
||||
let clientSecret: string;
|
||||
|
|
@ -31,20 +28,18 @@ describe("POST /api/v1/apps/", () => {
|
|||
formData.append("redirect_uris", "https://example.com");
|
||||
formData.append("scopes", "read write");
|
||||
|
||||
const response = await sendTestRequest(
|
||||
new Request(new URL("/api/v1/apps", config.http.base_url), {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
client_name: "Test Application",
|
||||
website: "https://example.com",
|
||||
redirect_uris: "https://example.com",
|
||||
scopes: "read write",
|
||||
}),
|
||||
const response = await fakeRequest("/api/v1/apps", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
client_name: "Test Application",
|
||||
website: "https://example.com",
|
||||
redirect_uris: "https://example.com",
|
||||
scopes: "read write",
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
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("password", passwords[0]);
|
||||
|
||||
const response = await sendTestRequest(
|
||||
new Request(
|
||||
new URL(
|
||||
`/api/auth/login?client_id=${clientId}&redirect_uri=https://example.com&response_type=code&scope=read+write`,
|
||||
baseUrl,
|
||||
),
|
||||
{
|
||||
method: "POST",
|
||||
body: formData,
|
||||
},
|
||||
),
|
||||
const response = await fakeRequest(
|
||||
`/api/auth/login?client_id=${clientId}&redirect_uri=https://example.com&response_type=code&scope=read+write`,
|
||||
{
|
||||
method: "POST",
|
||||
body: formData,
|
||||
},
|
||||
);
|
||||
|
||||
expect(response.status).toBe(302);
|
||||
|
|
@ -98,22 +88,20 @@ describe("POST /api/auth/login/", () => {
|
|||
|
||||
describe("GET /oauth/authorize/", () => {
|
||||
test("should get a code", async () => {
|
||||
const response = await sendTestRequest(
|
||||
new Request(new URL("/oauth/authorize", baseUrl), {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Cookie: `jwt=${jwt}`,
|
||||
},
|
||||
body: new URLSearchParams({
|
||||
client_id: clientId,
|
||||
client_secret: clientSecret,
|
||||
redirect_uri: "https://example.com",
|
||||
response_type: "code",
|
||||
scope: "read write",
|
||||
max_age: "604800",
|
||||
}),
|
||||
const response = await fakeRequest("/oauth/authorize", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Cookie: `jwt=${jwt}`,
|
||||
},
|
||||
body: new URLSearchParams({
|
||||
client_id: clientId,
|
||||
client_secret: clientSecret,
|
||||
redirect_uri: "https://example.com",
|
||||
response_type: "code",
|
||||
scope: "read write",
|
||||
max_age: "604800",
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
expect(response.status).toBe(302);
|
||||
expect(response.headers.get("location")).toBeDefined();
|
||||
|
|
@ -130,23 +118,21 @@ describe("GET /oauth/authorize/", () => {
|
|||
|
||||
describe("POST /oauth/token/", () => {
|
||||
test("should get an access token", async () => {
|
||||
const response = await sendTestRequest(
|
||||
new Request(wrapRelativeUrl("/oauth/token", baseUrl), {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${jwt}`,
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
body: new URLSearchParams({
|
||||
grant_type: "authorization_code",
|
||||
code,
|
||||
redirect_uri: "https://example.com",
|
||||
client_id: clientId,
|
||||
client_secret: clientSecret,
|
||||
scope: "read write",
|
||||
}),
|
||||
const response = await fakeRequest("/oauth/token", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${jwt}`,
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
body: new URLSearchParams({
|
||||
grant_type: "authorization_code",
|
||||
code,
|
||||
redirect_uri: "https://example.com",
|
||||
client_id: clientId,
|
||||
client_secret: clientSecret,
|
||||
scope: "read write",
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
const json = await response.json();
|
||||
|
||||
|
|
@ -170,16 +156,11 @@ describe("POST /oauth/token/", () => {
|
|||
|
||||
describe("GET /api/v1/apps/verify_credentials", () => {
|
||||
test("should return the authenticated application's credentials", async () => {
|
||||
const response = await sendTestRequest(
|
||||
new Request(
|
||||
wrapRelativeUrl("/api/v1/apps/verify_credentials", baseUrl),
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.access_token}`,
|
||||
},
|
||||
},
|
||||
),
|
||||
);
|
||||
const response = await fakeRequest("/api/v1/apps/verify_credentials", {
|
||||
headers: {
|
||||
Authorization: `Bearer ${token.access_token}`,
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("content-type")).toContain(
|
||||
|
|
|
|||
|
|
@ -19,18 +19,10 @@ if (config.sonic.enabled) {
|
|||
|
||||
const app = await appFactory();
|
||||
|
||||
/**
|
||||
* This allows us to send a test request to the server even when it isnt running
|
||||
* @param req Request to send
|
||||
* @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 function fakeRequest(url: URL | string, init?: RequestInit) {
|
||||
return Promise.resolve(
|
||||
app.fetch(new Request(new URL(url, config.http.base_url), init)),
|
||||
);
|
||||
}
|
||||
|
||||
export const deleteOldTestUsers = async () => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue