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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
import { eq } from "drizzle-orm";
import { assert, beforeEach, describe, expect, test } from "vitest";
import { bookmarkLinks, users } from "@karakeep/db/schema";
import { BookmarkTypes } from "@karakeep/shared/types/bookmarks";
import type { CustomTestContext } from "../testUtils";
import { buildTestContext, getApiCaller } from "../testUtils";
beforeEach<CustomTestContext>(async (context) => {
const testContext = await buildTestContext(true);
Object.assign(context, testContext);
});
describe("Admin Routes", () => {
describe("getBookmarkDebugInfo", () => {
test<CustomTestContext>("admin can access bookmark debug info for link bookmark", async ({
apiCallers,
db,
}) => {
// Create an admin user
const adminUser = await db
.insert(users)
.values({
name: "Admin User",
email: "admin@test.com",
role: "admin",
})
.returning();
const adminApi = getApiCaller(
db,
adminUser[0].id,
adminUser[0].email,
"admin",
);
// Create a bookmark as a regular user
const bookmark = await apiCallers[0].bookmarks.createBookmark({
url: "https://example.com",
type: BookmarkTypes.LINK,
});
// Update the bookmark link with some metadata
await db
.update(bookmarkLinks)
.set({
crawlStatus: "success",
crawlStatusCode: 200,
crawledAt: new Date(),
htmlContent: "<html><body>Test content</body></html>",
title: "Test Title",
description: "Test Description",
})
.where(eq(bookmarkLinks.id, bookmark.id));
// Admin should be able to access debug info
const debugInfo = await adminApi.admin.getBookmarkDebugInfo({
bookmarkId: bookmark.id,
});
expect(debugInfo.id).toEqual(bookmark.id);
expect(debugInfo.type).toEqual(BookmarkTypes.LINK);
expect(debugInfo.linkInfo).toBeDefined();
assert(debugInfo.linkInfo);
expect(debugInfo.linkInfo.url).toEqual("https://example.com");
expect(debugInfo.linkInfo.crawlStatus).toEqual("success");
expect(debugInfo.linkInfo.crawlStatusCode).toEqual(200);
expect(debugInfo.linkInfo.hasHtmlContent).toEqual(true);
expect(debugInfo.linkInfo.htmlContentPreview).toBeDefined();
expect(debugInfo.linkInfo.htmlContentPreview).toContain("Test content");
});
test<CustomTestContext>("admin can access bookmark debug info for text bookmark", async ({
apiCallers,
db,
}) => {
// Create an admin user
const adminUser = await db
.insert(users)
.values({
name: "Admin User",
email: "admin@test.com",
role: "admin",
})
.returning();
const adminApi = getApiCaller(
db,
adminUser[0].id,
adminUser[0].email,
"admin",
);
// Create a text bookmark
const bookmark = await apiCallers[0].bookmarks.createBookmark({
text: "This is a test text bookmark",
type: BookmarkTypes.TEXT,
});
// Admin should be able to access debug info
const debugInfo = await adminApi.admin.getBookmarkDebugInfo({
bookmarkId: bookmark.id,
});
expect(debugInfo.id).toEqual(bookmark.id);
expect(debugInfo.type).toEqual(BookmarkTypes.TEXT);
expect(debugInfo.textInfo).toBeDefined();
assert(debugInfo.textInfo);
expect(debugInfo.textInfo.hasText).toEqual(true);
});
test<CustomTestContext>("admin can see bookmark tags in debug info", async ({
apiCallers,
db,
}) => {
// Create an admin user
const adminUser = await db
.insert(users)
.values({
name: "Admin User",
email: "admin@test.com",
role: "admin",
})
.returning();
const adminApi = getApiCaller(
db,
adminUser[0].id,
adminUser[0].email,
"admin",
);
// Create a bookmark with tags
const bookmark = await apiCallers[0].bookmarks.createBookmark({
url: "https://example.com",
type: BookmarkTypes.LINK,
});
// Add tags to the bookmark
await apiCallers[0].bookmarks.updateTags({
bookmarkId: bookmark.id,
attach: [{ tagName: "test-tag-1" }, { tagName: "test-tag-2" }],
detach: [],
});
// Admin should be able to see tags in debug info
const debugInfo = await adminApi.admin.getBookmarkDebugInfo({
bookmarkId: bookmark.id,
});
expect(debugInfo.tags).toHaveLength(2);
expect(debugInfo.tags.map((t) => t.name).sort()).toEqual([
"test-tag-1",
"test-tag-2",
]);
expect(debugInfo.tags[0].attachedBy).toEqual("human");
});
test<CustomTestContext>("non-admin user cannot access bookmark debug info", async ({
apiCallers,
}) => {
// Create a bookmark
const bookmark = await apiCallers[0].bookmarks.createBookmark({
url: "https://example.com",
type: BookmarkTypes.LINK,
});
// Non-admin user should not be able to access debug info
// The admin procedure itself will throw FORBIDDEN
await expect(() =>
apiCallers[0].admin.getBookmarkDebugInfo({ bookmarkId: bookmark.id }),
).rejects.toThrow(/FORBIDDEN/);
});
test<CustomTestContext>("debug info includes asset URLs with signed tokens", async ({
apiCallers,
db,
}) => {
// Create an admin user
const adminUser = await db
.insert(users)
.values({
name: "Admin User",
email: "admin@test.com",
role: "admin",
})
.returning();
const adminApi = getApiCaller(
db,
adminUser[0].id,
adminUser[0].email,
"admin",
);
// Create a bookmark
const bookmark = await apiCallers[0].bookmarks.createBookmark({
url: "https://example.com",
type: BookmarkTypes.LINK,
});
// Get debug info
const debugInfo = await adminApi.admin.getBookmarkDebugInfo({
bookmarkId: bookmark.id,
});
// Check that assets array is present
expect(debugInfo.assets).toBeDefined();
expect(Array.isArray(debugInfo.assets)).toBe(true);
// If there are assets, check that they have signed URLs
if (debugInfo.assets.length > 0) {
const asset = debugInfo.assets[0];
expect(asset.url).toBeDefined();
expect(asset.url).toContain("/api/public/assets/");
expect(asset.url).toContain("token=");
}
});
test<CustomTestContext>("debug info truncates HTML content preview", async ({
apiCallers,
db,
}) => {
// Create an admin user
const adminUser = await db
.insert(users)
.values({
name: "Admin User",
email: "admin@test.com",
role: "admin",
})
.returning();
const adminApi = getApiCaller(
db,
adminUser[0].id,
adminUser[0].email,
"admin",
);
// Create a bookmark
const bookmark = await apiCallers[0].bookmarks.createBookmark({
url: "https://example.com",
type: BookmarkTypes.LINK,
});
// Create a large HTML content
const largeContent = "<html><body>" + "x".repeat(2000) + "</body></html>";
await db
.update(bookmarkLinks)
.set({
htmlContent: largeContent,
})
.where(eq(bookmarkLinks.id, bookmark.id));
// Get debug info
const debugInfo = await adminApi.admin.getBookmarkDebugInfo({
bookmarkId: bookmark.id,
});
// Check that HTML preview is truncated to 1000 characters
assert(debugInfo.linkInfo);
expect(debugInfo.linkInfo.htmlContentPreview).toBeDefined();
expect(debugInfo.linkInfo.htmlContentPreview!.length).toBeLessThanOrEqual(
1000,
);
});
});
});
|