Skip to content

Commit

Permalink
chore: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maxakuru committed Jul 15, 2024
1 parent cf3730b commit 6f4afe4
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 3 deletions.
7 changes: 4 additions & 3 deletions packages/helix-shared-storage/src/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,15 +303,16 @@ class Bucket {
* @returns result obtained from S3
*/
async copy(src, dst, opts = {}) {
const key = sanitizeKey(src);
const input = {
Bucket: this.bucket,
CopySource: `${this.bucket}/${sanitizeKey(src)}`,
CopySource: `${this.bucket}/${key}`,
Key: sanitizeKey(dst),
};

try {
if (opts.addMetadata) {
const meta = await this.metadata(src);
const meta = await this.metadata(key);
input.Metadata = { ...meta, ...opts.addMetadata };
input.MetadataDirective = 'REPLACE';
}
Expand Down Expand Up @@ -464,7 +465,7 @@ class Bucket {
};
try {
if (opts.addMetadata) {
const meta = await this.metadata(src);
const meta = await this.metadata(task.src);
input.Metadata = { ...meta, ...opts.addMetadata };
input.MetadataDirective = 'REPLACE';
}
Expand Down
159 changes: 159 additions & 0 deletions packages/helix-shared-storage/test/storage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,111 @@ describe('Storage test', () => {
assert.deepEqual(puts.r2, expectedPuts);
});

it('can copy objects and add metadata', async () => {
const listReply = JSON.parse(await fs.readFile(path.resolve(__testdir, 'fixtures', 'list-reply-copy.json'), 'utf-8'));
const puts = { s3: [], r2: [] };
const putsHeaders = { s3: [], r2: [] };
const heads = [];
nock('https://helix-code-bus.s3.fake.amazonaws.com')
.get('/?list-type=2&prefix=owner%2Frepo%2Fref%2F')
.reply(200, listReply[0])
.get('/?continuation-token=1%2Fs4dr7BSKNScrN4njX9%2BCpBNimYkuEzMWg3niTSAPMdculBmycyUPM6kv0xi46j4hdc1lFPkE%2FICI8TxG%2BVNV9Hh91Ou0hqeBYzqTRzSBSs%3D&list-type=2&prefix=owner%2Frepo%2Fref%2F')
.reply(200, listReply[1])
.head(/.*/)
.times(10)
.reply((uri) => {
heads.push(uri);
// reject first uri
if (puts.s3.length === 1) {
return [404];
}
return [200, undefined, {
'x-amz-meta-x-dont-overwrite': 'foo',
'x-amz-meta-x-last-modified-by': 'anonymous',
}];
})
.put(/.*/)
.times(10)
.reply(function f(uri) {
puts.s3.push(uri);
putsHeaders.s3.push({
'x-amz-metadata-directive': this.req.headers['x-amz-metadata-directive'],
'x-amz-meta-x-dont-overwrite': this.req.headers['x-amz-meta-x-dont-overwrite'],
'x-amz-meta-x-last-modified-by': this.req.headers['x-amz-meta-x-last-modified-by'],
});
// reject first uri
if (puts.s3.length === 1) {
return [404];
}
return [200, '<?xml version="1.0" encoding="UTF-8"?>\n<CopyObjectResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><LastModified>2021-05-05T08:37:23.000Z</LastModified><ETag>&quot;f278c0035a9b4398629613a33abe6451&quot;</ETag></CopyObjectResult>'];
});
nock(`https://helix-code-bus.${CLOUDFLARE_ACCOUNT_ID}.r2.cloudflarestorage.com`)
.put(/.*/)
.times(10)
.reply(function f(uri) {
puts.r2.push(uri);
putsHeaders.r2.push({
'x-amz-metadata-directive': this.req.headers['x-amz-metadata-directive'],
'x-amz-meta-x-dont-overwrite': this.req.headers['x-amz-meta-x-dont-overwrite'],
'x-amz-meta-x-last-modified-by': this.req.headers['x-amz-meta-x-last-modified-by'],
});
// reject first uri
if (puts.s3.length === 1) {
return [404];
}
return [200, '<?xml version="1.0" encoding="UTF-8"?>\n<CopyObjectResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><LastModified>2021-05-05T08:37:23.000Z</LastModified><ETag>&quot;f278c0035a9b4398629613a33abe6451&quot;</ETag></CopyObjectResult>'];
});

const bus = storage.codeBus();
await bus.copyDeep('/owner/repo/ref/', '/bar/', undefined, { addMetadata: { 'x-last-modified-by': '[email protected]' } });

puts.s3.sort();
puts.r2.sort();
heads.sort();

assert.strictEqual(putsHeaders.s3.length, 10);
assert.strictEqual(putsHeaders.r2.length, 10);

Object.values(putsHeaders).forEach((s3r2) => {
s3r2.forEach((headers) => {
assert.deepEqual(headers, {
'x-amz-meta-x-dont-overwrite': 'foo',
'x-amz-meta-x-last-modified-by': '[email protected]',
'x-amz-metadata-directive': 'REPLACE',
});
});
});

const expectedHeads = [
'/owner/repo/ref/.circleci/config.yml',
'/owner/repo/ref/.gitignore',
'/owner/repo/ref/.vscode/launch.json',
'/owner/repo/ref/.vscode/settings.json',
'/owner/repo/ref/README.md',
'/owner/repo/ref/helix_logo.png',
'/owner/repo/ref/htdocs/favicon.ico',
'/owner/repo/ref/htdocs/style.css',
'/owner/repo/ref/index.md',
'/owner/repo/ref/src/html.pre.js',
];
assert.deepEqual(heads, expectedHeads);

const expectedPuts = [
'/bar/.circleci/config.yml?x-id=CopyObject',
'/bar/.gitignore?x-id=CopyObject',
'/bar/.vscode/launch.json?x-id=CopyObject',
'/bar/.vscode/settings.json?x-id=CopyObject',
'/bar/README.md?x-id=CopyObject',
'/bar/helix_logo.png?x-id=CopyObject',
'/bar/htdocs/favicon.ico?x-id=CopyObject',
'/bar/htdocs/style.css?x-id=CopyObject',
'/bar/index.md?x-id=CopyObject',
'/bar/src/html.pre.js?x-id=CopyObject',
];
assert.deepEqual(puts.s3, expectedPuts);
assert.deepEqual(puts.r2, expectedPuts);
});

it('can copy object (non deep)', async () => {
const puts = { s3: [], r2: [] };
nock('https://helix-code-bus.s3.fake.amazonaws.com')
Expand Down Expand Up @@ -503,6 +608,60 @@ describe('Storage test', () => {
assert.deepEqual(puts.r2, expectedPuts);
});

it('can copy object, and add metadata (non deep)', async () => {
const puts = { s3: [], r2: [] };
const putsHeaders = { s3: undefined, r2: undefined };
nock('https://helix-code-bus.s3.fake.amazonaws.com')
.head('/owner/repo/ref/foo.md')
.reply(200, undefined, {
'x-amz-meta-x-dont-overwrite': 'foo',
'x-amz-meta-x-last-modified-by': 'anonymous',
})
.put('/owner/repo/ref/foo/bar.md?x-id=CopyObject')
.reply(function f(uri) {
putsHeaders.s3 = {
'x-amz-metadata-directive': this.req.headers['x-amz-metadata-directive'],
'x-amz-meta-x-dont-overwrite': this.req.headers['x-amz-meta-x-dont-overwrite'],
'x-amz-meta-x-last-modified-by': this.req.headers['x-amz-meta-x-last-modified-by'],
};
puts.s3.push(uri);
return [200, '<?xml version="1.0" encoding="UTF-8"?>\n<CopyObjectResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><LastModified>2021-05-05T08:37:23.000Z</LastModified><ETag>&quot;f278c0035a9b4398629613a33abe6451&quot;</ETag></CopyObjectResult>'];
});
nock(`https://helix-code-bus.${CLOUDFLARE_ACCOUNT_ID}.r2.cloudflarestorage.com`)
.put('/owner/repo/ref/foo/bar.md?x-id=CopyObject')
.reply(function f(uri) {
putsHeaders.r2 = {
'x-amz-metadata-directive': this.req.headers['x-amz-metadata-directive'],
'x-amz-meta-x-dont-overwrite': this.req.headers['x-amz-meta-x-dont-overwrite'],
'x-amz-meta-x-last-modified-by': this.req.headers['x-amz-meta-x-last-modified-by'],
};
puts.r2.push(uri);
return [200, '<?xml version="1.0" encoding="UTF-8"?>\n<CopyObjectResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><LastModified>2021-05-05T08:37:23.000Z</LastModified><ETag>&quot;f278c0035a9b4398629613a33abe6451&quot;</ETag></CopyObjectResult>'];
});

const bus = storage.codeBus();
await bus.copy('/owner/repo/ref/foo.md', '/owner/repo/ref/foo/bar.md', { addMetadata: { 'x-last-modified-by': '[email protected]' } });

puts.s3.sort();
puts.r2.sort();
const expectedPuts = [
'/owner/repo/ref/foo/bar.md?x-id=CopyObject',
];
assert.deepEqual(puts.s3, expectedPuts);
assert.deepEqual(puts.r2, expectedPuts);

assert.deepEqual(putsHeaders.s3, {
'x-amz-metadata-directive': 'REPLACE',
'x-amz-meta-x-dont-overwrite': 'foo',
'x-amz-meta-x-last-modified-by': '[email protected]',
});
assert.deepEqual(putsHeaders.r2, {
'x-amz-metadata-directive': 'REPLACE',
'x-amz-meta-x-dont-overwrite': 'foo',
'x-amz-meta-x-last-modified-by': '[email protected]',
});
});

it('can copy object can fail (non deep)', async () => {
nock('https://helix-code-bus.s3.fake.amazonaws.com')
.put('/owner/repo/ref/foo/bar.md?x-id=CopyObject')
Expand Down

0 comments on commit 6f4afe4

Please sign in to comment.