diff options
| author | Mohamed Bassem <me@mbassem.com> | 2024-12-30 16:55:49 +0000 |
|---|---|---|
| committer | Mohamed Bassem <me@mbassem.com> | 2024-12-30 16:56:23 +0000 |
| commit | 058e7238840b362135fd080045478025e31bf720 (patch) | |
| tree | f094b35163961065d3d145e8ff826219b0bb3506 /packages/e2e_tests/utils | |
| parent | 5aee3404aa7f446a221a88c9b2cd529d1249e22f (diff) | |
| download | karakeep-058e7238840b362135fd080045478025e31bf720.tar.zst | |
chore: Setup and add e2e tests for the API endpoints
Diffstat (limited to 'packages/e2e_tests/utils')
| -rw-r--r-- | packages/e2e_tests/utils/api.ts | 57 | ||||
| -rw-r--r-- | packages/e2e_tests/utils/trpc.ts | 20 |
2 files changed, 77 insertions, 0 deletions
diff --git a/packages/e2e_tests/utils/api.ts b/packages/e2e_tests/utils/api.ts new file mode 100644 index 00000000..84a6eb91 --- /dev/null +++ b/packages/e2e_tests/utils/api.ts @@ -0,0 +1,57 @@ +import { getTrpcClient } from "./trpc"; + +export function getAuthHeader(apiKey: string) { + return { + "Content-Type": "application/json", + authorization: `Bearer ${apiKey}`, + }; +} + +export async function uploadTestAsset( + apiKey: string, + port: number, + file: File, +) { + const formData = new FormData(); + formData.append("file", file); + + const response = await fetch(`http://localhost:${port}/api/assets`, { + method: "POST", + headers: { + authorization: `Bearer ${apiKey}`, + }, + body: formData, + }); + + if (!response.ok) { + throw new Error(`Failed to upload asset: ${response.statusText}`); + } + + return response.json() as Promise<{ + assetId: string; + contentType: string; + fileName: string; + }>; +} + +export async function createTestUser() { + const trpc = getTrpcClient(); + + const random = Math.random().toString(36).substring(7); + const email = `testuser+${random}@example.com`; + + await trpc.users.create.mutate({ + name: "Test User", + email, + password: "test1234", + confirmPassword: "test1234", + }); + + const { key } = await trpc.apiKeys.exchange.mutate({ + email, + password: "test1234", + keyName: "test-key", + }); + + return key; +} diff --git a/packages/e2e_tests/utils/trpc.ts b/packages/e2e_tests/utils/trpc.ts new file mode 100644 index 00000000..7d916d93 --- /dev/null +++ b/packages/e2e_tests/utils/trpc.ts @@ -0,0 +1,20 @@ +import { createTRPCClient, httpBatchLink } from "@trpc/client"; +import superjson from "superjson"; + +import type { AppRouter } from "@hoarder/trpc/routers/_app"; + +export function getTrpcClient(apiKey?: string) { + return createTRPCClient<AppRouter>({ + links: [ + httpBatchLink({ + transformer: superjson, + url: `http://localhost:${process.env.HOARDER_PORT}/api/trpc`, + headers() { + return { + authorization: apiKey ? `Bearer ${apiKey}` : undefined, + }; + }, + }), + ], + }); +} |
