133 lines
4.0 KiB
Markdown
133 lines
4.0 KiB
Markdown
# Backup Service Tests
|
|
|
|
## Test Structure
|
|
|
|
```
|
|
test/
|
|
├── unit/ # Unit tests (no external dependencies)
|
|
│ ├── api/ # Controller tests
|
|
│ ├── application/ # Handler tests
|
|
│ ├── domain/ # Entity and value object tests
|
|
│ ├── infrastructure/ # Service tests
|
|
│ └── shared/ # Guard, filter, interceptor tests
|
|
├── integration/ # Integration tests (mocked DB)
|
|
├── e2e/ # End-to-end tests
|
|
│ ├── backup-share-mock.e2e-spec.ts # Mock DB E2E tests
|
|
│ └── backup-share.e2e-spec.ts # Real DB E2E tests
|
|
├── setup/ # Test setup files
|
|
│ ├── global-setup.ts # DB container setup
|
|
│ ├── global-teardown.ts # DB container cleanup
|
|
│ ├── jest-e2e-setup.ts # Real DB test setup
|
|
│ ├── jest-mock-setup.ts # Mock test setup
|
|
│ └── test-database.helper.ts # DB helper utilities
|
|
└── utils/ # Test utilities
|
|
├── mock-prisma.service.ts # Mock Prisma service
|
|
└── test-utils.ts # Helper functions
|
|
```
|
|
|
|
## Running Tests
|
|
|
|
### Unit Tests
|
|
```bash
|
|
npm run test:unit
|
|
```
|
|
Runs 37 unit tests. No external dependencies required.
|
|
|
|
### Mock E2E Tests
|
|
```bash
|
|
npm run test:e2e:mock
|
|
# or
|
|
npm run test:e2e
|
|
```
|
|
Runs 21 E2E tests with mocked database. No Docker or PostgreSQL required.
|
|
|
|
### All Tests (Unit + Mock E2E)
|
|
```bash
|
|
npm run test:all
|
|
```
|
|
Runs all 58 tests that don't require a real database.
|
|
|
|
### Real Database E2E Tests
|
|
|
|
#### Option 1: With Docker Desktop
|
|
```bash
|
|
# Ensure Docker Desktop is running
|
|
npm run test:e2e:db
|
|
```
|
|
This will:
|
|
1. Start PostgreSQL container (port 5434)
|
|
2. Push Prisma schema
|
|
3. Run 32 E2E tests
|
|
4. Stop and cleanup container
|
|
|
|
#### Option 2: With Existing PostgreSQL
|
|
```bash
|
|
# 1. Create a test database
|
|
psql -c "CREATE DATABASE rwa_backup_test;"
|
|
|
|
# 2. Update DATABASE_URL in .env.test
|
|
# DATABASE_URL="postgresql://user:password@localhost:5432/rwa_backup_test"
|
|
|
|
# 3. Setup database schema
|
|
npm run db:test:setup
|
|
|
|
# 4. Run tests
|
|
npm run test:e2e:db:manual
|
|
```
|
|
|
|
## Test Commands
|
|
|
|
| Command | Description |
|
|
|---------|-------------|
|
|
| `npm run test` | Run all Jest tests |
|
|
| `npm run test:unit` | Run unit tests only |
|
|
| `npm run test:e2e` | Run mock E2E tests |
|
|
| `npm run test:e2e:mock` | Run mock E2E tests |
|
|
| `npm run test:e2e:db` | Run real DB E2E tests (Docker) |
|
|
| `npm run test:e2e:db:manual` | Run real DB E2E tests (existing DB) |
|
|
| `npm run test:all` | Run unit + mock E2E tests |
|
|
| `npm run test:cov` | Run tests with coverage |
|
|
| `npm run db:test:up` | Start test DB container |
|
|
| `npm run db:test:down` | Stop test DB container |
|
|
| `npm run db:test:setup` | Setup existing DB for tests |
|
|
|
|
## Test Environment Variables
|
|
|
|
Located in `.env.test`:
|
|
|
|
```env
|
|
DATABASE_URL="postgresql://postgres:testpassword@localhost:5434/rwa_backup_test"
|
|
APP_PORT=3003
|
|
APP_ENV="test"
|
|
SERVICE_JWT_SECRET="test-super-secret-service-jwt-key-for-e2e-testing"
|
|
ALLOWED_SERVICES="identity-service,recovery-service"
|
|
BACKUP_ENCRYPTION_KEY="0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
|
|
BACKUP_ENCRYPTION_KEY_ID="test-key-v1"
|
|
```
|
|
|
|
## Test Utilities
|
|
|
|
### generateServiceToken(service, secret?)
|
|
Generates a JWT service token for testing authenticated endpoints.
|
|
|
|
### createStoreSharePayload(overrides?)
|
|
Creates a valid store share request payload.
|
|
|
|
### createRetrieveSharePayload(overrides?)
|
|
Creates a valid retrieve share request payload.
|
|
|
|
### createRevokeSharePayload(overrides?)
|
|
Creates a valid revoke share request payload.
|
|
|
|
### MockPrismaService
|
|
Full mock implementation of PrismaService for testing without a database.
|
|
|
|
## Coverage Report
|
|
|
|
Generate coverage report:
|
|
```bash
|
|
npm run test:cov
|
|
```
|
|
|
|
Coverage output in `./coverage/` directory.
|