-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestFileApi.js
More file actions
169 lines (149 loc) · 6.2 KB
/
testFileApi.js
File metadata and controls
169 lines (149 loc) · 6.2 KB
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
import BosBase from "bosbase";
const baseUrl = process.env.BOSBASE_BASE_URL ?? "http://127.0.0.1:8090";
const authEmail =
process.env.BOSBASE_EMAIL ??
process.env.BOSBASE_SUPERUSER_EMAIL ??
"try@bosbase.com";
const authPassword =
process.env.BOSBASE_PASSWORD ??
process.env.BOSBASE_SUPERUSER_PASSWORD ??
"bosbasepass";
async function main() {
let pb;
let testCollectionName = null;
let testRecordId = null;
try {
console.log("[INFO] FILE_API.md doc test starting...");
pb = new BosBase(baseUrl);
await pb.collection("_superusers").authWithPassword(authEmail, authPassword);
console.log("[SUCCESS] Authenticated as superuser");
// Test getToken()
console.log("\n[INFO] Testing getToken()...");
const token = await pb.files.getToken();
if (token && token.length > 0) {
console.log("[SUCCESS] File token obtained, length:", token.length);
} else {
throw new Error("File token is empty");
}
// Test getURL() - Create a test collection and record with a file field
console.log("\n[INFO] Testing getURL()...");
try {
// Create a test collection with a file field
testCollectionName = `file_test_${Date.now()}`;
const collection = await pb.collections.create({
name: testCollectionName,
type: "base",
schema: [
{ name: "title", type: "text", required: true },
{ name: "image", type: "file" },
],
});
console.log(`[SUCCESS] Created test collection: ${testCollectionName}`);
// Create a record (even without a file, we can test URL generation)
const record = await pb.collection(testCollectionName).create({
title: "Test Record",
});
testRecordId = record.id;
console.log(`[SUCCESS] Created test record: ${testRecordId}`);
// Test getURL() with various parameters
const testFilename = "test_image.jpg";
// Test basic getURL()
const basicUrl = pb.files.getURL(record, testFilename);
// Check that URL contains the record ID and filename (it may use collectionId instead of collectionName)
if (basicUrl && basicUrl.includes(testRecordId) && basicUrl.includes(testFilename)) {
console.log("[SUCCESS] Basic getURL() works:", basicUrl.substring(0, 80) + "...");
} else {
throw new Error("Basic getURL() did not generate correct URL");
}
// Test getURL() with collectionId instead of collectionName
const recordWithId = { ...record, collectionId: collection.id };
const urlWithId = pb.files.getURL(recordWithId, testFilename);
if (urlWithId && urlWithId.includes(collection.id)) {
console.log("[SUCCESS] getURL() works with collectionId");
}
// Test getURL() with thumb parameter
const thumbUrl = pb.files.getURL(record, testFilename, { thumb: "100x100" });
if (thumbUrl && thumbUrl.includes("thumb=100x100")) {
console.log("[SUCCESS] getURL() with thumb parameter works");
} else {
throw new Error("getURL() with thumb parameter did not generate correct URL");
}
// Test getURL() with download parameter
const downloadUrl = pb.files.getURL(record, testFilename, { download: true });
if (downloadUrl && downloadUrl.includes("download=")) {
console.log("[SUCCESS] getURL() with download parameter works");
} else {
throw new Error("getURL() with download parameter did not generate correct URL");
}
// Test getURL() with token parameter
const tokenUrl = pb.files.getURL(record, testFilename, { token: token });
if (tokenUrl && tokenUrl.includes("token=")) {
console.log("[SUCCESS] getURL() with token parameter works");
} else {
throw new Error("getURL() with token parameter did not generate correct URL");
}
// Test getURL() with multiple parameters
const complexUrl = pb.files.getURL(record, testFilename, {
thumb: "200x200",
token: token,
download: false,
});
if (complexUrl && complexUrl.includes("thumb=200x200") && complexUrl.includes("token=")) {
console.log("[SUCCESS] getURL() with multiple parameters works");
}
// Test getURL() edge cases
console.log("\n[INFO] Testing getURL() edge cases...");
// Test with missing filename
const emptyUrl = pb.files.getURL(record, "");
if (emptyUrl === "") {
console.log("[SUCCESS] getURL() returns empty string for missing filename");
}
// Test with missing record id
const invalidRecord = { collectionName: testCollectionName };
const invalidUrl = pb.files.getURL(invalidRecord, testFilename);
if (invalidUrl === "") {
console.log("[SUCCESS] getURL() returns empty string for invalid record");
}
// Test deprecated getUrl() method
if (typeof pb.files.getUrl === "function") {
const deprecatedUrl = pb.files.getUrl(record, testFilename);
if (deprecatedUrl === basicUrl) {
console.log("[SUCCESS] Deprecated getUrl() works and matches getURL()");
}
}
} catch (urlTestError) {
console.log(
"[WARN] getURL() test encountered issues:",
urlTestError?.response?.data?.message || urlTestError.message,
);
// Continue even if URL tests fail - may be due to missing test data
}
console.log("\n========== FILE_API.md doc test completed ==========");
} catch (error) {
console.error("[ERROR] FILE_API.md doc test failed:");
if (error?.response) {
console.error("Status:", error.response.status);
console.error("Data:", JSON.stringify(error.response.data, null, 2));
if (error.response.data?.message) {
console.error("Message:", error.response.data.message);
}
} else {
console.error(error);
}
process.exit(1);
} finally {
// Cleanup: delete test collection if created
if (pb && testCollectionName) {
try {
await pb.collections.delete(testCollectionName);
console.log(`[CLEANUP] Deleted test collection: ${testCollectionName}`);
} catch (cleanupError) {
console.warn(
"[CLEANUP] Failed to delete test collection:",
cleanupError?.message || cleanupError,
);
}
}
}
}
main();