# DMP Artists Participants API Test Script

Python test script for the new `/external/roster` endpoint.

## Prerequisites

```bash
pip install requests
```

Or with requirements file:
```bash
pip install -r requirements.txt
```

## Setup

### 1. Get Auth0 Credentials

You need Auth0 **Machine-to-Machine** credentials for QA environment:

- **Client ID**: Get from Auth0 dashboard or team
- **Client Secret**: Get from Auth0 dashboard or team
- **Audience**: `https://qa-fan-response-jwt-authorizer` (already configured)
- **Domain**: `qa-orchard.auth0.com` (already configured)

Ask the Fansifter team or check AWS Secrets Manager for Auth0 credentials.

### 2. Set Environment Variables

```bash
export AUTH0_CLIENT_ID='your-client-id-here'
export AUTH0_CLIENT_SECRET='your-client-secret-here'
```

Optional variables (have defaults):
```bash
export AUTH0_DOMAIN='qa-orchard.auth0.com'
export AUTH0_AUDIENCE='https://qa-fan-response-jwt-authorizer'
export DMP_PARTICIPANTS_API_URL='https://qa-fan-response-api.theorchard.io/external/roster'
```

## Usage

### Basic Run

```bash
python test_dmp_participants.py
```

### Run with Custom Parameters

Edit the script's `main()` function to customize test parameters:

```python
# Example: Filter by vendor
result = api_client.get_participants(
    vendor_id=123,
    limit=20
)

# Example: Search for specific artist
result = api_client.get_participants(
    search="taylor",
    limit=10,
    offset=0
)

# Example: Pagination
result = api_client.get_participants(
    limit=50,
    offset=100
)
```

## API Parameters

All parameters are optional:

- **vendorId** (int): Account vendor id
- **subaccountId** (int): Account subaccount id
- **search** (string): Search by partial name match
- **limit** (int, 1-200): Page size
- **offset** (int, ≥0): Page offset

## Expected Output

```
============================================================
DMP Artists Participants API Test
============================================================
Auth0 Domain:  qa-orchard.auth0.com
Client ID:     abc12345...
Audience:      https://qa-fan-response-jwt-authorizer
API URL:       https://qa-fan-response-api.theorchard.io/external/roster
============================================================

Requesting new JWT from Auth0...
✓ Received new JWT from Auth0 (expires in 86400s)

============================================================
Test 1: Get first 10 participants
============================================================

→ Calling https://qa-fan-response-api.theorchard.io/external/roster
  Parameters: {'limit': 10, 'offset': 0}
← Response status: 200
✓ Success! Total items: 1234

Items returned: 10
Total count: 1234
Limit: 10
Offset: 0

First item sample:
  id: 12345
  name: Artist Name
  ...
```

## Troubleshooting

### Error: Unauthorized (401)

- Token expired (should auto-refresh)
- Invalid Auth0 credentials
- Check `AUTH0_CLIENT_ID` and `AUTH0_CLIENT_SECRET`

### Error: Forbidden (403)

- **IP not whitelisted**: Only SMF IPs can access this endpoint
- Check if your IP is in the WAF allow list (`qa/lambda-fan-response/waf.tf`)
- You need to run this from an allowed IP address

### Error: 500 Internal Server Error

- Backend issue with OWS DMP service
- Script will auto-retry with exponential backoff
- Check backend logs in DataDog

## Security Notes

⚠️ **Never commit credentials to git!**

- Always use environment variables
- Add `.env` files to `.gitignore`
- For CI/CD, use AWS Secrets Manager or parameter store

## Features

✅ Auto token refresh (cached for 24h)
✅ Exponential backoff retry for 5xx errors
✅ Comprehensive error handling
✅ Multiple test scenarios
✅ Clear output formatting

## Architecture

```
Script → Auth0 (get JWT token)
       ↓
Script → API Gateway (with JWT)
       ↓
API Gateway → WAF (IP check: SMF + NAT)
       ↓
API Gateway → Authorizer Lambda (validate JWT)
       ↓
API Gateway → VPC Link → NLB → ALB → OWS DMP Backend
```

## Files

- `test_dmp_participants.py` - Main test script
- `requirements.txt` - Python dependencies
- `TEST_README.md` - This file