Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: ACL Tests

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
test:
runs-on: ubuntu-22.04

env:
MONGOMS_VERSION: 7.0.14
CI: true

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20
cache: npm

- name: Cache MongoDB binary
uses: actions/cache@v4
with:
path: ~/.cache/mongodb-binaries
key: mongodb-7.0.14-linux

- name: Install dependencies
run: npm ci

- name: Download MongoDB binary
run: |
node -e "const { MongoMemoryServer } = require('mongodb-memory-server'); (async () => { const db = await MongoMemoryServer.create(); await db.stop(); })().catch(err => { console.error(err); process.exit(1); });"

- name: Run Jest tests
run: npm test -- --runInBand
137 changes: 0 additions & 137 deletions .vscode/tasks.json

This file was deleted.

3 changes: 3 additions & 0 deletions babel.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
presets: [["@babel/preset-env", { targets: { node: "current" } }]],
};
52 changes: 44 additions & 8 deletions client/src/api.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,46 @@
import axios from 'axios'
import axios from "axios";

// `withCredentials: true` makes the browser send the httpOnly auth cookie on
// every request to the API — including requests triggered by a different site.
// That convenience is exactly what leaves this app open to CSRF: there is no
// second proof that a state-changing request actually came from our own UI.
export const api = axios.create({
baseURL: 'http://localhost:4000/api',
withCredentials: true
})
baseURL: "http://localhost:4000/api",
withCredentials: true,
});

// Cache the token for the current browser session.
let csrfToken = null;
let csrfTokenPromise = null;

// Automatically attach a CSRF token to every state-changing request.
api.interceptors.request.use(async (config) => {
const method = (config.method || "get").toLowerCase();

const mutatingMethods = ["post", "put", "patch", "delete"];

if (!mutatingMethods.includes(method)) {
return config;
}

// Fetch the token only when needed.
if (!csrfToken) {
if (!csrfTokenPromise) {
csrfTokenPromise = api
.get("/csrf-token")
.then((response) => {
csrfToken = response.data.csrfToken;
return csrfToken;
})
.finally(() => {
csrfTokenPromise = null;
});
}

await csrfTokenPromise;
}

if (!csrfToken) {
throw new Error("Unable to obtain CSRF token");
}

config.headers.set("x-csrf-token", csrfToken);

return config;
});
20 changes: 20 additions & 0 deletions jest.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module.exports = {
projects: [
{
displayName: "server",
testEnvironment: "node",
testMatch: ["<rootDir>/tests/server/**/*.test.js"],
transform: {
"^.+\\.js$": "babel-jest",
},
},
{
displayName: "client",
testEnvironment: "jsdom",
testMatch: ["<rootDir>/tests/client/**/*.test.js"],
transform: {
"^.+\\.js$": "babel-jest",
},
},
],
};
39 changes: 39 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
"bcryptjs": "^2.4.3",
"cookie-parser": "^1.4.7",
"cors": "^2.8.5",
"csrf-csrf": "^4.1.0",
"dotenv": "^16.4.5",
"express": "^4.19.2",
"express-rate-limit": "^8.7.4",
"joi": "^17.12.3",
"jsonwebtoken": "^9.0.2",
"mongoose": "^8.6.0",
Expand Down
Loading