quickbase-js 1.0.1-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/LICENSE +21 -0
- package/README.md +102 -0
- package/build.js +66 -0
- package/dist/quickbaseClient.d.ts +3888 -0
- package/dist/quickbaseClient.js +5812 -0
- package/dist/quickbaseClient.js.map +1 -0
- package/examples/react.js +22 -0
- package/examples/vanillajs.html +23 -0
- package/logGen.yaml +1820 -0
- package/package.json +72 -0
- package/playwright.config.ts +37 -0
- package/rollup.config.js +70 -0
- package/src/code-generation/fix-spec-definitions.ts +136 -0
- package/src/code-generation/fix-spec-main.ts +176 -0
- package/src/code-generation/fix-spec-paths.ts +163 -0
- package/src/code-generation/generate-unified-interface.ts +152 -0
- package/src/code-generation/output/quickbase-fixed.json +8435 -0
- package/src/code-generation/regenerate-client.ts +110 -0
- package/src/generated-unified/QuickbaseClient.ts +59 -0
- package/src/index.ts +1 -0
- package/src/log-gen.ts +122 -0
- package/src/quickbaseClient.ts +320 -0
- package/src/specs/QuickBase_RESTful_API_2025-03-04T06_22_39.725Z.json +46878 -0
- package/src/tokenCache.ts +51 -0
- package/src/utils.ts +5 -0
- package/test-artifacts/.last-run.json +4 -0
- package/test-artifacts/test-results.json +1801 -0
- package/test-results.json +2050 -0
- package/tests/playwright/qb/auth/cacheSwitching.test.ts +189 -0
- package/tests/playwright/qb/auth/fetchTempToken401.test.ts +217 -0
- package/tests/playwright/qb/auth/getAppWithUserToken.test.ts +151 -0
- package/tests/playwright/qb/auth/getTempToken.test.ts +180 -0
- package/tests/playwright/qb/auth/tokenPrefetch.test.ts +192 -0
- package/tests/playwright/qb/auth/tokenRenewal.test.ts +110 -0
- package/tests/setup.ts +19 -0
- package/tests/vitest/qb/apps/getApp.test.ts +46 -0
- package/tests/vitest/qb/fields/getFields.test.ts +234 -0
- package/tests/vitest/qb/index.ts +4 -0
- package/tests/vitest/qb/tables/getAppTables.test.ts +67 -0
- package/tests/vitest/unit/apps/getApp.test.ts +81 -0
- package/tests/vitest/unit/auth/fetchTempToken401.test.ts +59 -0
- package/tests/vitest/unit/auth/getTempToken.test.ts +83 -0
- package/tests/vitest/unit/auth/retryOn401.test.ts +72 -0
- package/tests/vitest/unit/auth/two401s.test.ts +77 -0
- package/tests/vitest/unit/fields/getFields.test.ts +115 -0
- package/tests/vitest/unit/index.ts +4 -0
- package/tests/vitest/unit/tables/getAppTables.test.ts +72 -0
- package/text.txt +104 -0
- package/tsconfig.json +28 -0
- package/versionrc.json +9 -0
- package/vitest.config.ts +21 -0
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
// tests/playwright/qb/auth/cacheSwitching.test.ts
|
|
2
|
+
import { test, expect } from "@playwright/test";
|
|
3
|
+
import { quickbaseClient } from "../../../../src/quickbaseClient.ts";
|
|
4
|
+
import fetch from "node-fetch";
|
|
5
|
+
import dotenv from "dotenv";
|
|
6
|
+
|
|
7
|
+
dotenv.config();
|
|
8
|
+
|
|
9
|
+
const loginToQuickbase = async (
|
|
10
|
+
page: import("@playwright/test").Page,
|
|
11
|
+
quickbaseUrl: string,
|
|
12
|
+
username: string,
|
|
13
|
+
password: string
|
|
14
|
+
): Promise<boolean> => {
|
|
15
|
+
console.log("Logging in to QuickBase");
|
|
16
|
+
let loginSuccess = false;
|
|
17
|
+
const maxLoginAttempts = 3;
|
|
18
|
+
let loginAttempt = 0;
|
|
19
|
+
|
|
20
|
+
while (!loginSuccess && loginAttempt < maxLoginAttempts) {
|
|
21
|
+
try {
|
|
22
|
+
loginAttempt++;
|
|
23
|
+
if (loginAttempt > 1) console.log(`Login attempt ${loginAttempt}`);
|
|
24
|
+
|
|
25
|
+
console.log("Navigating to login page");
|
|
26
|
+
await page.goto(quickbaseUrl, { timeout: 30000 });
|
|
27
|
+
console.log("Waiting for loginid selector");
|
|
28
|
+
await page.waitForSelector("input[name='loginid']", { timeout: 30000 });
|
|
29
|
+
await page.fill("input[name='loginid']", username);
|
|
30
|
+
await page.fill("input[name='password']", password);
|
|
31
|
+
console.log("Clicking signin");
|
|
32
|
+
await page.click("#signin");
|
|
33
|
+
console.log("Waiting for navigation");
|
|
34
|
+
await page.waitForURL(`https://*.quickbase.com/**`, {
|
|
35
|
+
waitUntil: "networkidle",
|
|
36
|
+
timeout: 30000,
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const loginError = await page.$(".login-error");
|
|
40
|
+
if (loginError) {
|
|
41
|
+
throw new Error("Login failed. Please check your credentials.");
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
console.log("Signed In to QuickBase.");
|
|
45
|
+
loginSuccess = true;
|
|
46
|
+
} catch (error) {
|
|
47
|
+
console.error(
|
|
48
|
+
`Login attempt ${loginAttempt} failed: ${(error as Error).message}`
|
|
49
|
+
);
|
|
50
|
+
if (loginAttempt >= maxLoginAttempts) {
|
|
51
|
+
console.error("Max login attempts reached. Exiting.");
|
|
52
|
+
await page.context().browser()!.close();
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
await new Promise((resolve) => setTimeout(resolve, 5000));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return loginSuccess;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
test.describe("QuickbaseClient Integration - Cache Switching with Temp Tokens", () => {
|
|
62
|
+
// Removed test.use({ context: ... })
|
|
63
|
+
|
|
64
|
+
test("caches and switches tokens for different DBIDs with getApp → getFields → getApp sequence", async ({
|
|
65
|
+
page,
|
|
66
|
+
}) => {
|
|
67
|
+
console.log("Starting cacheSwitching test");
|
|
68
|
+
const realm = process.env.QB_REALM;
|
|
69
|
+
const appId = process.env.QB_APP_ID;
|
|
70
|
+
const tableId = process.env.QB_TABLE_ID;
|
|
71
|
+
const username = process.env.QB_USERNAME;
|
|
72
|
+
const password = process.env.QB_PASSWORD;
|
|
73
|
+
|
|
74
|
+
if (!realm) throw new Error("QB_REALM is not defined in .env");
|
|
75
|
+
if (!appId) throw new Error("QB_APP_ID is not defined in .env");
|
|
76
|
+
if (!tableId) throw new Error("QB_TABLE_ID is not defined in .env");
|
|
77
|
+
if (!username) throw new Error("QB_USERNAME is not defined in .env");
|
|
78
|
+
if (!password) throw new Error("QB_PASSWORD is not defined in .env");
|
|
79
|
+
|
|
80
|
+
const quickbaseUrl = `https://${realm}.quickbase.com/db/main?a=SignIn`;
|
|
81
|
+
const loginSuccess = await loginToQuickbase(
|
|
82
|
+
page,
|
|
83
|
+
quickbaseUrl,
|
|
84
|
+
username,
|
|
85
|
+
password
|
|
86
|
+
);
|
|
87
|
+
if (!loginSuccess) {
|
|
88
|
+
throw new Error("Failed to log in to QuickBase after max attempts.");
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
await page.goto(`https://${realm}.quickbase.com/db/${appId}`, {
|
|
92
|
+
waitUntil: "networkidle",
|
|
93
|
+
timeout: 30000,
|
|
94
|
+
});
|
|
95
|
+
console.log("Post-login URL after app navigation:", page.url());
|
|
96
|
+
|
|
97
|
+
const browserFetch = async (url: string, init?: RequestInit) => {
|
|
98
|
+
const response = await page.evaluate(
|
|
99
|
+
async ([fetchUrl, fetchInit]) => {
|
|
100
|
+
const res = await fetch(fetchUrl, {
|
|
101
|
+
...fetchInit,
|
|
102
|
+
credentials: "include",
|
|
103
|
+
});
|
|
104
|
+
const body = await res.text();
|
|
105
|
+
return {
|
|
106
|
+
ok: res.ok,
|
|
107
|
+
status: res.status,
|
|
108
|
+
statusText: res.statusText,
|
|
109
|
+
headers: Object.fromEntries(res.headers.entries()),
|
|
110
|
+
body,
|
|
111
|
+
};
|
|
112
|
+
},
|
|
113
|
+
[url, init] as [string, RequestInit]
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
console.log(`Raw response from ${url}:`, {
|
|
117
|
+
status: response.status,
|
|
118
|
+
statusText: response.statusText,
|
|
119
|
+
body: response.body,
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
const fetchResponse = new Response(response.body || null, {
|
|
123
|
+
status: response.status,
|
|
124
|
+
statusText: response.statusText,
|
|
125
|
+
headers: response.headers,
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
fetchResponse.json = async () => {
|
|
129
|
+
if (!response.body) throw new Error("Empty response body from API");
|
|
130
|
+
try {
|
|
131
|
+
return JSON.parse(response.body);
|
|
132
|
+
} catch (e) {
|
|
133
|
+
throw new SyntaxError(`Invalid JSON response: ${response.body}`);
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
return fetchResponse;
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
const client = quickbaseClient({
|
|
141
|
+
realm,
|
|
142
|
+
useTempTokens: true,
|
|
143
|
+
debug: true,
|
|
144
|
+
fetchApi: async (url, init) => {
|
|
145
|
+
if (url.includes("/auth/temporary/")) {
|
|
146
|
+
return browserFetch(url, init);
|
|
147
|
+
}
|
|
148
|
+
console.log(`Fetching ${url} with init:`, init);
|
|
149
|
+
const response = await fetch(url, init as RequestInit);
|
|
150
|
+
const body = await response.text();
|
|
151
|
+
|
|
152
|
+
console.log(`Raw response from ${url}:`, {
|
|
153
|
+
status: response.status,
|
|
154
|
+
statusText: response.statusText,
|
|
155
|
+
body,
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
const fetchResponse = new Response(body || null, {
|
|
159
|
+
status: response.status,
|
|
160
|
+
statusText: response.statusText,
|
|
161
|
+
headers: Object.fromEntries(response.headers.entries()),
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
fetchResponse.json = async () => {
|
|
165
|
+
if (!body) throw new Error("Empty response body from API");
|
|
166
|
+
try {
|
|
167
|
+
return JSON.parse(body);
|
|
168
|
+
} catch (e) {
|
|
169
|
+
throw new SyntaxError(`Invalid JSON response: ${body}`);
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
return fetchResponse;
|
|
174
|
+
},
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
const appResult1 = await client.getApp({ appId });
|
|
178
|
+
console.log("First getApp response:", appResult1);
|
|
179
|
+
expect(appResult1.id).toBe(appId);
|
|
180
|
+
|
|
181
|
+
const fieldsResult = await client.getFields({ tableId });
|
|
182
|
+
console.log("getFields response:", fieldsResult);
|
|
183
|
+
expect(fieldsResult.length).toBeGreaterThan(0);
|
|
184
|
+
|
|
185
|
+
const appResult2 = await client.getApp({ appId });
|
|
186
|
+
console.log("Second getApp response:", appResult2);
|
|
187
|
+
expect(appResult2.id).toBe(appId);
|
|
188
|
+
});
|
|
189
|
+
});
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
// tests/playwright/qb/auth/tokenInvalidation.test.ts
|
|
2
|
+
import { test, expect } from "@playwright/test";
|
|
3
|
+
import { quickbaseClient } from "../../../../src/quickbaseClient.ts";
|
|
4
|
+
import fetch from "node-fetch";
|
|
5
|
+
import dotenv from "dotenv";
|
|
6
|
+
|
|
7
|
+
dotenv.config();
|
|
8
|
+
|
|
9
|
+
const loginToQuickbase = async (
|
|
10
|
+
page: import("@playwright/test").Page,
|
|
11
|
+
quickbaseUrl: string,
|
|
12
|
+
username: string,
|
|
13
|
+
password: string
|
|
14
|
+
): Promise<boolean> => {
|
|
15
|
+
console.log("Logging in to QuickBase");
|
|
16
|
+
let loginSuccess = false;
|
|
17
|
+
const maxLoginAttempts = 3;
|
|
18
|
+
let loginAttempt = 0;
|
|
19
|
+
|
|
20
|
+
while (!loginSuccess && loginAttempt < maxLoginAttempts) {
|
|
21
|
+
try {
|
|
22
|
+
loginAttempt++;
|
|
23
|
+
if (loginAttempt > 1) console.log(`Login attempt ${loginAttempt}`);
|
|
24
|
+
|
|
25
|
+
await page.goto(quickbaseUrl, { timeout: 60000 });
|
|
26
|
+
await page.waitForSelector("input[name='loginid']", { timeout: 60000 });
|
|
27
|
+
await page.fill("input[name='loginid']", username);
|
|
28
|
+
await page.fill("input[name='password']", password);
|
|
29
|
+
await page.click("#signin");
|
|
30
|
+
await page.waitForURL(`https://*.quickbase.com/**`, {
|
|
31
|
+
waitUntil: "networkidle",
|
|
32
|
+
timeout: 60000,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
const loginError = await page.$(".login-error");
|
|
36
|
+
if (loginError) {
|
|
37
|
+
throw new Error("Login failed. Please check your credentials.");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
console.log("Signed In to QuickBase.");
|
|
41
|
+
loginSuccess = true;
|
|
42
|
+
} catch (error) {
|
|
43
|
+
console.error(
|
|
44
|
+
`Login attempt ${loginAttempt} failed: ${(error as Error).message}`
|
|
45
|
+
);
|
|
46
|
+
if (loginAttempt >= maxLoginAttempts) {
|
|
47
|
+
console.error("Max login attempts reached. Exiting.");
|
|
48
|
+
await page.context().browser()!.close();
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
await new Promise((resolve) => setTimeout(resolve, 5000));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return loginSuccess;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
test.describe("QuickbaseClient Integration - Token Invalidation with Cached Token", () => {
|
|
58
|
+
test("retries with new token when cached token is unexpectedly invalidated", async ({
|
|
59
|
+
page,
|
|
60
|
+
}) => {
|
|
61
|
+
const realm = process.env.QB_REALM;
|
|
62
|
+
const appId = process.env.QB_APP_ID;
|
|
63
|
+
const username = process.env.QB_USERNAME;
|
|
64
|
+
const password = process.env.QB_PASSWORD;
|
|
65
|
+
|
|
66
|
+
if (!realm) throw new Error("QB_REALM is not defined in .env");
|
|
67
|
+
if (!appId) throw new Error("QB_APP_ID is not defined in .env");
|
|
68
|
+
if (!username) throw new Error("QB_USERNAME is not defined in .env");
|
|
69
|
+
if (!password) throw new Error("QB_PASSWORD is not defined in .env");
|
|
70
|
+
|
|
71
|
+
// Login to QuickBase
|
|
72
|
+
const quickbaseUrl = `https://${realm}.quickbase.com/db/main?a=SignIn`;
|
|
73
|
+
const loginSuccess = await loginToQuickbase(
|
|
74
|
+
page,
|
|
75
|
+
quickbaseUrl,
|
|
76
|
+
username,
|
|
77
|
+
password
|
|
78
|
+
);
|
|
79
|
+
if (!loginSuccess) {
|
|
80
|
+
throw new Error("Failed to log in to QuickBase after max attempts.");
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Navigate to the app page to ensure session context
|
|
84
|
+
await page.goto(`https://${realm}.quickbase.com/db/${appId}`, {
|
|
85
|
+
waitUntil: "networkidle",
|
|
86
|
+
timeout: 60000,
|
|
87
|
+
});
|
|
88
|
+
console.log("Post-login URL after app navigation:", page.url());
|
|
89
|
+
|
|
90
|
+
// Track tokens and call counts
|
|
91
|
+
let callCount = 0;
|
|
92
|
+
let getAppCallCount = 0;
|
|
93
|
+
let initialToken: string | null = null;
|
|
94
|
+
let retryToken: string | null = null;
|
|
95
|
+
|
|
96
|
+
const browserFetch = async (url: string, init?: RequestInit) => {
|
|
97
|
+
console.log(`browserFetch called for ${url}, callCount: ${callCount}`);
|
|
98
|
+
const response = await page.evaluate(
|
|
99
|
+
async ([fetchUrl, fetchInit]) => {
|
|
100
|
+
const res = await fetch(fetchUrl, {
|
|
101
|
+
...fetchInit,
|
|
102
|
+
credentials: "include",
|
|
103
|
+
});
|
|
104
|
+
const body = await res.text();
|
|
105
|
+
return {
|
|
106
|
+
ok: res.ok,
|
|
107
|
+
status: res.status,
|
|
108
|
+
statusText: res.statusText,
|
|
109
|
+
headers: Object.fromEntries(res.headers.entries()),
|
|
110
|
+
body,
|
|
111
|
+
};
|
|
112
|
+
},
|
|
113
|
+
[url, init] as [string, RequestInit]
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
console.log(`Raw response from ${url}:`, {
|
|
117
|
+
status: response.status,
|
|
118
|
+
statusText: response.statusText,
|
|
119
|
+
body: response.body,
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
const fetchResponse = new Response(response.body || null, {
|
|
123
|
+
status: response.status,
|
|
124
|
+
statusText: response.statusText,
|
|
125
|
+
headers: response.headers,
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
// Capture token from response
|
|
129
|
+
if (url.includes("/auth/temporary/")) {
|
|
130
|
+
const json = await fetchResponse.clone().json();
|
|
131
|
+
const token = json.temporaryAuthorization;
|
|
132
|
+
if (!initialToken) {
|
|
133
|
+
initialToken = token;
|
|
134
|
+
console.log(`Captured initial token: ${initialToken}`);
|
|
135
|
+
} else if (!retryToken) {
|
|
136
|
+
retryToken = token;
|
|
137
|
+
console.log(`Captured retry token: ${retryToken}`);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return fetchResponse;
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
const client = quickbaseClient({
|
|
145
|
+
realm,
|
|
146
|
+
useTempTokens: true,
|
|
147
|
+
debug: true,
|
|
148
|
+
fetchApi: async (url, init) => {
|
|
149
|
+
callCount++;
|
|
150
|
+
console.log(`fetchApi called for ${url}, callCount: ${callCount}`);
|
|
151
|
+
|
|
152
|
+
if (url.includes("/auth/temporary/")) {
|
|
153
|
+
return browserFetch(url, init); // Use browser fetch for temp tokens
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (url.includes(`/apps/${appId}`)) {
|
|
157
|
+
getAppCallCount++;
|
|
158
|
+
console.log(`getApp call count: ${getAppCallCount}`);
|
|
159
|
+
// Simulate 401 on the first getApp call to trigger retry
|
|
160
|
+
if (getAppCallCount === 1) {
|
|
161
|
+
console.log("Simulating 401 Unauthorized for first getApp call");
|
|
162
|
+
return new Response(JSON.stringify({ message: "Unauthorized" }), {
|
|
163
|
+
status: 401,
|
|
164
|
+
statusText: "Unauthorized",
|
|
165
|
+
headers: { "Content-Type": "application/json" },
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Use node-fetch for subsequent calls (including retries)
|
|
171
|
+
console.log(`Fetching ${url} with init:`, init);
|
|
172
|
+
const response = await fetch(url, init as RequestInit);
|
|
173
|
+
const body = await response.text();
|
|
174
|
+
|
|
175
|
+
console.log(`Raw response from ${url}:`, {
|
|
176
|
+
status: response.status,
|
|
177
|
+
statusText: response.statusText,
|
|
178
|
+
body,
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
return new Response(body || null, {
|
|
182
|
+
status: response.status,
|
|
183
|
+
statusText: response.statusText,
|
|
184
|
+
headers: Object.fromEntries(response.headers.entries()),
|
|
185
|
+
});
|
|
186
|
+
},
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
// Pre-fetch token to simulate cached token
|
|
190
|
+
console.log("Pre-fetching initial token...");
|
|
191
|
+
const preFetchResult = await client.getTempTokenDBID({ dbid: appId });
|
|
192
|
+
console.log("Initial token pre-fetched and cached:", preFetchResult);
|
|
193
|
+
|
|
194
|
+
// Call getApp, expecting a 401 followed by a retry with a new token
|
|
195
|
+
const appResult = await client.getApp({ appId });
|
|
196
|
+
console.log("App response after token invalidation and retry:", appResult);
|
|
197
|
+
|
|
198
|
+
// Validate the app response
|
|
199
|
+
expect(appResult).toHaveProperty("id", appId);
|
|
200
|
+
expect(appResult).toHaveProperty("name");
|
|
201
|
+
|
|
202
|
+
// Verify call counts and token behavior
|
|
203
|
+
console.log(
|
|
204
|
+
`Final callCount: ${callCount}, Final getAppCallCount: ${getAppCallCount}`
|
|
205
|
+
);
|
|
206
|
+
expect(callCount).toBe(4); // Pre-fetch, 401, token retry, getApp success
|
|
207
|
+
expect(getAppCallCount).toBe(2); // Initial 401, retry success
|
|
208
|
+
|
|
209
|
+
// Validate pre-fetch and retry tokens
|
|
210
|
+
console.log(`Initial token: ${initialToken}`);
|
|
211
|
+
console.log(`Retry token: ${retryToken}`);
|
|
212
|
+
expect(initialToken).toBeDefined();
|
|
213
|
+
expect(initialToken).toBe(preFetchResult.temporaryAuthorization); // Confirm pre-fetch token was used
|
|
214
|
+
expect(retryToken).toBeDefined();
|
|
215
|
+
expect(initialToken).not.toBe(retryToken); // Confirm retry used a new token
|
|
216
|
+
});
|
|
217
|
+
});
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
// tests/integration/auth/getAppWithUserToken.test.ts
|
|
2
|
+
import { test, expect } from "@playwright/test";
|
|
3
|
+
import { quickbaseClient } from "../../../../src/quickbaseClient.ts";
|
|
4
|
+
import dotenv from "dotenv";
|
|
5
|
+
|
|
6
|
+
dotenv.config();
|
|
7
|
+
|
|
8
|
+
const loginToQuickbase = async (
|
|
9
|
+
page: import("@playwright/test").Page,
|
|
10
|
+
quickbaseUrl: string,
|
|
11
|
+
username: string,
|
|
12
|
+
password: string
|
|
13
|
+
): Promise<boolean> => {
|
|
14
|
+
console.log("Logging in to QuickBase");
|
|
15
|
+
let loginSuccess = false;
|
|
16
|
+
const maxLoginAttempts = 3;
|
|
17
|
+
let loginAttempt = 0;
|
|
18
|
+
|
|
19
|
+
while (!loginSuccess && loginAttempt < maxLoginAttempts) {
|
|
20
|
+
try {
|
|
21
|
+
loginAttempt++;
|
|
22
|
+
if (loginAttempt > 1) console.log(`Login attempt ${loginAttempt}`);
|
|
23
|
+
|
|
24
|
+
await page.goto(quickbaseUrl, { timeout: 60000 });
|
|
25
|
+
await page.waitForSelector("input[name='loginid']", { timeout: 60000 });
|
|
26
|
+
await page.fill("input[name='loginid']", username);
|
|
27
|
+
await page.fill("input[name='password']", password);
|
|
28
|
+
await page.click("#signin");
|
|
29
|
+
await page.waitForURL(`https://*.quickbase.com/**`, {
|
|
30
|
+
waitUntil: "networkidle",
|
|
31
|
+
timeout: 60000,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const loginError = await page.$(".login-error");
|
|
35
|
+
if (loginError) {
|
|
36
|
+
throw new Error("Login failed. Please check your credentials.");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
console.log("Signed In to QuickBase.");
|
|
40
|
+
loginSuccess = true;
|
|
41
|
+
} catch (error) {
|
|
42
|
+
console.error(
|
|
43
|
+
`Login attempt ${loginAttempt} failed: ${(error as Error).message}`
|
|
44
|
+
);
|
|
45
|
+
if (loginAttempt >= maxLoginAttempts) {
|
|
46
|
+
console.error("Max login attempts reached. Exiting.");
|
|
47
|
+
await page.context().browser()!.close();
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
await new Promise((resolve) => setTimeout(resolve, 5000));
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return loginSuccess;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
test.describe("QuickbaseClient Integration - getApp with User Token", () => {
|
|
57
|
+
test("uses user token for getApp in browser", async ({ page }) => {
|
|
58
|
+
const realm = process.env.QB_REALM;
|
|
59
|
+
const appId = process.env.QB_APP_ID;
|
|
60
|
+
const username = process.env.QB_USERNAME;
|
|
61
|
+
const password = process.env.QB_PASSWORD;
|
|
62
|
+
const userToken = process.env.QB_USER_TOKEN;
|
|
63
|
+
|
|
64
|
+
if (!realm) throw new Error("QB_REALM is not defined in .env");
|
|
65
|
+
if (!appId) throw new Error("QB_APP_ID is not defined in .env");
|
|
66
|
+
if (!username) throw new Error("QB_USERNAME is not defined in .env");
|
|
67
|
+
if (!password) throw new Error("QB_PASSWORD is not defined in .env");
|
|
68
|
+
if (!userToken) throw new Error("QB_USER_TOKEN is not defined in .env");
|
|
69
|
+
|
|
70
|
+
// Login to Quickbase (optional, to mimic code page context)
|
|
71
|
+
const quickbaseUrl = `https://${realm}.quickbase.com/db/main?a=SignIn`;
|
|
72
|
+
const loginSuccess = await loginToQuickbase(
|
|
73
|
+
page,
|
|
74
|
+
quickbaseUrl,
|
|
75
|
+
username,
|
|
76
|
+
password
|
|
77
|
+
);
|
|
78
|
+
if (!loginSuccess) {
|
|
79
|
+
throw new Error("Failed to log in to QuickBase after max attempts.");
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Navigate to the app page to ensure session context (optional)
|
|
83
|
+
await page.goto(`https://${realm}.quickbase.com/db/${appId}`, {
|
|
84
|
+
waitUntil: "networkidle",
|
|
85
|
+
timeout: 60000,
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
const currentUrl = page.url();
|
|
89
|
+
console.log("Post-login URL after app navigation:", currentUrl);
|
|
90
|
+
|
|
91
|
+
// Create Quickbase client with user token
|
|
92
|
+
const client = quickbaseClient({
|
|
93
|
+
realm,
|
|
94
|
+
userToken, // Use user token instead of temp tokens
|
|
95
|
+
debug: true,
|
|
96
|
+
fetchApi: async (url, init) => {
|
|
97
|
+
console.log(`Fetching ${url} with init:`, init);
|
|
98
|
+
const response = await page.evaluate(
|
|
99
|
+
async ([fetchUrl, fetchInit]) => {
|
|
100
|
+
const res = await fetch(fetchUrl, {
|
|
101
|
+
...fetchInit,
|
|
102
|
+
credentials: "omit", // No cookies needed with user token
|
|
103
|
+
});
|
|
104
|
+
const body = await res.text();
|
|
105
|
+
return {
|
|
106
|
+
ok: res.ok,
|
|
107
|
+
status: res.status,
|
|
108
|
+
statusText: res.statusText,
|
|
109
|
+
headers: Object.fromEntries(res.headers.entries()),
|
|
110
|
+
body,
|
|
111
|
+
};
|
|
112
|
+
},
|
|
113
|
+
[url, init] as [string, RequestInit]
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
console.log(`Raw response from ${url}:`, {
|
|
117
|
+
status: response.status,
|
|
118
|
+
statusText: response.statusText,
|
|
119
|
+
body: response.body,
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
const fetchResponse = new Response(response.body || null, {
|
|
123
|
+
status: response.status,
|
|
124
|
+
statusText: response.statusText,
|
|
125
|
+
headers: response.headers,
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
fetchResponse.json = async () => {
|
|
129
|
+
if (!response.body) {
|
|
130
|
+
throw new Error("Empty response body from API");
|
|
131
|
+
}
|
|
132
|
+
try {
|
|
133
|
+
return JSON.parse(response.body);
|
|
134
|
+
} catch (e) {
|
|
135
|
+
throw new SyntaxError(`Invalid JSON response: ${response.body}`);
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
return fetchResponse;
|
|
140
|
+
},
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
// Call getApp directly
|
|
144
|
+
const appResult = await client.getApp({ appId });
|
|
145
|
+
console.log("App response using user token:", appResult);
|
|
146
|
+
|
|
147
|
+
// Validate the app response
|
|
148
|
+
expect(appResult).toHaveProperty("id", appId);
|
|
149
|
+
expect(appResult).toHaveProperty("name");
|
|
150
|
+
});
|
|
151
|
+
});
|