aboutsummaryrefslogtreecommitdiffstats
path: root/packages/e2e_tests/utils/api.ts
blob: 84a6eb9192cc9e2c20bc89564d10a4e8a5dff8aa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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;
}