# PP-1384: PDP SDK 401 Error Handling UATs

This repository contains User Acceptance Tests (UATs) for the PDP SDK 401 error handling functionality in both Python
and TypeScript.

## Purpose

These UAT scripts verify that:

- Authentication errors (401) are **always** raised as `UnauthenticatedException`, regardless of the
  `raise_when_unauthorized`/`raiseWhenUnauthorized` flag
- Authorization errors (403) are raised as `UnauthorizedException` only when the flag is set to `True`/`true`
- When the flag is `False`/`false`, authorization failures return `False` instead of raising an exception


## Quick Start

### Python

```bash
cd python
cp .env.example .env
# Edit .env and add your REAL_TOKEN (optional)
make test
```

### TypeScript

```bash
cd typescript
cp .env.example .env
# Edit .env and add your GITHUB_TOKEN and REAL_TOKEN (optional)
npm install
npm test
```

## Environment Variables

Both scripts automatically load environment variables from `.env` files in their respective directories:
- **Python** uses the `environs` library
- **TypeScript** uses the `dotenv` library

**Required for TypeScript:**
- **`GITHUB_TOKEN`** - Required for accessing the private npm registry at `https://npm.pkg.github.com`

**Optional for both:**
- **`REAL_TOKEN`** - A valid authentication token for testing successful authorization scenarios. If not set, those tests will be skipped.
    - For Python: Should include "Bearer " prefix (e.g., `Bearer eyJhbGc...`)
    - For TypeScript: Just the token value (e.g., `eyJhbGc...`)

### Setup

Create `.env` files from the provided templates:

```bash
# Python
cd python
cp .env.example .env
# Edit .env and add your REAL_TOKEN

# TypeScript
cd typescript
cp .env.example .env
# Edit .env and add your GITHUB_TOKEN and REAL_TOKEN
```

The `.env` files will be automatically loaded when you run the tests. No need to manually export variables.

## Test Scenarios

Both scripts test the following scenarios:

1. No token provided, `raise_when_unauthorized=false`
2. Invalid/bad token, `raise_when_unauthorized=false`
3. Valid token, `raise_when_unauthorized=false` (requires `REAL_TOKEN`)
4. No token provided, `raise_when_unauthorized=true`
5. Invalid/bad token, `raise_when_unauthorized=true`
6. Valid token, `raise_when_unauthorized=true` (requires `REAL_TOKEN`)

## Expected Results

✓ **Authentication errors (401)** should always raise an exception:

- Python: `UnauthenticatedException`
- TypeScript: `UnauthenticatedException`

✓ **Authorization errors (403)** should only raise when `raise_when_unauthorized=True`:

- Python: `UnauthorizedException`
- TypeScript: `UnauthorizedException`

✓ **When `raise_when_unauthorized=False`**, authorization failures return `False` instead of raising.

