about summary refs log tree commit diff stats
path: root/scripts/record-api-responses.sh
diff options
context:
space:
mode:
authorfox <fox@fakecake.org>2026-06-06 14:39:15 +0300
committerfox <fox@fakecake.org>2026-06-06 14:39:15 +0300
commitf8ee23be63b4babcd7d4cb81af8c38448299ebca (patch)
treef99648471d193d5aac6ef71cc97c98a7c6ef2d1e /scripts/record-api-responses.sh
parent6b78dec41da6f4b0526191ef52cef7c51cf29aeb (diff)
parentf9fb62d7282d7e1ec310a12b0fcb922267689d6d (diff)
downloadtt-rss-android-f8ee23be63b4babcd7d4cb81af8c38448299ebca.tar.gz
tt-rss-android-f8ee23be63b4babcd7d4cb81af8c38448299ebca.tar.xz
Merge branch 'dev' into 'master' 26.06-f8ee23be-signed
Dev

See merge request git/tt-rss/tt-rss-android!73
Diffstat (limited to 'scripts/record-api-responses.sh')
-rwxr-xr-xscripts/record-api-responses.sh186
1 files changed, 186 insertions, 0 deletions
diff --git a/scripts/record-api-responses.sh b/scripts/record-api-responses.sh
new file mode 100755
index 00000000..c6e7a8b0
--- /dev/null
+++ b/scripts/record-api-responses.sh
@@ -0,0 +1,186 @@
+#!/usr/bin/env bash
+#
+# record-api-responses.sh
+# Records TT-RSS API v2 responses as JSON fixtures for unit tests.
+#
+# Usage:
+#   ./scripts/record-api-responses.sh -u <username> -p '<password>' -h https://example.com/tt-rss
+#   # or via environment:
+#   export TTRSS_URL_ENV=https://example.com/tt-rss
+#   export TTRSS_USER_ENV=<username>
+#   export TTRSS_PASS_ENV='<password>'
+#   ./scripts/record-api-responses.sh
+#
+# Individual endpoints can be recorded by passing endpoint names:
+#   ./scripts/record-api-responses.sh -u <user> -p '<pass>' -h https://example.com/tt-rss -- feeds headlines
+#
+# Available endpoints: feeds headlines article_detail categories feed_tree unread counters config version labels login login_error not_logged_in
+
+set -euo pipefail
+
+# ── Defaults ──────────────────────────────────────────────────────────────
+OUTPUT_DIR=""
+ARTICLE_ID=""
+ALL_ENDPOINTS="feeds headlines article_detail categories feed_tree unread counters config version labels login login_error not_logged_in"
+ENDPOINTS_TO_RECORD=()
+
+# ── Parse arguments ───────────────────────────────────────────────────────
+while [[ $# -gt 0 ]]; do
+    case "$1" in
+        -u|--user)   TTRSS_USER="$2"; shift 2 ;;
+        -p|--pass)   TTRSS_PASS="$2"; shift 2 ;;
+        -h|--host)   TTRSS_URL="$2";  shift 2 ;;
+        -o|--output) OUTPUT_DIR="$2"; shift 2 ;;
+        --)          ENDPOINTS_TO_RECORD=("${@:2}"); break ;;
+        *)           ENDPOINTS_TO_RECORD+=("$1"); shift ;;
+    esac
+done
+
+# Fall back to environment variables (only if not set via args)
+if [[ -z "${TTRSS_URL:-}" ]]; then TTRSS_URL="$TTRSS_URL_ENV"; fi
+if [[ -z "${TTRSS_USER:-}" ]]; then TTRSS_USER="$TTRSS_USER_ENV"; fi
+if [[ -z "${TTRSS_PASS:-}" ]]; then TTRSS_PASS="$TTRSS_PASS_ENV"; fi
+
+# Resolve output directory (default: src/test/resources/responses/)
+if [[ -z "$OUTPUT_DIR" ]]; then
+    SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
+    PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
+    OUTPUT_DIR="$PROJECT_DIR/org.fox.ttrss/src/test/resources/responses"
+fi
+
+# ── Validation ────────────────────────────────────────────────────────────
+if [[ -z "$TTRSS_URL" || -z "$TTRSS_USER" || -z "$TTRSS_PASS" ]]; then
+    echo "Error: TTRSS_URL, TTRSS_USER, and TTRSS_PASS must be set (via args or env)."
+    echo "  Example: ./scripts/record-api-responses.sh -u <user> -p '<pass>' -h https://example.com/tt-rss"
+    exit 1
+fi
+
+mkdir -p "$OUTPUT_DIR"
+
+# Strip trailing slash
+TTRSS_URL="${TTRSS_URL%/}"
+BASE_URL="$TTRSS_URL/api/v2"
+
+# ── Determine which endpoints to record ───────────────────────────────────
+if [[ ${#ENDPOINTS_TO_RECORD[@]} -eq 0 ]]; then
+    ENDPOINTS_TO_RECORD=($ALL_ENDPOINTS)
+fi
+
+# Validate endpoint names
+for ep in "${ENDPOINTS_TO_RECORD[@]}"; do
+    if [[ ! " $ALL_ENDPOINTS " =~ " $ep " ]]; then
+        echo "Warning: Unknown endpoint '$ep', skipping."
+        continue
+    fi
+done
+
+# ── Login ─────────────────────────────────────────────────────────────────
+echo "Logging in to $TTRSS_URL ..."
+LOGIN_RESPONSE=$(curl -s -X POST "$BASE_URL/auth/login" \
+  -H "Content-Type: text/json" \
+  -d "{\"user\":\"$TTRSS_USER\", \"password\": \"$TTRSS_PASS\"}")
+
+SESSION_ID=$(echo "$LOGIN_RESPONSE" | grep -o '"session_id":"[^"]*"' | cut -d'"' -f4)
+if [[ -z "$SESSION_ID" ]]; then
+    echo "Error: Login failed. Response: $LOGIN_RESPONSE"
+    exit 1
+fi
+echo "  Session ID: $SESSION_ID"
+
+# Save login response (replace real session_id with placeholder)
+echo "$LOGIN_RESPONSE" | sed "s/$SESSION_ID/SESSION_ID_PLACEHOLDER/g" > "$OUTPUT_DIR/login.json"
+echo "  Saved: login.json"
+
+# ── Record endpoints ─────────────────────────────────────────────────────
+record_endpoint() {
+    local name="$1"
+    local method="${2:-GET}"
+    local path="$3"
+    local body="${4:-}"
+    local headers="${5:-}"
+
+    local outfile="$OUTPUT_DIR/${name}.json"
+
+    if [[ "$method" == "GET" ]]; then
+        local response
+        response=$(curl -s "$BASE_URL/$path" \
+          -H "X-Session-ID: $SESSION_ID" \
+          $headers)
+    elif [[ "$method" == "POST" ]]; then
+        local response
+        response=$(curl -s -X POST "$BASE_URL/$path" \
+          -H "Content-Type: text/json" \
+          -H "X-Session-ID: $SESSION_ID" \
+          $headers \
+          -d "$body")
+    fi
+
+    echo "$response" > "$outfile"
+    local size
+    size=$(wc -c < "$outfile")
+    echo "  Saved: ${name}.json ($size bytes)"
+}
+
+# Record each requested endpoint
+for ep in "${ENDPOINTS_TO_RECORD[@]}"; do
+    case "$ep" in
+        feeds)
+            record_endpoint "feeds" "GET" "feeds"
+            ;;
+        headlines)
+            record_endpoint "headlines" "GET" "articles?unread=true&limit=20&sort=updated&order=descending"
+            ;;
+        article_detail)
+            # Need an article ID from headlines first
+            if [[ -z "$ARTICLE_ID" && -f "$OUTPUT_DIR/headlines.json" ]]; then
+                ARTICLE_ID=$(grep -o '"id":[0-9]*' "$OUTPUT_DIR/headlines.json" | head -1 | cut -d: -f2)
+            fi
+            if [[ -z "$ARTICLE_ID" ]]; then
+                echo "  Skipping: article_detail (no article_id available, record headlines first)"
+                continue
+            fi
+            record_endpoint "article_detail" "GET" "articles/$ARTICLE_ID"
+            ;;
+        categories)
+            record_endpoint "categories" "GET" "categories"
+            ;;
+        feed_tree)
+            record_endpoint "feed_tree" "GET" "feed-tree"
+            ;;
+        unread)
+            record_endpoint "unread" "GET" "unread"
+            ;;
+        counters)
+            record_endpoint "counters" "GET" "counters"
+            ;;
+        config)
+            record_endpoint "config" "GET" "config"
+            ;;
+        version)
+            record_endpoint "version" "GET" "version"
+            ;;
+        labels)
+            if [[ -z "$ARTICLE_ID" && -f "$OUTPUT_DIR/headlines.json" ]]; then
+                ARTICLE_ID=$(grep -o '"id":[0-9]*' "$OUTPUT_DIR/headlines.json" | head -1 | cut -d: -f2)
+            fi
+            if [[ -z "$ARTICLE_ID" ]]; then
+                echo "  Skipping: labels (no article_id available, record headlines first)"
+                continue
+            fi
+            record_endpoint "labels" "GET" "labels?article_id=$ARTICLE_ID"
+            ;;
+        login_error)
+            record_endpoint "login_error" "POST" "auth/login" '{"user":"unknown","password":"wrong"}'
+            ;;
+        not_logged_in)
+            outfile="$OUTPUT_DIR/not_logged_in.json"
+            response=$(curl -s "$BASE_URL/feeds" -H "X-Session-ID: invalid-session")
+            echo "$response" > "$outfile"
+            size=$(wc -c < "$outfile")
+            echo "  Saved: not_logged_in.json ($size bytes)"
+            ;;
+    esac
+done
+
+echo ""
+echo "Done. All fixtures saved to: $OUTPUT_DIR"