Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/storage/utils/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ export async function invalidateCollab(api, url, env) {
return;
}

const invPath = `/api/v1/${api}?doc=${url}`;
const { DA_COLLAB } = env;

// Use dacollab service binding, hostname is not relevant
const invURL = `https://localhost${invPath}`;
await env.dacollab.fetch(invURL);
const invURL = `${DA_COLLAB}/api/v1/${api}?doc=${url}`;

await fetch(invURL);
}
35 changes: 20 additions & 15 deletions test/routes/source.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,9 @@ import esmock from 'esmock';
import { getAclCtx } from '../../src/utils/auth.js';

describe('Source Route', () => {
it('Test invalidate using service binding', async () => {
const sb_callbacks = [];
const dacollab = {
fetch: async (url) => sb_callbacks.push(url)
};
it('Test invalidate using direct fetch', async () => {
const fetch_callbacks = [];
const env = {
dacollab,
DA_COLLAB: 'http://localhost:4444'
};

Expand All @@ -39,17 +35,26 @@ describe('Source Route', () => {
}
});

const headers = new Map();
headers.set('x-da-initiator', 'blah');
const savedFetch = globalThis.fetch;
try {
globalThis.fetch = async (url) => {
fetch_callbacks.push(url);
};

const req = {
headers,
url: 'http://localhost:9876/source/somedoc.html'
};
const headers = new Map();
headers.set('x-da-initiator', 'blah');

const resp = await postSource({ req, env, daCtx });
assert.equal(200, resp.status);
assert.deepStrictEqual(['https://localhost/api/v1/syncadmin?doc=http://localhost:9876/source/somedoc.html'], sb_callbacks);
const req = {
headers,
url: 'http://localhost:9876/source/somedoc.html'
};

const resp = await postSource({ req, env, daCtx });
assert.equal(200, resp.status);
assert.deepStrictEqual(['http://localhost:4444/api/v1/syncadmin?doc=http://localhost:9876/source/somedoc.html'], fetch_callbacks);
} finally {
globalThis.fetch = savedFetch;
}
});

it('Test postSource from collab does not trigger invalidate callback', async () => {
Expand Down
163 changes: 94 additions & 69 deletions test/storage/object/copy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,7 @@ describe('Object copy', () => {
}));

const collabcalls = [];
const dacollab = {
fetch: (url) => {
collabcalls.push(url);
}
}
const env = { dacollab };
const env = { DA_COLLAB: 'https://localhost' };
const ctx = {
bucket: 'root-bucket',
env,
Expand All @@ -160,28 +155,38 @@ describe('Object copy', () => {
source: 'mydir',
destination: 'mydir/newdir',
};
await copyObject(env, ctx, details, false);

assert.strictEqual(s3Sent.length, 3);
const savedFetch = globalThis.fetch;
try {
globalThis.fetch = async (url) => {
collabcalls.push(url);
};

await copyObject(env, ctx, details, false);

// Make the order in s3Sent predictable
s3Sent.sort((a, b) => a.Key.localeCompare(b.Key));
assert.strictEqual(s3Sent.length, 3);

const input = s3Sent[2];
assert.strictEqual(input.Bucket, 'root-bucket');
assert.strictEqual(input.CopySource, 'root-bucket/foo/mydir/xyz.html');
assert.strictEqual(input.Key, 'foo/mydir/newdir/xyz.html');
// Make the order in s3Sent predictable
s3Sent.sort((a, b) => a.Key.localeCompare(b.Key));

const md = input.Metadata;
assert(md.ID, "ID should be set");
assert(md.Version, "Version should be set");
assert.strictEqual(typeof (md.Timestamp), 'string', 'Timestamp should be set as a string');
assert.strictEqual(md.Users, '[{"email":"haha@foo.com"}]');
assert.strictEqual(md.Path, 'mydir/newdir/xyz.html');
const input = s3Sent[2];
assert.strictEqual(input.Bucket, 'root-bucket');
assert.strictEqual(input.CopySource, 'root-bucket/foo/mydir/xyz.html');
assert.strictEqual(input.Key, 'foo/mydir/newdir/xyz.html');

assert.strictEqual(1, collabcalls.length);
assert.deepStrictEqual(collabcalls,
['https://localhost/api/v1/syncAdmin?doc=somehost.sometld/source/foo/mydir/newdir/xyz.html']);
const md = input.Metadata;
assert(md.ID, "ID should be set");
assert(md.Version, "Version should be set");
assert.strictEqual(typeof (md.Timestamp), 'string', 'Timestamp should be set as a string');
assert.strictEqual(md.Users, '[{"email":"haha@foo.com"}]');
assert.strictEqual(md.Path, 'mydir/newdir/xyz.html');

assert.strictEqual(1, collabcalls.length);
assert.deepStrictEqual(collabcalls,
['https://localhost/api/v1/syncAdmin?doc=somehost.sometld/source/foo/mydir/newdir/xyz.html']);
} finally {
globalThis.fetch = savedFetch;
}
});

it('Copies a file for rename', async () => {
Expand All @@ -193,33 +198,38 @@ describe('Object copy', () => {
}));

const collabcalls = [];
const dacollab = {
fetch: (url) => {
collabcalls.push(url);
}
}
const env = { dacollab };
const env = { DA_COLLAB: 'https://localhost' };
const ctx = { bucket: 'root-bucket', org: 'testorg', key: 'mydir/dir1', origin: 'http://localhost:3000' };
ctx.aclCtx = await getAclCtx(env, ctx.org, ctx.users, '/');
const details = {
source: 'mydir/dir1',
destination: 'mydir/dir2',
};
await copyObject(env, ctx, details, true);

assert.strictEqual(s3Sent.length, 3);
const savedFetch = globalThis.fetch;
try {
globalThis.fetch = async (url) => {
collabcalls.push(url);
};

await copyObject(env, ctx, details, true);

// Make the order in s3Sent predictable
s3Sent.sort((a, b) => a.Key.localeCompare(b.Key));
assert.strictEqual(s3Sent.length, 3);

const input = s3Sent[2];
assert.strictEqual(input.Bucket, 'root-bucket');
assert.strictEqual(input.CopySource, 'root-bucket/testorg/mydir/dir1/myfile.html');
assert.strictEqual(input.Key, 'testorg/mydir/dir2/myfile.html');
assert.ifError(input.Metadata);
// Make the order in s3Sent predictable
s3Sent.sort((a, b) => a.Key.localeCompare(b.Key));

assert.deepStrictEqual(collabcalls,
['https://localhost/api/v1/syncAdmin?doc=http://localhost:3000/source/testorg/mydir/dir2/myfile.html']);
const input = s3Sent[2];
assert.strictEqual(input.Bucket, 'root-bucket');
assert.strictEqual(input.CopySource, 'root-bucket/testorg/mydir/dir1/myfile.html');
assert.strictEqual(input.Key, 'testorg/mydir/dir2/myfile.html');
assert.ifError(input.Metadata);

assert.deepStrictEqual(collabcalls,
['https://localhost/api/v1/syncAdmin?doc=http://localhost:3000/source/testorg/mydir/dir2/myfile.html']);
} finally {
globalThis.fetch = savedFetch;
}
});

it('Adds copy condition', async () => {
Expand All @@ -246,9 +256,7 @@ describe('Object copy', () => {

const collabCalled = [];
const env = {
dacollab: {
fetch: (x) => { collabCalled.push(x); },
},
DA_COLLAB: 'https://localhost',
};
const daCtx = {
bucket: 'root-bucket',
Expand All @@ -261,7 +269,11 @@ describe('Object copy', () => {
source: 'mysrc',
destination: 'mydst',
};
const resp = await copyFile({}, env, daCtx, 'mysrc/abc/def.html', details, false);

const savedFetch = globalThis.fetch;
try {
globalThis.fetch = async (x) => { collabCalled.push(x); };
const resp = await copyFile({}, env, daCtx, 'mysrc/abc/def.html', details, false);

assert.strictEqual(resp.constructor.name, 'CopyObjectCommand');
assert.strictEqual(resp.input.Bucket, 'root-bucket');
Expand Down Expand Up @@ -298,6 +310,9 @@ describe('Object copy', () => {

assert.deepStrictEqual(collabCalled,
['https://localhost/api/v1/syncAdmin?doc=https://blahblah:7890/source/myorg/mydst/abc/def.html']);
} finally {
globalThis.fetch = savedFetch;
}
});

it('Copy content when destination already exists', async () => {
Expand Down Expand Up @@ -344,27 +359,32 @@ describe('Object copy', () => {

const collabCalled = [];
const env = {
dacollab: {
fetch: (x) => { collabCalled.push(x); },
},
DA_COLLAB: 'https://localhost',
};
const daCtx = { bucket: 'mybucket', org: 'xorg' };
daCtx.aclCtx = await getAclCtx(env, daCtx.org, daCtx.users, '/');
const details = {
source: 'xsrc',
destination: 'xdst',
};
const resp = await copyFile({}, env, daCtx, 'xsrc/abc/def.html', details, false);
assert.strictEqual(resp, 'beuaaark!');

assert.strictEqual(puwv.length, 1);
assert.strictEqual(puwv[0].c, daCtx);
assert.strictEqual(puwv[0].e, env);
assert.strictEqual(puwv[0].u.body, 'original body');
assert.strictEqual(puwv[0].u.contentLength, 42);
assert.strictEqual(puwv[0].u.key, 'xdst/abc/def.html');
assert.strictEqual(puwv[0].u.org, 'xorg');
assert.strictEqual(puwv[0].u.type, 'text/html');

const savedFetch = globalThis.fetch;
try {
globalThis.fetch = async (x) => { collabCalled.push(x); };
const resp = await copyFile({}, env, daCtx, 'xsrc/abc/def.html', details, false);
assert.strictEqual(resp, 'beuaaark!');

assert.strictEqual(puwv.length, 1);
assert.strictEqual(puwv[0].c, daCtx);
assert.strictEqual(puwv[0].e, env);
assert.strictEqual(puwv[0].u.body, 'original body');
assert.strictEqual(puwv[0].u.contentLength, 42);
assert.strictEqual(puwv[0].u.key, 'xdst/abc/def.html');
assert.strictEqual(puwv[0].u.org, 'xorg');
assert.strictEqual(puwv[0].u.type, 'text/html');
} finally {
globalThis.fetch = savedFetch;
}
});

it('Copy content when origin does not exists', async () => {
Expand All @@ -389,20 +409,25 @@ describe('Object copy', () => {

const collabCalled = [];
const env = {
dacollab: {
fetch: (x) => { collabCalled.push(x); },
},
DA_COLLAB: 'https://localhost',
};
const daCtx = { org: 'qqqorg', origin: 'http://qqq' };
daCtx.aclCtx = await getAclCtx(env, daCtx.org, daCtx.users, '/');
const details = {
source: 'qqqsrc',
destination: 'qqqdst',
};
const resp = await copyFile({}, env, daCtx, 'qqqsrc/abc/def.html', details, false);
assert.strictEqual(resp.$metadata, error.$metadata);
assert.deepStrictEqual(collabCalled,
['https://localhost/api/v1/syncAdmin?doc=http://qqq/source/qqqorg/qqqdst/abc/def.html']);

const savedFetch = globalThis.fetch;
try {
globalThis.fetch = async (x) => { collabCalled.push(x); };
const resp = await copyFile({}, env, daCtx, 'qqqsrc/abc/def.html', details, false);
assert.strictEqual(resp.$metadata, error.$metadata);
assert.deepStrictEqual(collabCalled,
['https://localhost/api/v1/syncAdmin?doc=http://qqq/source/qqqorg/qqqdst/abc/def.html']);
} finally {
globalThis.fetch = savedFetch;
}
});
});

Expand All @@ -417,7 +442,7 @@ describe('Object copy', () => {
s3Sent.push(input);
}));

const env = { dacollab: { fetch: () => {} } };
const env = { DA_COLLAB: 'https://localhost' };
const ctx = {
org: 'foo',
key: 'mydir',
Expand All @@ -442,7 +467,7 @@ describe('Object copy', () => {
DA_JOBS[key] = value;
}
},
dacollab: { fetch: () => {} }
DA_COLLAB: 'https://localhost'
}
s3Mock.on(ListObjectsV2Command)
.resolves({
Expand Down Expand Up @@ -498,7 +523,7 @@ describe('Object copy', () => {
return DA_JOBS[key];
}
},
dacollab: { fetch: () => {} }
DA_COLLAB: 'https://localhost'
}

const ctx = {
Expand Down Expand Up @@ -540,7 +565,7 @@ describe('Object copy', () => {
delete DA_JOBS[key];
}
},
dacollab: { fetch: () => {} }
DA_COLLAB: 'https://localhost'
}

const ctx = {
Expand Down
Loading