@uploadista/client-core 0.0.6 → 0.0.7
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/dist/index.d.ts +53 -32
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/auth/__tests__/auth-http-client.test.ts +95 -41
- package/src/auth/__tests__/{saas-auth.test.ts → uploadista-cloud-auth.test.ts} +129 -92
- package/src/auth/auth-http-client.ts +10 -7
- package/src/auth/index.ts +1 -1
- package/src/auth/types.ts +9 -14
- package/src/auth/{saas-auth.ts → uploadista-cloud-auth.ts} +5 -5
- package/src/client/create-uploadista-client.ts +44 -13
- package/src/client/uploadista-api.ts +97 -12
- package/src/error.ts +3 -1
- package/src/upload/flow-upload.ts +2 -2
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uploadista/client-core",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.7",
|
|
5
5
|
"description": "Platform-agnostic core upload client logic for Uploadista",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"author": "Uploadista",
|
|
@@ -26,12 +26,12 @@
|
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"js-base64": "3.7.8",
|
|
28
28
|
"zod": "4.1.12",
|
|
29
|
-
"@uploadista/core": "0.0.
|
|
29
|
+
"@uploadista/core": "0.0.7"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"tsdown": "0.15.9",
|
|
33
33
|
"vitest": "3.2.4",
|
|
34
|
-
"@uploadista/typescript-config": "0.0.
|
|
34
|
+
"@uploadista/typescript-config": "0.0.7"
|
|
35
35
|
},
|
|
36
36
|
"scripts": {
|
|
37
37
|
"build": "tsdown",
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
-
import type { HttpClient } from "../../
|
|
2
|
+
import type { HttpClient } from "../../services";
|
|
3
3
|
import { AuthHttpClient } from "../auth-http-client";
|
|
4
4
|
import { DirectAuthManager } from "../direct-auth";
|
|
5
5
|
import { NoAuthManager } from "../no-auth";
|
|
6
|
-
import {
|
|
7
|
-
import
|
|
6
|
+
import type { DirectAuthConfig, UploadistaCloudAuthConfig } from "../types";
|
|
7
|
+
import { UploadistaCloudAuthManager } from "../uploadista-cloud-auth";
|
|
8
8
|
|
|
9
9
|
// Mock fetch globally
|
|
10
10
|
global.fetch = vi.fn();
|
|
@@ -74,10 +74,10 @@ describe("AuthHttpClient", () => {
|
|
|
74
74
|
|
|
75
75
|
expect(mockHttpClient.request).toHaveBeenCalledWith(
|
|
76
76
|
"https://api.example.com/upload",
|
|
77
|
-
{
|
|
77
|
+
expect.objectContaining({
|
|
78
78
|
method: "POST",
|
|
79
79
|
headers: { "Content-Type": "application/json" },
|
|
80
|
-
},
|
|
80
|
+
}),
|
|
81
81
|
);
|
|
82
82
|
});
|
|
83
83
|
});
|
|
@@ -100,13 +100,13 @@ describe("AuthHttpClient", () => {
|
|
|
100
100
|
|
|
101
101
|
expect(mockHttpClient.request).toHaveBeenCalledWith(
|
|
102
102
|
"https://api.example.com/upload",
|
|
103
|
-
{
|
|
103
|
+
expect.objectContaining({
|
|
104
104
|
method: "POST",
|
|
105
|
-
headers: {
|
|
105
|
+
headers: expect.objectContaining({
|
|
106
106
|
"Content-Type": "application/json",
|
|
107
107
|
Authorization: "Bearer direct-token",
|
|
108
|
-
},
|
|
109
|
-
},
|
|
108
|
+
}),
|
|
109
|
+
}),
|
|
110
110
|
);
|
|
111
111
|
});
|
|
112
112
|
|
|
@@ -125,27 +125,72 @@ describe("AuthHttpClient", () => {
|
|
|
125
125
|
|
|
126
126
|
expect(mockHttpClient.request).toHaveBeenCalledWith(
|
|
127
127
|
"https://api.example.com/upload",
|
|
128
|
-
{
|
|
128
|
+
expect.objectContaining({
|
|
129
129
|
headers: { Authorization: "Bearer async-token" },
|
|
130
|
-
},
|
|
130
|
+
}),
|
|
131
131
|
);
|
|
132
132
|
});
|
|
133
133
|
});
|
|
134
134
|
|
|
135
|
-
describe("with
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
135
|
+
describe("with UploadistaCloudAuthManager", () => {
|
|
136
|
+
let uploadAuthMockHttpClient: HttpClient;
|
|
137
|
+
|
|
138
|
+
beforeEach(() => {
|
|
139
|
+
// Create a separate mock HTTP client for auth requests
|
|
140
|
+
uploadAuthMockHttpClient = {
|
|
141
|
+
request: vi.fn(),
|
|
142
|
+
getMetrics: vi.fn(() => ({
|
|
143
|
+
activeConnections: 0,
|
|
144
|
+
totalConnections: 0,
|
|
145
|
+
reuseRate: 0,
|
|
146
|
+
averageConnectionTime: 0,
|
|
147
|
+
})),
|
|
148
|
+
getDetailedMetrics: vi.fn(() => ({
|
|
149
|
+
activeConnections: 0,
|
|
150
|
+
totalConnections: 0,
|
|
151
|
+
reuseRate: 0,
|
|
152
|
+
averageConnectionTime: 0,
|
|
153
|
+
health: {
|
|
154
|
+
status: "healthy" as const,
|
|
155
|
+
score: 100,
|
|
156
|
+
issues: [],
|
|
157
|
+
recommendations: [],
|
|
158
|
+
},
|
|
159
|
+
requestsPerSecond: 0,
|
|
160
|
+
errorRate: 0,
|
|
161
|
+
timeouts: 0,
|
|
162
|
+
retries: 0,
|
|
163
|
+
fastConnections: 0,
|
|
164
|
+
slowConnections: 0,
|
|
165
|
+
http2Info: {
|
|
166
|
+
supported: true,
|
|
167
|
+
detected: false,
|
|
168
|
+
version: "h2",
|
|
169
|
+
multiplexingActive: false,
|
|
170
|
+
},
|
|
171
|
+
})),
|
|
172
|
+
reset: vi.fn(),
|
|
173
|
+
close: vi.fn(async () => {}),
|
|
174
|
+
warmupConnections: vi.fn(async () => {}),
|
|
175
|
+
};
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it("should attach JWT token from UploadistaCloudAuthManager", async () => {
|
|
179
|
+
const config: UploadistaCloudAuthConfig = {
|
|
180
|
+
mode: "uploadista-cloud",
|
|
139
181
|
authServerUrl: "https://auth.example.com/token",
|
|
140
|
-
|
|
182
|
+
clientId: "client-id-123",
|
|
141
183
|
};
|
|
142
184
|
|
|
143
|
-
vi.mocked(
|
|
185
|
+
vi.mocked(uploadAuthMockHttpClient.request).mockResolvedValueOnce({
|
|
144
186
|
ok: true,
|
|
145
187
|
json: async () => ({ token: "jwt-token-123" }),
|
|
146
|
-
} as
|
|
188
|
+
} as any);
|
|
147
189
|
|
|
148
|
-
const authManager = new
|
|
190
|
+
const authManager = new UploadistaCloudAuthManager(
|
|
191
|
+
config,
|
|
192
|
+
uploadAuthMockHttpClient,
|
|
193
|
+
);
|
|
149
194
|
const authClient = new AuthHttpClient(mockHttpClient, authManager);
|
|
150
195
|
|
|
151
196
|
await authClient.request("https://api.example.com/upload", {
|
|
@@ -155,29 +200,32 @@ describe("AuthHttpClient", () => {
|
|
|
155
200
|
|
|
156
201
|
expect(mockHttpClient.request).toHaveBeenCalledWith(
|
|
157
202
|
"https://api.example.com/upload",
|
|
158
|
-
{
|
|
203
|
+
expect.objectContaining({
|
|
159
204
|
method: "POST",
|
|
160
|
-
headers: {
|
|
205
|
+
headers: expect.objectContaining({
|
|
161
206
|
"Content-Type": "application/json",
|
|
162
207
|
Authorization: "Bearer jwt-token-123",
|
|
163
|
-
},
|
|
164
|
-
},
|
|
208
|
+
}),
|
|
209
|
+
}),
|
|
165
210
|
);
|
|
166
211
|
});
|
|
167
212
|
|
|
168
213
|
it("should extract job ID from upload URL", async () => {
|
|
169
|
-
const config:
|
|
170
|
-
mode: "
|
|
214
|
+
const config: UploadistaCloudAuthConfig = {
|
|
215
|
+
mode: "uploadista-cloud",
|
|
171
216
|
authServerUrl: "https://auth.example.com/token",
|
|
172
|
-
|
|
217
|
+
clientId: "client-id-123",
|
|
173
218
|
};
|
|
174
219
|
|
|
175
|
-
vi.mocked(
|
|
220
|
+
vi.mocked(uploadAuthMockHttpClient.request).mockResolvedValueOnce({
|
|
176
221
|
ok: true,
|
|
177
222
|
json: async () => ({ token: "jwt-token-for-upload-123" }),
|
|
178
|
-
} as
|
|
223
|
+
} as any);
|
|
179
224
|
|
|
180
|
-
const authManager = new
|
|
225
|
+
const authManager = new UploadistaCloudAuthManager(
|
|
226
|
+
config,
|
|
227
|
+
uploadAuthMockHttpClient,
|
|
228
|
+
);
|
|
181
229
|
const authClient = new AuthHttpClient(mockHttpClient, authManager);
|
|
182
230
|
|
|
183
231
|
await authClient.request(
|
|
@@ -191,18 +239,21 @@ describe("AuthHttpClient", () => {
|
|
|
191
239
|
});
|
|
192
240
|
|
|
193
241
|
it("should extract job ID from flow URL", async () => {
|
|
194
|
-
const config:
|
|
195
|
-
mode: "
|
|
242
|
+
const config: UploadistaCloudAuthConfig = {
|
|
243
|
+
mode: "uploadista-cloud",
|
|
196
244
|
authServerUrl: "https://auth.example.com/token",
|
|
197
|
-
|
|
245
|
+
clientId: "client-id-123",
|
|
198
246
|
};
|
|
199
247
|
|
|
200
|
-
vi.mocked(
|
|
248
|
+
vi.mocked(uploadAuthMockHttpClient.request).mockResolvedValueOnce({
|
|
201
249
|
ok: true,
|
|
202
250
|
json: async () => ({ token: "jwt-token-for-flow-456" }),
|
|
203
|
-
} as
|
|
251
|
+
} as any);
|
|
204
252
|
|
|
205
|
-
const authManager = new
|
|
253
|
+
const authManager = new UploadistaCloudAuthManager(
|
|
254
|
+
config,
|
|
255
|
+
uploadAuthMockHttpClient,
|
|
256
|
+
);
|
|
206
257
|
const authClient = new AuthHttpClient(mockHttpClient, authManager);
|
|
207
258
|
|
|
208
259
|
await authClient.request(
|
|
@@ -216,18 +267,21 @@ describe("AuthHttpClient", () => {
|
|
|
216
267
|
});
|
|
217
268
|
|
|
218
269
|
it("should extract job ID from jobs URL", async () => {
|
|
219
|
-
const config:
|
|
220
|
-
mode: "
|
|
270
|
+
const config: UploadistaCloudAuthConfig = {
|
|
271
|
+
mode: "uploadista-cloud",
|
|
221
272
|
authServerUrl: "https://auth.example.com/token",
|
|
222
|
-
|
|
273
|
+
clientId: "client-id-123",
|
|
223
274
|
};
|
|
224
275
|
|
|
225
|
-
vi.mocked(
|
|
276
|
+
vi.mocked(uploadAuthMockHttpClient.request).mockResolvedValueOnce({
|
|
226
277
|
ok: true,
|
|
227
278
|
json: async () => ({ token: "jwt-token-for-job-789" }),
|
|
228
|
-
} as
|
|
279
|
+
} as any);
|
|
229
280
|
|
|
230
|
-
const authManager = new
|
|
281
|
+
const authManager = new UploadistaCloudAuthManager(
|
|
282
|
+
config,
|
|
283
|
+
uploadAuthMockHttpClient,
|
|
284
|
+
);
|
|
231
285
|
const authClient = new AuthHttpClient(mockHttpClient, authManager);
|
|
232
286
|
|
|
233
287
|
await authClient.request(
|
|
@@ -1,35 +1,70 @@
|
|
|
1
1
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
-
import {
|
|
3
|
-
import type {
|
|
2
|
+
import type { HttpClient } from "../../services";
|
|
3
|
+
import type { UploadistaCloudAuthConfig } from "../types";
|
|
4
|
+
import { UploadistaCloudAuthManager } from "../uploadista-cloud-auth";
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
|
|
6
|
+
describe("UploadistaCloudAuthManager", () => {
|
|
7
|
+
let mockHttpClient: HttpClient;
|
|
7
8
|
|
|
8
|
-
describe("SaasAuthManager", () => {
|
|
9
9
|
beforeEach(() => {
|
|
10
10
|
vi.clearAllMocks();
|
|
11
|
+
|
|
12
|
+
// Create mock HTTP client
|
|
13
|
+
mockHttpClient = {
|
|
14
|
+
request: vi.fn(),
|
|
15
|
+
getMetrics: vi.fn(() => ({
|
|
16
|
+
activeConnections: 0,
|
|
17
|
+
totalConnections: 0,
|
|
18
|
+
reuseRate: 0,
|
|
19
|
+
averageConnectionTime: 0,
|
|
20
|
+
})),
|
|
21
|
+
getDetailedMetrics: vi.fn(() => ({
|
|
22
|
+
activeConnections: 0,
|
|
23
|
+
totalConnections: 0,
|
|
24
|
+
reuseRate: 0,
|
|
25
|
+
averageConnectionTime: 0,
|
|
26
|
+
health: {
|
|
27
|
+
status: "healthy" as const,
|
|
28
|
+
score: 100,
|
|
29
|
+
issues: [],
|
|
30
|
+
recommendations: [],
|
|
31
|
+
},
|
|
32
|
+
requestsPerSecond: 0,
|
|
33
|
+
errorRate: 0,
|
|
34
|
+
timeouts: 0,
|
|
35
|
+
retries: 0,
|
|
36
|
+
fastConnections: 0,
|
|
37
|
+
slowConnections: 0,
|
|
38
|
+
http2Info: {
|
|
39
|
+
supported: true,
|
|
40
|
+
detected: false,
|
|
41
|
+
version: "h2",
|
|
42
|
+
multiplexingActive: false,
|
|
43
|
+
},
|
|
44
|
+
})),
|
|
45
|
+
reset: vi.fn(),
|
|
46
|
+
close: vi.fn(async () => {}),
|
|
47
|
+
warmupConnections: vi.fn(async () => {}),
|
|
48
|
+
};
|
|
11
49
|
});
|
|
12
50
|
|
|
13
51
|
describe("fetchToken", () => {
|
|
14
52
|
it("should fetch token from auth server", async () => {
|
|
15
|
-
const config:
|
|
16
|
-
mode: "
|
|
53
|
+
const config: UploadistaCloudAuthConfig = {
|
|
54
|
+
mode: "uploadista-cloud",
|
|
17
55
|
authServerUrl: "https://auth.example.com/token",
|
|
18
|
-
|
|
19
|
-
username: "user",
|
|
20
|
-
password: "pass",
|
|
21
|
-
}),
|
|
56
|
+
clientId: "client-id-123",
|
|
22
57
|
};
|
|
23
58
|
|
|
24
|
-
vi.mocked(
|
|
59
|
+
vi.mocked(mockHttpClient.request).mockResolvedValueOnce({
|
|
25
60
|
ok: true,
|
|
26
61
|
json: async () => ({
|
|
27
62
|
token: "jwt-token-123",
|
|
28
63
|
expiresIn: 3600,
|
|
29
64
|
}),
|
|
30
|
-
});
|
|
65
|
+
} as any);
|
|
31
66
|
|
|
32
|
-
const manager = new
|
|
67
|
+
const manager = new UploadistaCloudAuthManager(config, mockHttpClient);
|
|
33
68
|
const result = await manager.fetchToken();
|
|
34
69
|
|
|
35
70
|
expect(result).toEqual({
|
|
@@ -37,48 +72,50 @@ describe("SaasAuthManager", () => {
|
|
|
37
72
|
expiresIn: 3600,
|
|
38
73
|
});
|
|
39
74
|
|
|
40
|
-
expect(
|
|
41
|
-
"https://auth.example.com/token",
|
|
75
|
+
expect(mockHttpClient.request).toHaveBeenCalledWith(
|
|
76
|
+
"https://auth.example.com/token/client-id-123",
|
|
42
77
|
{
|
|
43
|
-
method: "
|
|
78
|
+
method: "GET",
|
|
44
79
|
headers: {
|
|
45
80
|
"Content-Type": "application/json",
|
|
46
81
|
},
|
|
47
|
-
body: JSON.stringify({ username: "user", password: "pass" }),
|
|
48
82
|
},
|
|
49
83
|
);
|
|
50
84
|
});
|
|
51
85
|
|
|
52
86
|
it("should handle auth server errors", async () => {
|
|
53
|
-
const config:
|
|
54
|
-
mode: "
|
|
87
|
+
const config: UploadistaCloudAuthConfig = {
|
|
88
|
+
mode: "uploadista-cloud",
|
|
55
89
|
authServerUrl: "https://auth.example.com/token",
|
|
56
|
-
|
|
90
|
+
clientId: "client-id-123",
|
|
57
91
|
};
|
|
58
92
|
|
|
59
|
-
vi.mocked(
|
|
93
|
+
vi.mocked(mockHttpClient.request).mockResolvedValueOnce({
|
|
60
94
|
ok: false,
|
|
61
95
|
status: 401,
|
|
96
|
+
statusText: "Unauthorized",
|
|
62
97
|
text: async () => JSON.stringify({ error: "Invalid credentials" }),
|
|
63
|
-
});
|
|
98
|
+
} as any);
|
|
64
99
|
|
|
65
|
-
const manager = new
|
|
100
|
+
const manager = new UploadistaCloudAuthManager(config, mockHttpClient);
|
|
66
101
|
|
|
67
102
|
await expect(manager.fetchToken()).rejects.toThrow(
|
|
68
|
-
"Failed to fetch auth token
|
|
103
|
+
"Failed to fetch auth token",
|
|
69
104
|
);
|
|
70
105
|
});
|
|
71
106
|
|
|
72
107
|
it("should handle network errors", async () => {
|
|
73
|
-
const config:
|
|
74
|
-
mode: "
|
|
108
|
+
const config: UploadistaCloudAuthConfig = {
|
|
109
|
+
mode: "uploadista-cloud",
|
|
75
110
|
authServerUrl: "https://auth.example.com/token",
|
|
76
|
-
|
|
111
|
+
clientId: "client-id-123",
|
|
77
112
|
};
|
|
78
113
|
|
|
79
|
-
vi.mocked(
|
|
114
|
+
vi.mocked(mockHttpClient.request).mockRejectedValueOnce(
|
|
115
|
+
new Error("Network error"),
|
|
116
|
+
);
|
|
80
117
|
|
|
81
|
-
const manager = new
|
|
118
|
+
const manager = new UploadistaCloudAuthManager(config, mockHttpClient);
|
|
82
119
|
|
|
83
120
|
await expect(manager.fetchToken()).rejects.toThrow(
|
|
84
121
|
"Failed to fetch auth token: Network error",
|
|
@@ -86,18 +123,18 @@ describe("SaasAuthManager", () => {
|
|
|
86
123
|
});
|
|
87
124
|
|
|
88
125
|
it("should validate token response format", async () => {
|
|
89
|
-
const config:
|
|
90
|
-
mode: "
|
|
126
|
+
const config: UploadistaCloudAuthConfig = {
|
|
127
|
+
mode: "uploadista-cloud",
|
|
91
128
|
authServerUrl: "https://auth.example.com/token",
|
|
92
|
-
|
|
129
|
+
clientId: "client-id-123",
|
|
93
130
|
};
|
|
94
131
|
|
|
95
|
-
vi.mocked(
|
|
132
|
+
vi.mocked(mockHttpClient.request).mockResolvedValueOnce({
|
|
96
133
|
ok: true,
|
|
97
134
|
json: async () => ({ noToken: "here" }), // Missing token field
|
|
98
|
-
});
|
|
135
|
+
} as any);
|
|
99
136
|
|
|
100
|
-
const manager = new
|
|
137
|
+
const manager = new UploadistaCloudAuthManager(config, mockHttpClient);
|
|
101
138
|
|
|
102
139
|
await expect(manager.fetchToken()).rejects.toThrow(
|
|
103
140
|
"Auth server response missing 'token' field",
|
|
@@ -107,18 +144,18 @@ describe("SaasAuthManager", () => {
|
|
|
107
144
|
|
|
108
145
|
describe("attachToken", () => {
|
|
109
146
|
it("should attach token as Bearer header", async () => {
|
|
110
|
-
const config:
|
|
111
|
-
mode: "
|
|
147
|
+
const config: UploadistaCloudAuthConfig = {
|
|
148
|
+
mode: "uploadista-cloud",
|
|
112
149
|
authServerUrl: "https://auth.example.com/token",
|
|
113
|
-
|
|
150
|
+
clientId: "client-id-123",
|
|
114
151
|
};
|
|
115
152
|
|
|
116
|
-
vi.mocked(
|
|
153
|
+
vi.mocked(mockHttpClient.request).mockResolvedValueOnce({
|
|
117
154
|
ok: true,
|
|
118
155
|
json: async () => ({ token: "jwt-token-123" }),
|
|
119
|
-
});
|
|
156
|
+
} as any);
|
|
120
157
|
|
|
121
|
-
const manager = new
|
|
158
|
+
const manager = new UploadistaCloudAuthManager(config, mockHttpClient);
|
|
122
159
|
const result = await manager.attachToken({
|
|
123
160
|
"Content-Type": "application/json",
|
|
124
161
|
});
|
|
@@ -130,46 +167,46 @@ describe("SaasAuthManager", () => {
|
|
|
130
167
|
});
|
|
131
168
|
|
|
132
169
|
it("should cache token and reuse it", async () => {
|
|
133
|
-
const config:
|
|
134
|
-
mode: "
|
|
170
|
+
const config: UploadistaCloudAuthConfig = {
|
|
171
|
+
mode: "uploadista-cloud",
|
|
135
172
|
authServerUrl: "https://auth.example.com/token",
|
|
136
|
-
|
|
173
|
+
clientId: "client-id-123",
|
|
137
174
|
};
|
|
138
175
|
|
|
139
|
-
vi.mocked(
|
|
176
|
+
vi.mocked(mockHttpClient.request).mockResolvedValueOnce({
|
|
140
177
|
ok: true,
|
|
141
178
|
json: async () => ({ token: "jwt-token-123" }),
|
|
142
|
-
});
|
|
179
|
+
} as any);
|
|
143
180
|
|
|
144
|
-
const manager = new
|
|
181
|
+
const manager = new UploadistaCloudAuthManager(config, mockHttpClient);
|
|
145
182
|
|
|
146
183
|
// First call - should fetch token
|
|
147
184
|
await manager.attachToken();
|
|
148
|
-
expect(
|
|
185
|
+
expect(mockHttpClient.request).toHaveBeenCalledTimes(1);
|
|
149
186
|
|
|
150
187
|
// Second call - should use cached token
|
|
151
188
|
await manager.attachToken();
|
|
152
|
-
expect(
|
|
189
|
+
expect(mockHttpClient.request).toHaveBeenCalledTimes(1); // Still 1, not 2
|
|
153
190
|
});
|
|
154
191
|
|
|
155
192
|
it("should cache tokens per job ID", async () => {
|
|
156
|
-
const config:
|
|
157
|
-
mode: "
|
|
193
|
+
const config: UploadistaCloudAuthConfig = {
|
|
194
|
+
mode: "uploadista-cloud",
|
|
158
195
|
authServerUrl: "https://auth.example.com/token",
|
|
159
|
-
|
|
196
|
+
clientId: "client-id-123",
|
|
160
197
|
};
|
|
161
198
|
|
|
162
|
-
vi.mocked(
|
|
199
|
+
vi.mocked(mockHttpClient.request)
|
|
163
200
|
.mockResolvedValueOnce({
|
|
164
201
|
ok: true,
|
|
165
202
|
json: async () => ({ token: "jwt-token-1" }),
|
|
166
|
-
})
|
|
203
|
+
} as any)
|
|
167
204
|
.mockResolvedValueOnce({
|
|
168
205
|
ok: true,
|
|
169
206
|
json: async () => ({ token: "jwt-token-2" }),
|
|
170
|
-
});
|
|
207
|
+
} as any);
|
|
171
208
|
|
|
172
|
-
const manager = new
|
|
209
|
+
const manager = new UploadistaCloudAuthManager(config, mockHttpClient);
|
|
173
210
|
|
|
174
211
|
// Fetch token for job 1
|
|
175
212
|
const result1 = await manager.attachToken({}, "job-1");
|
|
@@ -184,31 +221,31 @@ describe("SaasAuthManager", () => {
|
|
|
184
221
|
expect(result3.Authorization).toBe("Bearer jwt-token-1");
|
|
185
222
|
|
|
186
223
|
// Should have fetched twice (once per job)
|
|
187
|
-
expect(
|
|
224
|
+
expect(mockHttpClient.request).toHaveBeenCalledTimes(2);
|
|
188
225
|
});
|
|
189
226
|
|
|
190
227
|
it("should refetch expired tokens", async () => {
|
|
191
|
-
const config:
|
|
192
|
-
mode: "
|
|
228
|
+
const config: UploadistaCloudAuthConfig = {
|
|
229
|
+
mode: "uploadista-cloud",
|
|
193
230
|
authServerUrl: "https://auth.example.com/token",
|
|
194
|
-
|
|
231
|
+
clientId: "client-id-123",
|
|
195
232
|
};
|
|
196
233
|
|
|
197
234
|
// First token expires in 1 second
|
|
198
|
-
vi.mocked(
|
|
235
|
+
vi.mocked(mockHttpClient.request)
|
|
199
236
|
.mockResolvedValueOnce({
|
|
200
237
|
ok: true,
|
|
201
238
|
json: async () => ({
|
|
202
239
|
token: "jwt-token-old",
|
|
203
240
|
expiresIn: 0.001, // Expires very soon
|
|
204
241
|
}),
|
|
205
|
-
})
|
|
242
|
+
} as any)
|
|
206
243
|
.mockResolvedValueOnce({
|
|
207
244
|
ok: true,
|
|
208
245
|
json: async () => ({ token: "jwt-token-new" }),
|
|
209
|
-
});
|
|
246
|
+
} as any);
|
|
210
247
|
|
|
211
|
-
const manager = new
|
|
248
|
+
const manager = new UploadistaCloudAuthManager(config, mockHttpClient);
|
|
212
249
|
|
|
213
250
|
// First call - fetch initial token
|
|
214
251
|
await manager.attachToken();
|
|
@@ -219,66 +256,66 @@ describe("SaasAuthManager", () => {
|
|
|
219
256
|
// Second call - should fetch new token because old one expired
|
|
220
257
|
const result = await manager.attachToken();
|
|
221
258
|
expect(result.Authorization).toBe("Bearer jwt-token-new");
|
|
222
|
-
expect(
|
|
259
|
+
expect(mockHttpClient.request).toHaveBeenCalledTimes(2);
|
|
223
260
|
});
|
|
224
261
|
});
|
|
225
262
|
|
|
226
263
|
describe("clearToken", () => {
|
|
227
264
|
it("should clear cached token for specific job", async () => {
|
|
228
|
-
const config:
|
|
229
|
-
mode: "
|
|
265
|
+
const config: UploadistaCloudAuthConfig = {
|
|
266
|
+
mode: "uploadista-cloud",
|
|
230
267
|
authServerUrl: "https://auth.example.com/token",
|
|
231
|
-
|
|
268
|
+
clientId: "client-id-123",
|
|
232
269
|
};
|
|
233
270
|
|
|
234
|
-
vi.mocked(
|
|
271
|
+
vi.mocked(mockHttpClient.request)
|
|
235
272
|
.mockResolvedValueOnce({
|
|
236
273
|
ok: true,
|
|
237
274
|
json: async () => ({ token: "jwt-token-1" }),
|
|
238
|
-
})
|
|
275
|
+
} as any)
|
|
239
276
|
.mockResolvedValueOnce({
|
|
240
277
|
ok: true,
|
|
241
278
|
json: async () => ({ token: "jwt-token-2" }),
|
|
242
|
-
});
|
|
279
|
+
} as any);
|
|
243
280
|
|
|
244
|
-
const manager = new
|
|
281
|
+
const manager = new UploadistaCloudAuthManager(config, mockHttpClient);
|
|
245
282
|
|
|
246
283
|
// Cache token for job
|
|
247
284
|
await manager.attachToken({}, "job-1");
|
|
248
|
-
expect(
|
|
285
|
+
expect(mockHttpClient.request).toHaveBeenCalledTimes(1);
|
|
249
286
|
|
|
250
287
|
// Clear token
|
|
251
288
|
manager.clearToken("job-1");
|
|
252
289
|
|
|
253
290
|
// Next call should fetch new token
|
|
254
291
|
await manager.attachToken({}, "job-1");
|
|
255
|
-
expect(
|
|
292
|
+
expect(mockHttpClient.request).toHaveBeenCalledTimes(2);
|
|
256
293
|
});
|
|
257
294
|
});
|
|
258
295
|
|
|
259
296
|
describe("clearAllTokens", () => {
|
|
260
297
|
it("should clear all cached tokens", async () => {
|
|
261
|
-
const config:
|
|
262
|
-
mode: "
|
|
298
|
+
const config: UploadistaCloudAuthConfig = {
|
|
299
|
+
mode: "uploadista-cloud",
|
|
263
300
|
authServerUrl: "https://auth.example.com/token",
|
|
264
|
-
|
|
301
|
+
clientId: "client-id-123",
|
|
265
302
|
};
|
|
266
303
|
|
|
267
|
-
vi.mocked(
|
|
304
|
+
vi.mocked(mockHttpClient.request)
|
|
268
305
|
.mockResolvedValueOnce({
|
|
269
306
|
ok: true,
|
|
270
307
|
json: async () => ({ token: "jwt-token-1" }),
|
|
271
|
-
})
|
|
308
|
+
} as any)
|
|
272
309
|
.mockResolvedValueOnce({
|
|
273
310
|
ok: true,
|
|
274
311
|
json: async () => ({ token: "jwt-token-2" }),
|
|
275
|
-
})
|
|
312
|
+
} as any)
|
|
276
313
|
.mockResolvedValueOnce({
|
|
277
314
|
ok: true,
|
|
278
315
|
json: async () => ({ token: "jwt-token-3" }),
|
|
279
|
-
});
|
|
316
|
+
} as any);
|
|
280
317
|
|
|
281
|
-
const manager = new
|
|
318
|
+
const manager = new UploadistaCloudAuthManager(config, mockHttpClient);
|
|
282
319
|
|
|
283
320
|
// Cache tokens for multiple jobs
|
|
284
321
|
await manager.attachToken({}, "job-1");
|
|
@@ -289,33 +326,33 @@ describe("SaasAuthManager", () => {
|
|
|
289
326
|
|
|
290
327
|
// Next calls should fetch new tokens
|
|
291
328
|
await manager.attachToken();
|
|
292
|
-
expect(
|
|
329
|
+
expect(mockHttpClient.request).toHaveBeenCalledTimes(3);
|
|
293
330
|
});
|
|
294
331
|
});
|
|
295
332
|
|
|
296
333
|
describe("getCacheStats", () => {
|
|
297
334
|
it("should return cache statistics", async () => {
|
|
298
|
-
const config:
|
|
299
|
-
mode: "
|
|
335
|
+
const config: UploadistaCloudAuthConfig = {
|
|
336
|
+
mode: "uploadista-cloud",
|
|
300
337
|
authServerUrl: "https://auth.example.com/token",
|
|
301
|
-
|
|
338
|
+
clientId: "client-id-123",
|
|
302
339
|
};
|
|
303
340
|
|
|
304
|
-
vi.mocked(
|
|
341
|
+
vi.mocked(mockHttpClient.request)
|
|
305
342
|
.mockResolvedValueOnce({
|
|
306
343
|
ok: true,
|
|
307
344
|
json: async () => ({ token: "jwt-token-1" }),
|
|
308
|
-
})
|
|
345
|
+
} as any)
|
|
309
346
|
.mockResolvedValueOnce({
|
|
310
347
|
ok: true,
|
|
311
348
|
json: async () => ({ token: "jwt-token-2" }),
|
|
312
|
-
})
|
|
349
|
+
} as any)
|
|
313
350
|
.mockResolvedValueOnce({
|
|
314
351
|
ok: true,
|
|
315
352
|
json: async () => ({ token: "jwt-token-3" }),
|
|
316
|
-
});
|
|
353
|
+
} as any);
|
|
317
354
|
|
|
318
|
-
const manager = new
|
|
355
|
+
const manager = new UploadistaCloudAuthManager(config, mockHttpClient);
|
|
319
356
|
|
|
320
357
|
// Initially empty
|
|
321
358
|
expect(manager.getCacheStats()).toEqual({
|