server/packages/api/routes/oauth.test.ts

61 lines
1.7 KiB
TypeScript
Raw Normal View History

2024-04-14 02:46:33 +02:00
import { afterAll, describe, expect, test } from "bun:test";
import {
fakeRequest,
generateClient,
getTestUsers,
} from "@versia-server/tests";
2023-09-14 04:25:45 +02:00
let clientId: string;
2024-04-14 02:46:33 +02:00
const { users, passwords, deleteUsers } = await getTestUsers(1);
2023-09-14 04:25:45 +02:00
2024-04-14 02:46:33 +02:00
afterAll(async () => {
await deleteUsers();
});
describe("Login flow", () => {
test("should create a client", async () => {
const client = await generateClient(users[0]);
2024-04-07 07:30:49 +02:00
const { ok, data } = await client.createApp("Test Client", {
redirect_uris: "https://example.com",
website: "https://example.com",
scopes: ["read", "write"],
});
2024-04-07 07:30:49 +02:00
expect(ok).toBe(true);
expect(data).toEqual({
name: "Test Client",
2024-04-07 07:30:49 +02:00
website: "https://example.com",
client_id: expect.any(String),
client_secret: expect.any(String),
client_secret_expires_at: "0",
2024-04-07 07:30:49 +02:00
redirect_uri: "https://example.com",
redirect_uris: ["https://example.com"],
scopes: ["read", "write"],
2024-04-07 07:30:49 +02:00
});
clientId = data.client_id;
2024-04-07 07:30:49 +02:00
});
2023-09-14 04:25:45 +02:00
test("should get a JWT", async () => {
2024-04-07 07:30:49 +02:00
const formData = new FormData();
formData.append("identifier", users[0]?.data.email ?? "");
2024-04-14 02:46:33 +02:00
formData.append("password", passwords[0]);
2024-04-07 07:30:49 +02:00
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,
},
2024-04-07 07:30:49 +02:00
);
expect(response.status).toBe(302);
//jwt = response.headers.get("Set-Cookie")?.match(/jwt=([^;]+);/)?.[1] ?? "";
2024-04-07 07:30:49 +02:00
});
2023-09-14 04:25:45 +02:00
// TODO: Test full flow including OpenID part
2023-09-14 04:25:45 +02:00
});