-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathwebdav-operations.test.ts
More file actions
374 lines (330 loc) · 11.3 KB
/
webdav-operations.test.ts
File metadata and controls
374 lines (330 loc) · 11.3 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
* Copyright (c) 2025, Salesforce, Inc.
* SPDX-License-Identifier: Apache-2
* For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
*/
import {expect} from 'chai';
import {execa} from 'execa';
import * as fs from 'node:fs/promises';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import {getSharedContext, hasSharedSandbox} from './shared-context.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
/**
* E2E Tests for WebDAV File Operations
*
* This test suite covers WebDAV operations:
* 1. Upload file (put)
* 2. List files (ls)
* 3. Download file (get)
* 4. Delete file (rm)
* 5. Create directory (mkdir)
* 6. List directory contents
* 7. Delete directory
* 8. Zip directory
* 9. Verify zip exists
* 10. Unzip archive
* 11. Verify extracted files
*/
describe('WebDAV Operations E2E Tests', function () {
this.timeout(600_000); // 10 minutes
this.retries(2);
const CLI_BIN = path.resolve(__dirname, '../../../bin/run.js');
const TEST_FIXTURES_DIR = path.resolve(__dirname, '../fixtures');
const TEST_OUTPUT_DIR = path.resolve(__dirname, '../test-output');
let serverHostname: string;
let ownSandboxId: null | string = null;
const testFileName = `e2e-test-${Date.now()}.txt`;
const testDirName = `e2e-test-dir-${Date.now()}`;
const remoteBasePath = 'src/instance';
const remoteFilePath = `${remoteBasePath}/${testFileName}`;
const remoteDirPath = `${remoteBasePath}/${testDirName}`;
const remoteZipPath = `${remoteDirPath}.zip`;
function entryName(entry: any): string {
if (entry?.displayName) return String(entry.displayName);
if (entry?.href) {
const parts = String(entry.href).split('/').filter(Boolean);
return decodeURIComponent(parts.at(-1) ?? '');
}
return '';
}
// WebDAV is eventually consistent — poll instead of asserting immediately
async function waitFor(fn: () => Promise<boolean>, timeoutMs = 60_000, intervalMs = 2000) {
const start = Date.now();
while (Date.now() - start < timeoutMs) {
// eslint-disable-next-line no-await-in-loop
if (await fn()) return;
// eslint-disable-next-line no-await-in-loop
await new Promise<void>((resolve) => {
setTimeout(resolve, intervalMs);
});
}
throw new Error('Timed out waiting for WebDAV visibility');
}
before(async function () {
// Check required environment variables
if (!process.env.SFCC_CLIENT_ID || !process.env.SFCC_CLIENT_SECRET) {
this.skip();
}
// Use shared sandbox if available
if (hasSharedSandbox()) {
const shared = getSharedContext();
serverHostname = shared.hostname!;
console.log(`✓ Using shared sandbox hostname: ${serverHostname}`);
} else if (process.env.TEST_INSTANCE_HOSTNAME) {
// Fallback to env var
serverHostname = process.env.TEST_INSTANCE_HOSTNAME;
console.log(`Using hostname from TEST_INSTANCE_HOSTNAME: ${serverHostname}`);
} else {
// Fallback: Create own sandbox
console.log('No shared sandbox available, creating dedicated sandbox for WebDAV tests...');
this.timeout(720_000); // 12 minutes for sandbox creation
if (!process.env.TEST_REALM) {
throw new Error('TEST_REALM required to create sandbox');
}
const result = await runCLI([
'ods',
'create',
'--realm',
process.env.TEST_REALM,
'--ttl',
'4',
'--wait',
'--set-permissions',
'--json',
]);
expect(result.exitCode).to.equal(0, `Failed to create sandbox: ${result.stderr}`);
const sandbox = JSON.parse(result.stdout);
ownSandboxId = sandbox.id;
serverHostname = sandbox.hostName;
console.log(`Created dedicated sandbox ${ownSandboxId} at ${serverHostname}`);
}
// Create test output directory
await fs.mkdir(TEST_OUTPUT_DIR, {recursive: true});
// Create test file
await fs.writeFile(path.join(TEST_FIXTURES_DIR, testFileName), `E2E Test Content - ${Date.now()}`);
});
async function runCLI(args: string[]) {
return execa('node', [CLI_BIN, ...args], {
env: {
...process.env,
SFCC_LOG_LEVEL: 'silent',
},
reject: false,
});
}
after(async function () {
this.timeout(180_000); // 3 minutes for cleanup
// Cleanup: Delete test files from WebDAV in parallel
if (serverHostname) {
await Promise.all([
runCLI(['webdav', 'rm', remoteFilePath, '--server', serverHostname, '--force', '--root', 'impex']),
runCLI(['webdav', 'rm', remoteDirPath, '--server', serverHostname, '--force', '--root', 'impex']),
runCLI(['webdav', 'rm', remoteZipPath, '--server', serverHostname, '--force', '--root', 'impex']),
]);
}
// Cleanup: Delete local test files
await fs.unlink(path.join(TEST_FIXTURES_DIR, testFileName)).catch(() => {});
await fs.rm(TEST_OUTPUT_DIR, {recursive: true, force: true});
// Delete own sandbox if we created one
if (ownSandboxId) {
console.log(`Cleaning up dedicated sandbox ${ownSandboxId}...`);
await runCLI(['ods', 'delete', ownSandboxId, '--force']);
console.log('Dedicated sandbox deleted');
}
});
describe('Step 1: Upload File', function () {
it('should upload file to WebDAV', async function () {
const localFile = path.join(TEST_FIXTURES_DIR, testFileName);
const result = await runCLI([
'webdav',
'put',
localFile,
remoteFilePath,
'--server',
serverHostname,
'--root',
'impex',
]);
if (result.exitCode !== 0) {
const msg = result.stderr || result.stdout;
if (/not\s+allowed|unauthorized|forbidden|401|403/i.test(msg)) {
this.skip();
}
expect(result.exitCode).to.equal(0, msg);
}
});
});
describe('Step 2: List Files', function () {
it('should list files in WebDAV directory', async function () {
await waitFor(async () => {
const result = await runCLI([
'webdav',
'ls',
remoteBasePath,
'--server',
serverHostname,
'--root',
'impex',
'--json',
]);
if (result.exitCode !== 0) return false;
const response = JSON.parse(result.stdout);
return response.entries?.some((e: any) => entryName(e) === testFileName);
});
});
});
describe('Step 3: Download File', function () {
it('should download file from WebDAV', async function () {
const localOutput = path.join(TEST_OUTPUT_DIR, testFileName);
const result = await runCLI([
'webdav',
'get',
remoteFilePath,
'--output',
localOutput,
'--server',
serverHostname,
'--root',
'impex',
]);
expect(result.exitCode).to.equal(0);
await fs.access(localOutput);
});
});
describe('Step 4: Delete File', function () {
it('should delete file from WebDAV', async function () {
await runCLI(['webdav', 'rm', remoteFilePath, '--server', serverHostname, '--force', '--root', 'impex']);
await waitFor(async () => {
const result = await runCLI([
'webdav',
'ls',
remoteBasePath,
'--server',
serverHostname,
'--root',
'impex',
'--json',
]);
if (result.exitCode !== 0) return false;
const response = JSON.parse(result.stdout);
return !response.entries?.some((e: any) => entryName(e) === testFileName);
});
});
});
describe('Step 5: Create Directory', function () {
it('should create directory on WebDAV', async function () {
const result = await runCLI(['webdav', 'mkdir', remoteDirPath, '--server', serverHostname, '--root', 'impex']);
expect(result.exitCode).to.equal(0);
});
});
describe('Step 6: List Directory Contents', function () {
it('should list directory contents recursively', async function () {
const localFile = path.join(TEST_FIXTURES_DIR, testFileName);
await runCLI([
'webdav',
'put',
localFile,
`${remoteDirPath}/${testFileName}`,
'--server',
serverHostname,
'--root',
'impex',
]);
const result = await runCLI([
'webdav',
'ls',
remoteDirPath,
'--server',
serverHostname,
'--root',
'impex',
'--json',
]);
expect(result.exitCode).to.equal(0);
});
});
describe('Step 7: Delete Directory', function () {
it('should delete directory from WebDAV', async function () {
await runCLI(['webdav', 'rm', remoteDirPath, '--server', serverHostname, '--force', '--root', 'impex']);
await waitFor(async () => {
const result = await runCLI([
'webdav',
'ls',
remoteBasePath,
'--server',
serverHostname,
'--root',
'impex',
'--json',
]);
if (result.exitCode !== 0) return false;
const response = JSON.parse(result.stdout);
return !response.entries?.some((e: any) => entryName(e) === testDirName);
});
});
});
describe('Step 8: Zip Directory', function () {
it('should create a zip archive of directory', async function () {
await runCLI(['webdav', 'mkdir', remoteDirPath, '--server', serverHostname, '--root', 'impex']);
const localFile = path.join(TEST_FIXTURES_DIR, testFileName);
await runCLI([
'webdav',
'put',
localFile,
`${remoteDirPath}/${testFileName}`,
'--server',
serverHostname,
'--root',
'impex',
]);
const result = await runCLI(['webdav', 'zip', remoteDirPath, '--server', serverHostname, '--root', 'impex']);
expect(result.exitCode).to.equal(0);
});
});
describe('Step 9: Verify Zip Exists', function () {
it('should find zip file in directory listing', async function () {
await waitFor(async () => {
const result = await runCLI([
'webdav',
'ls',
remoteBasePath,
'--server',
serverHostname,
'--root',
'impex',
'--json',
]);
if (result.exitCode !== 0) return false;
const response = JSON.parse(result.stdout);
return response.entries?.some((e: any) => entryName(e) === `${testDirName}.zip`);
});
});
});
describe('Step 10: Unzip Archive', function () {
it('should extract zip archive', async function () {
const result = await runCLI(['webdav', 'unzip', remoteZipPath, '--server', serverHostname, '--root', 'impex']);
expect(result.exitCode).to.equal(0);
});
});
describe('Step 11: Verify Extracted Files', function () {
it('should find extracted files in directory', async function () {
await waitFor(async () => {
const result = await runCLI([
'webdav',
'ls',
remoteDirPath,
'--server',
serverHostname,
'--root',
'impex',
'--json',
]);
if (result.exitCode !== 0) return false;
const response = JSON.parse(result.stdout);
return response.entries?.some((e: any) => entryName(e) === testFileName);
}, 300_000);
});
});
});