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
42 changes: 42 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Tests

# Run the test suite on every pull request, and on pushes to main.
on:
pull_request:
push:
branches: [main]

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Check out the code
uses: actions/checkout@v4

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

# Keep the MongoDB binary used by mongodb-memory-server between runs,
# so it is only downloaded once instead of on every pull request.
- name: Cache MongoDB binary
uses: actions/cache@v4
with:
path: ~/.cache/mongodb-binaries
key: mongodb-binaries-${{ runner.os }}-${{ hashFiles('package-lock.json') }}

- name: Install dependencies
run: npm ci

# Download MongoDB before the tests start, so the tests' 60s
# beforeAll timeout is not spent on the download.
- name: Pre-download MongoDB
run: node -e "import('mongodb-memory-server').then(async m => { const s = await m.MongoMemoryServer.create(); await s.stop(); console.log('MongoDB ready') })"

# The tests set their own JWT_SECRET / CSRF_SECRET and use an in-memory
# database, so no secrets or .env file are needed here.
- name: Run tests
run: npm test
6 changes: 6 additions & 0 deletions babel.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Lets jest load the project's ESM source (server + client) through babel-jest.
module.exports = {
presets: [
['@babel/preset-env', { targets: { node: 'current' } }]
]
};
25 changes: 21 additions & 4 deletions client/src/api.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
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.
// `withCredentials: true` makes the browser send the httpOnly auth cookie (and
// the CSRF cookie) on every request to the API.
export const api = axios.create({
baseURL: 'http://localhost:4000/api',
withCredentials: true
})

const SAFE_METHODS = ['get', 'head', 'options']
const CSRF_EXEMPT = ['/auth/login', '/auth/register']

// Before every state-changing request, fetch a fresh CSRF token and echo it in
// the `x-csrf-token` header. The token is bound to the session cookie, which
// changes on login/register, so fetching per request never sends a stale one.
api.interceptors.request.use(async (config) => {
const method = (config.method || 'get').toLowerCase()
const url = config.url || ''
if (SAFE_METHODS.includes(method) || CSRF_EXEMPT.some((p) => url.includes(p))) {
return config
}

// This is a GET, so it skips the branch above — no recursion.
const { data } = await api.get('/csrf-token')
config.headers['x-csrf-token'] = data.csrfToken
return config
})
24 changes: 24 additions & 0 deletions jest.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// One jest run with two environments:
// - "server" tests run in node (they boot the Express app + in-memory Mongo)
// - "client" tests run in jsdom (they exercise the browser-side api client)
// `npm test` runs both.
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'
}
}
]
};
Loading