-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestBackupsApi.js
More file actions
160 lines (147 loc) · 5.88 KB
/
testBackupsApi.js
File metadata and controls
160 lines (147 loc) · 5.88 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
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 createdBackupKey = null;
let uploadedBackupKey = null;
try {
console.log("[INFO] BACKUPS_API.md doc test starting...");
pb = new BosBase(baseUrl);
await pb.collection("_superusers").authWithPassword(authEmail, authPassword);
console.log("[SUCCESS] Authenticated as superuser");
// Test getFullList()
console.log("\n[INFO] Testing getFullList()...");
const backups = await pb.backups.getFullList();
console.log("[SUCCESS] Backups available:", backups.length);
// Test create()
console.log("\n[INFO] Testing create()...");
const timestamp = new Date().toISOString().replace(/[:.]/g, "-").slice(0, 19);
const backupName = `test_backup_${timestamp}.zip`;
try {
await pb.backups.create(backupName);
createdBackupKey = backupName;
console.log(`[SUCCESS] Created backup: ${backupName}`);
// Verify the backup exists in the list
const updatedBackups = await pb.backups.getFullList();
const foundBackup = updatedBackups.find((b) => b.key === backupName);
if (foundBackup) {
console.log("[SUCCESS] Backup found in list:", foundBackup.key);
} else {
console.log(
"[WARN] Backup may not appear immediately due to async processing",
);
}
} catch (createError) {
if (createError?.status === 400) {
console.log(
"[WARN] Backup creation failed (may be due to concurrent operation or invalid name):",
createError?.response?.data?.message || createError.message,
);
} else {
throw createError;
}
}
// Test getDownloadURL()
console.log("\n[INFO] Testing getDownloadURL()...");
if (createdBackupKey || backups.length > 0) {
const testKey = createdBackupKey || backups[0].key;
const token = await pb.files.getToken();
const downloadUrl = pb.backups.getDownloadURL(token, testKey);
if (downloadUrl && downloadUrl.includes(testKey) && downloadUrl.includes("token=")) {
console.log("[SUCCESS] Download URL generated:", downloadUrl.substring(0, 100) + "...");
} else {
throw new Error("Download URL format is incorrect");
}
} else {
console.log("[SKIP] Skipping getDownloadURL() test - no backup available");
}
// Test upload() - Skip in test environment as it requires a real ZIP file
console.log("\n[INFO] Testing upload() method signature...");
// We can't actually upload without a real backup file, but we can verify the method exists
if (typeof pb.backups.upload === "function") {
console.log("[SUCCESS] upload() method is available");
} else {
throw new Error("upload() method is not available");
}
// Test delete()
console.log("\n[INFO] Testing delete()...");
if (createdBackupKey) {
try {
// Wait a bit to ensure backup creation completed
await new Promise((resolve) => setTimeout(resolve, 2000));
await pb.backups.delete(createdBackupKey);
console.log(`[SUCCESS] Deleted backup: ${createdBackupKey}`);
createdBackupKey = null; // Mark as cleaned up
} catch (deleteError) {
if (deleteError?.status === 400) {
console.log(
"[WARN] Backup deletion failed (may be in use):",
deleteError?.response?.data?.message || deleteError.message,
);
} else if (deleteError?.status === 404) {
console.log("[WARN] Backup not found (may have been auto-deleted)");
createdBackupKey = null;
} else {
throw deleteError;
}
}
} else {
console.log("[SKIP] Skipping delete() test - no backup created");
}
// Test restore() - Skip actual restore as it would restart the app
console.log("\n[INFO] Testing restore() method signature...");
if (typeof pb.backups.restore === "function") {
console.log("[SUCCESS] restore() method is available");
console.log("[INFO] restore() functional test skipped (would restart app)");
} else {
throw new Error("restore() method is not available");
}
// Test deprecated getDownloadUrl() method
console.log("\n[INFO] Testing deprecated getDownloadUrl() method...");
if (backups.length > 0) {
const token = await pb.files.getToken();
const deprecatedUrl = pb.backups.getDownloadUrl(token, backups[0].key);
const newUrl = pb.backups.getDownloadURL(token, backups[0].key);
if (deprecatedUrl === newUrl) {
console.log("[SUCCESS] Deprecated getDownloadUrl() works and matches getDownloadURL()");
} else {
throw new Error("Deprecated method should return same result as new method");
}
}
console.log("\n========== BACKUPS_API.md doc test completed ==========");
} catch (error) {
console.error("[ERROR] BACKUPS_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: try to delete created backup if it still exists
if (pb && createdBackupKey) {
try {
await pb.backups.delete(createdBackupKey);
console.log(`[CLEANUP] Deleted test backup: ${createdBackupKey}`);
} catch (cleanupError) {
console.warn(
"[CLEANUP] Failed to delete test backup:",
cleanupError?.message || cleanupError,
);
}
}
}
}
main();