# API Key Management Integration Tests ## Test Environment Setup ### Prerequisites ```bash # Start services sudo launchctl load /Library/LaunchDaemons/com.momentry.postgresql.plist sudo launchctl load /Library/LaunchDaemons/com.momentry.redis.plist # Set environment variables export DATABASE_URL="postgres://accusys@localhost:5432/momentry" export REDIS_URL="redis://:accusys@localhost:6379" export GITEA_URL="http://localhost:3000" export N8N_URL="https://n8n.momentry.ddns.net" ``` ### Run Tests ```bash # Run all unit tests cargo test --lib # Run API key specific tests cargo test --lib api_key # Run with output cargo test --lib -- --nocapture ``` --- ## Test Cases ### 1. API Key Creation ```bash # Test: Create a service key momentry api-key create test-key --key-type service --ttl 90 # Expected Output: # ✅ API Key created successfully! # Key ID: msvc_... # API Key: msvc_... # Expires: 2026-06-19 ``` ### 2. API Key Validation ```bash # Test: Validate the created key momentry api-key validate --key "msvc_..." # Expected Output: # ✅ API Key is valid # Key ID: msvc_... # Name: test-key # Type: service ``` ### 3. API Key Listing ```bash # Test: List all keys momentry api-key list # Expected Output: # 📋 API Key List # ┌────────────────────────────────────────────────────────────────────────────┐ # │ Status │ Name │ Type │ Usage │ Last Used │ # ├────────────────────────────────────────────────────────────────────────────┤ # │ ✓ active │ test-key │ "service" │ 0 │ never │ # └────────────────────────────────────────────────────────────────────────────┘ ``` ### 4. API Key Statistics ```bash # Test: Show statistics momentry api-key stats # Expected Output: # 📊 API Key Statistics # ┌─────────────────────────────────────────┐ # │ Total Keys: 1 │ # │ Active Keys: 1 │ # │ Expired Keys: 0 │ # └─────────────────────────────────────────┘ ``` ### 5. Gitea Token Creation ```bash # Test: Create Gitea token momentry gitea create \ --username admin \ --password "Test3200Test3200Test3200" \ --token-name "test-token" \ --scopes "read:repository,write:repository" # Expected Output: # ✅ Gitea Token created successfully! # Token ID: ... # SHA1: ... ``` ### 6. n8n API Key Creation ```bash # Test: Create n8n API key momentry n8n create \ --api-key "existing-n8n-key" \ --label "test-key" \ --expires-in-days 90 # Expected Output: # ✅ n8n API Key created successfully! # Key ID: ... # API Key: ... ``` --- ## Automated Test Script ```bash #!/bin/bash # integration_test.sh set -e echo "=== API Key Integration Tests ===" # 1. Create API key echo "1. Testing API key creation..." momentry api-key create integration-test --key-type service --ttl 30 echo "✅ API key created" # 2. List keys echo "2. Testing API key listing..." momentry api-key list echo "✅ API key list OK" # 3. Show stats echo "3. Testing statistics..." momentry api-key stats echo "✅ Statistics OK" # 4. Test Gitea integration echo "4. Testing Gitea integration..." GITEA_URL="http://localhost:3000" \ momentry gitea list --username admin --password "Test3200Test3200Test3200" echo "✅ Gitea integration OK" echo "" echo "=== All Tests Passed ===" ``` --- ## Unit Test Coverage | Module | Tests | Status | |--------|-------|--------| | `models.rs` | 0 | ✅ | | `service.rs` | 5 | ✅ | | `validator.rs` | 2 | ✅ | | `gitea.rs` | 3 | ✅ | | `n8n.rs` | 2 | ✅ | | `rotation.rs` | 4 | ✅ | | `anomaly.rs` | 0 | ✅ | | `blacklist.rs` | 5 | ✅ | | `encryption.rs` | 2 | ✅ | | `webhook.rs` | 2 | ✅ | | `error.rs` | 3 | ✅ | | `report.rs` | 1 | ✅ | | `cleanup.rs` | 1 | ✅ | | **Total** | **30** | **✅** | --- ## CI/CD Integration ### GitHub Actions / Gitea Actions ```yaml name: API Key Tests on: [push, pull_request] jobs: test: runs-on: ubuntu-latest services: postgres: image: postgres:15 env: POSTGRES_USER: accusys POSTGRES_DB: momentry_test ports: - 5432:5432 redis: image: redis:7 ports: - 6379:6379 steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@stable - run: cargo test --lib api_key ``` --- ## Troubleshooting ### Common Issues 1. **Database connection failed** ```bash # Check PostgreSQL status pg_isready -h localhost -p 5432 ``` 2. **Redis connection failed** ```bash # Check Redis status redis-cli -a accusys ping ``` 3. **Gitea authentication failed** ```bash # Verify credentials curl -u admin:password http://localhost:3000/api/v1/user ```