This project provides a test suite and Postman collection for testing Keycloak authentication flows.
- Docker and Docker Compose
- Python 3.12 or higher
- Poetry (Python package manager)
- Postman (for API testing)
.
├── docker-compose.yml # Docker configuration for Keycloak and PostgreSQL
├── .env # Environment variables
├── tests/
│ └── test_auth.py # Python test suite
└── postman/
├── postman-collection.json # Postman API collection
└── postman-environment.json # Postman environment variables
KEYCLOAK_URL=http://localhost:8080
KEYCLOAK_ADMIN=admin
KEYCLOAK_ADMIN_PASSWORD=admin
TEST_REALM_NAME=test-realm
TEST_CLIENT_ID=test-client
TEST_USERNAME=test-user
TEST_PASSWORD=test-password
- Install dependencies:
poetry install
- Start Keycloak:
docker compose up -d
- Wait for Keycloak to be ready (about 30 seconds)
Run the Python test suite:
poetry run pytest tests/test_auth.py -v
-
Import both files from the
postman
directory into Postman:postman-collection.json
postman-environment.json
-
Select the "Keycloak Local" environment
-
Run the requests in order:
- Get Admin Token
- Create Realm
- Create Client
- Get Clients (gets client UUID)
- Generate Client Secret
- Create User
- Password Grant Token
- Client Credentials Token
- Refresh Token
The collection includes test scripts that automatically set environment variables from responses.
-
Password Grant Flow
- Used for direct user authentication
- Requires username and password
- Returns access and refresh tokens
-
Client Credentials Flow
- Used for service-to-service authentication
- Requires client_id and client_secret
- Returns access token
-
Refresh Token Flow
- Used to get new access token using refresh token
- Requires refresh token from password grant