mirror of
https://github.com/tiennm99/is-a-dev.git
synced 2026-10-15 00:17:35 +00:00
optimise + improve tests
This commit is contained in:
1 parent
c84fd1377c
commit
bcf071b6f2
4 files changed
+244
-248
No files matched your search
+98
-51
@@ -2,13 +2,16 @@ const t = require("ava");
|
||||
const fs = require("fs-extra");
|
||||
const path = require("path");
|
||||
|
||||
const ignoredRootJSONFiles = ["package-lock.json", "package.json"];
|
||||
|
||||
const requiredFields = {
|
||||
owner: "object",
|
||||
record: "object"
|
||||
};
|
||||
|
||||
const optionalFields = {
|
||||
proxied: "boolean"
|
||||
proxied: "boolean",
|
||||
redirect_config: "object"
|
||||
};
|
||||
|
||||
const requiredOwnerFields = {
|
||||
@@ -19,31 +22,87 @@ const optionalOwnerFields = {
|
||||
email: "string"
|
||||
};
|
||||
|
||||
const optionalRedirectConfigFields = {
|
||||
custom_paths: "object",
|
||||
redirect_paths: "boolean"
|
||||
};
|
||||
|
||||
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
|
||||
const hostnameRegex = /^(?=.{1,253}$)(?:(?:[_a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)\.)+[a-zA-Z]{2,63}$/;
|
||||
|
||||
const exceptedDomains = require("../util/excepted-domains.json");
|
||||
const reservedDomains = require("../util/reserved-domains.json");
|
||||
const domainsPath = path.resolve("domains");
|
||||
const files = fs.readdirSync(domainsPath);
|
||||
|
||||
function validateRequiredFields(t, obj, requiredFields, file) {
|
||||
Object.keys(requiredFields).forEach((key) => {
|
||||
t.true(obj.hasOwnProperty(key), `${file}: Missing required field: ${key}`);
|
||||
t.is(typeof obj[key], requiredFields[key], `${file}: Field ${key} should be of type ${requiredFields[key]}`);
|
||||
const expandReservedDomains = (reserved) => {
|
||||
const expandedList = [...reserved];
|
||||
|
||||
reserved.forEach((item) => {
|
||||
const rangeMatch = item.match(/\[(\d+)-(\d+)\]/);
|
||||
|
||||
if (rangeMatch) {
|
||||
const prefix = item.split("[")[0];
|
||||
const start = parseInt(rangeMatch[1], 10);
|
||||
const end = parseInt(rangeMatch[2], 10);
|
||||
|
||||
if (start < end) {
|
||||
for (let i = start; i <= end; i++) {
|
||||
expandedList.push(prefix + i);
|
||||
}
|
||||
expandedList.splice(expandedList.indexOf(item), 1);
|
||||
} else {
|
||||
throw new Error(`[util/reserved-domains.json] Invalid range [${start}-${end}] in "${item}"`);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return expandedList;
|
||||
};
|
||||
|
||||
const expandedReservedDomains = expandReservedDomains(reservedDomains);
|
||||
|
||||
function validateFields(t, obj, fields, file, prefix = "") {
|
||||
Object.keys(fields).forEach((key) => {
|
||||
const fieldPath = prefix ? `${prefix}.${key}` : key;
|
||||
|
||||
function validateOptionalFields(t, obj, optionalFields, file) {
|
||||
Object.keys(optionalFields).forEach((key) => {
|
||||
if (obj.hasOwnProperty(key)) {
|
||||
t.is(
|
||||
typeof obj[key],
|
||||
optionalFields[key],
|
||||
`${file}: Field ${key} should be of type ${optionalFields[key]}`
|
||||
);
|
||||
t.is(typeof obj[key], fields[key], `${file}: Field ${fieldPath} should be of type ${fields[key]}`);
|
||||
} else if (fields === requiredFields) {
|
||||
t.true(false, `${file}: Missing required field: ${fieldPath}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function validateFileName(t, file) {
|
||||
t.true(file.endsWith(".json"), `${file}: File does not have .json extension`);
|
||||
t.false(file.includes(".is-a.dev"), `${file}: File name should not contain .is-a.dev`);
|
||||
t.true(file === file.toLowerCase(), `${file}: File name should be all lowercase`);
|
||||
|
||||
// Ignore root domain
|
||||
if (file !== "@.json") {
|
||||
const subdomain = file.replace(/\.json$/, "");
|
||||
|
||||
t.regex(
|
||||
subdomain + ".is-a.dev",
|
||||
hostnameRegex,
|
||||
`${file}: FQDN must be 1-253 characters, use letters, numbers, dots, or hyphens, and not start or end with a hyphen.`
|
||||
);
|
||||
t.true(!expandedReservedDomains.includes(subdomain), `${file}: Subdomain name is reserved`);
|
||||
|
||||
if (subdomain.split(".").length === 1 && !exceptedDomains.includes(subdomain)) {
|
||||
t.false(subdomain.startsWith("_"), `${file}: Root subdomains should not start with an underscore`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
t("JSON files should not be in the root directory", (t) => {
|
||||
const rootFiles = fs
|
||||
.readdirSync(path.resolve())
|
||||
.filter((file) => file.endsWith(".json") && !ignoredRootJSONFiles.includes(file));
|
||||
t.is(rootFiles.length, 0, "JSON files should not be in the root directory");
|
||||
});
|
||||
|
||||
t("All files should be valid JSON", (t) => {
|
||||
files.forEach((file) => {
|
||||
t.notThrows(() => fs.readJsonSync(path.join(domainsPath, file)), `${file}: Invalid JSON file`);
|
||||
@@ -52,59 +111,47 @@ t("All files should be valid JSON", (t) => {
|
||||
|
||||
t("All files should have valid file names", (t) => {
|
||||
files.forEach((file) => {
|
||||
t.true(file.endsWith(".json"), `${file}: File does not have .json extension`);
|
||||
t.false(file.includes(".is-a.dev"), `${file}: File name should not contain .is-a.dev`);
|
||||
t.true(file === file.toLowerCase(), `${file}: File name should be lowercase`);
|
||||
|
||||
// Ignore root domain
|
||||
if (file !== "@.json") {
|
||||
const subdomain = file.replace(/\.json$/, "");
|
||||
t.regex(
|
||||
subdomain + ".is-a.dev",
|
||||
hostnameRegex,
|
||||
`${file}: FQDN must be 1-253 characters, use letters, numbers, dots, or hyphens, and not start or end with a hyphen.`
|
||||
);
|
||||
}
|
||||
validateFileName(t, file);
|
||||
});
|
||||
});
|
||||
|
||||
t("All files should have the required fields", (t) => {
|
||||
t("All files should have valid required and optional fields", (t) => {
|
||||
files.forEach((file) => {
|
||||
const data = fs.readJsonSync(path.join(domainsPath, file));
|
||||
|
||||
// Validate top-level required fields
|
||||
validateRequiredFields(t, data, requiredFields, file);
|
||||
validateFields(t, data, requiredFields, file);
|
||||
|
||||
// Validate owner object fields
|
||||
validateRequiredFields(t, data.owner, requiredOwnerFields, file);
|
||||
// Validate owner fields
|
||||
validateFields(t, data.owner, requiredOwnerFields, file, "owner");
|
||||
validateFields(t, data.owner, optionalOwnerFields, file, "owner");
|
||||
|
||||
// Validate optional fields for top-level and redirect config
|
||||
validateFields(t, data, optionalFields, file);
|
||||
if (data.redirect_config) {
|
||||
validateFields(t, data.redirect_config, optionalRedirectConfigFields, file, "redirect_config");
|
||||
}
|
||||
|
||||
// Validate email format
|
||||
if (data.owner.email) {
|
||||
t.regex(data.owner.email, emailRegex, `${file}: Owner email should be a valid email address`);
|
||||
}
|
||||
|
||||
// Ensure 'record' field is not empty
|
||||
t.true(Object.keys(data.record).length > 0, `${file}: Missing DNS records`);
|
||||
});
|
||||
});
|
||||
|
||||
t("All files should have valid optional fields", (t) => {
|
||||
files.forEach((file) => {
|
||||
const data = fs.readJsonSync(path.join(domainsPath, file));
|
||||
t("Reserved domains file should be valid", (t) => {
|
||||
const subdomainRegex = /^_?[a-zA-Z0-9]+([-\.][a-zA-Z0-9]+)*(\[\d+-\d+\])?$/;
|
||||
|
||||
// Validate optional fields at top level
|
||||
validateOptionalFields(t, data, optionalFields, file);
|
||||
|
||||
// Validate optional fields for owner object
|
||||
validateOptionalFields(t, data.owner, optionalOwnerFields, file);
|
||||
|
||||
// Email validation (if provided)
|
||||
if (data.owner.email) {
|
||||
t.regex(data.owner.email, emailRegex, `${file}: Owner email should be a valid email address`);
|
||||
}
|
||||
expandedReservedDomains.forEach((item, index) => {
|
||||
t.regex(
|
||||
item,
|
||||
subdomainRegex,
|
||||
`[util/reserved-domains.json] Invalid subdomain name "${item}" at index ${index}`
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
const ignoredJSONFiles = ["package-lock.json", "package.json"];
|
||||
|
||||
t("JSON files should not be in the root directory", (t) => {
|
||||
const rootFiles = fs
|
||||
.readdirSync(path.resolve())
|
||||
.filter((file) => file.endsWith(".json") && !ignoredJSONFiles.includes(file));
|
||||
t.is(rootFiles.length, 0, "JSON files should not be in the root directory");
|
||||
t.pass();
|
||||
});
|
||||
Reference in new issue
Block a user