aboutsummaryrefslogtreecommitdiffstats
path: root/packages/e2e_tests/tests/api/assets.test.ts
blob: 0fab3d3fd4d1d2be76d898605f943f4845b92074 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { createHoarderClient } from "@hoarderapp/sdk";
import { assert, beforeEach, describe, expect, inject, it } from "vitest";

import { createTestUser, uploadTestAsset } from "../../utils/api";

describe("Assets API", () => {
  const port = inject("hoarderPort");

  if (!port) {
    throw new Error("Missing required environment variables");
  }

  let client: ReturnType<typeof createHoarderClient>;
  let apiKey: string;

  beforeEach(async () => {
    apiKey = await createTestUser();
    client = createHoarderClient({
      baseUrl: `http://localhost:${port}/api/v1/`,
      headers: {
        "Content-Type": "application/json",
        authorization: `Bearer ${apiKey}`,
      },
    });
  });

  it("should upload and retrieve an asset", async () => {
    // Create a test file
    const file = new File(["test content"], "test.pdf", {
      type: "application/pdf",
    });

    // Upload the asset
    const uploadResponse = await uploadTestAsset(apiKey, port, file);
    expect(uploadResponse.assetId).toBeDefined();
    expect(uploadResponse.contentType).toBe("application/pdf");
    expect(uploadResponse.fileName).toBe("test.pdf");

    // Retrieve the asset
    const resp = await fetch(
      `http://localhost:${port}/api/assets/${uploadResponse.assetId}`,
      {
        headers: {
          authorization: `Bearer ${apiKey}`,
        },
      },
    );

    expect(resp.status).toBe(200);
  });

  it("should attach an asset to a bookmark", async () => {
    // Create a test file
    const file = new File(["test content"], "test.pdf", {
      type: "application/pdf",
    });

    // Upload the asset
    const uploadResponse = await uploadTestAsset(apiKey, port, file);

    // Create a bookmark
    const { data: createdBookmark } = await client.POST("/bookmarks", {
      body: {
        type: "asset",
        title: "Test Asset Bookmark",
        assetType: "pdf",
        assetId: uploadResponse.assetId,
      },
    });

    expect(createdBookmark).toBeDefined();
    expect(createdBookmark?.id).toBeDefined();

    // Get the bookmark and verify asset
    const { data: retrievedBookmark } = await client.GET(
      "/bookmarks/{bookmarkId}",
      {
        params: {
          path: {
            bookmarkId: createdBookmark!.id,
          },
        },
      },
    );

    expect(retrievedBookmark).toBeDefined();
    assert(retrievedBookmark!.content.type === "asset");
    expect(retrievedBookmark!.content.assetId).toBe(uploadResponse.assetId);
  });

  it("should delete asset when deleting bookmark", async () => {
    // Create a test file
    const file = new File(["test content"], "test.pdf", {
      type: "application/pdf",
    });

    // Upload the asset
    const uploadResponse = await uploadTestAsset(apiKey, port, file);

    // Create a bookmark
    const { data: createdBookmark } = await client.POST("/bookmarks", {
      body: {
        type: "asset",
        title: "Test Asset Bookmark",
        assetType: "pdf",
        assetId: uploadResponse.assetId,
      },
    });

    // Delete the bookmark
    const { response: deleteResponse } = await client.DELETE(
      "/bookmarks/{bookmarkId}",
      {
        params: {
          path: {
            bookmarkId: createdBookmark!.id,
          },
        },
      },
    );
    expect(deleteResponse.status).toBe(204);

    // Verify asset is deleted
    const assetResponse = await fetch(
      `http://localhost:${port}/api/assets/${uploadResponse.assetId}`,
      {
        headers: {
          authorization: `Bearer ${apiKey}`,
        },
      },
    );
    expect(assetResponse.status).toBe(404);
  });
});