refactor(api): 🎨 Finish Hono refactor

This commit is contained in:
Jesse Wierzbinski 2024-05-06 08:19:42 +00:00
parent 826a260e90
commit 959dd27ad6
No known key found for this signature in database
20 changed files with 309 additions and 316 deletions

View file

@ -16,6 +16,12 @@ afterAll(async () => {
await deleteUsers();
});
const getFormData = (object: Record<string, string | number | boolean>) =>
Object.keys(object).reduce((formData, key) => {
formData.append(key, String(object[key]));
return formData;
}, new FormData());
describe("API Tests", () => {
describe("PATCH /api/v1/accounts/update_credentials", () => {
test("should update the authenticated user's display name", async () => {
@ -29,9 +35,8 @@ describe("API Tests", () => {
method: "PATCH",
headers: {
Authorization: `Bearer ${token.accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
body: getFormData({
display_name: "New Display Name",
}),
},

View file

@ -1,7 +1,6 @@
import { afterAll, describe, expect, test } from "bun:test";
import { config } from "config-manager";
import { getTestUsers, sendTestRequest, wrapRelativeUrl } from "~tests/utils";
import type { Account as APIAccount } from "~types/mastodon/account";
import type { AsyncAttachment as APIAsyncAttachment } from "~types/mastodon/async_attachment";
import type { Context as APIContext } from "~types/mastodon/context";
import type { Status as APIStatus } from "~types/mastodon/status";
@ -60,13 +59,12 @@ describe("API Tests", () => {
method: "POST",
headers: {
Authorization: `Bearer ${token.accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
body: new URLSearchParams({
status: "Hello, world!",
visibility: "public",
media_ids: [media1?.id],
federate: false,
"media_ids[]": media1?.id ?? "",
federate: "false",
}),
},
),
@ -108,13 +106,12 @@ describe("API Tests", () => {
method: "POST",
headers: {
Authorization: `Bearer ${token.accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
body: new URLSearchParams({
status: "This is a reply!",
visibility: "public",
in_reply_to_id: status?.id,
federate: false,
in_reply_to_id: status?.id ?? "",
federate: "false",
}),
},
),

View file

@ -1,4 +1,5 @@
import { afterAll, describe, expect, test } from "bun:test";
import { config } from "~packages/config-manager";
import type { Application as APIApplication } from "~types/mastodon/application";
import type { Token as APIToken } from "~types/mastodon/token";
import {
@ -8,7 +9,7 @@ import {
wrapRelativeUrl,
} from "./utils";
const base_url = "http://lysand.localhost:8080"; //config.http.base_url;
const base_url = config.http.base_url;
let client_id: string;
let client_secret: string;
@ -19,8 +20,8 @@ const { users, passwords, deleteUsers } = await getTestUsers(1);
afterAll(async () => {
await deleteUsers();
await deleteOldTestUsers();
});
describe("POST /api/v1/apps/", () => {
test("should create an application", async () => {
const formData = new FormData();
@ -31,7 +32,7 @@ describe("POST /api/v1/apps/", () => {
formData.append("scopes", "read write");
const response = await sendTestRequest(
new Request(wrapRelativeUrl("/api/v1/apps/", base_url), {
new Request(new URL("/api/v1/apps", config.http.base_url), {
method: "POST",
body: formData,
}),
@ -66,8 +67,8 @@ describe("POST /api/auth/login/", () => {
const response = await sendTestRequest(
new Request(
wrapRelativeUrl(
`/api/auth/login/?client_id=${client_id}&redirect_uri=https://example.com&response_type=code&scope=read+write`,
new URL(
`/api/auth/login?client_id=${client_id}&redirect_uri=https://example.com&response_type=code&scope=read+write`,
base_url,
),
{
@ -77,8 +78,6 @@ describe("POST /api/auth/login/", () => {
),
);
console.log(await response.text());
expect(response.status).toBe(302);
expect(response.headers.get("location")).toBeDefined();
const locationHeader = new URL(
@ -102,24 +101,28 @@ describe("POST /api/auth/login/", () => {
});
});
describe("POST /oauth/authorize/", () => {
describe("GET /oauth/authorize/", () => {
test("should get a code", async () => {
const response = await sendTestRequest(
new Request(wrapRelativeUrl("/oauth/authorize", base_url), {
method: "POST",
headers: {
Cookie: `jwt=${jwt}`,
"Content-Type": "application/x-www-form-urlencoded",
new Request(
new URL(
`/oauth/authorize?${new URLSearchParams({
client_id,
client_secret,
redirect_uri: "https://example.com",
response_type: "code",
scope: "read write",
max_age: "604800",
})}`,
base_url,
),
{
method: "POST",
headers: {
Cookie: `jwt=${jwt}`,
},
},
body: new URLSearchParams({
client_id,
client_secret,
redirect_uri: "https://example.com",
response_type: "code",
scope: "read write",
max_age: "604800",
}),
}),
),
);
expect(response.status).toBe(302);
@ -138,7 +141,7 @@ describe("POST /oauth/authorize/", () => {
describe("POST /oauth/token/", () => {
test("should get an access token", async () => {
const response = await sendTestRequest(
new Request(wrapRelativeUrl("/oauth/token/", base_url), {
new Request(wrapRelativeUrl("/oauth/token", base_url), {
method: "POST",
headers: {
Authorization: `Bearer ${jwt}`,