Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BREAKING] Throw an error on write of a resource when path does not match virBasePath of the respective adapter #453

Merged
merged 4 commits into from
Dec 16, 2022
Merged
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
5 changes: 5 additions & 0 deletions lib/adapters/AbstractAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ class AbstractAdapter extends AbstractReaderWriter {
}

_write(resource) {
if (!resource.getPath().startsWith(this._virBasePath)) {
throw new Error("The path of the resource '" + resource.getPath() + "' does not starts with " +
"the configured virtual base path of the adapter '" + this._virBasePath + "'");
flovogt marked this conversation as resolved.
Show resolved Hide resolved
}

if (this._project) {
// Assign project to resource if necessary
if (resource.hasProject()) {
Expand Down
20 changes: 20 additions & 0 deletions test/lib/adapters/AbstractAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,23 @@ test("Write resource with another project than provided in the adapter", (t) =>
t.is(error.message,
"Unable to write resource associated with project test.lib into adapter of project test.lib1: /test.js");
});

test("Create a resource with a path not starting with path configured in the adapter", (t) => {
const resource = createResource({
path: "/dest2/tmp/test.js",
string: "MyContent"
});

const writer = new MyAbstractAdapter({
virBasePath: "/dest2/writer/",
project: {
getName: () => "test.lib1",
getVersion: () => "2.0.0"
}
});

const error = t.throws(() => writer._write(resource));
t.is(error.message,
"The path of the resource '/dest2/tmp/test.js' does not starts with the configured " +
"virtual base path of the adapter '/dest2/writer/'");
});
43 changes: 30 additions & 13 deletions test/lib/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ for (const adapter of adapters) {
});

const resource = createResource({
path: "/dest/writer/test.js",
path: "/dest/writer/content/test.js",
string: "MyInitialContent"
});

await dest.write(resource);

resource.setString("MyNewContent");

const resource1 = await dest.byPath("/dest/writer/test.js");
const resource1 = await dest.byPath("/dest/writer/content/test.js");

t.is(await resource.getString(), "MyNewContent");
t.is(await resource1.getString(), "MyInitialContent");
Expand All @@ -88,7 +88,7 @@ for (const adapter of adapters) {

await dest.write(resource);

const resource2 = await dest.byPath("/dest/writer/test.js");
const resource2 = await dest.byPath("/dest/writer/content/test.js");
t.is(await resource.getString(), "MyNewContent");
t.is(await resource2.getString(), "MyNewContent");
});
Expand All @@ -100,35 +100,52 @@ for (const adapter of adapters) {
});

const resource = createResource({
path: "/dest/writer/test.js",
path: "/dest/writer/path/test.js",
string: "MyInitialContent"
});

await dest.write(resource);

resource.setPath("/dest/writer/test2.js");
resource.setPath("/dest/writer/path/test2.js");

const resourceOldPath = await dest.byPath("/dest/writer/test.js");
const resourceNewPath = await dest.byPath("/dest/writer/test2.js");
const resourceOldPath = await dest.byPath("/dest/writer/path/test.js");
flovogt marked this conversation as resolved.
Show resolved Hide resolved
const resourceNewPath = await dest.byPath("/dest/writer/path/test2.js");

t.is(await resource.getPath(), "/dest/writer/test2.js");
t.is(await resource.getPath(), "/dest/writer/path/test2.js");
t.truthy(resourceOldPath);
t.is(await resourceOldPath.getString(), await resource.getString());
t.is(await resourceOldPath.getPath(), "/dest/writer/test.js");
t.is(await resourceOldPath.getPath(), "/dest/writer/path/test.js");
t.not(resourceNewPath);

await dest.write(resource);

const resourceOldPath1 = await dest.byPath("/dest/writer/test.js");
const resourceNewPath1 = await dest.byPath("/dest/writer/test2.js");
const resourceOldPath1 = await dest.byPath("/dest/writer/path/test.js");
const resourceNewPath1 = await dest.byPath("/dest/writer/path/test2.js");

t.is(await resource.getPath(), "/dest/writer/test2.js");
t.is(await resource.getPath(), "/dest/writer/path/test2.js");
t.truthy(resourceNewPath1);
t.is(await resourceNewPath1.getString(), await resource.getString());
t.is(await resourceNewPath1.getPath(), "/dest/writer/test2.js");
t.is(await resourceNewPath1.getPath(), "/dest/writer/path/test2.js");
t.not(resourceOldPath1);
});

test(adapter + ": Create a resource with a path not starting with path configured in the adapter", async (t) => {
t.pass(2);
const dest = await getAdapter({
fsBasePath: "./test/tmp/writer/",
virBasePath: "/dest2/writer/"
});

const resource = createResource({
path: "/dest2/tmp/test.js",
string: "MyContent"
});

const error = await t.throwsAsync(dest.write(resource));
t.is(error.message, "The path of the resource '/dest2/tmp/test.js' does not starts with the configured " +
"virtual base path of the adapter '/dest2/writer/'");
});

test(adapter + ": Filter resources", async (t) => {
const source = createAdapter({
fsBasePath: "./test/fixtures/application.a/webapp",
Expand Down