Skip to content

Commit

Permalink
ci: Add automated tests for the project and run them in CI (#94)
Browse files Browse the repository at this point in the history
* only apply the original csp header to the root path

* allow running the build plugin locally

* add tests to confirm the plugin is working as expected

* refactor: always use the relative path and don't assume the package exists in the top-level of the relative node_modules directory
  • Loading branch information
JakeChampion authored Nov 6, 2024
1 parent 406ba4e commit d3e2c71
Show file tree
Hide file tree
Showing 11 changed files with 2,345 additions and 67 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: CI
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
on:
pull_request:
push:
branches: [main]
defaults:
run:
shell: bash

jobs:
test-npm-package:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
cache: npm
- run: npm install netlify-cli -g
- run: npm install
- run: npm test
- run: HOST='https://deploy-preview-${{ github.event.pull_request.number }}--csp-nonce.netlify.app' npm test
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ node_modules

# Local Netlify folder
.netlify

netlify/edge-functions
deno.lock
38 changes: 19 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
/* eslint-disable no-console */
import fs, { copyFileSync } from "fs";
import { getBuildInfo } from '@netlify/build-info/node';
import fs, { copyFileSync } from "node:fs";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from 'node:url'
import { getBuildInfo } from "@netlify/build-info/node";

const SITE_ID = "321a7119-6008-49a8-9d2f-e20602b1b349";
const __dirname = dirname(fileURLToPath(import.meta.url))

async function projectUsesNextJS() {
for (const framework of (await getBuildInfo()).frameworks) {
if (framework.id === 'next') {
if (framework.id === "next") {
return true;
}
}
Expand Down Expand Up @@ -46,50 +48,48 @@ export const onPreBuild = async ({
}

console.log(` Current working directory: ${process.cwd()}`);
const basePath =
build.environment.SITE_ID === SITE_ID
? "./src"
: "./node_modules/@netlify/plugin-csp-nonce/src";

// make the directory in case it actually doesn't exist yet
await utils.run.command(`mkdir -p ${INTERNAL_EDGE_FUNCTIONS_SRC}`);
console.log(
` Writing nonce edge function to ${INTERNAL_EDGE_FUNCTIONS_SRC}...`,
` Writing nonce edge function to ${INTERNAL_EDGE_FUNCTIONS_SRC}...`
);
copyFileSync(
`${basePath}/__csp-nonce.ts`,
`${INTERNAL_EDGE_FUNCTIONS_SRC}/__csp-nonce.ts`,
resolve(__dirname, `./src/__csp-nonce.ts`),
`${INTERNAL_EDGE_FUNCTIONS_SRC}/__csp-nonce.ts`
);

const usesNext = await projectUsesNextJS();

// Do not invoke the CSP Edge Function for Netlify Image CDN requests.
inputs.excludedPath.push('/.netlify/images');
inputs.excludedPath.push("/.netlify/images");

// If using NextJS, do not invoke the CSP Edge Function for NextJS Image requests.
if (usesNext) {
inputs.excludedPath.push('/_next/image');
inputs.excludedPath.push("/_next/image");
}

fs.writeFileSync(
`${INTERNAL_EDGE_FUNCTIONS_SRC}/__csp-nonce-inputs.json`,
JSON.stringify(inputs, null, 2),
JSON.stringify(inputs, null, 2)
);

// if no reportUri in config input, deploy function on site's behalf
if (!inputs.reportUri) {
// make the directory in case it actually doesn't exist yet
await utils.run.command(`mkdir -p ${INTERNAL_FUNCTIONS_SRC}`);
console.log(
` Writing violations logging function to ${INTERNAL_FUNCTIONS_SRC}...`,
` Writing violations logging function to ${INTERNAL_FUNCTIONS_SRC}...`
);
copyFileSync(
`${basePath}/__csp-violations.ts`,
`${INTERNAL_FUNCTIONS_SRC}/__csp-violations.ts`,
resolve(__dirname, `./src/__csp-violations.ts`),
`${INTERNAL_FUNCTIONS_SRC}/__csp-violations.ts`
);
} else {
console.log(` Using ${inputs.reportUri} as report-uri directive...`);
}

console.log(` Done.`);
};

export const onPreDev = onPreBuild;
5 changes: 4 additions & 1 deletion netlify.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
[[plugins]]
package = "/"
[plugins.inputs]
reportOnly = false
reportOnly = false

[dev]
publish = "site"
Loading

0 comments on commit d3e2c71

Please sign in to comment.