#!/usr/bin/env bash set -euo pipefail BASE_URL="http://127.0.0.1:3000" ACCESS_TOKEN="" REFRESH_TOKEN="" # Colors for output GREEN='\033[0;32m' RED='\033[0;31m' NC='\033[0m' log() { echo -e "${GREEN}[$(date '+%H:%M:%S')]${NC} $1" } error() { echo -e "${RED}[ERROR]${NC} $1" } # 1. Health check log "Health check" curl -fsS "$BASE_URL/health" | jq . # 2. Auth: login as admin log "Login as admin" LOGIN_RESP=$(curl -fsS -X POST "$BASE_URL/auth/login" \ -H "Content-Type: application/json" \ -d '{"email":"admin@crm.local","password":"admin123"}') ACCESS_TOKEN=$(echo "$LOGIN_RESP" | jq -r .accessToken) REFRESH_TOKEN=$(echo "$LOGIN_RESP" | jq -r .refreshToken) if [[ -z "$ACCESS_TOKEN" || "$ACCESS_TOKEN" == "null" ]]; then error "Login failed" exit 1 fi log "Access token obtained" # Helper for authenticated requests auth_get() { curl -fsS -H "Authorization: Bearer $ACCESS_TOKEN" "$BASE_URL$1" } auth_post() { curl -fsS -X POST -H "Authorization: Bearer $ACCESS_TOKEN" -H "Content-Type: application/json" "$BASE_URL$1" -d "$2" } # 3. Users: list (admin only) log "List users" auth_get "/users" | jq . # 4. Contractors: list log "List contractors" auth_get "/contractors" | jq . # 5. Create contractor (admin/director/mechanic) log "Create contractor" auth_post "/contractors" '{"name":"Test Contractor","address":"Test address"}' | jq . # 6. Units: list log "List units" auth_get "/units" | jq . # 7. Create unit and identifier log "Create unit" UNIT_RESP=$(auth_post "/units" '{}') UNIT_ID=$(echo "$UNIT_RESP" | jq -r .id) if [[ -z "$UNIT_ID" || "$UNIT_ID" == "null" ]]; then error "Unit create failed" echo "$UNIT_RESP" | jq . exit 1 fi log "Unit created: $UNIT_ID" log "Add identifier to unit" auth_post "/units/$UNIT_ID/identifiers" '{"type":"SERIAL","value":"TESTSN001"}' | jq . # 8. RepairOrders: list log "List repair orders" auth_get "/repair-orders" | jq . # 9. Create repair order (worker+) log "Create repair order" FIRST_CONTRACTOR_ID=$(auth_get "/contractors" | jq -r '.[0].id') if [[ -z "$FIRST_CONTRACTOR_ID" || "$FIRST_CONTRACTOR_ID" == "null" ]]; then error "No contractors found" exit 1 fi REPAIR_RESP=$(auth_post "/repair-orders" '{"unitId":"'$UNIT_ID'","contractorId":"'$FIRST_CONTRACTOR_ID'","description":"Test repair"}') REPAIR_ID=$(echo "$REPAIR_RESP" | jq -r .id) if [[ -z "$REPAIR_ID" || "$REPAIR_ID" == "null" ]]; then error "Repair order create failed" echo "$REPAIR_RESP" | jq . exit 1 fi log "Repair order created: $REPAIR_ID" # 10. Trips: list log "List trips" auth_get "/trips" | jq . # 11. Create trip (worker+) log "Create trip" auth_post "/trips" '{"contractorId":"'$FIRST_CONTRACTOR_ID'","scheduledFor":"'$(date -d '+1 day' -Iseconds)'","description":"Test trip"}' | jq . # 12. Calculations: list log "List calculations" auth_get "/calculations" | jq . # 13. Create calculation (mechanic+) log "Create calculation" auth_post "/calculations" '{"repairOrderId":"'$REPAIR_ID'"}' | jq . # 14. Refresh token log "Refresh token" REFRESH_RESP=$(curl -fsS -X POST "$BASE_URL/auth/refresh" \ -H "Content-Type: application/json" \ -d "{\"refreshToken\":\"$REFRESH_TOKEN\"}") NEW_ACCESS=$(echo "$REFRESH_RESP" | jq -r .accessToken) if [[ -z "$NEW_ACCESS" || "$NEW_ACCESS" == "null" ]]; then error "Refresh failed" exit 1 fi ACCESS_TOKEN="$NEW_ACCESS" log "Token refreshed" log "All tests passed"