-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestAutoRefresh.js
More file actions
107 lines (90 loc) · 3.16 KB
/
testAutoRefresh.js
File metadata and controls
107 lines (90 loc) · 3.16 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
import BosBase from "bosbase";
function createToken(expiresInSeconds) {
const header = Buffer.from(JSON.stringify({ alg: "none", typ: "JWT" })).toString("base64url");
const payload = Buffer.from(
JSON.stringify({
exp: Math.floor(Date.now() / 1000) + expiresInSeconds,
type: "auth",
collectionId: "pbc_3142635823",
}),
).toString("base64url");
return `${header}.${payload}.sig`;
}
async function testAutoRefreshThreshold() {
console.log("[INFO] Testing autoRefreshThreshold for superuser auth...");
const pb = new BosBase("http://api.test.local");
const shortLivedToken = createToken(30); // expires within the 60s threshold
const refreshedToken = createToken(300);
const calls = [];
const originalFetch = global.fetch;
const stubFetch = async (url, options = {}) => {
const path = typeof url === "string" ? url : url?.toString?.() || "";
if (path.includes("/auth-with-password")) {
calls.push({ step: "login", authHeader: options.headers?.Authorization });
return {
status: 200,
url: path,
json: async () => ({
token: shortLivedToken,
record: { id: "admin1", collectionName: "_superusers" },
}),
};
}
if (path.includes("/auth-refresh")) {
calls.push({ step: "refresh", authHeader: options.headers?.Authorization });
return {
status: 200,
url: path,
json: async () => ({
token: refreshedToken,
record: { id: "admin1", collectionName: "_superusers" },
}),
};
}
calls.push({ step: "protected", authHeader: options.headers?.Authorization });
return {
status: 200,
url: path,
json: async () => ({
authHeader: options.headers?.Authorization || null,
}),
};
};
global.fetch = stubFetch;
try {
await pb.collection("_superusers").authWithPassword("demo@example.com", "pass", {
autoRefreshThreshold: 60,
});
if (pb.authStore.token !== shortLivedToken) {
throw new Error("Initial login token was not stored in the auth store");
}
if (!pb.authStore.isSuperuser) {
throw new Error("Auth store should mark the saved token as superuser");
}
const result = await pb.send("/api/protected", { method: "GET" });
if (pb.authStore.token !== refreshedToken) {
throw new Error("Token was not refreshed before the next request");
}
if (result.authHeader !== refreshedToken) {
throw new Error("Protected request did not send the refreshed Authorization header");
}
const order = calls.map((c) => c.step).join(",");
if (order !== "login,refresh,protected") {
throw new Error(`Auto refresh call order incorrect, got: ${order}`);
}
console.log("[SUCCESS] autoRefreshThreshold triggered authRefresh before the next request");
} finally {
global.fetch = originalFetch;
}
}
async function main() {
try {
await testAutoRefreshThreshold();
console.log("\n========== Auto refresh helper test completed ==========");
} catch (error) {
console.error("[ERROR] Auto refresh helper test failed:");
console.error(error?.stack || error);
process.exit(1);
}
}
main();