db(couchdb): auto-provision databases on startup for production strategy; add TokenService with CouchDB-backed token storage and localStorage fallback; switch OAuth to unified config for client IDs and redirect URI; express Request typing for req.user; align exportAsEnvVars with show-config expectations; remove Vite importmap from index.html; prefer babel-jest over ts-jest; remove duplicate uuid mocking from Jest config

This commit is contained in:
William Valentin
2025-09-09 12:30:38 -07:00
parent 062e0973c1
commit 15170a4f43
17 changed files with 1097 additions and 67 deletions

View File

@@ -25,7 +25,9 @@ services/
└── auth/
└── __tests__/ # Unit tests for authentication services
├── auth.integration.test.ts
── emailVerification.test.ts
── emailVerification.test.ts
└── integration/
└── token.service.integration.test.ts
```
## Running Tests
@@ -68,6 +70,79 @@ bun run test:e2e:debug
bun run test:e2e:report
```
#### TokenService CouchDB Integration
##### Using Docker Compose locally
You can spin up a local CouchDB for integration tests using the included docker-compose file:
```bash
docker compose -f meds/docker-compose.ci.yml up -d couchdb
export VITE_COUCHDB_URL=http://localhost:5984
export VITE_COUCHDB_USER=admin
export VITE_COUCHDB_PASSWORD=password
bun run test:integration
```
- The compose file is intended for local/CI testing only.
- Data is stored in a named volume (couchdb-test-data) and can be removed with:
- docker compose -f meds/docker-compose.ci.yml down -v
##### Optional CI workflow
An optional GitHub Actions workflow exists to run these CouchDB-backed tests:
- Name: Integration Tests (CouchDB)
- Triggers:
- Manually via “Run workflow” (workflow_dispatch), or
- Automatically if repository variable RUN_COUCHDB_INTEGRATION is set to "true"
- It provisions a CouchDB service and runs:
- bun run test:integration
To enable automatic runs, set the repository variable RUN_COUCHDB_INTEGRATION to "true" in your repo settings. You can also override credentials via:
- VITE_COUCHDB_URL (defaults to http://couchdb:5984 in CI)
- VITE_COUCHDB_USER (defaults to admin)
- VITE_COUCHDB_PASSWORD (defaults to password if not set as a secret)
Integration tests for the TokenService talk to a live CouchDB if available. They automatically skip when CouchDB is not reachable.
```bash
# Run TokenService integration tests (requires CouchDB at VITE_COUCHDB_URL)
# Defaults: VITE_COUCHDB_URL=http://localhost:5984, VITE_COUCHDB_USER=admin, VITE_COUCHDB_PASSWORD=password
VITE_COUCHDB_URL=http://localhost:5984 \
VITE_COUCHDB_USER=admin \
VITE_COUCHDB_PASSWORD=password \
bun run test meds/services/auth/__tests__/integration/token.service.integration.test.ts
```
- Provide your own CouchDB credentials via environment variables if different from the defaults above.
- These tests will provision an `auth_tokens` database automatically and clean up test data as part of the lifecycle.
#### OAuth Redirect Configuration
OAuth redirect URIs are derived from the unified configuration:
- Redirect URI = `${APP_BASE_URL}/auth/callback`
- Development default: `APP_BASE_URL=http://localhost:5173`
- Test default: `APP_BASE_URL=http://localhost:3000`
To exercise OAuth URL construction with your own IDs, set:
- `VITE_GOOGLE_CLIENT_ID`
- `VITE_GITHUB_CLIENT_ID`
Example:
```bash
APP_BASE_URL=http://localhost:5173 \
VITE_GOOGLE_CLIENT_ID=your-google-client-id \
VITE_GITHUB_CLIENT_ID=your-github-client-id \
bun run dev
```
### Manual Testing Scripts
#### Admin Login Debug
@@ -122,7 +197,7 @@ bun tests/manual/auth-db-debug.js
### Jest Configuration (`jest.config.json`)
- TypeScript support with ts-jest
- TypeScript transformed via babel-jest (@babel/preset-typescript); import.meta handled in Babel config
- jsdom environment for DOM testing
- Coverage reporting
- Module path mapping