Ir para o conteúdo

MundiX Test Instructions

Quick Start

cd /opt/mundix
./run_tests.sh

This runs the full test suite in isolated Docker containers with: - ✅ Postgres 16 (test database) - ✅ Redis 7 (test cache) - ✅ 27 authentication tests - ✅ Coverage report

Expected output: ✅ All tests passed!


What Gets Tested

1. User Registration (5 tests)

  • ✅ Successful registration
  • ✅ Duplicate username rejection
  • ✅ Duplicate email rejection
  • ✅ Invalid email format
  • ✅ Weak password rejection

2. Login (4 tests)

  • ✅ Successful login with tokens
  • ✅ Invalid username rejection
  • ✅ Invalid password rejection
  • ✅ Inactive user rejection

3. Refresh Token Flow (5 tests)

  • ✅ Valid refresh returns new tokens
  • ✅ Token rotation (old token revoked)
  • ✅ Invalid token rejection
  • ✅ Expired token rejection
  • ✅ Revoked token rejection

4. Logout (3 tests)

  • ✅ Successful logout revokes token
  • ✅ Post-logout refresh fails
  • ✅ Invalid token rejection

5. Protected Endpoints (5 tests)

  • ✅ Valid token accesses /auth/me
  • ✅ Missing token rejected
  • ✅ Invalid token rejected
  • ✅ Expired token rejected
  • ✅ Malformed token rejected

6. Security (3 tests)

  • ✅ No user enumeration in login errors
  • ✅ Password never returned in responses
  • ✅ Refresh token single-use enforced

7. Admin Protection (2 tests)

  • ✅ Admin users can be created
  • ✅ Regular users are not admins by default

Total: 27 tests


Manual Testing

Prerequisites

# Install dependencies
pip install pytest pytest-cov httpx

# Or use Docker (recommended)
docker pull postgres:16
docker pull redis:7-alpine

Run Tests Locally

# 1. Start test services
docker run -d --name mundix-test-db \
  -e POSTGRES_USER=mundix_test \
  -e POSTGRES_PASSWORD=test \
  -e POSTGRES_DB=mundix_test \
  -p 5433:5432 postgres:16

docker run -d --name mundix-test-redis \
  -p 6380:6379 redis:7-alpine

# 2. Set environment
export REGISTRY_POSTGRES_URL=postgresql://mundix_test:test@localhost:5433/mundix_test
export REDIS_URL=redis://localhost:6380/0
export ORCHESTRATOR_API_SECRET_KEY=test-secret-key-minimum-32-characters

# 3. Run tests
cd /opt/mundix/orchestrator
pytest tests/test_auth.py -v

# 4. With coverage
pytest tests/test_auth.py -v --cov=api/auth --cov=common/auth --cov-report=html

# 5. Cleanup
docker rm -f mundix-test-db mundix-test-redis

Test Output Examples

Success

tests/test_auth.py::TestUserRegistration::test_register_success PASSED       [ 3%]
tests/test_auth.py::TestUserRegistration::test_register_duplicate_username PASSED [ 7%]
...
tests/test_auth.py::TestSecurityConsiderations::test_refresh_token_single_use PASSED [100%]

============================== 27 passed in 2.34s ===============================

Coverage report:
  api/auth.py         234    12    95%
  common/auth.py      89     3     97%
  ----------------------------------------
  TOTAL              323    15    95%

Failure Example

tests/test_auth.py::TestLogin::test_login_success FAILED                     [14%]

FAILED tests/test_auth.py::TestLogin::test_login_success - AssertionError: assert 500 == 200
  Response body: {"detail": "Internal server error"}

Troubleshooting

Issue: Tests fail with "Connection refused"

Cause: Database not ready

Solution:

# Check if Postgres is running
docker ps | grep mundix-test-db

# Check health
docker exec mundix-test-db pg_isready

# Wait for startup
sleep 5 && pytest tests/

Issue: Tests fail with "Redis connection error"

Cause: Redis not available

Solution:

# Check Redis
docker ps | grep mundix-test-redis

# Test connection
docker exec mundix-test-redis redis-cli ping
# Should return: PONG

# Check REDIS_URL env var
echo $REDIS_URL

Issue: "Import error: No module named X"

Cause: Missing dependencies

Solution:

pip install -r orchestrator/requirements.txt
pip install pytest pytest-cov httpx

Issue: Tests pass locally but fail in Docker

Cause: Environment variable mismatch

Solution:

# Check Docker env vars
docker-compose -f infra/agents/docker-compose.test.yml config

# Verify DATABASE_URL in container
docker exec mundix-test-runner env | grep POSTGRES


Coverage Targets

Module Target Current
api/auth.py 90% 95%+ ✅
common/auth.py 90% 97%+ ✅
common/rate_limit.py 80% TBD
worker/cleanup.py 80% TBD

Overall target: 85%+


CI/CD Integration

GitHub Actions

# .github/workflows/test.yml
name: Tests

on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3

      - name: Build and test
        run: |
          cd /opt/mundix
          docker-compose -f infra/agents/docker-compose.test.yml up --abort-on-container-exit --build

      - name: Upload coverage
        uses: codecov/codecov-action@v3
        with:
          files: ./test-results/coverage.xml

GitLab CI

# .gitlab-ci.yml
test:
  image: docker:latest
  services:
    - docker:dind
  script:
    - cd /opt/mundix
    - docker-compose -f infra/agents/docker-compose.test.yml up --abort-on-container-exit --build
  artifacts:
    reports:
      junit: test-results/junit.xml
      coverage_report:
        coverage_format: cobertura
        path: test-results/coverage.xml

Next Steps

After tests pass:

  1. Review coverage report:

    firefox test-results/htmlcov/index.html
    

  2. Add more tests (optional):

  3. Rate limiting tests
  4. Concurrent refresh tests
  5. Cleanup job tests

  6. Run in staging:

    docker-compose -f infra/agents/docker-compose.yml up -d
    

  7. Monitor production:

  8. 429 error rates
  9. Token table growth
  10. Redis memory usage

Support

Issues with tests? Check: 1. ✅ Docker daemon running 2. ✅ Ports 5433, 6380 available 3. ✅ Environment variables set 4. ✅ Dependencies installed

Still stuck? Review logs:

docker-compose -f infra/agents/docker-compose.test.yml logs test-runner