Repository navigation
Expand file tree
/
Copy pathsmoke.test.js
More file actions
312 lines (272 loc) · 9.44 KB
/
Copy pathsmoke.test.js
File metadata and controls
312 lines (272 loc) · 9.44 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
* Copyright 2025 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
/* eslint-disable prefer-arrow-callback, func-names */
import { spawn } from 'child_process';
import path from 'path';
import fs from 'fs';
import kill from 'tree-kill';
import config from 'dotenv';
import { generateKeyPair, exportJWK, SignJWT } from 'jose';
import { createServer } from 'http';
import S3rver from 's3rver';
import itTests from './it-tests.js';
config.config();
const S3_PORT = 4569;
const SERVER_PORT = 8788;
const LOCAL_SERVER_URL = `http://localhost:${SERVER_PORT}`;
const IMS_LOCAL_PORT = 9999;
const IMS_LOCAL_KID = 'ims';
const IMS_STAGE = {
ENDPOINT: process.env.IT_IMS_STAGE_ENDPOINT,
CLIENT_ID_SUPER_USER: process.env.IT_IMS_STAGE_CLIENT_ID_SUPER_USER,
CLIENT_SECRET_SUPER_USER: process.env.IT_IMS_STAGE_CLIENT_SECRET_SUPER_USER,
CLIENT_ID_LIMITED_USER: process.env.IT_IMS_STAGE_CLIENT_ID_LIMITED_USER,
CLIENT_SECRET_LIMITED_USER: process.env.IT_IMS_STAGE_CLIENT_SECRET_LIMITED_USER,
SCOPES: process.env.IT_IMS_STAGE_SCOPES,
};
const S3_DIR = './test/it/bucket';
const IT_ORG = 'da-admin-ci-it-org';
const IT_DEFAULT_REPO = 'test-repo';
describe('Integration Tests: smoke tests', function () {
let s3rver;
let devServer;
let imsServer;
let publicKeyJwk;
const context = {
serverUrl: LOCAL_SERVER_URL,
org: IT_ORG,
repo: IT_DEFAULT_REPO,
accessToken: '',
};
const cleanupWranglerState = () => {
const wranglerState = path.join(process.cwd(), '.wrangler/state');
if (fs.existsSync(wranglerState)) {
fs.rmSync(wranglerState, { recursive: true });
}
};
const getIMSProfile = async (accessToken) => {
const res = await fetch(`${IMS_STAGE.ENDPOINT}/ims/profile/v1`, {
headers: { Authorization: `Bearer ${accessToken}` },
});
if (res.ok) {
const json = await res.json();
return json;
}
throw new Error(`Failed to fetch IMS profile: ${res.status}`);
};
const connectToIMS = async (clientId, clientSecret) => {
const postData = {
grant_type: 'client_credentials',
client_id: clientId,
client_secret: clientSecret,
scope: IMS_STAGE.SCOPES,
};
const form = new FormData();
Object.entries(postData).forEach(([k, v]) => {
form.append(k, v);
});
let res;
try {
res = await fetch(`${IMS_STAGE.ENDPOINT}/ims/token/v3`, {
method: 'POST',
body: form,
});
} catch (e) {
throw new Error(`cannot send request to IMS: ${e.message}`);
}
if (res.ok) {
const json = await res.json();
const profile = await getIMSProfile(json.access_token);
return {
accessToken: json.access_token,
email: profile.email,
userId: profile.userId,
};
}
throw new Error(`error response from IMS with status: ${res.status} and body: ${await res.text()}`);
};
/* eslint-disable max-len */
const connectAsSuperUser = async () => connectToIMS(IMS_STAGE.CLIENT_ID_SUPER_USER, IMS_STAGE.CLIENT_SECRET_SUPER_USER);
/* eslint-disable max-len */
const connectAsLimitedUser = async () => connectToIMS(IMS_STAGE.CLIENT_ID_LIMITED_USER, IMS_STAGE.CLIENT_SECRET_LIMITED_USER);
const localTokenCache = {};
let IMSPrivateKey;
const setupIMSLocalKey = async () => {
const { privateKey, publicKey } = await generateKeyPair('RS256');
IMSPrivateKey = privateKey;
publicKeyJwk = await exportJWK(publicKey);
publicKeyJwk.use = 'sig';
publicKeyJwk.kid = IMS_LOCAL_KID;
publicKeyJwk.alg = 'RS256';
};
const getIMSLocalToken = async (userId) => {
const email = `${userId}@example.com`;
const accessToken = await new SignJWT({
type: 'access_token',
user_id: email,
created_at: String(Date.now() - 1000),
expires_in: '86400000',
})
.setProtectedHeader({ alg: 'RS256', kid: IMS_LOCAL_KID })
.sign(IMSPrivateKey);
localTokenCache[accessToken] = {
accessToken,
email,
userId: email,
};
return localTokenCache[accessToken];
};
const setupIMSServer = async () => {
// Start mock IMS server
imsServer = createServer((req, res) => {
res.setHeader('Content-Type', 'application/json');
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Headers', 'Authorization, Content-Type');
// Log requests for debugging
console.log(`[IMS Mock] ${req.method} ${req.url}`);
if (req.method === 'OPTIONS') {
res.writeHead(204);
res.end();
} else if (req.url === '/ims/keys') {
res.writeHead(200);
res.end(JSON.stringify({ keys: [publicKeyJwk] }));
} else if (req.url === '/ims/profile/v1') {
const cachedToken = localTokenCache[req.headers.authorization.split(' ').pop()];
if (!cachedToken) {
res.writeHead(401);
res.end(JSON.stringify({ error: 'Unauthorized' }));
return;
}
res.writeHead(200);
res.end(JSON.stringify({
email: cachedToken.email,
userId: cachedToken.userId,
}));
} else if (req.url === '/ims/organizations/v5') {
res.writeHead(200);
res.end(JSON.stringify([]));
} else {
console.log(`[IMS Mock] 404 Not Found: ${req.url}`);
res.writeHead(404);
res.end(JSON.stringify({ error: 'Not found' }));
}
});
await new Promise((resolve) => {
imsServer.listen(IMS_LOCAL_PORT, '127.0.0.1', resolve);
});
};
const setupS3rver = async () => {
s3rver = new S3rver({
port: S3_PORT,
address: '127.0.0.1',
directory: path.resolve(S3_DIR),
silent: true,
});
await s3rver.run();
};
const setupDevServer = async () => {
devServer = spawn('npx', [
'wrangler', 'dev',
'--port', SERVER_PORT.toString(),
'--env', 'it',
// '--log-level', 'debug',
], {
stdio: 'pipe', // Capture output for debugging
detached: false, // Keep in same process group for easier cleanup
});
// Wait for server to be ready
await new Promise((resolve, reject) => {
let started = false;
devServer.stdout.on('data', (data) => {
const str = data.toString();
// Always log wrangler output including errors
// console.log('[Wrangler]', str.trim());
if (str.includes('Ready on http://localhost') && !started) {
started = true;
resolve();
}
});
devServer.stderr.on('data', (data) => {
console.error('[Wrangler Err]', data.toString());
});
devServer.on('error', reject);
});
};
before(async function () {
// Increase timeout for server startup
this.timeout(30000);
if (process.env.WORKER_PREVIEW_URL) {
if (!IMS_STAGE.ENDPOINT
|| !IMS_STAGE.CLIENT_ID_SUPER_USER
|| !IMS_STAGE.CLIENT_SECRET_SUPER_USER
|| !IMS_STAGE.CLIENT_ID_LIMITED_USER
|| !IMS_STAGE.CLIENT_SECRET_LIMITED_USER
|| !IMS_STAGE.SCOPES) {
throw new Error('IT_IMS_STAGE_ENDPOINT, IT_IMS_STAGE_CLIENT_ID_SUPER_USER, IT_IMS_STAGE_CLIENT_SECRET_SUPER_USER, IT_IMS_STAGE_CLIENT_ID_LIMITED_USER, IT_IMS_STAGE_CLIENT_SECRET_LIMITED_USER, and IT_IMS_STAGE_SCOPES must be set');
}
context.local = false;
context.serverUrl = process.env.WORKER_PREVIEW_URL;
const branch = process.env.WORKER_PREVIEW_BRANCH;
if (!branch) {
throw new Error('WORKER_PREVIEW_BRANCH must be set');
}
context.repo += `-${branch.toLowerCase().replace(/[ /_]/g, '-')}`;
context.superUser = await connectAsSuperUser();
context.limitedUser = await connectAsLimitedUser();
} else {
context.local = true;
await setupIMSLocalKey();
context.superUser = await getIMSLocalToken('super-user-id');
context.limitedUser = await getIMSLocalToken('limited-user-id');
cleanupWranglerState();
await setupIMSServer();
await setupS3rver();
await setupDevServer();
}
console.log('Running tests with context:', context);
});
after(async function () {
if (process.env.WORKER_PREVIEW_URL) {
return;
}
this.timeout(10000);
// Cleanup - forcefully kill processes
if (devServer && devServer.pid) {
// Remove all listeners to prevent hanging
devServer.stdout?.removeAllListeners();
devServer.stderr?.removeAllListeners();
devServer.removeAllListeners();
// Kill entire process tree (wrangler spawns child processes)
await new Promise((resolve) => {
kill(devServer.pid, 'SIGTERM', (err) => {
if (err) {
// If SIGTERM fails, force kill
kill(devServer.pid, 'SIGKILL', () => resolve());
} else {
resolve();
}
});
// Fallback timeout
setTimeout(resolve, 3000);
});
}
if (s3rver) {
await s3rver.close();
}
if (imsServer) {
await new Promise((resolve) => {
imsServer.close(resolve);
});
}
});
itTests(context);
});