2024-05-08 10:02:05 +02:00
|
|
|
/**
|
|
|
|
|
* @deprecated
|
|
|
|
|
*/
|
2024-04-14 02:46:33 +02:00
|
|
|
import { afterAll, describe, expect, test } from "bun:test";
|
2024-06-29 08:36:15 +02:00
|
|
|
import type {
|
|
|
|
|
Application as ApiApplication,
|
|
|
|
|
Token as ApiToken,
|
2024-08-26 19:40:15 +02:00
|
|
|
} from "@versia/client/types";
|
2024-10-04 15:22:48 +02:00
|
|
|
import { fakeRequest, getTestUsers } from "./utils.ts";
|
2023-09-14 04:25:45 +02:00
|
|
|
|
2024-06-13 04:26:43 +02:00
|
|
|
let clientId: string;
|
|
|
|
|
let clientSecret: string;
|
2023-09-14 04:25:45 +02:00
|
|
|
let code: string;
|
2024-04-18 10:42:12 +02:00
|
|
|
let jwt: string;
|
2024-06-29 08:36:15 +02:00
|
|
|
let token: ApiToken;
|
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();
|
|
|
|
|
});
|
2024-05-06 10:19:42 +02:00
|
|
|
|
2023-09-22 03:14:03 +02:00
|
|
|
describe("POST /api/v1/apps/", () => {
|
2024-04-07 07:30:49 +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");
|
|
|
|
|
|
2024-08-27 21:25:26 +02:00
|
|
|
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",
|
2024-04-07 07:30:49 +02:00
|
|
|
}),
|
2024-08-27 21:25:26 +02:00
|
|
|
});
|
2024-04-07 07:30:49 +02:00
|
|
|
|
|
|
|
|
expect(response.status).toBe(200);
|
2024-08-19 21:03:59 +02:00
|
|
|
expect(response.headers.get("content-type")).toContain(
|
|
|
|
|
"application/json",
|
|
|
|
|
);
|
2024-04-07 07:30:49 +02:00
|
|
|
|
|
|
|
|
const json = await response.json();
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
});
|
|
|
|
|
|
2024-06-13 04:26:43 +02:00
|
|
|
clientId = json.client_id;
|
|
|
|
|
clientSecret = json.client_secret;
|
2024-04-07 07:30:49 +02:00
|
|
|
});
|
2023-09-14 04:25:45 +02:00
|
|
|
});
|
|
|
|
|
|
2024-04-09 06:33:59 +02:00
|
|
|
describe("POST /api/auth/login/", () => {
|
2024-04-18 10:42:12 +02:00
|
|
|
test("should get a JWT", async () => {
|
2024-04-07 07:30:49 +02:00
|
|
|
const formData = new FormData();
|
|
|
|
|
|
2024-06-13 02:45:07 +02:00
|
|
|
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
|
|
|
|
2024-08-27 21:25:26 +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);
|
2024-04-18 10:42:12 +02:00
|
|
|
|
|
|
|
|
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
|
|
|
});
|
|
|
|
|
|
2024-05-06 10:19:42 +02:00
|
|
|
describe("GET /oauth/authorize/", () => {
|
2024-04-18 10:42:12 +02:00
|
|
|
test("should get a code", async () => {
|
2024-08-27 21:25:26 +02:00
|
|
|
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",
|
2024-05-06 10:40:26 +02:00
|
|
|
}),
|
2024-08-27 21:25:26 +02:00
|
|
|
});
|
2024-04-18 10:42:12 +02:00
|
|
|
|
|
|
|
|
expect(response.status).toBe(302);
|
|
|
|
|
expect(response.headers.get("location")).toBeDefined();
|
|
|
|
|
const locationHeader = new URL(
|
|
|
|
|
response.headers.get("Location") ?? "",
|
|
|
|
|
"",
|
|
|
|
|
);
|
2024-04-07 07:30:49 +02:00
|
|
|
|
2024-04-18 10:42:12 +02:00
|
|
|
expect(locationHeader.origin).toBe("https://example.com");
|
2024-04-07 07:30:49 +02:00
|
|
|
|
2024-04-18 10:42:12 +02:00
|
|
|
code = locationHeader.searchParams.get("code") ?? "";
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("POST /oauth/token/", () => {
|
|
|
|
|
test("should get an access token", async () => {
|
2024-08-27 21:25:26 +02:00
|
|
|
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",
|
2024-04-07 07:30:49 +02:00
|
|
|
}),
|
2024-08-27 21:25:26 +02:00
|
|
|
});
|
2024-04-07 07:30:49 +02:00
|
|
|
|
|
|
|
|
const json = await response.json();
|
|
|
|
|
|
|
|
|
|
expect(response.status).toBe(200);
|
2024-08-19 21:03:59 +02:00
|
|
|
expect(response.headers.get("content-type")).toContain(
|
|
|
|
|
"application/json",
|
|
|
|
|
);
|
2024-04-07 07:30:49 +02:00
|
|
|
expect(json).toEqual({
|
|
|
|
|
access_token: expect.any(String),
|
|
|
|
|
token_type: "Bearer",
|
|
|
|
|
scope: "read write",
|
2024-05-13 02:01:37 +02:00
|
|
|
created_at: expect.any(Number),
|
2024-04-18 10:42:12 +02:00
|
|
|
expires_in: expect.any(Number),
|
|
|
|
|
id_token: null,
|
|
|
|
|
refresh_token: null,
|
2024-04-07 07:30:49 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
token = json;
|
|
|
|
|
});
|
2023-09-22 03:41:12 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("GET /api/v1/apps/verify_credentials", () => {
|
2024-04-07 07:30:49 +02:00
|
|
|
test("should return the authenticated application's credentials", async () => {
|
2024-08-27 21:25:26 +02:00
|
|
|
const response = await fakeRequest("/api/v1/apps/verify_credentials", {
|
|
|
|
|
headers: {
|
|
|
|
|
Authorization: `Bearer ${token.access_token}`,
|
|
|
|
|
},
|
|
|
|
|
});
|
2024-04-07 07:30:49 +02:00
|
|
|
|
|
|
|
|
expect(response.status).toBe(200);
|
2024-08-19 21:03:59 +02:00
|
|
|
expect(response.headers.get("content-type")).toContain(
|
|
|
|
|
"application/json",
|
|
|
|
|
);
|
2024-04-07 07:30:49 +02:00
|
|
|
|
2024-06-29 08:36:15 +02:00
|
|
|
const credentials = (await response.json()) as Partial<ApiApplication>;
|
2024-04-07 07:30:49 +02:00
|
|
|
|
|
|
|
|
expect(credentials.name).toBe("Test Application");
|
|
|
|
|
expect(credentials.website).toBe("https://example.com");
|
|
|
|
|
});
|
2023-09-14 04:25:45 +02:00
|
|
|
});
|