add collection name validation (#37)

* add collection name validation

* linter fix

* add tests and optimize

* linter fix

* move to validateargs

* properly reference collection

* Update regex and error message

Co-authored-by: Emma Dickson <emmadickson@Emmas-MacBook-Pro.local>
This commit is contained in:
Emma Dickson 2021-04-07 20:24:01 -04:00 committed by GitHub
parent 24e2c4ddf8
commit c9f8fe051c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 3 deletions

View file

@ -305,6 +305,12 @@ class Crawler {
argv.scope = [new RegExp("^" + this.rxEscape(argv.url.slice(0, argv.url.lastIndexOf("/") + 1)))];
}
// Check that the collection name is valid.
if (argv.collection.search(/^[\w][\w-]*$/) === -1){
throw new Error(`\n${argv.collection} is an invalid collection name. Please supply a collection name only using alphanumeric characters and the following characters [_ - ]\n`);
}
argv.timeout *= 1000;
// waitUntil condition must be: load, domcontentloaded, networkidle0, networkidle2

View file

@ -0,0 +1,37 @@
const util = require("util");
const exec = util.promisify(require("child_process").exec);
test("check that the collection name is properly validation", async () => {
jest.setTimeout(30000);
let passed = "";
try{
const data = await exec("docker-compose run crawler crawl --url http://www.example.com/ --collection valid_collection-nameisvalid");
if (data.stdout.includes("Waiting 5s to ensure WARCs are finished")){
passed = true;
}
else{
passed = false;
}
}
catch (error) {
passed = false;
}
expect(passed).toBe(true);
});
test("check that the collection name is not accepted if it doesn't meets our standards", async () => {
jest.setTimeout(30000);
let passed = "";
try{
await exec("docker-compose run crawler crawl --url http://www.example.com/ --collection invalid_c!!ollection-nameisvalid");
passed = true;
}
catch(e){
passed = false;
}
expect(passed).toBe(false);
});