Skip to content

Commit

Permalink
Add site verification, tests; modularize validators
Browse files Browse the repository at this point in the history
  • Loading branch information
wesley-dean-gsa committed Nov 19, 2024
1 parent 965c661 commit 47ee176
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 37 deletions.
50 changes: 14 additions & 36 deletions .eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const yaml = require("js-yaml");
const { sassPlugin } = require("esbuild-sass-plugin");
const svgSprite = require("eleventy-plugin-svg-sprite");
const { imageShortcode, imageWithClassShortcode } = require("./config");
const environmentValidators = require("./js/environmentValidators.js");

require("dotenv").config();

Expand Down Expand Up @@ -66,36 +67,6 @@ module.exports = function (config) {

const { hosts } = yaml.load(fs.readFileSync("./_data/site.yaml", "utf8"));

function isValidGitBranch(branch) {
const validGitBranch = /^[a-zA-Z0-9_\-\.\/]+$/;
return validGitBranch.test(branch);
}

function isValidTwitterHandle(handle) {
const validTwitterHandle = /^\w{1,15}$/;
return validTwitterHandle.test(handle);
}

function isValidDapAgency(agency) {
const validDapAgency = /^\w{1,15}$/;
return validDapAgency.test(agency);
}

function isValidAnalyticsId(ga) {
const validAnalyticsId = /^(G|UA|YT|MO)-[a-zA-Z0-9-]+$/;
return validAnalyticsId.test(ga);
}

function isValidSearchKey(accessKey) {
const validSearchKey = /^[0-9a-zA-Z]{1,}=*$/;
return validSearchKey.test(accessKey);
}

function isValidSearchAffiliate(affiliate) {
const validSearchAffiliate = /^[0-9a-z-]{1,}$/;
return validSearchAffiliate.test(affiliate);
}

if (process.env.BRANCH && isValidGitBranch(process.env.BRANCH)) {
switch (process.env.BRANCH) {
case "main":
Expand All @@ -112,35 +83,42 @@ module.exports = function (config) {
config.addGlobalData("baseUrl", baseUrl);
config.addGlobalData("site.baseUrl", baseUrl);

if (process.env.TWITTER && isValidTwitterHandle(process.env.TWITTER)) {
if (process.env.TWITTER && environmentValidators.isValidTwitterHandle(process.env.TWITTER)) {
config.addGlobalData("site.twitter", process.env.TWITTER);
}

if (process.env.DAP_AGENCY && isValidDapAgency(process.env.DAP_AGENCY)) {
if (process.env.DAP_AGENCY && environmentValidators.isValidDapAgency(process.env.DAP_AGENCY)) {
config.addGlobalData("site.dap.agency", process.env.DAP_AGENCY);
}

if (
process.env.DAP_SUBAGENCY &&
isValidDapAgency(process.env.DAP_SUBAGENCY)
environmentValidators.isValidDapAgency(process.env.DAP_SUBAGENCY)
) {
config.addGlobalData("site.dap.subagency", process.env.DAP_SUBAGENCY);
}

if (process.env.GA && isValidAnalyticsId(process.env.GA)) {
if (process.env.GA && environmentValidators.isValidAnalyticsId(process.env.GA)) {
config.addGlobalData("site.ga", process.env.GA);
}

if (
process.env.GOOGLE_VERIFICATION_TOKEN &&
environmentValidators.isValidVerificationToken (process.env.GOOGLE_VERIFICATION_TOKEN)
) {
config.addGlobalData("site.google_verification_token", process.env.GOOGLE_VERIFICATION_TOKEN)
}

if (
process.env.SEARCH_ACCESS_KEY &&
isValidSearchKey(process.env.SEARCH_ACCESS_KEY)
environmentValidators.isValidSearchKey(process.env.SEARCH_ACCESS_KEY)
) {
config.addGlobalData("site.access_key", process.env.SEARCH_ACCESS_KEY);
}

if (
process.env.SEARCH_AFFILIATE &&
isValidSearchAffiliate(process.env.SEARCH_AFFILIATE)
environmentValidators.isValidSearchAffiliate(process.env.SEARCH_AFFILIATE)
) {
config.addGlobalData("site.affiliate", process.env.SEARCH_AFFILIATE);
}
Expand Down
4 changes: 3 additions & 1 deletion _includes/meta.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@
<!-- tell robots to follow links in the content and index all the things -->
<meta name="robots" content="index, follow" />

<meta name="google-site-verification" content="bpH2FtDPiSmeRUJSQWfFw_-vMHjs3mXAgGbMF3TEwxc" />
{%- if site.google_verification_token -%}
<meta name="google-site-verification" content="{{ site.google_verification_token }}" />
{%- endif -%}

<meta property="og:title" content="{{ effective_title }}" />
<meta name="description" content="{{ effective_description }}" />
Expand Down
44 changes: 44 additions & 0 deletions js/environmentValidators.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
function isValidGitBranch(branch) {
const validGitBranch = /^[a-zA-Z0-9_\-\.\/]+$/;
return validGitBranch.test(branch);
}

function isValidTwitterHandle(handle) {
const validTwitterHandle = /^\w{1,15}$/;
return validTwitterHandle.test(handle);
}

function isValidDapAgency(agency) {
const validDapAgency = /^\w{1,15}$/;
return validDapAgency.test(agency);
}

function isValidAnalyticsId(ga) {
const validAnalyticsId = /^(G|UA|YT|MO)-[a-zA-Z0-9-]+$/;
return validAnalyticsId.test(ga);
}

function isValidVerificationToken(token) {
const validToken = /^[a-zA-Z0-9_-]{42,44}$/;
return validToken.test(token);
}

function isValidSearchKey(accessKey) {
const validSearchKey = /^[0-9a-zA-Z]{1,}=*$/;
return validSearchKey.test(accessKey);
}

function isValidSearchAffiliate(affiliate) {
const validSearchAffiliate = /^[0-9a-z-]{1,}$/;
return validSearchAffiliate.test(affiliate);
}

module.exports = {
isValidGitBranch,
isValidTwitterHandle,
isValidDapAgency,
isValidAnalyticsId,
isValidVerificationToken,
isValidSearchKey,
isValidSearchAffiliate
};

0 comments on commit 47ee176

Please sign in to comment.