server/tests/oauth.test.ts

156 lines
4.6 KiB
TypeScript
Raw Normal View History

2023-09-14 04:25:45 +02:00
import { getConfig } from "@config";
import type { Application, Token } from "@prisma/client";
2023-09-14 04:25:45 +02:00
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import { client } from "~database/datasource";
import { createNewLocalUser } from "~database/entities/User";
2023-09-14 04:25:45 +02:00
const config = getConfig();
let client_id: string;
let client_secret: string;
let code: string;
2023-09-22 03:41:12 +02:00
let token: Token;
2023-09-14 04:25:45 +02:00
beforeAll(async () => {
// Init test user
await createNewLocalUser({
2023-09-22 00:42:59 +02:00
email: "test@test.com",
username: "test",
password: "test",
display_name: "",
});
2023-09-14 04:25:45 +02:00
});
2023-09-22 03:14:03 +02:00
describe("POST /api/v1/apps/", () => {
2023-09-14 04:25:45 +02:00
test("should create an application", async () => {
const formData = new FormData();
formData.append("client_name", "Test Application");
formData.append("website", "https://example.com");
formData.append("redirect_uris", "https://example.com");
formData.append("scopes", "read write");
// @ts-expect-error FormData works
const response = await fetch(`${config.http.base_url}/api/v1/apps/`, {
method: "POST",
body: formData,
});
2023-09-14 04:25:45 +02:00
expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toBe("application/json");
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const json = (await response.json()) as any;
2023-09-14 04:25:45 +02:00
expect(json).toEqual({
id: expect.any(String),
name: "Test Application",
website: "https://example.com",
client_id: expect.any(String),
client_secret: expect.any(String),
redirect_uri: "https://example.com",
vapid_link: null,
});
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
client_id = json.client_id;
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
client_secret = json.client_secret;
});
});
describe("POST /auth/login/", () => {
test("should get a code", async () => {
const formData = new FormData();
formData.append("email", "test@test.com");
2023-09-14 04:25:45 +02:00
formData.append("password", "test");
// @ts-expect-error FormData works
2023-09-14 04:25:45 +02:00
const response = await fetch(
`${config.http.base_url}/auth/login/?client_id=${client_id}&redirect_uri=https://example.com&response_type=code&scope=read+write`,
2023-09-14 04:25:45 +02:00
{
method: "POST",
body: formData,
2023-09-14 04:49:26 +02:00
redirect: "manual",
2023-09-14 04:25:45 +02:00
}
);
expect(response.status).toBe(302);
2023-09-14 04:49:26 +02:00
expect(response.headers.get("Location")).toMatch(
2023-09-14 04:25:45 +02:00
/https:\/\/example.com\?code=/
);
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
code = response.headers.get("location")?.split("=")[1] || "";
});
});
2023-09-15 03:22:27 +02:00
describe("POST /oauth/token/", () => {
2023-09-14 04:25:45 +02:00
test("should get an access token", async () => {
const formData = new FormData();
formData.append("grant_type", "authorization_code");
formData.append("code", code);
formData.append("redirect_uri", "https://example.com");
formData.append("client_id", client_id);
formData.append("client_secret", client_secret);
formData.append("scope", "read+write");
2023-09-14 04:25:45 +02:00
// @ts-expect-error FormData works
const response = await fetch(`${config.http.base_url}/oauth/token/`, {
method: "POST",
2023-10-23 02:38:33 +02:00
// Do not set the Content-Type header for some reason
body: formData,
});
2023-09-14 04:25:45 +02:00
2023-09-14 04:49:26 +02:00
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const json = (await response.json()) as any;
2023-09-14 04:49:26 +02:00
2023-09-14 04:25:45 +02:00
expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toBe("application/json");
2023-09-14 04:49:26 +02:00
expect(json).toEqual({
2023-09-14 04:25:45 +02:00
access_token: expect.any(String),
2023-09-14 04:49:26 +02:00
token_type: "Bearer",
2023-09-14 04:25:45 +02:00
scope: "read write",
2023-09-14 04:49:26 +02:00
created_at: expect.any(String),
2023-09-14 04:25:45 +02:00
});
2023-09-22 03:41:12 +02:00
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
token = json;
});
});
describe("GET /api/v1/apps/verify_credentials", () => {
test("should return the authenticated application's credentials", async () => {
const response = await fetch(
`${config.http.base_url}/api/v1/apps/verify_credentials`,
2023-09-22 03:41:12 +02:00
{
method: "GET",
headers: {
Authorization: `Bearer ${token.access_token}`,
"Content-Type": "application/json",
},
}
);
expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toBe("application/json");
const credentials = (await response.json()) as Partial<Application>;
2023-09-22 03:41:12 +02:00
expect(credentials.name).toBe("Test Application");
expect(credentials.website).toBe("https://example.com");
expect(credentials.redirect_uris).toBe("https://example.com");
expect(credentials.scopes).toBe("read write");
2023-09-14 04:25:45 +02:00
});
});
afterAll(async () => {
// Clean up user
await client.user.delete({
where: {
username: "test",
},
2023-09-14 04:25:45 +02:00
});
});