Skip to content

Commit

Permalink
test: await t.rejects and resolves
Browse files Browse the repository at this point in the history
  • Loading branch information
wkillerud committed Nov 15, 2024
1 parent ea68c2c commit 92436a9
Showing 1 changed file with 56 additions and 66 deletions.
122 changes: 56 additions & 66 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ const DEFAULT_CONFIG = {
projectId: "eik-test",
};

await tap.test("Sink() - Object type", (t) => {
tap.test("Sink() - Object type", (t) => {
const sink = new Sink(DEFAULT_CONFIG);
const name = Object.prototype.toString.call(sink);
t.ok(name.startsWith("[object Sink"), "should begin with Sink");
t.end();
});

await tap.test('Sink() - Argument "storageOptions" not provided', (t) => {
tap.test('Sink() - Argument "storageOptions" not provided', (t) => {
t.plan(1);
t.throws(
() => {
Expand All @@ -110,7 +110,7 @@ await tap.test('Sink() - Argument "storageOptions" not provided', (t) => {
t.end();
});

await tap.test('Sink() - Argument "storageOptions" is of wrong type', (t) => {
tap.test('Sink() - Argument "storageOptions" is of wrong type', (t) => {
t.plan(1);
t.throws(
() => {
Expand All @@ -131,28 +131,26 @@ await tap.test("Sink() - .write()", async (t) => {
const writeFrom = readFileStream("../fixtures/import-map.json");
const writeTo = await sink.write(file, "application/json");

t.resolves(pipe(writeFrom, writeTo), "should write file to sink");
await t.resolves(pipe(writeFrom, writeTo), "should write file to sink");

// Clean up sink
await sink.delete(dir);
t.end();
});

await tap.test("Sink() - .write() - arguments is illegal", async (t) => {
const sink = new Sink(DEFAULT_CONFIG);
const dir = slug();

t.rejects(
await t.rejects(
sink.write(300, "application/octet-stream"),
new TypeError("Argument must be a String"),
"should reject on illegal filepath",
);
t.rejects(
await t.rejects(
sink.write(`${dir}/bar/map.json`, 300),
new TypeError("Argument must be a String"),
"should reject on illegal mime type",
);
t.end();
});

await tap.test("Sink() - .write() - timeout", async (t) => {
Expand All @@ -165,12 +163,11 @@ await tap.test("Sink() - .write() - timeout", async (t) => {
const writeFrom = readFileStream("../fixtures/import-map.json");
const writeTo = await sink.write(file, "application/json");

t.rejects(
await t.rejects(
pipe(writeFrom, writeTo),
/network timeout at/,
"should reject on timeout",
);
t.end();
});

await tap.test(
Expand All @@ -179,40 +176,39 @@ await tap.test(
const sink = new Sink(DEFAULT_CONFIG);
const dir = slug();

t.rejects(
await t.rejects(
sink.write(`../../${dir}/sensitive.data`, "application/octet-stream"),
new Error("Directory traversal"),
"should reject on ../../ at beginning of filepath",
);
t.rejects(
await t.rejects(
sink.write(`../${dir}/sensitive.data`, "application/octet-stream"),
new Error("Directory traversal"),
"should reject on ../ at beginning of filepath",
);
t.rejects(
await t.rejects(
sink.write(
`/${dir}/../../../foo/sensitive.data`,
"application/octet-stream",
),
new Error("Directory traversal"),
"should reject on path traversal in the middle of filepath",
);
t.resolves(
await t.resolves(
sink.write(`./${dir}/sensitive.data`, "application/octet-stream"),
"should resolve on ./ at beginning of filepath",
);
t.resolves(
await t.resolves(
sink.write(`/${dir}/sensitive.data`, "application/octet-stream"),
"should resolve on / at beginning of filepath",
);
t.resolves(
await t.resolves(
sink.write(`//${dir}/sensitive.data`, "application/octet-stream"),
"should resolve on // at beginning of filepath",
);

// Clean up sink
await sink.delete(dir);
t.end();
},
);

Expand Down Expand Up @@ -262,24 +258,21 @@ await tap.test("Sink() - .read() - File exists", async (t) => {

// Clean up sink
await sink.delete(dir);
t.end();
});

await tap.test("Sink() - .read() - File does NOT exist", (t) => {
await tap.test("Sink() - .read() - File does NOT exist", async (t) => {
const sink = new Sink(DEFAULT_CONFIG);
const dir = slug();
t.rejects(sink.read(`/${dir}/foo/not-exist.json`), "should reject");
t.end();
await t.rejects(sink.read(`/${dir}/foo/not-exist.json`), "should reject");
});

await tap.test("Sink() - .read() - arguments is illegal", async (t) => {
const sink = new Sink(DEFAULT_CONFIG);
t.rejects(
await t.rejects(
sink.read(300),
new TypeError("Argument must be a String"),
"should reject on illegal filepath",
);
t.end();
});

await tap.test(
Expand All @@ -294,37 +287,36 @@ await tap.test(

await pipe(writeFrom, writeTo);

t.rejects(
await t.rejects(
sink.read(`../../${dir}/sensitive.data`),
new Error("Directory traversal"),
"should reject on ../../ at beginning of filepath",
);
t.rejects(
await t.rejects(
sink.read(`../${dir}/sensitive.data`),
new Error("Directory traversal"),
"should reject on ../ at beginning of filepath",
);
t.rejects(
await t.rejects(
sink.read(`/${dir}/../../../foo/sensitive.data`),
new Error("Directory traversal"),
"should reject on path traversal in the middle of filepath",
);
t.resolves(
await t.resolves(
sink.read(`./${file}`),
"should resolve on ./ at beginning of filepath",
);
t.resolves(
await t.resolves(
sink.read(`/${file}`),
"should resolve on / at beginning of filepath",
);
t.resolves(
await t.resolves(
sink.read(`//${file}`),
"should resolve on // at beginning of filepath",
);

// Clean up sink
await sink.delete(dir);
t.end();
},
);

Expand All @@ -339,24 +331,22 @@ await tap.test("Sink() - .delete() - Delete existing file", async (t) => {

await pipe(writeFrom, writeTo);

t.resolves(
await t.resolves(
sink.exist(file),
"should resolve - file is in sink before deletion",
);

await sink.delete(file);

t.rejects(sink.exist(file), "should reject - file was deleted");
await t.rejects(sink.exist(file), "should reject - file was deleted");

// Clean up sink
await sink.delete(dir);
t.end();
});

await tap.test("Sink() - .delete() - Delete non existing file", (t) => {
await tap.test("Sink() - .delete() - Delete non existing file", async (t) => {
const sink = new Sink(DEFAULT_CONFIG);
t.resolves(sink.delete("/bar/foo/not-exist.json"), "should resolve");
t.end();
await t.resolves(sink.delete("/bar/foo/not-exist.json"), "should resolve");
});

await tap.test(
Expand All @@ -377,26 +367,27 @@ await tap.test(

await sink.delete(fileA);

t.rejects(sink.exist(fileA), "should reject on file A - file was deleted");
t.resolves(
await t.rejects(
sink.exist(fileA),
"should reject on file A - file was deleted",
);
await t.resolves(
sink.exist(fileB),
"should resolve on file B - file was NOT deleted",
);

// Clean up sink
await sink.delete(dir);
t.end();
},
);

await tap.test("Sink() - .delete() - arguments is illegal", async (t) => {
const sink = new Sink(DEFAULT_CONFIG);
t.rejects(
await t.rejects(
sink.delete(300),
new TypeError("Argument must be a String"),
"should reject on illegal filepath",
);
t.end();
});

await tap.test("Sink() - .delete() - Delete files recursively", async (t) => {
Expand All @@ -415,10 +406,14 @@ await tap.test("Sink() - .delete() - Delete files recursively", async (t) => {

await sink.delete(dir);

t.rejects(sink.exist(fileA), "should reject on file A - file was deleted");
t.rejects(sink.exist(fileB), "should reject on file B - file was deleted");

t.end();
await t.rejects(
sink.exist(fileA),
"should reject on file A - file was deleted",
);
await t.rejects(
sink.exist(fileB),
"should reject on file B - file was deleted",
);
});

await tap.test(
Expand All @@ -428,37 +423,36 @@ await tap.test(
const dir = slug();
const file = `${dir}/map.json`;

t.rejects(
await t.rejects(
sink.delete(`../../${dir}/sensitive.data`),
new Error("Directory traversal"),
"should reject on ../../ at beginning of filepath",
);
t.rejects(
await t.rejects(
sink.delete(`../${dir}/sensitive.data`),
new Error("Directory traversal"),
"should reject on ../ at beginning of filepath",
);
t.rejects(
await t.rejects(
sink.delete(`/${dir}/../../../foo/sensitive.data`),
new Error("Directory traversal"),
"should reject on path traversal in the middle of filepath",
);
t.resolves(
await t.resolves(
sink.delete(`./${file}`),
"should resolve on ./ at beginning of filepath",
);
t.resolves(
await t.resolves(
sink.delete(`/${file}`),
"should resolve on / at beginning of filepath",
);
t.resolves(
await t.resolves(
sink.delete(`//${file}`),
"should resolve on // at beginning of filepath",
);

// Clean up sink
await sink.delete(dir);
t.end();
},
);

Expand All @@ -472,30 +466,27 @@ await tap.test("Sink() - .exist() - Check existing file", async (t) => {

await pipe(writeFrom, writeTo);

t.resolves(sink.exist(file), "should resolve - file is in sink");
await t.resolves(sink.exist(file), "should resolve - file is in sink");

// Clean up sink
await sink.delete(dir);
t.end();
});

await tap.test("Sink() - .exist() - Check non existing file", (t) => {
await tap.test("Sink() - .exist() - Check non existing file", async (t) => {
const sink = new Sink(DEFAULT_CONFIG);
t.rejects(
await t.rejects(
sink.exist("/bar/foo/not-exist.json"),
"should reject - file does not exist",
);
t.end();
});

await tap.test("Sink() - .exist() - arguments is illegal", async (t) => {
const sink = new Sink(DEFAULT_CONFIG);
t.rejects(
await t.rejects(
sink.exist(300),
new TypeError("Argument must be a String"),
"should reject on illegal filepath",
);
t.end();
});

await tap.test(
Expand All @@ -510,37 +501,36 @@ await tap.test(

await pipe(writeFrom, writeTo);

t.rejects(
await t.rejects(
sink.exist(`../../${dir}/sensitive.data`),
new Error("Directory traversal"),
"should reject on ../../ at beginning of filepath",
);
t.rejects(
await t.rejects(
sink.exist(`../${dir}/sensitive.data`),
new Error("Directory traversal"),
"should reject on ../ at beginning of filepath",
);
t.rejects(
await t.rejects(
sink.exist(`/${dir}/../../../foo/sensitive.data`),
new Error("Directory traversal"),
"should reject on path traversal in the middle of filepath",
);
t.resolves(
await t.resolves(
sink.exist(`./${file}`),
"should resolve on ./ at beginning of filepath",
);
t.resolves(
await t.resolves(
sink.exist(`/${file}`),
"should resolve on / at beginning of filepath",
);
t.resolves(
await t.resolves(
sink.exist(`//${file}`),
"should resolve on // at beginning of filepath",
);

// Clean up sink
await sink.delete(dir);
t.end();
},
);

Expand Down

0 comments on commit 92436a9

Please sign in to comment.