domains.test.js (2364B)
1 import t from "ava"; 2 import fs from "fs-extra"; 3 import path from "path"; 4 5 const domainsPath = path.resolve("domains"); 6 const files = fs.readdirSync(domainsPath).filter((file) => file.endsWith(".json")); 7 8 const domainCache = {}; 9 10 function getDomainData(subdomain) { 11 if (domainCache[subdomain]) { 12 return domainCache[subdomain]; 13 } 14 15 try { 16 const data = fs.readJsonSync(path.join(domainsPath, `${subdomain}.json`)); 17 domainCache[subdomain] = data; // Cache the domain data 18 return data; 19 } catch (error) { 20 throw new Error(`Failed to read JSON for ${subdomain}: ${error.message}`); 21 } 22 } 23 24 t("Nested subdomains should not exist without a parent subdomain", (t) => { 25 files.forEach((file) => { 26 const subdomain = file.replace(/\.json$/, ""); 27 const parts = subdomain.split("."); 28 29 for (let i = 1; i < parts.length; i++) { 30 const parent = parts.slice(i).join("."); 31 if (parent.startsWith("_")) continue; 32 33 t.true(files.includes(`${parent}.json`), `${file}: Parent subdomain "${parent}" does not exist`); 34 } 35 }); 36 37 t.pass(); 38 }); 39 40 t("Nested subdomains should not exist if any parent subdomain has NS records", (t) => { 41 files.forEach((file) => { 42 const subdomain = file.replace(/\.json$/, ""); 43 const parts = subdomain.split("."); 44 45 for (let i = 1; i < parts.length; i++) { 46 const parent = parts.slice(i).join("."); 47 if (parent.startsWith("_") || !files.includes(`${parent}.json`)) continue; 48 const parentData = getDomainData(parent); 49 50 t.true(!parentData.records.NS, `${file}: Parent subdomain "${parent}" has NS records`); 51 } 52 }); 53 54 t.pass(); 55 }); 56 57 t("Nested subdomains should be owned by the parent subdomain's owner", (t) => { 58 files.forEach((file) => { 59 const subdomain = file.replace(/\.json$/, ""); 60 const parentDomain = subdomain.split(".").reverse()[0]; 61 62 if (parentDomain !== subdomain) { 63 const data = getDomainData(subdomain); 64 const parentData = getDomainData(parentDomain); 65 66 t.true( 67 data.owner.username.toLowerCase() === parentData.owner.username.toLowerCase(), 68 `${file}: Owner does not match the parent subdomain` 69 ); 70 } 71 }); 72 73 t.pass(); 74 });