Skip to content

Commit e219181

Browse files
committed
feat(backend): add Postman collection, Newman CI, and workspace sync
- Postman collection covering all backend endpoints with assertions and request chaining (token/id/slug captured across requests) - Environment file (baseUrl, token) for local runs - push.mjs to sync the collection to a Postman workspace via the API - CI workflow (api-tests.yml) running Newman against a booted backend, with orchestration in .github/scripts/run-api-tests.sh per repo convention - npm scripts: test:api, postman:push; add newman dev dependency
1 parent 18809a8 commit e219181

8 files changed

Lines changed: 2637 additions & 39 deletions

File tree

.github/scripts/run-api-tests.sh

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Runs the DevCard Postman collection against a freshly-booted backend.
4+
#
5+
# Invoked by .github/workflows/api-tests.yml. Keeping the orchestration here
6+
# (rather than inline in the YAML) matches the repo convention of housing
7+
# workflow logic under .github/scripts/.
8+
#
9+
# Expects to be run from the repository root with these env vars set:
10+
# DATABASE_URL, REDIS_URL, JWT_SECRET, ENCRYPTION_KEY, NODE_ENV, PORT
11+
#
12+
# Steps: apply migrations + seed -> start server -> wait for /health ->
13+
# run Newman -> always stop the server on exit.
14+
15+
set -euo pipefail
16+
17+
PORT="${PORT:-3000}"
18+
BASE_URL="http://localhost:${PORT}"
19+
HEALTH_RETRIES="${HEALTH_RETRIES:-30}"
20+
HEALTH_INTERVAL="${HEALTH_INTERVAL:-2}"
21+
COLLECTION="apps/backend/postman/DevCard.postman_collection.json"
22+
23+
SERVER_PID=""
24+
25+
# Always stop the server, however this script exits.
26+
cleanup() {
27+
if [[ -n "${SERVER_PID}" ]] && kill -0 "${SERVER_PID}" 2>/dev/null; then
28+
echo "Stopping API server (pid ${SERVER_PID})"
29+
kill "${SERVER_PID}" 2>/dev/null || true
30+
fi
31+
}
32+
trap cleanup EXIT
33+
34+
echo "::group::Apply migrations and seed"
35+
npm --prefix apps/backend exec prisma migrate deploy
36+
npm --prefix apps/backend run db:seed
37+
echo "::endgroup::"
38+
39+
echo "::group::Start API server"
40+
npm --prefix apps/backend exec tsx src/server.ts &
41+
SERVER_PID=$!
42+
echo "Server started (pid ${SERVER_PID})"
43+
echo "::endgroup::"
44+
45+
echo "::group::Wait for server health"
46+
for ((i = 1; i <= HEALTH_RETRIES; i++)); do
47+
if curl -sf "${BASE_URL}/health" > /dev/null; then
48+
echo "Server is up after ${i} attempt(s)"
49+
break
50+
fi
51+
if [[ "${i}" -eq "${HEALTH_RETRIES}" ]]; then
52+
echo "Server did not become healthy within $((HEALTH_RETRIES * HEALTH_INTERVAL))s"
53+
exit 1
54+
fi
55+
sleep "${HEALTH_INTERVAL}"
56+
done
57+
echo "::endgroup::"
58+
59+
echo "::group::Run Newman"
60+
npx --yes newman run "${COLLECTION}" \
61+
--env-var "baseUrl=${BASE_URL}" \
62+
--reporters cli
63+
echo "::endgroup::"

.github/workflows/api-tests.yml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
name: API Tests (Newman)
2+
3+
# Runs the DevCard Postman collection against a freshly-booted backend.
4+
on:
5+
pull_request:
6+
paths:
7+
- 'apps/backend/**'
8+
- 'packages/shared/**'
9+
- '.github/workflows/api-tests.yml'
10+
workflow_dispatch:
11+
12+
jobs:
13+
newman:
14+
runs-on: ubuntu-latest
15+
16+
services:
17+
postgres:
18+
image: postgres:16
19+
env:
20+
POSTGRES_USER: devcard
21+
POSTGRES_PASSWORD: devcard
22+
POSTGRES_DB: devcard
23+
ports:
24+
- 5432:5432
25+
options: >-
26+
--health-cmd "pg_isready -U devcard"
27+
--health-interval 10s
28+
--health-timeout 5s
29+
--health-retries 5
30+
redis:
31+
image: redis:7
32+
ports:
33+
- 6379:6379
34+
options: >-
35+
--health-cmd "redis-cli ping"
36+
--health-interval 10s
37+
--health-timeout 5s
38+
--health-retries 5
39+
40+
env:
41+
DATABASE_URL: postgresql://devcard:devcard@localhost:5432/devcard
42+
REDIS_URL: redis://localhost:6379
43+
JWT_SECRET: ci-test-secret-not-for-production-ci-test-secret-not-for-production
44+
ENCRYPTION_KEY: 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
45+
NODE_ENV: development
46+
PORT: '3000'
47+
PUBLIC_APP_URL: http://localhost:5173
48+
49+
steps:
50+
- uses: actions/checkout@v4
51+
52+
- uses: actions/setup-node@v4
53+
with:
54+
node-version: 22
55+
56+
- name: Install shared dependencies
57+
run: npm --prefix packages/shared install
58+
59+
- name: Install backend dependencies
60+
run: npm --prefix apps/backend install
61+
62+
- name: Run API tests
63+
run: bash ./.github/scripts/run-api-tests.sh

0 commit comments

Comments
 (0)