# Integration Test Example

## Complete Flow Test

### Step 1: Create Client
```bash
curl -X POST http://localhost:8000/api/admin/clients \
  -H "Content-Type: application/json" \
  -d '{"name":"Acme Corporation"}'
```

**Response:**
```json
{
  "message": "Client created successfully",
  "data": {
    "id": 1,
    "name": "Acme Corporation",
    "api_key": "face_T3j4R8eO73bvwGfCnxGUQvIo9biRzeWK7aV8QmRx",
    "is_active": true,
    "created_at": "2026-02-07T10:00:00.000000Z",
    "updated_at": "2026-02-07T10:00:00.000000Z"
  }
}
```

### Step 2: Register Face
```bash
curl -X POST http://localhost:8000/api/face/register \
  -H "X-API-KEY: face_T3j4R8eO73bvwGfCnxGUQvIo9biRzeWK7aV8QmRx" \
  -H "Content-Type: application/json" \
  -d '{
    "reference_id": "user_12345",
    "image": "/9j/4AAQSkZJRgABAQEAYABgAAD..."
  }'
```

**Success Response (from Python):**
```json
{
  "success": true,
  "reference_id": "user_12345",
  "message": "Face registered successfully"
}
```

### Step 3: Verify Face
```bash
curl -X POST http://localhost:8000/api/face/verify \
  -H "X-API-KEY: face_T3j4R8eO73bvwGfCnxGUQvIo9biRzeWK7aV8QmRx" \
  -H "Content-Type: application/json" \
  -d '{
    "reference_id": "user_12345",
    "image": "/9j/4AAQSkZJRgABAQEAYABgAAD..."
  }'
```

**Success Response (from Python):**
```json
{
  "success": true,
  "match": true,
  "confidence": 0.95,
  "reference_id": "user_12345"
}
```

### Step 4: Test Error Cases

#### No API Key
```bash
curl -X POST http://localhost:8000/api/face/verify \
  -H "Content-Type: application/json" \
  -d '{"reference_id":"user_123","image":"data"}'
```
**Response: 401**
```json
{
  "error": "API key required"
}
```

#### Invalid API Key
```bash
curl -X POST http://localhost:8000/api/face/verify \
  -H "X-API-KEY: invalid_key" \
  -H "Content-Type: application/json" \
  -d '{"reference_id":"user_123","image":"data"}'
```
**Response: 401**
```json
{
  "error": "Invalid API key"
}
```

#### Missing Fields
```bash
curl -X POST http://localhost:8000/api/face/verify \
  -H "X-API-KEY: face_T3j4R8eO73bvwGfCnxGUQvIo9biRzeWK7aV8QmRx" \
  -H "Content-Type: application/json" \
  -d '{"reference_id":"user_123"}'
```
**Response: 422**
```json
{
  "message": "The image field is required.",
  "errors": {
    "image": ["The image field is required."]
  }
}
```

#### Python Service Down
```bash
# Stop Python service, then:
curl -X POST http://localhost:8000/api/face/verify \
  -H "X-API-KEY: face_T3j4R8eO73bvwGfCnxGUQvIo9biRzeWK7aV8QmRx" \
  -H "Content-Type: application/json" \
  -d '{"reference_id":"user_123","image":"data"}'
```
**Response: 503**
```json
{
  "error": "Face engine unavailable"
}
```

### Step 5: Revoke Client
```bash
curl -X PATCH http://localhost:8000/api/admin/clients/1/status \
  -H "Content-Type: application/json" \
  -d '{"is_active":false}'
```

### Step 6: Test Revoked Key
```bash
curl -X POST http://localhost:8000/api/face/verify \
  -H "X-API-KEY: face_T3j4R8eO73bvwGfCnxGUQvIo9biRzeWK7aV8QmRx" \
  -H "Content-Type: application/json" \
  -d '{"reference_id":"user_123","image":"data"}'
```
**Response: 401**
```json
{
  "error": "Invalid API key"
}
```

## Test Summary

| Test Case | Expected Result | Status Code |
|-----------|----------------|-------------|
| Valid request | Python response | 200/4xx |
| No API key | Error message | 401 |
| Invalid API key | Error message | 401 |
| Revoked API key | Error message | 401 |
| Missing fields | Validation error | 422 |
| Python down | Service unavailable | 503 |

## Automated Test Script

```bash
#!/bin/bash

# Create client
RESPONSE=$(curl -s -X POST http://localhost:8000/api/admin/clients \
  -H "Content-Type: application/json" \
  -d '{"name":"Test Client"}')

API_KEY=$(echo "$RESPONSE" | jq -r '.data.api_key')

echo "Testing with API Key: $API_KEY"

# Test register
echo "Test 1: Register face"
curl -s -X POST http://localhost:8000/api/face/register \
  -H "X-API-KEY: $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"reference_id":"user_123","image":"test_data"}' | jq

# Test verify
echo "Test 2: Verify face"
curl -s -X POST http://localhost:8000/api/face/verify \
  -H "X-API-KEY: $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"reference_id":"user_123","image":"test_data"}' | jq

# Test missing field
echo "Test 3: Missing field"
curl -s -X POST http://localhost:8000/api/face/register \
  -H "X-API-KEY: $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"reference_id":"user_123"}' | jq

# Test no API key
echo "Test 4: No API key"
curl -s -X POST http://localhost:8000/api/face/register \
  -H "Content-Type: application/json" \
  -d '{"reference_id":"user_123","image":"data"}' | jq
```
