#!/usr/bin/env bash set -u # MyDreamJob backend RC smoke test. # Required tools: curl, grep, sed, head, tail. # Environment: # BASE_URL API base URL (default: http://127.0.0.1:8080/api/v1) # STUDENT_PHONE Fresh 11-digit phone; generated when omitted # COMPANY_TOKEN Company JWT; company status step is SKIP when omitted # SERVER_LOG Server log path used to read Mock SMS codes BASE_URL="${BASE_URL:-http://127.0.0.1:8080/api/v1}" SERVER_LOG="${SERVER_LOG:-}" PASS=0 FAIL=0 SKIP=0 if [ -n "${STUDENT_PHONE:-}" ]; then PHONE="$STUDENT_PHONE" else # 139 + eight digits = 11 digits. PHONE="139$(date +%s | tail -c 9)" fi say() { printf '%-58s' "$1"; } ok() { echo "[PASS] $1"; PASS=$((PASS + 1)); } fail() { echo "[FAIL] $1"; FAIL=$((FAIL + 1)); } skip() { echo "[SKIP] $1"; SKIP=$((SKIP + 1)); } resolve_log() { case "$SERVER_LOG" in [A-Za-z]:/*) local drive rest drive="$(printf '%s' "${SERVER_LOG:0:1}" | tr '[:upper:]' '[:lower:]')" rest="${SERVER_LOG:2}" if [ -d /mnt/c ]; then echo "/mnt/$drive$rest" else echo "/$drive$rest" fi ;; *) echo "$SERVER_LOG" ;; esac } report() { local response="$1" local http_code body biz_code request_id http_code="$(printf '%s\n' "$response" | tail -n 1)" body="$(printf '%s\n' "$response" | sed '$d')" biz_code="$(printf '%s' "$body" | grep -oE '"code"[[:space:]]*:[[:space:]]*[0-9]+' | head -n 1 || true)" request_id="$(printf '%s' "$body" | grep -oE '"requestId"[[:space:]]*:[[:space:]]*"[^"]*"' | head -n 1 || true)" echo " http=$http_code ${biz_code:-code=N/A} ${request_id:-requestId=N/A}" } extract_token() { printf '%s' "$1" | sed -n 's/.*"accessToken"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' | head -n 1 } extract_id() { printf '%s' "$1" | sed -n 's/.*"id"[[:space:]]*:[[:space:]]*\([0-9][0-9]*\).*/\1/p' | head -n 1 } sms_code() { local log_path log_path="$(resolve_log)" [ -n "$log_path" ] || return 1 [ -f "$log_path" ] || return 1 grep -a "MOCK SMS CODE.*phone=$PHONE" "$log_path" | tail -n 1 | grep -a -oE 'code=[0-9]{6}' | cut -d= -f2 } echo "=== MyDreamJob backend smoke test ===" echo "BASE_URL=$BASE_URL" echo "PHONE=$PHONE" say "GET /health" HEALTH="$(curl -sS -w '\n%{http_code}' "$BASE_URL/health" || true)" report "$HEALTH" if [ "$(printf '%s\n' "$HEALTH" | tail -n 1)" = "200" ]; then ok "health"; else fail "health"; fi say "POST /auth/sms/send and POST /auth/login" SMS="" for attempt in 1 2 3; do curl -sS -X POST "$BASE_URL/auth/sms/send?phone=$PHONE&purpose=login" \ -H "X-Request-Id: smoke-sms-$attempt" >/dev/null 2>&1 || true sleep 2 SMS="$(sms_code || true)" [ -n "$SMS" ] && break done if [ -z "$SMS" ]; then fail "cannot read Mock SMS code; set SERVER_LOG to the server log path" TOKEN="" else LOGIN="$(curl -sS -w '\n%{http_code}' -X POST "$BASE_URL/auth/login" \ -H 'Content-Type: application/json' \ -H 'X-Request-Id: smoke-login-001' \ -d "{\"phone\":\"$PHONE\",\"code\":\"$SMS\",\"purpose\":\"login\"}" || true)" report "$LOGIN" TOKEN="$(extract_token "$LOGIN")" if [ -n "$TOKEN" ]; then ok "login"; else fail "login"; fi fi if [ -z "$TOKEN" ]; then echo "PASS=$PASS FAIL=$FAIL SKIP=$SKIP" exit 1 fi AUTH="Authorization: Bearer $TOKEN" say "GET /jobs?size=5" JOBS="$(curl -sS -w '\n%{http_code}' "$BASE_URL/jobs?size=5" -H "$AUTH" || true)" report "$JOBS" JOB_ID="$(extract_id "$JOBS")" if printf '%s' "$JOBS" | grep -q 'items' && [ -n "$JOB_ID" ]; then ok "jobs list"; else fail "jobs list"; fi say "GET /jobs/public/$JOB_ID" PUBLIC="$(curl -sS -w '\n%{http_code}' "$BASE_URL/jobs/public/$JOB_ID" || true)" report "$PUBLIC" if printf '%s' "$PUBLIC" | grep -q 'title'; then ok "public job"; else fail "public job"; fi say "GET /jobs/$JOB_ID/match" MATCH="$(curl -sS -w '\n%{http_code}' "$BASE_URL/jobs/$JOB_ID/match" -H "$AUTH" || true)" report "$MATCH" if printf '%s' "$MATCH" | grep -q 'rulesVersion'; then ok "match diagnose"; else fail "match diagnose"; fi say "POST /applications" APP="$(curl -sS -w '\n%{http_code}' -X POST "$BASE_URL/applications" \ -H "$AUTH" -H 'Content-Type: application/json' \ -H 'X-Request-Id: smoke-apply-001' -d "{\"jobId\":$JOB_ID}" || true)" report "$APP" APP_ID="$(extract_id "$APP")" if [ -n "$APP_ID" ]; then ok "apply application $APP_ID"; else fail "apply application"; fi say "PATCH /applications/$APP_ID/status" if [ -z "${COMPANY_TOKEN:-}" ]; then skip "COMPANY_TOKEN not provided" else STATUS="$(curl -sS -w '\n%{http_code}' -X PATCH "$BASE_URL/applications/$APP_ID/status" \ -H "Authorization: Bearer $COMPANY_TOKEN" -H 'Content-Type: application/json' \ -H 'X-Request-Id: smoke-status-001' -d '{"status":"VIEWED"}' || true)" report "$STATUS" if printf '%s' "$STATUS" | grep -q 'VIEWED'; then ok "company status VIEWED"; else fail "company status"; fi fi say "GET /notifications" NOTIFICATIONS="$(curl -sS -w '\n%{http_code}' "$BASE_URL/notifications?size=5" -H "$AUTH" || true)" report "$NOTIFICATIONS" if printf '%s' "$NOTIFICATIONS" | grep -q 'items'; then ok "notifications list"; else fail "notifications list"; fi say "GET /notifications/unread-count" UNREAD="$(curl -sS -w '\n%{http_code}' "$BASE_URL/notifications/unread-count" -H "$AUTH" || true)" report "$UNREAD" if printf '%s' "$UNREAD" | grep -q 'unreadCount'; then ok "unread count"; else fail "unread count"; fi NOTIFICATION_ID="$(extract_id "$NOTIFICATIONS")" say "POST /notifications/$NOTIFICATION_ID/read" if [ -z "$NOTIFICATION_ID" ]; then skip "no notification available" else READ="$(curl -sS -w '\n%{http_code}' -X POST "$BASE_URL/notifications/$NOTIFICATION_ID/read" -H "$AUTH" || true)" report "$READ" if printf '%s' "$READ" | grep -q 'marked as read'; then ok "mark notification read"; else fail "mark notification read"; fi fi say "POST /applications/$APP_ID/withdraw" WITHDRAW="$(curl -sS -w '\n%{http_code}' -X POST "$BASE_URL/applications/$APP_ID/withdraw" \ -H "$AUTH" -H 'X-Request-Id: smoke-withdraw-001' || true)" report "$WITHDRAW" if printf '%s' "$WITHDRAW" | grep -q 'WITHDRAWN'; then ok "withdraw application"; else fail "withdraw application"; fi echo "============================================" echo "PASS=$PASS FAIL=$FAIL SKIP=$SKIP" echo "============================================" [ "$FAIL" -eq 0 ]