forked from 2factorauth/twofactorauth
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite final Ruby tests (2factorauth#8128)
Signed-off-by: Carl <[email protected]> Co-authored-by: H. Kamran <[email protected]>
- Loading branch information
Showing
10 changed files
with
171 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
*.md | ||
*.json | ||
*.yaml | ||
*.yml | ||
|
||
entries/ | ||
img/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,26 @@ | ||
{ | ||
"name": "twofactorauth", | ||
"private": true, | ||
"scripts": { | ||
"format": "prettier . --write" | ||
}, | ||
"dependencies": { | ||
"@actions/core": "^1.10.1", | ||
"dotenv": "^16.4.5", | ||
"glob": "^10.4.1" | ||
}, | ||
"optionalDependencies": { | ||
"algoliasearch": "^4.24.0" | ||
}, | ||
"devDependencies": { | ||
"@playwright/test": "^1.45.1", | ||
"@xmldom/xmldom": "^0.8.10", | ||
"abort-controller": "^3.0.0", | ||
"ajv": "^8.16.0", | ||
"ajv-errors": "^3.0.0", | ||
"ajv-formats": "^3.0.1", | ||
"algoliasearch": "^4.24.0", | ||
"dotenv": "^16.4.5", | ||
"glob": "^10.4.1" | ||
"prettier": "^3.3.3", | ||
"xml2js": "^0.6.2", | ||
"xpath": "^0.0.34" | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
const fs = require("fs"); | ||
const { DOMParser } = require("xmldom"); | ||
const xpath = require("xpath"); | ||
const core = require("@actions/core"); | ||
let errors = false; | ||
|
||
// Function to test the SVG content against an XPath expression | ||
function test(svgContent, xpathExpression) { | ||
try { | ||
const doc = new DOMParser().parseFromString(svgContent, "application/xml"); | ||
const nodes = xpath.select(xpathExpression, doc); | ||
return nodes.length > 0; | ||
} catch (err) { | ||
core.error(`Failed to parse SVG content: ${err.message}`); | ||
return false; | ||
} | ||
} | ||
|
||
// Function to handle file checking | ||
async function main() { | ||
const files = process.argv.slice(2); | ||
|
||
await Promise.allSettled( | ||
files.map(async (file) => { | ||
const error = (msg) => { | ||
core.error(msg, { file }); | ||
errors = true; | ||
}; | ||
|
||
const warn = (msg) => { | ||
core.warning(msg, { file }); | ||
}; | ||
|
||
const svg = fs.readFileSync(file, "utf8"); | ||
const doc = new DOMParser().parseFromString(svg, "application/xml"); | ||
const parseErrors = doc.getElementsByTagName("parsererror"); | ||
|
||
if (parseErrors.length > 0) error("Invalid SVG file"); | ||
if (svg.includes("<?")) error("Unnecessary processing instruction found"); | ||
if (test(svg, "//image")) error("Embedded image detected"); | ||
if (svg.split("\n").filter((line) => line.trim()).length > 1) | ||
error("Minimize file to one line"); | ||
if (test(svg, "//comment()")) warn("Remove comments"); | ||
if (fs.statSync(file).size > 5 * 1024) warn("Unusually large file size"); | ||
if ( | ||
test( | ||
svg, | ||
'//@*[(starts-with(name(), "data-") or starts-with(name(), "class-"))]', | ||
) | ||
) | ||
warn("Unnecessary data or class attribute"); | ||
if (test(svg, "//@width | //@height")) | ||
warn("Use viewBox instead of height/width"); | ||
if (test(svg, "/*/@id")) warn("Unnecessary id attribute in root element"); | ||
if (test(svg, '//*[@fill="#000" or @fill="#000000"]')) | ||
warn('Unnecessary fill="#000" attribute'); | ||
if (test(svg, "//*[@style]")) | ||
warn("Use attributes instead of style elements"); | ||
if (test(svg, "//*[@fill-opacity]")) | ||
warn("Use hex color instead of fill-opacity"); | ||
if (svg.includes("xml:space")) | ||
warn("Unnecessary XML:space declaration found"); | ||
if ( | ||
test( | ||
svg, | ||
"//*[@version or @fill-rule or @script or @a or @clipPath or @class]", | ||
) | ||
) | ||
warn("Unnecessary attribute(s) found: version, fill-rule, script, a, clipPath, or class"); | ||
}), | ||
); | ||
process.exit(+errors); | ||
} | ||
|
||
module.exports = main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
const fs = require("fs").promises; | ||
const core = require("@actions/core"); | ||
const AbortController = require("abort-controller"); | ||
|
||
// Helper function to create a timeout promise | ||
function timeout(ms) { | ||
return new Promise((_, reject) => | ||
setTimeout(() => reject(new Error("timeout")), ms), | ||
); | ||
} | ||
|
||
async function checkURL(url, file) { | ||
const controller = new AbortController(); | ||
const signal = controller.signal; | ||
|
||
try { | ||
const res = await Promise.race([ | ||
fetch(url, { | ||
headers: { | ||
"User-Agent": | ||
"2factorauth/URLValidator (+https://2fa.directory/bots)", | ||
}, | ||
signal, | ||
}), | ||
timeout(2000).then(() => controller.abort()), | ||
]); | ||
|
||
if (res.ok) return true; | ||
else if (res.status !== 403) | ||
core.warning(`Unable to fetch ${url} (${res.status})`, { file }); | ||
} catch (e) { | ||
core.warning(`Unable to fetch ${url}`, { file }); | ||
} | ||
return false; | ||
} | ||
|
||
async function main(files) { | ||
await Promise.all( | ||
files.map(async (file) => { | ||
const json = JSON.parse(await fs.readFile(file)); | ||
const entry = json[Object.keys(json)[0]]; | ||
let urls = [entry.url ? entry.url : `https://${entry.domain}/`]; | ||
|
||
entry["additional-domains"]?.forEach((domain) => | ||
urls.push(`https://${domain}/`), | ||
); | ||
|
||
await Promise.all(urls.map((url) => checkURL(url, file))); | ||
}), | ||
); | ||
|
||
return true; | ||
} | ||
|
||
main(process.argv.slice(2)).then(() => process.exit(0)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.