Skip to content

Fix API schema tests #3303

@dborovcanin

Description

@dborovcanin

API tests are failing. You can test locally by installing Schemathesis:

uv venv
uv pip install schemathesis

and start the composition. After that, you can use the following script:

#!/usr/bin/env bash
set -euo pipefail

SCHEMATHESIS=".venv/bin/schemathesis"

# Configuration
USERS_URL="${USERS_URL:-http://localhost:9002}"
TOKENS_URL="${TOKENS_URL:-http://localhost:9002/users/tokens/issue}"
USER_EMAIL="${USER_EMAIL:-admin@example.com}"
USER_USERNAME="${USER_USERNAME:-admin}"
USER_SECRET="${USER_SECRET:-12345678}"

# Register admin user for testing (ignore errors if already exists)
echo "▶ Registering test user..."
REGISTER_RESPONSE=$(curl -sSX POST "${USERS_URL}/users" \
  -H "Content-Type: application/json" \
  -d "{
    \"first_name\": \"Admin\",
    \"last_name\": \"User\",
    \"email\": \"${USER_EMAIL}\",
    \"credentials\": {
      \"username\": \"${USER_USERNAME}\",
      \"secret\": \"${USER_SECRET}\"
    }
  }" 2>&1)

if echo "$REGISTER_RESPONSE" | grep -q '"id"'; then
  echo "✔ User registered successfully"
elif echo "$REGISTER_RESPONSE" | grep -qi "already.*exist\|conflict"; then
  echo "✔ User already exists"
else
  echo "⚠ User registration returned: $REGISTER_RESPONSE"
  echo "  Attempting to continue..."
fi

# Get fresh access token from the API
echo "▶ Obtaining access token..."
USER_TOKEN=$(curl -sSX POST "${TOKENS_URL}" \
  -H "Content-Type: application/json" \
  -d "{\"username\":\"${USER_USERNAME}\",\"password\":\"${USER_SECRET}\"}" |
  jq -r '.access_token')

if [ -z "$USER_TOKEN" ] || [ "$USER_TOKEN" = "null" ]; then
  echo "✘ Failed to obtain access token"
  echo "  Make sure services are running and credentials are correct"
  exit 1
fi

echo "✔ Access token obtained"
echo

# Common arguments for all services
COMMON_ARGS=(
  --checks all
  --exclude-checks=positive_data_acceptance
  --suppress-health-check=filter_too_much
  --header "Authorization: Bearer ${USER_TOKEN}"
  --tls-verify false
)

# Service-specific configurations
# Format: "URL SPEC EXTRA_ARGS"
#
# Notes:
# - requestPasswordReset is excluded because it requires SMTP email service
# - Stateful testing creates resources dynamically, avoiding hardcoded example IDs
# - Examples phase disabled: uses hardcoded UUIDs that don't exist in test DB
declare -A SERVICES=(
  [users]="http://localhost:9002 apidocs/openapi/users.yaml --exclude-operation-id=requestPasswordReset --phases=stateful"
  [domains]="http://localhost:9003 apidocs/openapi/domains.yaml --phases=stateful"
  [groups]="http://localhost:9004 apidocs/openapi/groups.yaml --phases=stateful"
  [channels]="http://localhost:9005 apidocs/openapi/channels.yaml --phases=stateful"
  [clients]="http://localhost:9006 apidocs/openapi/clients.yaml --phases=stateful"
  [auth]="http://localhost:9001 apidocs/openapi/auth.yaml --phases=stateful"
  [journal]="http://localhost:9021 apidocs/openapi/journal.yaml --phases=stateful"
)

FAILED_SERVICES=()

for service in "${!SERVICES[@]}"; do
  read -r URL SPEC EXTRA_ARGS <<<"${SERVICES[$service]}"

  echo "▶ Running Schemathesis for ${service}"
  echo "  URL:  ${URL}"
  echo "  Spec: ${SPEC}"
  echo "  Args: ${EXTRA_ARGS}"

  if ${SCHEMATHESIS} run "${SPEC}" \
    --url "${URL}" \
    "${COMMON_ARGS[@]}" \
    ${EXTRA_ARGS}; then
    echo "${service} passed"
  else
    echo "${service} failed"
    FAILED_SERVICES+=("${service}")
  fi
  echo
done

# Summary
echo "═══════════════════════════════════════════════════════════"
if [ ${#FAILED_SERVICES[@]} -eq 0 ]; then
  echo "✔ All services passed API tests"
  exit 0
else
  echo "✘ Failed services: ${FAILED_SERVICES[*]}"
  exit 1
fi

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions